Skip to content

Commit 42e6a4d

Browse files
authored
Masterbar: Do not recreate masterbar button element on every render (#98897)
1 parent 2fa0c47 commit 42e6a4d

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

client/layout/masterbar/item.tsx

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -216,23 +216,14 @@ class MasterbarItem extends Component< MasterbarItemProps > {
216216
disabled: this.props.disabled,
217217
};
218218

219-
const MenuItem = ( props: React.HTMLAttributes< HTMLElement > ) =>
220-
this.props.url ? (
221-
<a
222-
href={ this.props.url }
223-
ref={ this.props.innerRef as LegacyRef< HTMLAnchorElement > }
224-
{ ...props }
225-
/>
226-
) : (
227-
<button ref={ this.props.innerRef as LegacyRef< HTMLButtonElement > } { ...props } />
228-
);
229-
230219
return (
231220
<div
232221
className={ clsx( 'masterbar__item-wrapper', this.props.wrapperClassName ) }
233222
ref={ this.wrapperRef }
234223
>
235224
<MenuItem
225+
url={ this.props.url }
226+
innerRef={ this.props.innerRef }
236227
{ ...attributes }
237228
onKeyDown={ this.props.subItems && this.toggleMenuByKey }
238229
onTouchEnd={ this.props.subItems && this.toggleMenuByTouch }
@@ -248,3 +239,16 @@ class MasterbarItem extends Component< MasterbarItemProps > {
248239
export default forwardRef< HTMLButtonElement | HTMLAnchorElement, MasterbarItemProps >(
249240
( props, ref ) => <MasterbarItem innerRef={ ref } { ...props } />
250241
);
242+
243+
type MenuItemProps< R > = {
244+
url?: string;
245+
innerRef?: R;
246+
} & React.HTMLAttributes< HTMLElement >;
247+
248+
function MenuItem< R >( { url, innerRef, ...props }: MenuItemProps< R > ) {
249+
return url ? (
250+
<a href={ url } ref={ innerRef as LegacyRef< HTMLAnchorElement > } { ...props } />
251+
) : (
252+
<button ref={ innerRef as LegacyRef< HTMLButtonElement > } { ...props } />
253+
);
254+
}

0 commit comments

Comments
 (0)