feat(ui): notice warning tone, glyph and non-live option
This commit is contained in:
1 parent
7575c67c47
commit
f061111e25
3 files changed
+67
-5
No files matched your search
@@ -21,6 +21,7 @@ export type IconName =
|
||||
| 'chevron-right'
|
||||
| 'chevron-down'
|
||||
| 'external'
|
||||
| 'wifi-off'
|
||||
|
||||
/** Stroke paths on a 24-unit grid; decorative by default (aria-hidden). */
|
||||
const PATHS: Record<IconName, string> = {
|
||||
@@ -42,6 +43,8 @@ const PATHS: Record<IconName, string> = {
|
||||
'chevron-down': 'M6 9l6 6 6-6',
|
||||
external: 'M14 4h6v6M20 4l-9 9M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5',
|
||||
lock: 'M6 11h12v9H6zM8 11V8a4 4 0 0 1 8 0v3',
|
||||
'wifi-off':
|
||||
'M3 3l18 18M8.5 16.1a6 6 0 0 1 7 0M5 12.5a11 11 0 0 1 5.2-2.4M16.7 11a11 11 0 0 1 2.3 1.5M1.5 9a16 16 0 0 1 4.6-2.9M10.7 5a16 16 0 0 1 11.8 4M12 20h.01',
|
||||
alert: 'M12 9v4M12 17h.01M10.3 3.9L2.8 17a2 2 0 0 0 1.7 3h15a2 2 0 0 0 1.7-3L13.7 3.9a2 2 0 0 0-3.4 0z',
|
||||
google:
|
||||
'M21 12.2c0-.7-.1-1.3-.2-1.9H12v3.6h5.1a4.4 4.4 0 0 1-1.9 2.9v2.4h3.1c1.8-1.7 2.7-4.1 2.7-7zM12 21c2.4 0 4.5-.8 6-2.2l-3.1-2.4c-.8.6-1.8.9-2.9.9-2.3 0-4.2-1.5-4.9-3.6H4v2.5A9 9 0 0 0 12 21zM7.1 13.7a5.4 5.4 0 0 1 0-3.4V7.8H4a9 9 0 0 0 0 8.4zM12 6.6c1.3 0 2.5.5 3.4 1.3l2.6-2.6A9 9 0 0 0 4 7.8l3.1 2.5c.7-2.1 2.6-3.7 4.9-3.7z',
|
||||
|
||||
@@ -1,27 +1,33 @@
|
||||
import type { ReactNode } from 'react'
|
||||
import Icon from './Icon'
|
||||
import Icon, { type IconName } from './Icon'
|
||||
|
||||
export interface NoticeProps {
|
||||
tone: 'success' | 'error' | 'info'
|
||||
tone: 'success' | 'error' | 'info' | 'warning'
|
||||
children: ReactNode
|
||||
className?: string
|
||||
/** The glyph; `check` for success and `alert` otherwise when omitted. */
|
||||
icon?: IconName
|
||||
/** False for a plain line (no live region), e.g. one of many cards failing at load. */
|
||||
live?: boolean
|
||||
}
|
||||
|
||||
const TONE: Record<NoticeProps['tone'], string> = {
|
||||
success: 'border-[var(--severity-mild)]/40 bg-[color-mix(in_srgb,var(--severity-mild)_10%,white)] text-[var(--color-ink)]',
|
||||
error: 'border-[var(--color-danger)]/40 bg-[color-mix(in_srgb,var(--color-danger)_8%,white)] text-[var(--color-danger-strong)]',
|
||||
info: 'border-[var(--color-border)] bg-[var(--color-surface-strong)] text-[var(--color-ink)]',
|
||||
warning: 'border-[var(--severity-moderate)]/40 bg-[color-mix(in_srgb,var(--severity-moderate)_8%,white)] text-[var(--color-ink)]',
|
||||
}
|
||||
|
||||
/** Inline status line: icon + text, never colour alone. */
|
||||
export default function Notice({ tone, children, className = '' }: NoticeProps) {
|
||||
export default function Notice({ tone, children, className = '', icon, live = true }: NoticeProps) {
|
||||
const role = tone === 'error' ? 'alert' : 'status'
|
||||
return (
|
||||
<div
|
||||
role={tone === 'error' ? 'alert' : 'status'}
|
||||
role={live ? role : undefined}
|
||||
className={`flex items-start gap-2 rounded-lg border px-3 py-2 text-sm ${TONE[tone]} ${className}`}
|
||||
>
|
||||
<Icon
|
||||
name={tone === 'success' ? 'check' : 'alert'}
|
||||
name={icon ?? (tone === 'success' ? 'check' : 'alert')}
|
||||
size={18}
|
||||
className="mt-0.5 shrink-0"
|
||||
/>
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
import { afterEach, describe, expect, it } from 'vitest'
|
||||
import { cleanup, render, screen } from '@testing-library/react'
|
||||
import Notice from '@/components/ui/Notice'
|
||||
|
||||
afterEach(() => cleanup())
|
||||
|
||||
/** The path of the glyph a notice draws (its only svg path). */
|
||||
function glyph(container: HTMLElement): string | null | undefined {
|
||||
return container.querySelector('svg path')?.getAttribute('d')
|
||||
}
|
||||
|
||||
function glyphOf(icon: 'alert' | 'check' | 'wifi-off'): string | null | undefined {
|
||||
const { container, unmount } = render(<Notice tone="info" icon={icon}>x</Notice>)
|
||||
const d = glyph(container)
|
||||
unmount()
|
||||
return d
|
||||
}
|
||||
|
||||
describe('Notice', () => {
|
||||
it('keeps today\'s roles and glyphs by default', () => {
|
||||
const alert = glyphOf('alert')
|
||||
const check = glyphOf('check')
|
||||
const { container: error } = render(<Notice tone="error">bad</Notice>)
|
||||
expect(screen.getByRole('alert')).toHaveTextContent('bad')
|
||||
expect(glyph(error)).toBe(alert)
|
||||
cleanup()
|
||||
const { container: success } = render(<Notice tone="success">ok</Notice>)
|
||||
expect(screen.getByRole('status')).toHaveTextContent('ok')
|
||||
expect(glyph(success)).toBe(check)
|
||||
cleanup()
|
||||
render(<Notice tone="info">note</Notice>)
|
||||
expect(screen.getByRole('status')).toHaveTextContent('note')
|
||||
})
|
||||
|
||||
it('drops the live role when live is false, for every tone', () => {
|
||||
for (const tone of ['error', 'success', 'info', 'warning'] as const) {
|
||||
render(<Notice tone={tone} live={false}>{tone}</Notice>)
|
||||
expect(screen.queryByRole('alert')).toBeNull()
|
||||
expect(screen.queryByRole('status')).toBeNull()
|
||||
expect(screen.getByText(tone)).toBeInTheDocument()
|
||||
cleanup()
|
||||
}
|
||||
})
|
||||
|
||||
it('draws the glyph it is given, the warning tone included', () => {
|
||||
const wifiOff = glyphOf('wifi-off')
|
||||
expect(wifiOff).toBeTruthy()
|
||||
expect(wifiOff).not.toBe(glyphOf('alert'))
|
||||
const { container } = render(<Notice tone="warning" icon="wifi-off">offline</Notice>)
|
||||
expect(glyph(container)).toBe(wifiOff)
|
||||
expect(screen.getByRole('status')).toHaveTextContent('offline')
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user