Sidebar
The navigation of an application in groups, collapsed off the page or to a rail of icons, and in a sheet on a phone.
Previews are drawn in the theme you made.
Edit itA rail of icons #
import { Avatar, Recipes, Sidebar, SidebarSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import {
Got,
LogoStyle,
PersonStyle,
SheetStyle,
TipStyle,
WORKSPACE_ROWS,
sidebarState,
workspace,
} from '../../snippets/sidebar.js'
// Collapsed to a rail of icons named in tooltips (a Tooltip per row, made by
// `init`'s `rows`); on a phone the opener shows the navigation in a Sheet. A
// rail keeps each end's mark, centred over the icons.
export default example({
title: 'A rail of icons',
slots: SidebarSlots,
recipe: Recipes.Sidebar,
state: sidebarState(
Sidebar.init({ id: 'icons-sidebar', rows: WORKSPACE_ROWS, open: ['settings'] }),
),
view: ({ style, model, onMessage, h }) =>
h.div(
[h.Style({ blockSize: '26rem', inlineSize: 'min(42rem, 100%)' })],
[
Sidebar.view(
{
model: model.sidebar,
onMessage: message => onMessage(Got({ message })),
label: 'Workspace',
groups: workspace(h),
header: {
mark: Avatar.view({ name: 'Acme', style: LogoStyle }, h),
content: h.strong([], ['Acme']),
},
footer: {
mark: Avatar.view({ name: 'Ada Lovelace', style: PersonStyle }, h),
content: h.span([], ['ada@acme.dev']),
},
collapse: 'icons',
style,
mixins: Sidebar.fluid,
tooltip: [TipStyle.mixin],
sheet: [SheetStyle.mixin],
},
h,
),
],
),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
Resizable #
The page.
import { Recipes, Sidebar, SidebarSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import {
Got,
SheetStyle,
TipStyle,
WORKSPACE_ROWS,
sidebarState,
workspace,
} from '../../snippets/sidebar.js'
// Its width is the sidebar's Model: drag the edge or use the arrows on it,
// and below half the least width it collapses, coming back at the width the
// drag began at.
export default example({
title: 'Resizable',
slots: SidebarSlots,
recipe: Recipes.Sidebar,
state: sidebarState(
Sidebar.init({
id: 'resizable-sidebar',
rows: WORKSPACE_ROWS,
open: ['settings'],
width: { size: 224, min: 176, max: 320 },
}),
),
view: ({ style, model, onMessage, h }) =>
h.div(
[h.Style({ display: 'flex', blockSize: '24rem', inlineSize: 'min(40rem, 100%)' })],
[
Sidebar.view(
{
model: model.sidebar,
onMessage: message => onMessage(Got({ message })),
label: 'Workspace',
groups: workspace(h),
collapse: 'icons',
style,
mixins: Sidebar.fluid,
tooltip: [TipStyle.mixin],
sheet: [SheetStyle.mixin],
},
h,
),
h.p([h.Style({ margin: '0', padding: '0.75rem' })], ['The page.']),
],
),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
Floating, with actions #
Hover a project for its actions.
import { Option, Schema } from 'effect'
import * as Command from 'foldkit/command'
import type { HtmlBuilder } from 'foldkit/html'
import { defineMessageUnion } from 'foldkit/message'
import { modifyFields } from 'foldkit/struct'
import { Kbd, Recipes, Sidebar, SidebarSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { outline } from '../../snippets/icon.js'
import { SheetStyle } from '../../snippets/sidebar.js'
const Floating = Recipes.Sidebar.style({ variant: 'floating' })
const Key = Recipes.Kbd.style()
const Model = Schema.Struct({ sidebar: Sidebar.Model, acted: Schema.Option(Schema.String) })
const Message = defineMessageUnion({
Got: { message: Sidebar.Message },
Acted: { what: Schema.String },
})
const projects = <Outer>(h: HtmlBuilder<Outer>, acted: (what: string) => Outer) => {
const more = (name: string) => ({
label: `More for ${name}`,
icon: outline(
h,
'M3 8a.5.5 0 101 0 .5.5 0 00-1 0zM7.5 8a.5.5 0 101 0 .5.5 0 00-1 0zM12 8a.5.5 0 101 0 .5.5 0 00-1 0z',
),
message: acted(`More for ${name}`),
})
const project = (id: string, label: string, badge?: string) => ({
id,
label,
href: '#',
...(badge === undefined ? {} : { badge }),
icon: outline(h, 'M6.5 3L5 13M11 3l-1.5 10M3 6.5h10M3 9.5h10'),
action: more(label),
})
const groups: ReadonlyArray<Sidebar.SidebarGroup<Outer>> = [
{
id: 'main',
rows: [
{
id: 'home',
label: 'Home',
href: '#',
current: true,
icon: outline(h, 'M2 7l6-5 6 5v7H2zM6 14V9h4v5'),
// Html at the row's end: here the key that goes there.
trailing: Kbd.view({ key: 'H', style: Key }, h),
},
{
id: 'inbox',
label: 'Inbox',
href: '#',
badge: '4',
icon: outline(h, 'M2 9h3l1 2h4l1-2h3M2 9l2-6h8l2 6v4H2z'),
},
],
},
{
id: 'projects',
title: 'Projects',
action: {
label: 'New project',
icon: outline(h, 'M8 3v10M3 8h10'),
message: acted('New project'),
},
rows: [
project('design', 'Design engineering', '8'),
project('sales', 'Sales and marketing'),
project('travel', 'Travel', '2'),
],
},
]
return groups
}
// Lifted off the page's edge as a card. A group's action adds to it; a row's
// takes the place of its count under the pointer (both show on a touch screen).
export default example({
title: 'Floating, with actions',
slots: SidebarSlots,
recipe: Recipes.Sidebar,
state: {
Model,
Message,
init: () => ({
model: {
sidebar: Sidebar.init({ id: 'floating-sidebar', rows: [], open: ['projects'] }),
acted: Option.none(),
},
}),
update: (model, message) =>
Message.match(message, {
Got: ({ message }) => {
const next = Sidebar.update(model.sidebar, message)
return {
model:
next.model === model.sidebar
? model
: modifyFields(model, { sidebar: () => next.model }),
commands: Command.mapMessages(next.commands ?? [], message => Message.Got({ message })),
}
},
Acted: ({ what }) => ({ model: modifyFields(model, { acted: () => Option.some(what) }) }),
}),
},
view: ({ model, onMessage, h }) =>
h.div(
[h.Style({ display: 'flex', blockSize: '26rem', inlineSize: 'min(42rem, 100%)' })],
[
Sidebar.view(
{
model: model.sidebar,
onMessage: message => onMessage(Message.Got({ message })),
label: 'Projects',
groups: projects(h, what => onMessage(Message.Acted({ what }))),
style: Floating,
mixins: Sidebar.fluid,
sheet: [SheetStyle.mixin],
},
h,
),
h.p(
[h.Style({ margin: '0', padding: '1rem' })],
[Option.getOrElse(model.acted, () => 'Hover a project for its actions.')],
),
],
),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
Inset #
Inbox
12 unread, 3 flagged
The page stands on a card of its own.
import { Card, Recipes, Sidebar, SidebarSlots } from 'foldkit-mixins-ui'
import { Theme } from 'foldkit-mixins/theme'
import { example } from '../../gallery/example.js'
import { Got, SheetStyle, sidebarState, workspace } from '../../snippets/sidebar.js'
const Inset = Recipes.Sidebar.style({ variant: 'inset' })
const Page = Recipes.Card.style({})
const token = Theme.ref(Theme.standard())
// Borderless, on the frame's muted surface; the page beside it is the card.
export default example({
title: 'Inset',
slots: SidebarSlots,
recipe: Recipes.Sidebar,
state: sidebarState(Sidebar.init({ id: 'inset-sidebar', rows: [], open: ['settings'] })),
view: ({ model, onMessage, h }) =>
h.div(
[
h.Style({
display: 'flex',
blockSize: '26rem',
inlineSize: 'min(42rem, 100%)',
boxSizing: 'border-box',
padding: token.space.xs,
gap: token.space['2xs'],
background: token.surface.muted,
borderRadius: token.radius.lg,
}),
],
[
Sidebar.view(
{
model: model.sidebar,
onMessage: message => onMessage(Got({ message })),
label: 'Workspace',
groups: workspace(h),
style: Inset,
mixins: Sidebar.fluid,
sheet: [SheetStyle.mixin],
},
h,
),
// A grid, so the card stands the frame's full height.
h.div(
[h.Style({ display: 'grid', flex: '1 1 0', minInlineSize: '0' })],
[
Card.view(
{
title: 'Inbox',
description: '12 unread, 3 flagged',
content: h.p([h.Style({ margin: '0' })], ['The page stands on a card of its own.']),
style: Page,
},
h,
),
],
),
],
),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
Collapsible sections #
Pages and layouts
import type { HtmlBuilder } from 'foldkit/html'
import { Avatar, Recipes, Sidebar, SidebarSlots } from 'foldkit-mixins-ui'
import { Theme } from 'foldkit-mixins/theme'
import { example } from '../../gallery/example.js'
import { outline } from '../../snippets/icon.js'
import { Got, LogoStyle, SheetStyle, sidebarState } from '../../snippets/sidebar.js'
const token = Theme.ref(Theme.standard())
const guide = (label: string, current = false) => ({ label, href: '#', current })
const docs = <Outer>(h: HtmlBuilder<Outer>): ReadonlyArray<Sidebar.SidebarGroup> => [
{
id: 'start',
title: 'Getting started',
rows: [
{
id: 'install',
label: 'Installation',
href: '#',
icon: outline(h, 'M8 2v8M5 7l3 3 3-3M3 13h10'),
},
{
id: 'structure',
label: 'Project structure',
href: '#',
icon: outline(h, 'M2 8l6 3 6-3M8 2l6 3-6 3-6-3z'),
},
],
},
{
id: 'build',
title: 'Building your app',
rows: [
{
id: 'routing',
label: 'Routing',
icon: outline(
h,
'M4 13a1.5 1.5 0 100-3 1.5 1.5 0 000 3zM12 6a1.5 1.5 0 100-3 1.5 1.5 0 000 3zM5.5 11.5H8a2.5 2.5 0 002.5-2.5V5.5',
),
items: [guide('Defining routes'), guide('Pages and layouts', true), guide('Linking')],
},
{
id: 'styling',
label: 'Styling',
href: '#',
badge: 'New',
icon: outline(h, 'M3 13l1-3 7-7 2 2-7 7zM9.5 4.5l2 2'),
},
],
},
{
id: 'reference',
title: 'API reference',
rows: [
{
id: 'components',
label: 'Components',
href: '#',
icon: outline(h, 'M5 4L1 8l4 4M11 4l4 4-4 4'),
},
],
},
]
// Each heading opens and shuts its group, several at once, and a branch nests
// its pages. A group shuts over the time it opens in.
export default example({
title: 'Collapsible sections',
slots: SidebarSlots,
recipe: Recipes.Sidebar,
state: sidebarState(
Sidebar.init({ id: 'sections-sidebar', rows: [], open: ['start', 'build', 'routing'] }),
),
view: ({ style, model, onMessage, h }) =>
h.div(
[h.Style({ display: 'flex', blockSize: '26rem', inlineSize: 'min(42rem, 100%)' })],
[
Sidebar.view(
{
model: model.sidebar,
onMessage: message => onMessage(Got({ message })),
label: 'Documentation',
groups: docs(h),
header: {
mark: Avatar.view({ name: 'Docs', initials: 'D', style: LogoStyle }, h),
content: h.span(
[h.Style({ display: 'grid', lineHeight: '1.25' })],
[
h.strong([], ['Documentation']),
h.small([h.Style({ color: token.text.muted })], ['v1.0.1']),
],
),
},
style,
mixins: Sidebar.fluid,
sheet: [SheetStyle.mixin],
},
h,
),
h.p([h.Style({ margin: '0', padding: '1rem' })], ['Pages and layouts']),
],
),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
Team switcher and account #
The Acme Inc inbox.
import { Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import * as Command from 'foldkit/command'
import { Avatar, Bundles, Menu, MenuSlots, Recipes, Sidebar, SidebarSlots } from 'foldkit-mixins-ui'
import { Theme } from 'foldkit-mixins/theme'
import { example } from '../../gallery/example.js'
import { outline } from '../../snippets/icon.js'
import {
LogoStyle,
PersonStyle,
TipStyle,
WORKSPACE_ROWS,
workspace,
} from '../../snippets/sidebar.js'
import { forSlots } from '../../snippets/theme.js'
const token = Theme.ref(Theme.standard())
const TEAMS: ReadonlyArray<Menu.Row> = ['Acme Inc', 'Acme Corp.', 'Evil Corp.'].map(team => ({
value: team,
label: team,
}))
const ACCOUNT: ReadonlyArray<Menu.Row> = ['Account', 'Notifications', 'Log out'].map(row => ({
value: row,
label: row,
}))
// A menu whose trigger is the whole end beside its mark, as wide as the column.
const EndMenu = forSlots(MenuSlots)(
Recipes.Menu.extend({
base: {
wrapper: { display: 'block', inlineSize: '100%' },
button: {
inlineSize: '100%',
justifyContent: 'space-between',
gap: token.space.xs,
paddingInline: token.space.xs,
border: '0',
textAlign: 'start',
fontWeight: 'inherit',
},
},
})({}),
)
// The header switches teams and the footer opens the account's menu, each a
// Menu Bundle; a rail keeps their marks.
const App = Bundle.compose({ team: Schema.String, sidebar: Sidebar.Model }).pipe(
Bundle.withMessages({ GotSidebar: { message: Sidebar.Message } }),
Bundle.withChild('teams', Bundles.Menu, {
args: { id: 'switcher-teams', rows: TEAMS, isAnimated: true },
onOut: out => model => ({
model: { ...model, team: Menu.OutMessage.match(out, { Selected: ({ value }) => value }) },
}),
}),
Bundle.withChild('account', Bundles.Menu, {
args: { id: 'switcher-account', rows: ACCOUNT, isAnimated: true },
onOut: Bundle.ignore,
}),
)
export default example({
title: 'Team switcher and account',
slots: SidebarSlots,
recipe: Recipes.Sidebar,
state: App.placements.runtime({
Model: App.Model,
initial: {
team: 'Acme Inc',
sidebar: Sidebar.init({ id: 'switcher-sidebar', rows: WORKSPACE_ROWS }),
},
update: App.placements.update((model, { message }) => {
const next = Sidebar.update(model.sidebar, message)
return {
model: next.model === model.sidebar ? model : { ...model, sidebar: next.model },
commands: Command.mapMessages(next.commands ?? [], message =>
App.Message.GotSidebar({ message }),
),
}
}),
}),
view: ({ style, model, onMessage, h }) => {
const upDown = outline(h, 'M5 6l3-3 3 3M5 10l3 3 3-3')
const lines = (first: string, second: string) =>
h.span(
[h.Style({ display: 'grid', minInlineSize: '0', lineHeight: '1.25' })],
[h.strong([], [first]), h.small([h.Style({ color: token.text.muted })], [second])],
)
return h.div(
[h.Style({ display: 'flex', blockSize: '26rem', inlineSize: 'min(42rem, 100%)' })],
[
Sidebar.view(
{
model: model.sidebar,
onMessage: message => onMessage(App.Message.GotSidebar({ message })),
label: 'Workspace',
groups: workspace(h),
header: {
mark: Avatar.view({ name: model.team, style: LogoStyle }, h),
content: Menu.view(
{
model: model.teams,
rows: TEAMS,
buttonContent: h.span(
[h.Style({ display: 'contents' })],
[lines(model.team, 'Enterprise'), upDown],
),
onMessage: message => onMessage(App.Message.GotTeamsMessage({ message })),
mixins: [EndMenu.mixin, ...Menu.fluid],
},
h,
),
},
footer: {
mark: Avatar.view({ name: 'Ada Lovelace', style: PersonStyle }, h),
content: Menu.view(
{
model: model.account,
rows: ACCOUNT,
buttonContent: h.span(
[h.Style({ display: 'contents' })],
[lines('Ada Lovelace', 'ada@acme.dev'), upDown],
),
onMessage: message => onMessage(App.Message.GotAccountMessage({ message })),
mixins: [EndMenu.mixin, ...Menu.fluid],
},
h,
),
},
collapse: 'icons',
style,
mixins: Sidebar.fluid,
tooltip: [TipStyle.mixin],
},
h,
),
h.p([h.Style({ margin: '0', padding: '1rem' })], [`The ${model.team} inbox.`]),
],
)
},
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
Usage
Each example above is a module of its own, shown whole and run as shown, over the theme module Create a theme writes. The page hands it the style picked above.
Related
- Right to left The direction switch above turns the preview over; this is what each widget reads.