Toast on sonner's behaviour; Dashboard figures as the canvas's KPI strip
Toast: sonner draws the notice and runs its clock, pauses it under the pointer and removes it. ui/Toast keeps the live region and the action with its permission check. Gone, because sonner has none of it: holding the clock while the keyboard focus is inside, restarting it when the notice gains a rule, Escape, and handing the focus back to a named element. The two Rules tests and three Toast tests about those are removed. Dashboard: the eleven figures are one KPI strip, every cell the same weight, an icon tile on the left, the name, the figure at 24 px and under it the share and the change. The sparklines and the larger Total Queries card are gone; a card still opens Query Logs. Check.module.css gets the switch drawing back: Blocking > Lists composes it, and dev/css-dead.mjs reported the missing class. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
1 parent
4c0a5de7e7
commit
f24f4972ba
9 files changed
+170
-352
No files matched your search
@@ -43,3 +43,58 @@
|
||||
*/
|
||||
max-width: 720px;
|
||||
}
|
||||
|
||||
/*
|
||||
The switch. It is the same `input[type=checkbox]`, redrawn: the track is the
|
||||
input's own box and the knob is its offset inset shadow, so neither an extra
|
||||
element nor a pseudo-element the browser paints its own way is needed.
|
||||
*/
|
||||
.toggle input {
|
||||
width: var(--ctrl-track-w);
|
||||
height: var(--ctrl-track-h);
|
||||
border-radius: var(--r-full);
|
||||
background: var(--field);
|
||||
background-image: none;
|
||||
box-shadow:
|
||||
var(--sunken),
|
||||
inset calc(var(--ctrl-track-w) - var(--ctrl-track-h)) 0 0 -1px transparent;
|
||||
position: relative;
|
||||
transition:
|
||||
background-color var(--t-fast),
|
||||
border-color var(--t-fast);
|
||||
}
|
||||
|
||||
/* The knob: a circle that travels from one end of the track to the other. */
|
||||
.toggle input::after {
|
||||
content: '';
|
||||
position: absolute;
|
||||
top: 2px;
|
||||
left: 2px;
|
||||
width: calc(var(--ctrl-track-h) - 6px);
|
||||
height: calc(var(--ctrl-track-h) - 6px);
|
||||
border-radius: var(--r-full);
|
||||
background: var(--mute);
|
||||
box-shadow: var(--shadow-knob);
|
||||
transition: transform var(--t-medium), background var(--t-fast);
|
||||
}
|
||||
|
||||
.toggle input:checked {
|
||||
background-color: var(--acc);
|
||||
background-image: none;
|
||||
border-color: var(--acc-ink);
|
||||
box-shadow: var(--edge-strong);
|
||||
}
|
||||
|
||||
.toggle input:checked::after {
|
||||
background: var(--on-acc);
|
||||
transform: translateX(calc(var(--ctrl-track-w) - var(--ctrl-track-h)));
|
||||
}
|
||||
|
||||
.toggle input:active:not(:disabled) { transform: none; }
|
||||
|
||||
.toggle .help { margin-left: calc(var(--ctrl-track-w) + var(--s-6)); }
|
||||
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.toggle input,
|
||||
.toggle input::after { transition: none; }
|
||||
}
|
||||
+27
-43
@@ -6,23 +6,22 @@ import { Toast } from './Toast'
|
||||
|
||||
afterEach(() => vi.useRealTimers())
|
||||
|
||||
function Host({ returnFocus, duration }: { returnFocus?: () => void; duration?: number }) {
|
||||
function Host({ duration, hold }: { duration?: number; hold?: boolean }) {
|
||||
const [open, setOpen] = useState(true)
|
||||
const [done, setDone] = useState(false)
|
||||
return (
|
||||
<>
|
||||
<input aria-label="Elsewhere" />
|
||||
<Toast
|
||||
message={open ? (done ? 'Done.' : 'Deleted.') : null}
|
||||
action={open && !done ? { label: 'Undo', onClick: () => setDone(true) } : undefined}
|
||||
duration={duration}
|
||||
returnFocus={returnFocus}
|
||||
onDismiss={() => setOpen(false)}
|
||||
/>
|
||||
</>
|
||||
<Toast
|
||||
message={open ? (done ? 'Done.' : 'Deleted.') : null}
|
||||
action={open && !done ? { label: 'Undo', onClick: () => setDone(true) } : undefined}
|
||||
duration={duration}
|
||||
hold={hold}
|
||||
onDismiss={() => setOpen(false)}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
/* The notice is ReUI's sonner: it draws it a moment after the state changes and keeps
|
||||
a closing one in the page while it animates out, so the controls are waited for. */
|
||||
describe('Toast', () => {
|
||||
it('its live region is in the page while closed, and empty', () => {
|
||||
render(<Toast message={null} onDismiss={() => {}} />)
|
||||
@@ -30,18 +29,11 @@ describe('Toast', () => {
|
||||
expect(screen.queryByRole('button')).toBeNull()
|
||||
})
|
||||
|
||||
it('says its message in the region, with a focusable action and a close button', async () => {
|
||||
it('says its message in the region, with an action and a close button', async () => {
|
||||
const run = vi.fn()
|
||||
render(<Toast message="Deleted." action={{ label: 'Undo', onClick: run }} onDismiss={() => {}} />)
|
||||
expect(screen.getByRole('status')).toHaveTextContent('Deleted.')
|
||||
/* sonner mounts the notice a moment after: the action is waited for. */
|
||||
await screen.findByRole('button', { name: 'Undo' })
|
||||
/* sonner's own wrapper is a tab stop before the action: one Tab more than the
|
||||
hand-made notice had. */
|
||||
await userEvent.tab()
|
||||
await userEvent.tab()
|
||||
expect(screen.getByRole('button', { name: 'Undo' })).toHaveFocus()
|
||||
await userEvent.keyboard('{Enter}')
|
||||
await userEvent.click(await screen.findByRole('button', { name: 'Undo' }))
|
||||
expect(run).toHaveBeenCalledOnce()
|
||||
expect(screen.getByRole('button', { name: 'Dismiss' })).toBeInTheDocument()
|
||||
})
|
||||
@@ -51,34 +43,26 @@ describe('Toast', () => {
|
||||
expect(await screen.findByRole('button', { name: 'Undo' })).toBeDisabled()
|
||||
})
|
||||
|
||||
it('goes by itself after its duration, and holds while the focus is inside', async () => {
|
||||
vi.useFakeTimers()
|
||||
it('the close button dismisses it', async () => {
|
||||
render(<Host />)
|
||||
await userEvent.click(await screen.findByRole('button', { name: 'Dismiss' }))
|
||||
expect(screen.getByRole('status')).toBeEmptyDOMElement()
|
||||
})
|
||||
|
||||
it('goes by itself after its duration', async () => {
|
||||
vi.useFakeTimers({ shouldAdvanceTime: true })
|
||||
render(<Host duration={1000} />)
|
||||
await act(async () => { await vi.advanceTimersByTimeAsync(100) })
|
||||
act(() => screen.getByRole('button', { name: 'Undo' }).focus())
|
||||
act(() => vi.advanceTimersByTime(5000))
|
||||
expect(screen.getByRole('status')).toHaveTextContent('Deleted.')
|
||||
act(() => screen.getByLabelText('Elsewhere').focus())
|
||||
act(() => vi.advanceTimersByTime(1000))
|
||||
await act(async () => { await vi.advanceTimersByTimeAsync(2000) })
|
||||
expect(screen.getByRole('status')).toBeEmptyDOMElement()
|
||||
})
|
||||
|
||||
it('Escape dismisses it, and the focus goes where the page says', async () => {
|
||||
const back = vi.fn(() => screen.getByLabelText('Elsewhere').focus())
|
||||
render(<Host returnFocus={back} />)
|
||||
await screen.findByRole('button', { name: 'Undo' })
|
||||
act(() => screen.getByRole('button', { name: 'Undo' }).focus())
|
||||
await userEvent.keyboard('{Escape}')
|
||||
expect(screen.getByRole('status')).toBeEmptyDOMElement()
|
||||
expect(back).toHaveBeenCalledOnce()
|
||||
expect(screen.getByLabelText('Elsewhere')).toHaveFocus()
|
||||
})
|
||||
|
||||
it('losing its action while focused gives the focus back too', async () => {
|
||||
const back = vi.fn(() => screen.getByLabelText('Elsewhere').focus())
|
||||
render(<Host returnFocus={back} />)
|
||||
await userEvent.click(await screen.findByRole('button', { name: 'Undo' }))
|
||||
expect(screen.getByRole('status')).toHaveTextContent('Done.')
|
||||
expect(back).toHaveBeenCalledOnce()
|
||||
it('stays open while it is held, whatever the duration', async () => {
|
||||
vi.useFakeTimers({ shouldAdvanceTime: true })
|
||||
render(<Host duration={1000} hold />)
|
||||
await act(async () => { await vi.advanceTimersByTimeAsync(100) })
|
||||
await act(async () => { await vi.advanceTimersByTimeAsync(5000) })
|
||||
expect(screen.getByRole('status')).toHaveTextContent('Deleted.')
|
||||
})
|
||||
})
|
||||
+45
-156
@@ -1,4 +1,4 @@
|
||||
import { useEffect, useLayoutEffect, useRef, useState, type ReactNode, type Ref } from 'react'
|
||||
import { useEffect, useRef, type ReactNode } from 'react'
|
||||
import { toast } from 'sonner'
|
||||
import { Toaster } from '@/components/ui/sonner'
|
||||
import { Icon } from './Icon'
|
||||
@@ -11,27 +11,19 @@ user, the deletion of rules in the table of Blocking › Rules (CONVENTIONS.md,
|
||||
deviation 2). Every other destructive action of the console still asks first
|
||||
(`ui/Confirm`).
|
||||
|
||||
What it owes, and why:
|
||||
It is ReUI's `sonner`: it places the notice, animates it, pauses its clock while the
|
||||
pointer is over it and takes it away when the clock runs out. What stays here:
|
||||
|
||||
- **The message is a live region that is always in the page** (`role="status"`),
|
||||
empty while closed. A region born together with its text is often not announced.
|
||||
- **The action is a real button**, reached with Tab and named by its text. When it
|
||||
needs a permission the session lacks it is drawn with the padlock and says which,
|
||||
as every `PermissionButton` does.
|
||||
- **It goes by itself after `duration`, but not while it is being used**: the
|
||||
pointer over it or the focus inside it holds the clock (WCAG 2.2.1), and the
|
||||
clock starts again whole when they leave. `timerKey` restarts it too, for a
|
||||
notice that gained something. Escape and the close button dismiss it.
|
||||
- **The focus is not lost with it.** If it closes, or loses its action, while the
|
||||
focus was inside it, the focus goes to `returnFocus`: the element that held it is
|
||||
gone, and the browser would leave it on the page's body.
|
||||
- **It moves only for those who want motion**: it rises into place, and with
|
||||
`prefers-reduced-motion` it simply appears.
|
||||
The notice's own copy of the text is `aria-hidden`, so it is said once.
|
||||
- **The action is a real button**, named by its text. When it needs a permission the
|
||||
session lacks it is drawn with the padlock and says which, as every
|
||||
`PermissionButton` does.
|
||||
|
||||
The notice itself is drawn by ReUI's `sonner` (placement, stacking, motion); the
|
||||
clock, the hold and the focus rules stay HERE, because sonner has none of them. Its
|
||||
text is `aria-hidden` there: the live region is a separate element that never leaves
|
||||
the page, so it is announced however sonner mounts its own.
|
||||
What this no longer does, because sonner does not: hold the clock while the keyboard
|
||||
focus is inside the notice, restart it when the notice gained something, and give the
|
||||
focus back to a named element when the notice goes.
|
||||
*/
|
||||
|
||||
export interface ToastAction {
|
||||
@@ -48,125 +40,27 @@ const DEFAULT_MS = 8000
|
||||
under the same one would be removed as soon as it appeared. */
|
||||
let counter = 0
|
||||
|
||||
/* What sonner draws: the text (hidden from assistive technology, the live region
|
||||
says it), the action and the dismiss button. */
|
||||
function Body({
|
||||
message,
|
||||
action,
|
||||
actionRef,
|
||||
onDismiss,
|
||||
onUse,
|
||||
onFocusIn,
|
||||
onFocusOut,
|
||||
}: {
|
||||
message: ReactNode
|
||||
action?: ToastAction
|
||||
actionRef?: Ref<HTMLButtonElement>
|
||||
onDismiss: () => void
|
||||
onUse: (using: boolean) => void
|
||||
onFocusIn: () => void
|
||||
onFocusOut: (to: Node | null) => void
|
||||
}) {
|
||||
const box = useRef<HTMLDivElement>(null)
|
||||
return (
|
||||
<div
|
||||
ref={box}
|
||||
className="flex w-(--width) max-w-[calc(100vw-2rem)] items-center gap-3 rounded-lg border border-border bg-popover py-2 pr-2 pl-4 text-sm text-popover-foreground shadow-md"
|
||||
onMouseEnter={() => onUse(true)}
|
||||
onMouseLeave={() => onUse(false)}
|
||||
onFocus={onFocusIn}
|
||||
onBlur={(e) => {
|
||||
const to = e.relatedTarget as Node | null
|
||||
if (to != null && box.current?.contains(to)) return
|
||||
onFocusOut(to)
|
||||
}}
|
||||
>
|
||||
<p className="m-0 min-w-0 flex-1 leading-snug break-words" aria-hidden="true">
|
||||
{message}
|
||||
</p>
|
||||
{action != null && (
|
||||
<PermissionButton
|
||||
ref={actionRef}
|
||||
size="sm"
|
||||
variant="primary"
|
||||
disabled={action.disabled}
|
||||
permission={action.permission}
|
||||
onClick={action.onClick}
|
||||
>
|
||||
{action.label}
|
||||
</PermissionButton>
|
||||
)}
|
||||
<button
|
||||
type="button"
|
||||
className="inline-flex size-7 shrink-0 items-center justify-center rounded-md border-0 bg-transparent text-muted-foreground transition-colors outline-none hover:bg-muted hover:text-foreground focus-visible:ring-3 focus-visible:ring-ring/50"
|
||||
aria-label="Dismiss"
|
||||
title="Dismiss"
|
||||
onClick={onDismiss}
|
||||
>
|
||||
<Icon name="close" size={14} />
|
||||
</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export function Toast({
|
||||
message,
|
||||
action,
|
||||
onDismiss,
|
||||
duration = DEFAULT_MS,
|
||||
timerKey,
|
||||
hold = false,
|
||||
returnFocus,
|
||||
actionRef,
|
||||
}: {
|
||||
/** `null` while closed. */
|
||||
message: ReactNode | null
|
||||
action?: ToastAction
|
||||
onDismiss: () => void
|
||||
duration?: number
|
||||
/** A new value restarts the clock. */
|
||||
timerKey?: unknown
|
||||
/** Keeps it open regardless of the clock: while its action is running, say. */
|
||||
hold?: boolean
|
||||
returnFocus?: () => void
|
||||
actionRef?: Ref<HTMLButtonElement>
|
||||
}) {
|
||||
const open = message != null
|
||||
const [pointer, setPointer] = useState(false)
|
||||
const [focused, setFocused] = useState(false)
|
||||
/* Whether the focus was inside when the content changed. A focused button that is
|
||||
removed leaves no blur with a destination, so only a blur to somewhere real
|
||||
clears it. */
|
||||
const hadFocus = useRef(false)
|
||||
const dismiss = useRef(onDismiss)
|
||||
const back = useRef(returnFocus)
|
||||
useEffect(() => {
|
||||
dismiss.current = onDismiss
|
||||
back.current = returnFocus
|
||||
})
|
||||
|
||||
/* Closing forgets the pointer and the focus: the box under them is gone, and no
|
||||
leave or blur may come to say so. Adjusted while rendering, as React advises
|
||||
for state that follows a prop. */
|
||||
const [wasOpen, setWasOpen] = useState(open)
|
||||
if (open !== wasOpen) {
|
||||
setWasOpen(open)
|
||||
if (!open) {
|
||||
setPointer(false)
|
||||
setFocused(false)
|
||||
}
|
||||
}
|
||||
|
||||
const paused = pointer || focused || hold
|
||||
useEffect(() => {
|
||||
if (!open || paused) return
|
||||
const t = setTimeout(() => dismiss.current(), duration)
|
||||
return () => clearTimeout(t)
|
||||
}, [open, paused, duration, timerKey])
|
||||
|
||||
/* The notice is shown, updated and removed through sonner. Its own clock is off
|
||||
(`Infinity`): the one above decides. */
|
||||
const hasAction = open && action != null
|
||||
const current = useRef<string | null>(null)
|
||||
useEffect(() => {
|
||||
if (!open) {
|
||||
@@ -175,25 +69,44 @@ export function Toast({
|
||||
return
|
||||
}
|
||||
current.current ??= `console-toast-${++counter}`
|
||||
const id = current.current
|
||||
toast.custom(
|
||||
() => (
|
||||
<Body
|
||||
message={message}
|
||||
action={action}
|
||||
actionRef={actionRef}
|
||||
onDismiss={() => dismiss.current()}
|
||||
onUse={setPointer}
|
||||
onFocusIn={() => {
|
||||
hadFocus.current = true
|
||||
setFocused(true)
|
||||
}}
|
||||
onFocusOut={(to) => {
|
||||
setFocused(false)
|
||||
if (to != null) hadFocus.current = false
|
||||
}}
|
||||
/>
|
||||
<div className="flex w-(--width) max-w-[calc(100vw-2rem)] items-center gap-3 rounded-lg border border-border bg-popover py-2 pr-2 pl-4 text-sm text-popover-foreground shadow-md">
|
||||
<p className="m-0 min-w-0 flex-1 leading-snug break-words" aria-hidden="true">
|
||||
{message}
|
||||
</p>
|
||||
{action != null && (
|
||||
<PermissionButton
|
||||
size="sm"
|
||||
variant="primary"
|
||||
disabled={action.disabled}
|
||||
permission={action.permission}
|
||||
onClick={action.onClick}
|
||||
>
|
||||
{action.label}
|
||||
</PermissionButton>
|
||||
)}
|
||||
<button
|
||||
type="button"
|
||||
className="inline-flex size-7 shrink-0 items-center justify-center rounded-md border-0 bg-transparent text-muted-foreground transition-colors outline-none hover:bg-muted hover:text-foreground focus-visible:ring-3 focus-visible:ring-ring/50"
|
||||
aria-label="Dismiss"
|
||||
title="Dismiss"
|
||||
onClick={() => dismiss.current()}
|
||||
>
|
||||
<Icon name="close" size={14} />
|
||||
</button>
|
||||
</div>
|
||||
),
|
||||
{ id: current.current, duration: Infinity, unstyled: true },
|
||||
{
|
||||
id,
|
||||
duration: hold ? Infinity : duration,
|
||||
unstyled: true,
|
||||
/* The clock running out, or the notice swiped away, is the page's business
|
||||
too: the screen forgets what the notice was about. */
|
||||
onAutoClose: () => dismiss.current(),
|
||||
onDismiss: () => dismiss.current(),
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
@@ -205,30 +118,6 @@ export function Toast({
|
||||
[],
|
||||
)
|
||||
|
||||
/* Once it is closed, or has lost its action, the button the focus was on is gone. */
|
||||
useLayoutEffect(() => {
|
||||
if (hasAction) return
|
||||
/* Sonner keeps a closing notice in the page while it animates out, so the
|
||||
element is still there: what says the focus is lost is that it WAS inside. */
|
||||
if (hadFocus.current) {
|
||||
hadFocus.current = false
|
||||
back.current?.()
|
||||
}
|
||||
}, [hasAction])
|
||||
|
||||
/* Escape dismisses it from wherever the focus is while it is open. */
|
||||
useEffect(() => {
|
||||
if (!open) return
|
||||
const escape = (e: KeyboardEvent) => {
|
||||
if (e.key === 'Escape') {
|
||||
e.preventDefault()
|
||||
dismiss.current()
|
||||
}
|
||||
}
|
||||
document.addEventListener('keydown', escape)
|
||||
return () => document.removeEventListener('keydown', escape)
|
||||
}, [open])
|
||||
|
||||
return (
|
||||
<>
|
||||
<Toaster position="bottom-center" />
|
||||
|
||||
Reference in new issue
Block a user