SideMenu Component
The <SideMenu> component renders a fixed navigation menu on the side of the screen. It supports nested items, dividers, a collapsed mode on desktop and a sliding menu on mobile. The active item is detected from the current location.
Basic Usage
The page content is rendered inside <SideMenu.pageHolder> as a child of the menu, so it gets the correct space next to it.
- Basic
- Nested Items
- Collapsable
import { useLocation } from "react-router-dom";
import { SideMenu } from "react-better-html";
function Layout({ children }) {
const location = useLocation();
return (
<SideMenu
items={[
{ type: "item", text: "Dashboard", iconName: "chevronRight", href: "/" },
{ type: "item", text: "Users", iconName: "eye", href: "/users" },
{ type: "divider", text: "Settings" },
{ type: "item", text: "Preferences", iconName: "filter", href: "/preferences" },
]}
location={location}
logoAssetName="logo"
logoText="My App"
>
<SideMenu.pageHolder>{children}</SideMenu.pageHolder>
</SideMenu>
);
}
import { SideMenu } from "react-better-html";
function Layout({ children }) {
return (
<SideMenu
items={[
{
type: "item",
text: "Products",
iconName: "filter",
children: [
{ type: "item", text: "All products", href: "/products" },
{ type: "item", text: "Categories", href: "/products/categories", withDot: true },
],
},
]}
location={location}
>
<SideMenu.pageHolder>{children}</SideMenu.pageHolder>
</SideMenu>
);
}
The group opens automatically when one of its children is active.
import { SideMenu } from "react-better-html";
function Layout({ children }) {
return (
<SideMenu
items={items}
bottomItems={[{ type: "item", text: "Log out", iconName: "XMark", onClick: logout }]}
location={location}
collapsable
>
<SideMenu.pageHolder>{children}</SideMenu.pageHolder>
</SideMenu>
);
}
Renders a button that collapses the menu to icons only. The item texts are shown as tooltips while collapsed.
Items
type SideMenuItem = {
text: string;
hidden?: boolean;
} & (
| {
type: "item";
iconName?: IconName;
leftElement?: ((item: SideMenuItem, isActive: boolean) => React.ReactNode) | React.ReactNode;
href?: string;
disabled?: boolean;
withDot?: boolean;
children?: SideMenuItem[];
onClickCloseSideMenu?: boolean;
onClick?: (item: SideMenuItem) => void;
}
| {
type: "divider";
shortText?: string;
}
);
text- the text of the item or the dividerhidden- the item is not renderediconName- an icon before the textleftElement- a custom element instead of the iconhref- the link of the item. The item is active when the current path starts with itdisabled- the item can not be clickedwithDot- renders a small dot on the itemchildren- nested itemsonClickCloseSideMenu- whether clicking the item closes the menu on mobile (defaults totrue)onClick- a click handlershortText- the text of the divider while the menu is collapsed. Without it a line is rendered
Links are rendered with the linkComponent from the components configuration, so you can pass the Link component from react-router-dom there.
Common Props
items- the items at the top of the menubottomItems- the items at the bottom of the menulocation- the current location (window.locationor the one fromuseLocation())position- the side of the screen -left(default) orrighttopSpace- space above the menu (e.g. for a fixed header)logoAssetName/logoUrl- the logo at the top of the menulogoText/logoFontFamily- text next to the logocollapsable- renders the collapse button on desktopwithCloseButton- renders anXbutton on mobilewidthMobileHandle- renders a handle on mobile to open the menumobileFoldToPosition- the side the menu slides to on mobile when closedisLoading- renders a loader instead of the itemsbackgroundColor- the background of the menu (defaults tobackgroundContent)activeItemColor/hoverItemColor- the background of the active and the hovered itemborder- the border of the menugap,paddingTop,paddingBottom- spacing of the itemssideBar- an additional narrow bar rendered next to the menu (e.g. for workspace switching)itemsAdditionalComponent,betweenItemsAdditionalComponent,bottomItemsAdditionalComponent- custom elements after the items, between the items and the bottom items and after the bottom itemsabsoluteComponent- an element positioned absolutely at the top of the menurenderItemsHolder/renderBottomItemsHolder- wrap the rendered items with a custom element
The width of the menu can be changed from the sideMenu key of the components configuration (defaults to 300).
Subcomponents
SideMenu.pageHolder
A <PageHolder> with the correct space for the side menu. It must be rendered as a child of <SideMenu>. All <PageHolder> props are supported as well as additionalTopComponent and additionalBottomComponent that are rendered outside of the page holder (e.g. a header or a footer).
SideMenu.burgerButton
An animated burger button that opens and closes the menu on mobile.
import { Div, SideMenu, useMediaQuery } from "react-better-html";
function Header() {
const mediaQuery = useMediaQuery();
return (
<Div.row alignItems="center" padding={16}>
{mediaQuery.size1000 && <SideMenu.burgerButton />}
</Div.row>
);
}
position- the alignment of the lines -left(default),centerorrightcolor- the color of the lines (defaults toborder)onClick- called with the new opened state
Controls
The menu can be controlled from anywhere (even outside of React components) with sideMenuControls.
import { sideMenuControls } from "react-better-html";
sideMenuControls.collapse();
expand()/collapse()/toggleExpanded()- the collapsed state on desktopopen()/close()/toggleOpened()- the opened state on mobile
The menu switches to the mobile mode on screens smaller than 1000px.