Notification preferences
A matrix of events and channels in a Table: every cell is a Checkbox, each column header has a Checkbox that sets the whole channel, and a Switch pauses everything for eight hours. Security alerts by email are locked on, with a note that explains why.
Notifications
Choose where Ledger reaches you for each kind of update.
Security alerts arrive even while paused.
| Event | |||
|---|---|---|---|
| MentionsSomeone @mentions you or your team. | |||
| CommentsReplies on items you created or follow. | |||
| AssignmentsAn item is assigned to you. | |||
| Weekly digestA Monday summary of your workspace. | |||
| Security alertsNew sign-ins and password changes. |
Security alerts always go to your email, so you can act quickly if someone else signs in.
Source · recipes/notification-preferences.tsx
'use client'
import {
BodySmall,
Button,
Card,
Checkbox,
Heading,
Stack,
Switch,
Table,
TableBody,
TableCell,
TableContainer,
TableHead,
TableHeader,
TableRow,
Text,
toastSuccess,
} from '@blocks/design-system'
import { Lock } from 'lucide-react'
import { useId, useState } from 'react'
import styles from './notification-preferences.module.css'
const channels = [
{ id: 'email', label: 'Email' },
{ id: 'push', label: 'Push' },
{ id: 'sms', label: 'SMS' },
] as const
type Channel = (typeof channels)[number]['id']
const events = [
{ id: 'mentions', label: 'Mentions', description: 'Someone @mentions you or your team.' },
{ id: 'comments', label: 'Comments', description: 'Replies on items you created or follow.' },
{ id: 'assignments', label: 'Assignments', description: 'An item is assigned to you.' },
{ id: 'digest', label: 'Weekly digest', description: 'A Monday summary of your workspace.' },
{ id: 'security', label: 'Security alerts', description: 'New sign-ins and password changes.' },
] as const
type EventId = (typeof events)[number]['id']
type Matrix = Record<EventId, Record<Channel, boolean>>
// Security alerts always go to email; that cell can't be turned off.
const isLocked = (event: EventId, channel: Channel) => event === 'security' && channel === 'email'
const initialMatrix: Matrix = {
mentions: { email: true, push: true, sms: false },
comments: { email: true, push: false, sms: false },
assignments: { email: true, push: true, sms: false },
digest: { email: true, push: false, sms: false },
security: { email: true, push: true, sms: true },
}
type Cell = [EventId, Channel]
const allCells: Cell[] = events.flatMap((event) => channels.map((channel): Cell => [event.id, channel.id]))
const columnCells = (channel: Channel) => allCells.filter(([, cellChannel]) => cellChannel === channel)
// A notification matrix: one checkbox per event and channel, plus a checkbox
// in each column header that sets the whole channel (mixed when only some are on).
export default function NotificationPreferences() {
const titleId = useId()
const pauseNoteId = useId()
const lockedNoteId = useId()
const [matrix, setMatrix] = useState(initialMatrix)
const [pausedUntil, setPausedUntil] = useState<string | null>(null)
const paused = pausedUntil !== null
// The combined state of a set of cells, ignoring the locked one.
function summarize(cells: Cell[]) {
const editable = cells.filter(([event, channel]) => !isLocked(event, channel))
const on = editable.filter(([event, channel]) => matrix[event][channel]).length
return { checked: on === editable.length, indeterminate: on > 0 && on < editable.length }
}
function setCells(cells: Cell[], checked: boolean) {
setMatrix((current) => {
const next = structuredClone(current)
for (const [event, channel] of cells) {
if (!isLocked(event, channel)) next[event][channel] = checked
}
return next
})
}
function pause(checked: boolean) {
// Computed in the handler, never during render, so server and client agree.
const until = new Date(Date.now() + 8 * 60 * 60 * 1000)
setPausedUntil(checked ? until.toLocaleTimeString([], { hour: 'numeric', minute: '2-digit' }) : null)
}
return (
<Card
as="section"
aria-labelledby={titleId}
padding={{ base: 'md', sm: 'lg' }}
borderRadius="xl"
dangerouslySetClassName={styles.preferences}
>
<Stack gap="lg">
<Stack gap="xs">
<Heading as="h3" id={titleId} fontSize="lg" margin="none">
Notifications
</Heading>
<BodySmall color="text-muted" margin="none">
Choose where Ledger reaches you for each kind of update.
</BodySmall>
</Stack>
<div className={styles.pause} data-paused={paused || undefined}>
<Switch checked={paused} onCheckedChange={pause} aria-describedby={pauseNoteId}>
<Text fontWeight="semibold" fontSize="sm">
Pause all for 8 hours
</Text>
</Switch>
<BodySmall id={pauseNoteId} color="text-muted" margin="none" role="status">
{paused
? `Paused until ${pausedUntil}. Security alerts still arrive.`
: 'Security alerts arrive even while paused.'}
</BodySmall>
</div>
<TableContainer tabIndex={0} role="region" aria-labelledby={titleId}>
<Table dangerouslySetClassName={styles.matrix}>
<TableHeader>
<TableRow>
<TableHead scope="col">Event</TableHead>
{channels.map((channel) => {
const column = summarize(columnCells(channel.id))
return (
<TableHead key={channel.id} scope="col" dangerouslySetClassName={styles.choice}>
<Checkbox
checked={column.checked}
indeterminate={column.indeterminate}
disabled={paused}
onCheckedChange={(checked) => setCells(columnCells(channel.id), checked)}
>
<span className={styles.channel}>{channel.label}</span>
</Checkbox>
</TableHead>
)
})}
</TableRow>
</TableHeader>
<TableBody>
{events.map((event) => (
<TableRow key={event.id}>
<TableHead scope="row">
<span className={styles.event}>{event.label}</span>
<span className={styles.description}>{event.description}</span>
</TableHead>
{channels.map((channel) => {
const locked = isLocked(event.id, channel.id)
return (
<TableCell key={channel.id} dangerouslySetClassName={styles.choice}>
{locked ? (
// Read-only rather than disabled: it stays on, at full strength, even while paused.
<span className={styles.cell}>
<Checkbox
aria-label={`${event.label} by ${channel.label}, always on`}
aria-describedby={lockedNoteId}
checked
readOnly
/>
<Lock aria-hidden size={12} className={styles.lock} />
</span>
) : (
<Checkbox
aria-label={`${event.label} by ${channel.label}`}
checked={matrix[event.id][channel.id]}
disabled={paused}
onCheckedChange={(checked) => setCells([[event.id, channel.id]], checked)}
/>
)}
</TableCell>
)
})}
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
<p id={lockedNoteId} className={styles.footnote}>
<Lock aria-hidden size={12} />
Security alerts always go to your email, so you can act quickly if someone else signs in.
</p>
<Stack direction="row" justify="end">
<Button type="button" variant="primary" onClick={() => toastSuccess('Notification preferences saved')}>
Save preferences
</Button>
</Stack>
</Stack>
</Card>
)
}How it works
- Columns summarize themselves. A channel's header checkbox is checked when every cell below it is on and
indeterminatewhen only some are, and it skips the locked cell when counting or toggling. - Every cell has a name. Cell checkboxes have no visible label, so each gets an
aria-labelsuch as “Mentions by Push”. The locked cell isreadOnlyrather than disabled, so it reads as on at full strength even while paused; its name ends “always on”, a lock icon sits beside it and the footnote under the table describes it, so the reason isn’t only visual. - Pause disables, it doesn’t forget. The switch disables the matrix without clearing it, and its status line says until when, a time computed in the click handler rather than during render.
- It fits a phone. Channel headers stack the label under the checkbox, the columns stay narrow and a container query hides the event descriptions below about 480px, so the table fits about 340px without scrolling. It still sits in a labelled, focusable
TableContainerin case it ever needs to scroll.
App-owned CSS
The pause panel and the compact matrix.
/* The pause row and a compact matrix that fits a phone without scrolling. */
.preferences {
container-type: inline-size;
}
.pause {
display: grid;
gap: var(--ds-space-sm);
padding: var(--ds-space-md) var(--ds-space-lg);
background: var(--ds-color-bg-subtle);
border: var(--ds-border-width-sm) solid var(--ds-color-border-subtle);
border-radius: var(--ds-border-radius-md);
&[data-paused] {
background: var(--ds-color-warning-subtle);
border-color: color-mix(in srgb, var(--ds-color-warning) 40%, transparent);
}
}
/* Event names and descriptions wrap; the package's headers don't. */
.matrix th {
white-space: normal;
}
/* "Event" sits on the same baseline as the channel names under their checkboxes. */
.matrix thead th {
vertical-align: bottom;
}
.matrix .choice {
width: 3.75rem;
padding-inline: var(--ds-space-sm);
text-align: center;
vertical-align: middle;
}
/* Column headers put the label under its checkbox to keep columns narrow. */
.choice label {
flex-direction: column;
gap: var(--ds-space-xs);
}
.channel {
font-size: var(--ds-font-size-xs);
font-weight: var(--ds-font-weight-semibold);
color: var(--ds-color-text-muted);
}
.event {
display: block;
font-size: var(--ds-font-size-sm);
font-weight: var(--ds-font-weight-semibold);
color: var(--ds-color-text);
}
.description {
display: block;
font-size: var(--ds-font-size-xs);
font-weight: var(--ds-font-weight-body);
color: var(--ds-color-text-muted);
}
.cell {
position: relative;
display: inline-flex;
}
/* Beside the box, not over it; the column has room to its right. */
.lock {
position: absolute;
top: 50%;
left: calc(100% + var(--ds-space-sm));
translate: 0 -50%;
color: var(--ds-color-text-muted);
}
/* On a phone the event names keep the room; the footnote and labels still explain each row. */
@container (max-width: 30rem) {
.description {
display: none;
}
}
.footnote {
display: flex;
align-items: baseline;
gap: var(--ds-space-sm);
margin: 0;
font-size: var(--ds-font-size-xs);
color: var(--ds-color-text-muted);
& svg {
flex: none;
translate: 0 1px;
}
}