Skip to content
Likable Aurora @likable-hair/svelte 5

SidebarLayout

Page shell of an app: a sidebar on the left (logo, menu, user), a bar at the top and the content. The window scrolls: the bar and the sidebar stay in place. On desktop the sidebar is a narrow rail of icons that widens according to expandOn: toggle with a button at the top of the sidebar, next to the logo (while the sidebar is narrow the logo turns into the button as soon as the pointer is on the sidebar or the button has the keyboard focus, and a click on an empty spot of the rail widens it too), pushing the content (default); hover and click over the content; none only through sidebarExpanded (sidebarExpanded with none keeps it always wide). Up to 1024px wide (the mAndDown breakpoint) the sidebar moves into a Drawer opened by a menu button in the bar, which closes when an option is chosen or currentPath changes. The menu is a HierarchyMenu drawn from options (pass currentPath={page.url.pathname} to select the option of the page), or menuSnippet; when it is taller than the window it scrolls by itself to show the selected option. The bar shows the menu button on mobile, then headerSnippet; with sidebarSnippet the toggle button of toggle stays in the bar. A "Skip to content" link appears on the first Tab. The state is exposed as data-expand-on and data-expanded on the root.

import { SidebarLayout } from '@likable-hair/svelte';

src/lib/components/layouts/SidebarLayout.svelte · Markdown for LLMs

Collapsible sidebar

The default expandOn="toggle": the button at the top of the sidebar widens and narrows it, pushing the content. Narrow, the logo turns into that button while the pointer is on the sidebar, and a click on an empty spot of the rail widens the sidebar too. Narrow, the menu shows only icons, with tooltips and the children of a parent in a menu. Make the window narrower than 1024px to get the menu button and the drawer. In an app currentPath is page.url.pathname; here the links only change path.

Open the example alone to try other window sizes.

Code
<script lang="ts">
	import {
		mdiAccountMultipleOutline,
		mdiBellOutline,
		mdiCalendarOutline,
		mdiChartBoxOutline,
		mdiCogOutline,
		mdiLogout,
		mdiPackageVariantClosed,
		mdiViewDashboardOutline
	} from '@mdi/js';
	import { Button, SidebarLayout, type HierarchyMenuOption } from '@likable-hair/svelte';

	const options: HierarchyMenuOption[] = [
		{ name: 'dashboard', label: 'Dashboard', icon: mdiViewDashboardOutline, href: '/' },
		{ name: 'agenda', label: 'Agenda', icon: mdiCalendarOutline, href: '/agenda', badge: 12 },
		{ name: 'clients', label: 'Clients', icon: mdiAccountMultipleOutline, href: '/clients' },
		{
			name: 'warehouse',
			label: 'Warehouse',
			icon: mdiPackageVariantClosed,
			href: '/warehouse',
			badge: 3
		},
		{ name: 'reports', label: 'Reports', icon: mdiChartBoxOutline, href: '/reports' },
		{
			name: 'settings',
			label: 'Settings',
			icon: mdiCogOutline,
			options: [
				{ name: 'general', label: 'General', href: '/settings/general' },
				{ name: 'hours', label: 'Opening hours', href: '/settings/hours' }
			]
		}
	];

	let path = $state('/agenda');
	let sidebarExpanded = $state(true);
	let selected: string | undefined = $state();
	const title = $derived(
		options.flatMap((option) => [option, ...(option.options ?? [])]).find((o) => o.name === selected)
			?.label
	);
</script>

<SidebarLayout
	{options}
	bind:sidebarExpanded
	bind:selected
	currentPath={path}
	onmenuSelect={({ option, nativeEvent }) => {
		nativeEvent.preventDefault();
		path = option.href ?? path;
	}}
>
	{#snippet logoSnippet({ collapsed })}
		<div class="logo">
			<span class="logo-mark">L</span>
			{#if !collapsed}<b>Likable</b>{/if}
		</div>
	{/snippet}
	{#snippet headerSnippet()}
		<span class="title">{title}</span>
		<Button buttonType="icon" variant="secondary" icon={mdiBellOutline} aria-label="Notifications" />
		<span class="avatar">GM</span>
	{/snippet}
	{#snippet userSnippet({ collapsed })}
		<Button
			buttonType={collapsed ? 'icon' : 'text'}
			variant="secondary"
			icon={mdiLogout}
			aria-label={collapsed ? 'Log out' : undefined}
		>
			{#if !collapsed}Log out{/if}
		</Button>
	{/snippet}

	<div class="cards">
		{#each { length: 8 } as _, index (index)}
			<div class="card"></div>
		{/each}
	</div>
</SidebarLayout>

<style>
	.logo {
		display: flex;
		align-items: center;
		gap: 10px;
		height: 36px;
		padding-left: 8px;
		font-size: 17px;
		white-space: nowrap;
	}

	.logo-mark {
		display: grid;
		flex-shrink: 0;
		place-items: center;
		width: 28px;
		height: 28px;
		border-radius: 6px;
		background: var(--global-gradient);
		color: var(--global-color-on-primary);
		font-weight: 700;
	}

	.title {
		flex: 1;
		font-weight: 600;
	}

	.avatar {
		display: grid;
		place-items: center;
		width: 32px;
		height: 32px;
		border-radius: 50%;
		background: var(--global-color-primary-soft);
		color: var(--global-color-primary);
		font-size: 12px;
		font-weight: 600;
	}

	.cards {
		display: grid;
		grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
		gap: 16px;
	}

	.card {
		height: 140px;
		border: 1px solid var(--global-color-border);
		border-radius: var(--global-radius-md);
		background: var(--global-color-surface);
	}
</style>

Expand on hover

With expandOn="hover" the sidebar stays a rail of icons and widens over the content while the pointer, or the keyboard focus, is on it, so the page does not move. expandOn="click" widens it with a click on the rail instead, until a click outside or Escape.

Open the example alone to try other window sizes.

Code
<script lang="ts">
	import {
		mdiAccountMultipleOutline,
		mdiCalendarOutline,
		mdiChartBoxOutline,
		mdiViewDashboardOutline
	} from '@mdi/js';
	import { SidebarLayout, type HierarchyMenuOption } from '@likable-hair/svelte';

	const options: HierarchyMenuOption[] = [
		{ name: 'dashboard', label: 'Dashboard', icon: mdiViewDashboardOutline },
		{ name: 'agenda', label: 'Agenda', icon: mdiCalendarOutline },
		{ name: 'clients', label: 'Clients', icon: mdiAccountMultipleOutline },
		{ name: 'reports', label: 'Reports', icon: mdiChartBoxOutline }
	];

	let selected = $state('dashboard');
</script>

<SidebarLayout expandOn="hover" {options} bind:selected>
	{#snippet logoSnippet({ collapsed })}
		<div class="logo">
			<span class="logo-mark">L</span>
			{#if !collapsed}<b>Likable</b>{/if}
		</div>
	{/snippet}
	{#snippet headerSnippet()}
		<b>{options.find((option) => option.name === selected)?.label}</b>
	{/snippet}

	<p>Move the pointer over the rail on the left.</p>
</SidebarLayout>

<style>
	.logo {
		display: flex;
		align-items: center;
		gap: 10px;
		padding-left: 8px;
		font-size: 17px;
		white-space: nowrap;
	}

	.logo-mark {
		display: grid;
		flex-shrink: 0;
		place-items: center;
		width: 28px;
		height: 28px;
		border-radius: 6px;
		background: var(--global-gradient);
		color: var(--global-color-on-primary);
		font-weight: 700;
	}

	p {
		margin: 0;
		color: var(--global-color-text-2);
	}
</style>

Always wide, with a custom menu

expandOn="none" with sidebarExpanded keeps the sidebar always wide, with no button. menuSnippet replaces the menu drawn from options: here two HierarchyMenu under their own headings. --sidebar-layout-* variables change sizes and colors for one instance.

Open the example alone to try other window sizes.

Code
<script lang="ts">
	import { mdiArchiveOutline, mdiInboxOutline, mdiSendOutline, mdiTagOutline } from '@mdi/js';
	import { HierarchyMenu, SidebarLayout, type HierarchyMenuOption } from '@likable-hair/svelte';

	const mail: HierarchyMenuOption[] = [
		{ name: 'inbox', label: 'Inbox', icon: mdiInboxOutline, badge: 4 },
		{ name: 'sent', label: 'Sent', icon: mdiSendOutline },
		{ name: 'archive', label: 'Archive', icon: mdiArchiveOutline }
	];
	const labels: HierarchyMenuOption[] = [
		{ name: 'clients', label: 'Clients', icon: mdiTagOutline },
		{ name: 'suppliers', label: 'Suppliers', icon: mdiTagOutline }
	];

	let selected = $state('inbox');
</script>

<SidebarLayout expandOn="none" sidebarExpanded --sidebar-layout-sidebar-expanded-width="15rem">
	{#snippet menuSnippet()}
		<h2>Mail</h2>
		<HierarchyMenu options={mail} bind:selected aria-label="Mail" />
		<h2>Labels</h2>
		<HierarchyMenu options={labels} bind:selected aria-label="Labels" />
	{/snippet}

	<p>Selected: <code>{selected}</code></p>
</SidebarLayout>

<style>
	h2 {
		margin: 16px 0 6px;
		padding-left: 13px;
		color: var(--global-color-text-3);
		font-size: 10px;
		font-weight: 600;
		letter-spacing: 0.14em;
		text-transform: uppercase;
	}

	p {
		margin: 0;
		color: var(--global-color-text-2);
	}
</style>

Props

NameTypeDefaultDescription
expandOn 'toggle' | 'hover' | 'click' | 'none''toggle'How the sidebar widens on desktop: toggle with a button at the top of the sidebar, or a click on an empty spot of the narrow rail, pushing the content; hover while the pointer or the keyboard focus is on it, over the content; click with a click on the rail (not on an option), over the content, until a click outside, Escape or the choice of an option; none only through sidebarExpanded.
sidebarExpanded bindable booleanWhether the sidebar is wide on desktop. undefined counts as narrow. It has no effect on the mobile drawer.
drawerOpened bindable booleanWhether the mobile drawer is open. undefined counts as closed.
options HierarchyMenuOption[][]Options of the menu, drawn with HierarchyMenu. Ignored with menuSnippet.
selected bindable stringname of the selected option of the menu.
currentPath stringPath of the current page, usually page.url.pathname: it selects the option whose href matches it, and a change closes the mobile drawer.
locale stringBCP 47 locale of the texts: it and en are built in, other languages fall back to English. Defaults to the one set with setDefaultLocale, else en.
menuLabel stringAccessible name of the menu button and of the drawer. Defaults to "Menu", in the language of locale.
sidebarLabel stringAccessible name of the button that widens the sidebar. Defaults to "Sidebar", in the language of locale.
skipLabel stringText of the link that skips to the content, shown on the first Tab. Defaults to "Skip to content", in the language of locale.
class { container?: ClassValue; sidebar?: ClassValue; header?: ClassValue; content?: ClassValue; drawer?: string; }{}Classes of the parts: container the root, sidebar the desktop sidebar, header the bar, content the <main>, drawer the panel of the mobile drawer.
sidebarLayoutElement bindable HTMLDivElementThe root element.

It also accepts the native attributes and events of <div> (for example id, title, aria-label, onkeydown), which are forwarded to the element.

Snippets

NameParametersDescription
childrenContent of the page, inside <main>.
headerSnippet{ hamburgerVisible: boolean; sidebarExpanded: boolean; collapsed: boolean }Content of the bar (after the menu button on mobile): search, account, actions. hamburgerVisible is true on mobile, collapsed while the desktop sidebar is narrow.
toggleSnippet{ toggle: () => void; hamburgerVisible: boolean; sidebarExpanded: boolean; drawerOpened: boolean }Replaces the buttons that open the mobile drawer and widen the sidebar, in the bar (the sidebar then has no toggle button). toggle opens the drawer on mobile and widens the sidebar on desktop.
sidebarSnippet{ hamburgerVisible: boolean; sidebarExpanded: boolean; collapsed: boolean }Replaces the whole content of the sidebar and of the drawer (logo, menu and user). In the drawer hamburgerVisible is true.
logoSnippet{ hamburgerVisible: boolean; sidebarExpanded: boolean; collapsed: boolean }Top of the sidebar, usually the logo; collapsed asks for its narrow version.
menuSnippet{ hamburgerVisible: boolean; sidebarExpanded: boolean; collapsed: boolean }Replaces the menu drawn from options. It scrolls when it does not fit.
userSnippet{ hamburgerVisible: boolean; sidebarExpanded: boolean; collapsed: boolean }Bottom of the sidebar, usually the account, the theme and the logout.

Events

NameTypeDescription
ondrawerChange(event: { opened: boolean }) => voidCalled when the layout opens or closes the mobile drawer by itself (menu button, Escape, backdrop, choice of an option, change of currentPath).
onsidebarToggle(event: { expanded: boolean }) => voidCalled when the layout widens or narrows the sidebar by itself (button, hover, click).
onmenuSelect(event: { option: HierarchyMenuOption; nativeEvent: MouseEvent }) => voidCalled when an option of the menu is chosen, after selected has changed.

Types

Exported by the package: import type { HierarchyMenuOption } from '@likable-hair/svelte';

HierarchyMenuOption

type HierarchyMenuOption = { … }

NameTypeDescription
name requiredstringUnique value among all the options of the menu: selected holds it.
label requiredstringVisible text.
icon stringSVG path of the icon before the text, for example from @mdi/js.
href stringMakes the option a link. With currentPath, the option whose href best matches the path is selected.
options HierarchyMenuOption[]Options shown below this one when it is open, nested to any depth.
badge string | numberShort count or text after the label, in a pill. A dot on the icon when the menu shows only icons.
disabled booleanThe option cannot be chosen.

CSS variables

For a single instance, pass them as style props: <SidebarLayout --sidebar-layout-background="tomato">. The --sidebar-layout-default-* defaults apply to the whole app: override them in :root or in a theme.

Per-instance variableDefault
--sidebar-layout-duration
--sidebar-layout-default-duration var(--global-duration-slow)
--sidebar-layout-easing
--sidebar-layout-default-easing var(--global-ease)
--sidebar-layout-sidebar-width
--sidebar-layout-default-sidebar-width 4rem
--sidebar-layout-sidebar-expanded-width
--sidebar-layout-default-sidebar-expanded-width 18rem
--sidebar-layout-sidebar-inline-padding
--sidebar-layout-default-sidebar-inline-padding 10px
--sidebar-layout-background
--sidebar-layout-default-background var(--global-color-bg)
--sidebar-layout-skip-background
--sidebar-layout-default-skip-background var(--global-color-surface-pop)
--sidebar-layout-skip-color
--sidebar-layout-default-skip-color var(--global-color-text)
--sidebar-layout-sidebar-gap
--sidebar-layout-default-sidebar-gap 8px
--sidebar-layout-sidebar-padding
--sidebar-layout-default-sidebar-padding 0 0 14px
--sidebar-layout-sidebar-border
--sidebar-layout-default-sidebar-border var(--global-border-width) solid var(--global-color-border)
--sidebar-layout-sidebar-background
--sidebar-layout-default-sidebar-background var(--global-color-surface)
--sidebar-layout-sidebar-overlay-background
--sidebar-layout-default-sidebar-overlay-background var(--global-color-surface-pop)
--sidebar-layout-sidebar-overlay-box-shadow
--sidebar-layout-default-sidebar-overlay-box-shadow var(--global-shadow-lg)
--sidebar-layout-header-height
--sidebar-layout-default-header-height 3.5rem
--sidebar-layout-dock-background
--sidebar-layout-default-dock-background transparent
--sidebar-layout-dock-border-color
--sidebar-layout-default-dock-border-color transparent
--sidebar-layout-dock-color
--sidebar-layout-default-dock-color var(--global-color-text-2)
--sidebar-layout-dock-hover-background
--sidebar-layout-default-dock-hover-background var(--global-color-surface-2)
--sidebar-layout-dock-hover-color
--sidebar-layout-default-dock-hover-color var(--global-color-text)
--sidebar-layout-header-position
--sidebar-layout-default-header-position sticky
--sidebar-layout-header-gap
--sidebar-layout-default-header-gap 12px
--sidebar-layout-header-padding
--sidebar-layout-default-header-padding 0 2rem
--sidebar-layout-header-border
--sidebar-layout-default-header-border var(--global-border-width) solid var(--global-color-border)
--sidebar-layout-header-background
--sidebar-layout-default-header-background color-mix(in srgb, var(--global-color-bg) 80%, transparent)
--sidebar-layout-header-backdrop-filter
--sidebar-layout-default-header-backdrop-filter blur(16px)
--sidebar-layout-content-padding
--sidebar-layout-default-content-padding 1rem 2rem
--sidebar-layout-drawer-width
--sidebar-layout-default-drawer-width 18rem
--sidebar-layout-drawer-padding
--sidebar-layout-default-drawer-padding 0 0 14px