Skip to content

Commit

Permalink
Masterbar: Do not recreate masterbar button element on every render (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sirbrillig authored Jan 24, 2025
1 parent 2fa0c47 commit 42e6a4d
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions client/layout/masterbar/item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,23 +216,14 @@ class MasterbarItem extends Component< MasterbarItemProps > {
disabled: this.props.disabled,
};

const MenuItem = ( props: React.HTMLAttributes< HTMLElement > ) =>
this.props.url ? (
<a
href={ this.props.url }
ref={ this.props.innerRef as LegacyRef< HTMLAnchorElement > }
{ ...props }
/>
) : (
<button ref={ this.props.innerRef as LegacyRef< HTMLButtonElement > } { ...props } />
);

return (
<div
className={ clsx( 'masterbar__item-wrapper', this.props.wrapperClassName ) }
ref={ this.wrapperRef }
>
<MenuItem
url={ this.props.url }
innerRef={ this.props.innerRef }
{ ...attributes }
onKeyDown={ this.props.subItems && this.toggleMenuByKey }
onTouchEnd={ this.props.subItems && this.toggleMenuByTouch }
Expand All @@ -248,3 +239,16 @@ class MasterbarItem extends Component< MasterbarItemProps > {
export default forwardRef< HTMLButtonElement | HTMLAnchorElement, MasterbarItemProps >(
( props, ref ) => <MasterbarItem innerRef={ ref } { ...props } />
);

type MenuItemProps< R > = {
url?: string;
innerRef?: R;
} & React.HTMLAttributes< HTMLElement >;

function MenuItem< R >( { url, innerRef, ...props }: MenuItemProps< R > ) {
return url ? (
<a href={ url } ref={ innerRef as LegacyRef< HTMLAnchorElement > } { ...props } />
) : (
<button ref={ innerRef as LegacyRef< HTMLButtonElement > } { ...props } />
);
}

0 comments on commit 42e6a4d

Please sign in to comment.