@cognitap/radiant 0.0.17 → 0.0.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -4
- package/dist/library/Accordion.js.map +1 -1
- package/dist/library/Alert.d.ts +37 -0
- package/dist/library/Alert.d.ts.map +1 -0
- package/dist/library/Alert.js +28 -0
- package/dist/library/Alert.js.map +1 -0
- package/dist/library/AlertDialog.js.map +1 -1
- package/dist/library/Button.js.map +1 -1
- package/dist/library/Card.js.map +1 -1
- package/dist/library/Checkbox.js.map +1 -1
- package/dist/library/Collapsible.js.map +1 -1
- package/dist/library/Combobox.js.map +1 -1
- package/dist/library/Dialog.js.map +1 -1
- package/dist/library/Field.js.map +1 -1
- package/dist/library/Input.js.map +1 -1
- package/dist/library/Link.js.map +1 -1
- package/dist/library/Menu.js.map +1 -1
- package/dist/library/Popover.js.map +1 -1
- package/dist/library/RadioGroup.js.map +1 -1
- package/dist/library/Select.js.map +1 -1
- package/dist/library/Slider.js.map +1 -1
- package/dist/library/Switch.js.map +1 -1
- package/dist/library/Tabs.js.map +1 -1
- package/dist/library/Textarea.js.map +1 -1
- package/dist/library/ThemeScope.js.map +1 -1
- package/dist/library/Toast.d.ts +49 -0
- package/dist/library/Toast.d.ts.map +1 -0
- package/dist/library/Toast.js +216 -0
- package/dist/library/Toast.js.map +1 -0
- package/dist/library/Tooltip.js.map +1 -1
- package/dist/library/actionStyles.js +1 -1
- package/dist/library/actionStyles.js.map +1 -1
- package/dist/library/cn.d.ts.map +1 -1
- package/dist/library/cn.js +22 -0
- package/dist/library/cn.js.map +1 -1
- package/dist/library/color/colorFamilies.js.map +1 -1
- package/dist/library/color/generateColorWall.js.map +1 -1
- package/dist/library/color/index.js.map +1 -1
- package/dist/library/color/oklch.js.map +1 -1
- package/dist/library/color/recommendColorPalette.js.map +1 -1
- package/dist/library/comboboxStyles.js.map +1 -1
- package/dist/library/controlStyles.js.map +1 -1
- package/dist/library/examples/expressive.js.map +1 -1
- package/dist/library/examples/glass.js.map +1 -1
- package/dist/library/examples/index.js.map +1 -1
- package/dist/library/examples/jelly.js.map +1 -1
- package/dist/library/examples/preset-values.js.map +1 -1
- package/dist/library/examples/radiant.js.map +1 -1
- package/dist/library/examples/sharp-3d.js.map +1 -1
- package/dist/library/examples/soft-squircle.js.map +1 -1
- package/dist/library/examples/ticket.js.map +1 -1
- package/dist/library/index.d.ts +4 -0
- package/dist/library/index.d.ts.map +1 -1
- package/dist/library/index.js +2 -0
- package/dist/library/index.js.map +1 -1
- package/dist/library/internal/events.js.map +1 -1
- package/dist/library/internal/ids.js.map +1 -1
- package/dist/library/internal/popover.js.map +1 -1
- package/dist/library/internal/select.js.map +1 -1
- package/dist/library/internal/selection.js.map +1 -1
- package/dist/library/internal/switchDrag.js.map +1 -1
- package/dist/library/internal/toast.d.ts +12 -0
- package/dist/library/internal/toast.d.ts.map +1 -0
- package/dist/library/internal/toast.js +37 -0
- package/dist/library/internal/toast.js.map +1 -0
- package/dist/library/motion/MotionLink.js.map +1 -1
- package/dist/library/motion/browser.js.map +1 -1
- package/dist/library/motion/config.js.map +1 -1
- package/dist/library/motion/context.js.map +1 -1
- package/dist/library/motion/drag.js.map +1 -1
- package/dist/library/motion/dragBrowser.js.map +1 -1
- package/dist/library/motion/follow.js.map +1 -1
- package/dist/library/motion/host.js.map +1 -1
- package/dist/library/motion/index.js.map +1 -1
- package/dist/library/motion/layout.js.map +1 -1
- package/dist/library/motion/layoutDom.js.map +1 -1
- package/dist/library/motion/popup.js.map +1 -1
- package/dist/library/motion/press.js.map +1 -1
- package/dist/library/motion/pressPolicy.js.map +1 -1
- package/dist/library/motion/react.js.map +1 -1
- package/dist/library/motion/runtime.js.map +1 -1
- package/dist/library/motion/transitions.js.map +1 -1
- package/dist/library/presentation.js.map +1 -1
- package/dist/library/react/useComboboxPopup.js.map +1 -1
- package/dist/library/react/useComposedRefs.js.map +1 -1
- package/dist/library/react/useControllableState.js.map +1 -1
- package/dist/library/react/useNativeOwner.js.map +1 -1
- package/dist/library/react/usePopoverSurface.js.map +1 -1
- package/dist/library/react/useSwitchDrag.js.map +1 -1
- package/dist/library/react/useToastTimer.d.ts +3 -0
- package/dist/library/react/useToastTimer.d.ts.map +1 -0
- package/dist/library/react/useToastTimer.js +58 -0
- package/dist/library/react/useToastTimer.js.map +1 -0
- package/dist/library/selectStyles.js.map +1 -1
- package/dist/library/styles.css +1 -1
- package/dist/library/themes/components.d.ts +31 -0
- package/dist/library/themes/components.d.ts.map +1 -1
- package/dist/library/themes/components.js +31 -0
- package/dist/library/themes/components.js.map +1 -1
- package/dist/library/themes/default.js.map +1 -1
- package/dist/library/themes/defineTheme.js.map +1 -1
- package/dist/library/themes/index.js.map +1 -1
- package/dist/library/themes/themeDocument.js.map +1 -1
- package/dist/library/themes/themeDocumentTypes.js.map +1 -1
- package/dist/library/themes/tokens.js.map +1 -1
- package/dist/library/themes/types.js.map +1 -1
- package/dist/library/themes/updateThemeAppearance.js.map +1 -1
- package/dist/library/typography/index.js.map +1 -1
- package/dist/library/typography/types.js.map +1 -1
- package/docs/architecture.md +4 -4
- package/docs/theming.md +1 -1
- package/package.json +1 -1
- package/src/Alert.tsx +95 -0
- package/src/Toast.tsx +381 -0
- package/src/actionStyles.ts +1 -1
- package/src/cn.ts +22 -0
- package/src/index.ts +33 -0
- package/src/internal/toast.ts +43 -0
- package/src/react/useToastTimer.ts +64 -0
- package/src/themes/components.ts +31 -0
package/docs/theming.md
CHANGED
|
@@ -90,7 +90,7 @@ example, `components.button.radius` changes Button only, while
|
|
|
90
90
|
component never falls back to another component's override.
|
|
91
91
|
|
|
92
92
|
Component keys are grouped by family under `components`: `button`, `link`,
|
|
93
|
-
`field`, `input`, `textarea`, `slider`, `select`, `combobox`, `checkbox`, `switch`, `radio`, `radioGroup`, `dialog`, `popover`, `tooltip`, `menu`, `collapsible`,
|
|
93
|
+
`field`, `input`, `textarea`, `slider`, `select`, `combobox`, `checkbox`, `switch`, `radio`, `radioGroup`, `dialog`, `popover`, `tooltip`, `menu`, `alert`, `toast`, `collapsible`,
|
|
94
94
|
`accordion`, `tabs` and `card`. Every key is listed in
|
|
95
95
|
[`src/themes/components.ts`](../src/themes/components.ts). Button-only fonts,
|
|
96
96
|
uppercase, shadows, squash and pressed shape belong under `components.button`.
|
package/package.json
CHANGED
package/src/Alert.tsx
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type { ComponentProps } from 'react'
|
|
2
|
+
import { cn } from './cn.js'
|
|
3
|
+
import { actionTone } from './actionStyles.js'
|
|
4
|
+
import { joinIds } from './internal/ids.js'
|
|
5
|
+
import type { Tone } from './presentation.js'
|
|
6
|
+
import type { ThemeStyle } from './themes/defineTheme.js'
|
|
7
|
+
|
|
8
|
+
export type AlertAnnouncement = 'off' | 'polite' | 'assertive'
|
|
9
|
+
export type AlertTone = Tone
|
|
10
|
+
export type AlertProps = Omit<
|
|
11
|
+
ComponentProps<'div'>,
|
|
12
|
+
'style' | 'role' | 'aria-live' | 'aria-atomic'
|
|
13
|
+
> & {
|
|
14
|
+
style?: ThemeStyle
|
|
15
|
+
'data-slot'?: string
|
|
16
|
+
/** Visual palette only; urgency is chosen independently through announce. */
|
|
17
|
+
tone?: AlertTone
|
|
18
|
+
/** Static information is silent by default. Live roles carry their native ARIA defaults. */
|
|
19
|
+
announce?: AlertAnnouncement
|
|
20
|
+
}
|
|
21
|
+
const headings = { 1: 'h1', 2: 'h2', 3: 'h3', 4: 'h4', 5: 'h5', 6: 'h6' } as const
|
|
22
|
+
export type AlertHeadingLevel = keyof typeof headings
|
|
23
|
+
export type AlertTitleProps = Omit<ComponentProps<'h2'>, 'style'> & {
|
|
24
|
+
style?: ThemeStyle
|
|
25
|
+
'data-slot'?: string
|
|
26
|
+
level?: AlertHeadingLevel
|
|
27
|
+
}
|
|
28
|
+
export type AlertDescriptionProps = Omit<ComponentProps<'p'>, 'style'> & {
|
|
29
|
+
style?: ThemeStyle
|
|
30
|
+
'data-slot'?: string
|
|
31
|
+
}
|
|
32
|
+
const colorMotion =
|
|
33
|
+
'transition-colors duration-[var(--rad-motion-color-duration,150ms)] motion-reduce:transition-none'
|
|
34
|
+
|
|
35
|
+
/** Inline content, never a focus manager, timer, dismissal owner or announcement queue. */
|
|
36
|
+
export function Alert({ announce = 'off', tone = 'neutral', className, ...props }: AlertProps) {
|
|
37
|
+
if (!['off', 'polite', 'assertive'].includes(announce))
|
|
38
|
+
throw new TypeError('Alert announce must be off, polite or assertive.')
|
|
39
|
+
const named = Boolean(props['aria-label'] || props['aria-labelledby'])
|
|
40
|
+
const role =
|
|
41
|
+
announce === 'assertive'
|
|
42
|
+
? 'alert'
|
|
43
|
+
: announce === 'polite'
|
|
44
|
+
? 'status'
|
|
45
|
+
: named
|
|
46
|
+
? 'group'
|
|
47
|
+
: undefined
|
|
48
|
+
return (
|
|
49
|
+
<div
|
|
50
|
+
{...props}
|
|
51
|
+
role={role}
|
|
52
|
+
aria-live={undefined}
|
|
53
|
+
aria-atomic={undefined}
|
|
54
|
+
data-slot={joinIds('alert', props['data-slot'])}
|
|
55
|
+
data-tone={tone}
|
|
56
|
+
data-announce={announce}
|
|
57
|
+
className={cn(
|
|
58
|
+
actionTone({ tone }),
|
|
59
|
+
colorMotion,
|
|
60
|
+
'box-border grid w-full min-w-0 break-words text-start antialiased gap-[var(--rad-alert-gap,var(--rad-spacing-header-gap,0.375rem))] p-[var(--rad-alert-padding,var(--rad-surface-padding,1.5rem))] rounded-[var(--rad-alert-radius,var(--rad-surface-radius,0.75rem))]',
|
|
61
|
+
'bg-[var(--rad-alert-background,var(--_action-soft))] [color:var(--rad-alert-color,var(--_action-ink))] [border-color:var(--rad-alert-border-color,var(--_action-edge))] [border-width:var(--rad-alert-border-width,var(--rad-surface-border-width,1px))] [border-style:var(--rad-alert-border-style,var(--rad-surface-border-style,solid))] [box-shadow:var(--rad-alert-shadow,none)]',
|
|
62
|
+
'font-[family-name:var(--rad-typography-roles-ui-body-family,system-ui,sans-serif)] text-[length:var(--rad-typography-roles-ui-body-size,0.875rem)] font-[number:var(--rad-typography-roles-ui-body-weight,400)] leading-[var(--rad-typography-roles-ui-body-line-height,1.5)] tracking-[var(--rad-typography-roles-ui-body-letter-spacing,-0.011em)]',
|
|
63
|
+
'forced-colors:border-[CanvasText] forced-colors:bg-[Canvas] forced-colors:text-[CanvasText] forced-colors:shadow-none',
|
|
64
|
+
className
|
|
65
|
+
)}
|
|
66
|
+
/>
|
|
67
|
+
)
|
|
68
|
+
}
|
|
69
|
+
export function AlertTitle({ level = 2, className, ...props }: AlertTitleProps) {
|
|
70
|
+
const Tag = headings[level]
|
|
71
|
+
return (
|
|
72
|
+
<Tag
|
|
73
|
+
{...props}
|
|
74
|
+
data-slot={joinIds('alert-title', props['data-slot'])}
|
|
75
|
+
className={cn(
|
|
76
|
+
colorMotion,
|
|
77
|
+
'm-0 min-w-0 break-words font-[family-name:var(--rad-typography-roles-ui-title-family,system-ui,sans-serif)] text-[length:var(--rad-alert-title-size,var(--rad-typography-roles-ui-title-size,1.125rem))] font-[number:var(--rad-alert-title-weight,var(--rad-typography-roles-ui-title-weight,600))] leading-[var(--rad-typography-roles-ui-title-line-height,1.3)] tracking-[var(--rad-typography-roles-ui-title-letter-spacing,-0.015em)]',
|
|
78
|
+
className
|
|
79
|
+
)}
|
|
80
|
+
/>
|
|
81
|
+
)
|
|
82
|
+
}
|
|
83
|
+
export function AlertDescription({ className, ...props }: AlertDescriptionProps) {
|
|
84
|
+
return (
|
|
85
|
+
<p
|
|
86
|
+
{...props}
|
|
87
|
+
data-slot={joinIds('alert-description', props['data-slot'])}
|
|
88
|
+
className={cn(
|
|
89
|
+
colorMotion,
|
|
90
|
+
'm-0 min-w-0 break-words text-[length:var(--rad-alert-description-size,var(--rad-typography-roles-ui-body-size,0.875rem))] leading-[var(--rad-typography-roles-ui-body-line-height,1.5)]',
|
|
91
|
+
className
|
|
92
|
+
)}
|
|
93
|
+
/>
|
|
94
|
+
)
|
|
95
|
+
}
|
package/src/Toast.tsx
ADDED
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import {
|
|
3
|
+
createContext,
|
|
4
|
+
useCallback,
|
|
5
|
+
useContext,
|
|
6
|
+
useEffect,
|
|
7
|
+
useId,
|
|
8
|
+
useLayoutEffect,
|
|
9
|
+
useRef,
|
|
10
|
+
useState,
|
|
11
|
+
type ComponentProps,
|
|
12
|
+
type ReactNode
|
|
13
|
+
} from 'react'
|
|
14
|
+
import {
|
|
15
|
+
Alert,
|
|
16
|
+
AlertTitle,
|
|
17
|
+
AlertDescription,
|
|
18
|
+
type AlertProps,
|
|
19
|
+
type AlertAnnouncement,
|
|
20
|
+
type AlertTitleProps,
|
|
21
|
+
type AlertDescriptionProps
|
|
22
|
+
} from './Alert.js'
|
|
23
|
+
import { Button, type ButtonProps, type ButtonRenderProps } from './Button.js'
|
|
24
|
+
import { cn } from './cn.js'
|
|
25
|
+
import { joinIds } from './internal/ids.js'
|
|
26
|
+
import { composeAction } from './internal/events.js'
|
|
27
|
+
import { validateToastDuration } from './internal/toast.js'
|
|
28
|
+
import { useControllableState } from './react/useControllableState.js'
|
|
29
|
+
import { useComposedRefs } from './react/useComposedRefs.js'
|
|
30
|
+
import { useToastTimer } from './react/useToastTimer.js'
|
|
31
|
+
import { useTransitionHost } from './motion/host.js'
|
|
32
|
+
import type { ThemeStyle } from './themes/defineTheme.js'
|
|
33
|
+
|
|
34
|
+
export type ToastProviderProps = { duration?: number; children?: ReactNode }
|
|
35
|
+
export type ToastAnnouncement = AlertAnnouncement
|
|
36
|
+
export type ToastProps = Omit<
|
|
37
|
+
AlertProps,
|
|
38
|
+
'hidden' | 'inert' | 'aria-hidden' | 'tabIndex' | 'announce' | 'onSelect'
|
|
39
|
+
> &
|
|
40
|
+
({ open: boolean; defaultOpen?: never } | { open?: undefined; defaultOpen?: boolean }) & {
|
|
41
|
+
onOpenChange?: (open: boolean) => void
|
|
42
|
+
duration?: number
|
|
43
|
+
announce?: ToastAnnouncement
|
|
44
|
+
announcement?: string
|
|
45
|
+
motion?: 'inherit' | 'off'
|
|
46
|
+
}
|
|
47
|
+
export type ToastState = { open: boolean; visible: boolean; setOpen: (open: boolean) => void }
|
|
48
|
+
export type ToastTitleProps = AlertTitleProps
|
|
49
|
+
export type ToastDescriptionProps = AlertDescriptionProps
|
|
50
|
+
export type ToastActionProps = ButtonProps & { altText: string }
|
|
51
|
+
export type ToastCloseProps = ButtonProps
|
|
52
|
+
export type ToastRenderProps = ButtonRenderProps
|
|
53
|
+
export type ToastViewportProps = Omit<
|
|
54
|
+
ComponentProps<'section'>,
|
|
55
|
+
'style' | 'role' | 'tabIndex' | 'aria-keyshortcuts'
|
|
56
|
+
> & { style?: ThemeStyle; 'data-slot'?: string; hotkey?: boolean }
|
|
57
|
+
type Queue = {
|
|
58
|
+
duration: number
|
|
59
|
+
queue: string[]
|
|
60
|
+
register(id: string): () => void
|
|
61
|
+
publish(id: string, text: string, urgency: ToastAnnouncement, view: Window): void
|
|
62
|
+
clear(id: string): void
|
|
63
|
+
}
|
|
64
|
+
const QueueContext = createContext<Queue | null>(null)
|
|
65
|
+
const ToastContext = createContext<ToastState | null>(null)
|
|
66
|
+
function useQueue() {
|
|
67
|
+
const value = useContext(QueueContext)
|
|
68
|
+
if (!value) throw new Error('Toast and ToastViewport must render inside ToastProvider.')
|
|
69
|
+
return value
|
|
70
|
+
}
|
|
71
|
+
export function useToast(): ToastState | null {
|
|
72
|
+
return useContext(ToastContext)
|
|
73
|
+
}
|
|
74
|
+
function useToastContext() {
|
|
75
|
+
const value = useToast()
|
|
76
|
+
if (!value) throw new Error('Toast parts must render inside Toast.')
|
|
77
|
+
return value
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** One visible toast at a time. Declarative open roots wait in registration order. */
|
|
81
|
+
export function ToastProvider({ duration = 0, children }: ToastProviderProps) {
|
|
82
|
+
validateToastDuration(duration)
|
|
83
|
+
const [queue, setQueue] = useState<string[]>([])
|
|
84
|
+
const [live, setLive] = useState({ polite: '', assertive: '' })
|
|
85
|
+
const current = useRef<{
|
|
86
|
+
id: string
|
|
87
|
+
text: string
|
|
88
|
+
urgency: ToastAnnouncement
|
|
89
|
+
view: Window
|
|
90
|
+
frame: number | null
|
|
91
|
+
} | null>(null)
|
|
92
|
+
const register = useCallback((id: string) => {
|
|
93
|
+
setQueue(values => (values.includes(id) ? values : [...values, id]))
|
|
94
|
+
return () => setQueue(values => values.filter(value => value !== id))
|
|
95
|
+
}, [])
|
|
96
|
+
const clear = useCallback((id: string) => {
|
|
97
|
+
if (current.current?.id !== id) return
|
|
98
|
+
if (current.current.frame !== null)
|
|
99
|
+
current.current.view.cancelAnimationFrame(current.current.frame)
|
|
100
|
+
current.current = null
|
|
101
|
+
setLive({ polite: '', assertive: '' })
|
|
102
|
+
}, [])
|
|
103
|
+
const publish = useCallback(
|
|
104
|
+
(id: string, text: string, urgency: ToastAnnouncement, view: Window) => {
|
|
105
|
+
const previous = current.current
|
|
106
|
+
if (previous?.id === id && previous.text === text && previous.urgency === urgency) return
|
|
107
|
+
if (previous?.frame != null) previous.view.cancelAnimationFrame(previous.frame)
|
|
108
|
+
setLive({ polite: '', assertive: '' })
|
|
109
|
+
const entry = { id, text, urgency, view, frame: null as number | null }
|
|
110
|
+
current.current = entry
|
|
111
|
+
if (urgency === 'off' || !text) return
|
|
112
|
+
// The live hosts already exist; clearing first also permits a distinct repeated message.
|
|
113
|
+
entry.frame = view.requestAnimationFrame(() => {
|
|
114
|
+
entry.frame = null
|
|
115
|
+
if (current.current !== entry) return
|
|
116
|
+
setLive({
|
|
117
|
+
polite: urgency === 'polite' ? text : '',
|
|
118
|
+
assertive: urgency === 'assertive' ? text : ''
|
|
119
|
+
})
|
|
120
|
+
})
|
|
121
|
+
},
|
|
122
|
+
[]
|
|
123
|
+
)
|
|
124
|
+
useEffect(
|
|
125
|
+
() => () => {
|
|
126
|
+
const entry = current.current
|
|
127
|
+
if (entry?.frame != null) entry.view.cancelAnimationFrame(entry.frame)
|
|
128
|
+
},
|
|
129
|
+
[]
|
|
130
|
+
)
|
|
131
|
+
return (
|
|
132
|
+
<QueueContext.Provider value={{ duration, queue, register, publish, clear }}>
|
|
133
|
+
{children}
|
|
134
|
+
<output
|
|
135
|
+
className="sr-only"
|
|
136
|
+
data-slot="toast-announcer"
|
|
137
|
+
>
|
|
138
|
+
{live.polite}
|
|
139
|
+
</output>
|
|
140
|
+
<div
|
|
141
|
+
className="sr-only"
|
|
142
|
+
data-slot="toast-announcer"
|
|
143
|
+
role="alert"
|
|
144
|
+
>
|
|
145
|
+
{live.assertive}
|
|
146
|
+
</div>
|
|
147
|
+
</QueueContext.Provider>
|
|
148
|
+
)
|
|
149
|
+
}
|
|
150
|
+
export function Toast({
|
|
151
|
+
open,
|
|
152
|
+
defaultOpen,
|
|
153
|
+
onOpenChange,
|
|
154
|
+
duration,
|
|
155
|
+
announce = 'polite',
|
|
156
|
+
announcement,
|
|
157
|
+
motion = 'inherit',
|
|
158
|
+
ref,
|
|
159
|
+
className,
|
|
160
|
+
onKeyDown,
|
|
161
|
+
onFocusCapture,
|
|
162
|
+
onBlurCapture,
|
|
163
|
+
...props
|
|
164
|
+
}: ToastProps) {
|
|
165
|
+
const owner = useQueue()
|
|
166
|
+
const { register, publish: announceText, clear: clearText } = owner
|
|
167
|
+
const state = useControllableState({
|
|
168
|
+
name: 'Toast',
|
|
169
|
+
value: open,
|
|
170
|
+
defaultValue: defaultOpen,
|
|
171
|
+
initialValue: false,
|
|
172
|
+
onChange: onOpenChange
|
|
173
|
+
})
|
|
174
|
+
const id = useId()
|
|
175
|
+
const visible = state.value && owner.queue[0] === id
|
|
176
|
+
const timeout = duration ?? owner.duration
|
|
177
|
+
validateToastDuration(timeout)
|
|
178
|
+
if (!['off', 'polite', 'assertive'].includes(announce))
|
|
179
|
+
throw new TypeError('Toast announce must be off, polite or assertive.')
|
|
180
|
+
const nodeRef = useRef<HTMLDivElement>(null)
|
|
181
|
+
const [label, setLabel] = useState('Notification')
|
|
182
|
+
const focused = useRef(false)
|
|
183
|
+
const returnTo = useRef<HTMLElement | null>(null)
|
|
184
|
+
const update = useRef(() => {})
|
|
185
|
+
const latest = useRef({ announce, announcement })
|
|
186
|
+
useLayoutEffect(() => {
|
|
187
|
+
latest.current = { announce, announcement }
|
|
188
|
+
update.current()
|
|
189
|
+
}, [announce, announcement])
|
|
190
|
+
useLayoutEffect(() => (state.value ? register(id) : undefined), [state.value, register, id])
|
|
191
|
+
useLayoutEffect(() => {
|
|
192
|
+
const node = nodeRef.current
|
|
193
|
+
if (!visible || !node) return
|
|
194
|
+
const doc = node.ownerDocument
|
|
195
|
+
const win = doc.defaultView!
|
|
196
|
+
returnTo.current = doc.activeElement instanceof win.HTMLElement ? doc.activeElement : null
|
|
197
|
+
const publish = () => {
|
|
198
|
+
const parts = Array.from(
|
|
199
|
+
node.querySelectorAll<HTMLElement>(
|
|
200
|
+
'[data-slot~="toast-title"], [data-slot~="toast-description"], [data-slot~="toast-action"]'
|
|
201
|
+
)
|
|
202
|
+
)
|
|
203
|
+
const title = parts.find(part => part.dataset.slot?.split(' ').includes('toast-title'))
|
|
204
|
+
setLabel(title?.innerText || 'Notification')
|
|
205
|
+
const text =
|
|
206
|
+
latest.current.announcement ??
|
|
207
|
+
parts
|
|
208
|
+
.map(part => part.dataset.toastAnnouncement ?? part.innerText)
|
|
209
|
+
.filter(Boolean)
|
|
210
|
+
.join(' ')
|
|
211
|
+
announceText(id, text, latest.current.announce, win)
|
|
212
|
+
}
|
|
213
|
+
update.current = publish
|
|
214
|
+
publish()
|
|
215
|
+
const observer = new win.MutationObserver(publish)
|
|
216
|
+
observer.observe(node, { childList: true, subtree: true, characterData: true })
|
|
217
|
+
return () => {
|
|
218
|
+
observer.disconnect()
|
|
219
|
+
update.current = () => {}
|
|
220
|
+
clearText(id)
|
|
221
|
+
const opener = returnTo.current
|
|
222
|
+
const inside =
|
|
223
|
+
node.contains(doc.activeElement) || (focused.current && doc.activeElement === doc.body)
|
|
224
|
+
const modal = Array.from(doc.querySelectorAll('dialog:modal')).at(-1)
|
|
225
|
+
if (
|
|
226
|
+
inside &&
|
|
227
|
+
opener?.isConnected &&
|
|
228
|
+
opener.checkVisibility() &&
|
|
229
|
+
!opener.closest('[inert]') &&
|
|
230
|
+
(!modal || modal.contains(opener))
|
|
231
|
+
)
|
|
232
|
+
opener.focus({ preventScroll: true })
|
|
233
|
+
focused.current = false
|
|
234
|
+
}
|
|
235
|
+
}, [visible, id, announceText, clearText])
|
|
236
|
+
useToastTimer(nodeRef, visible, timeout, () => state.request(false))
|
|
237
|
+
const host = useComposedRefs(nodeRef, ref)
|
|
238
|
+
const { ref: transitionRef, active, off } = useTransitionHost('popup', host, motion === 'off')
|
|
239
|
+
return (
|
|
240
|
+
<ToastContext.Provider value={{ open: state.value, visible, setOpen: state.request }}>
|
|
241
|
+
<Alert
|
|
242
|
+
{...props}
|
|
243
|
+
ref={transitionRef}
|
|
244
|
+
announce="off"
|
|
245
|
+
aria-label={props['aria-label'] ?? (props['aria-labelledby'] ? undefined : label)}
|
|
246
|
+
hidden={!visible}
|
|
247
|
+
inert={!visible}
|
|
248
|
+
aria-hidden={!visible || undefined}
|
|
249
|
+
tabIndex={-1}
|
|
250
|
+
data-slot={joinIds('toast', props['data-slot'])}
|
|
251
|
+
data-state={visible ? 'open' : 'closed'}
|
|
252
|
+
data-queued={(state.value && !visible) || undefined}
|
|
253
|
+
data-motion={motion}
|
|
254
|
+
data-urgency={announce}
|
|
255
|
+
className={cn(
|
|
256
|
+
'[&[hidden]]:hidden pointer-events-auto max-h-[calc(100dvh-2rem)] overflow-auto overscroll-contain outline-none p-[var(--rad-toast-padding,var(--rad-surface-padding,1.5rem))] gap-[var(--rad-toast-gap,var(--rad-spacing-header-gap,0.375rem))] rounded-[var(--rad-toast-radius,var(--rad-surface-radius,0.75rem))]',
|
|
257
|
+
'bg-[var(--rad-toast-background,var(--_action-soft))] [color:var(--rad-toast-color,var(--_action-ink))] [border-color:var(--rad-toast-border-color,var(--_action-edge))] [border-width:var(--rad-toast-border-width,var(--rad-surface-border-width,1px))] [border-style:var(--rad-toast-border-style,var(--rad-surface-border-style,solid))] [box-shadow:var(--rad-toast-shadow,var(--rad-surface-overlay-shadow,0_8px_32px_#0003))]',
|
|
258
|
+
'focus-visible:outline-solid focus-visible:outline-[var(--rad-color-focus)] focus-visible:outline-[length:var(--rad-focus-width,2px)] focus-visible:outline-offset-[var(--rad-focus-offset,2px)]',
|
|
259
|
+
'forced-colors:border-[CanvasText] forced-colors:bg-[Canvas] forced-colors:text-[CanvasText] forced-colors:shadow-none',
|
|
260
|
+
active && !off && 'opacity-[var(--_rad-popup-enter,0)] motion-reduce:opacity-100',
|
|
261
|
+
className
|
|
262
|
+
)}
|
|
263
|
+
onKeyDown={composeAction(onKeyDown, event => {
|
|
264
|
+
if (event.key !== 'Escape') return
|
|
265
|
+
event.preventDefault()
|
|
266
|
+
event.stopPropagation()
|
|
267
|
+
state.request(false)
|
|
268
|
+
})}
|
|
269
|
+
onFocusCapture={event => {
|
|
270
|
+
const previous = event.relatedTarget
|
|
271
|
+
if (
|
|
272
|
+
previous instanceof event.currentTarget.ownerDocument.defaultView!.HTMLElement &&
|
|
273
|
+
!event.currentTarget.contains(previous)
|
|
274
|
+
)
|
|
275
|
+
returnTo.current = previous
|
|
276
|
+
focused.current = true
|
|
277
|
+
onFocusCapture?.(event)
|
|
278
|
+
}}
|
|
279
|
+
onBlurCapture={event => {
|
|
280
|
+
if (event.relatedTarget && !event.currentTarget.contains(event.relatedTarget))
|
|
281
|
+
focused.current = false
|
|
282
|
+
onBlurCapture?.(event)
|
|
283
|
+
}}
|
|
284
|
+
/>
|
|
285
|
+
</ToastContext.Provider>
|
|
286
|
+
)
|
|
287
|
+
}
|
|
288
|
+
export function ToastTitle({ className, ...props }: ToastTitleProps) {
|
|
289
|
+
useToastContext()
|
|
290
|
+
return (
|
|
291
|
+
<AlertTitle
|
|
292
|
+
{...props}
|
|
293
|
+
data-slot={joinIds('toast-title', props['data-slot'])}
|
|
294
|
+
className={cn(
|
|
295
|
+
'text-[length:var(--rad-toast-title-size,var(--rad-typography-roles-ui-title-size,1.125rem))] font-[number:var(--rad-toast-title-weight,var(--rad-typography-roles-ui-title-weight,600))]',
|
|
296
|
+
className
|
|
297
|
+
)}
|
|
298
|
+
/>
|
|
299
|
+
)
|
|
300
|
+
}
|
|
301
|
+
export function ToastDescription({ className, ...props }: ToastDescriptionProps) {
|
|
302
|
+
useToastContext()
|
|
303
|
+
return (
|
|
304
|
+
<AlertDescription
|
|
305
|
+
{...props}
|
|
306
|
+
data-slot={joinIds('toast-description', props['data-slot'])}
|
|
307
|
+
className={cn(
|
|
308
|
+
'text-[length:var(--rad-toast-description-size,var(--rad-typography-roles-ui-body-size,0.875rem))]',
|
|
309
|
+
className
|
|
310
|
+
)}
|
|
311
|
+
/>
|
|
312
|
+
)
|
|
313
|
+
}
|
|
314
|
+
export function ToastAction({ altText, onClick, ...props }: ToastActionProps) {
|
|
315
|
+
const toast = useToastContext()
|
|
316
|
+
if (!altText.trim())
|
|
317
|
+
throw new TypeError('ToastAction needs meaningful altText for its announcement.')
|
|
318
|
+
return (
|
|
319
|
+
<Button
|
|
320
|
+
{...props}
|
|
321
|
+
data-slot={joinIds('toast-action', props['data-slot'])}
|
|
322
|
+
data-toast-announcement={altText}
|
|
323
|
+
onClick={composeAction(onClick, () => toast.setOpen(false))}
|
|
324
|
+
/>
|
|
325
|
+
)
|
|
326
|
+
}
|
|
327
|
+
export function ToastClose({ onClick, ...props }: ToastCloseProps) {
|
|
328
|
+
const toast = useToastContext()
|
|
329
|
+
return (
|
|
330
|
+
<Button
|
|
331
|
+
{...props}
|
|
332
|
+
data-slot={joinIds('toast-close', props['data-slot'])}
|
|
333
|
+
onClick={composeAction(onClick, () => toast.setOpen(false))}
|
|
334
|
+
/>
|
|
335
|
+
)
|
|
336
|
+
}
|
|
337
|
+
export function ToastViewport({ hotkey = true, ref, className, ...props }: ToastViewportProps) {
|
|
338
|
+
useQueue()
|
|
339
|
+
const nativeProps = { ...props, role: undefined }
|
|
340
|
+
const nodeRef = useRef<HTMLElement>(null)
|
|
341
|
+
const host = useComposedRefs(nodeRef, ref)
|
|
342
|
+
useEffect(() => {
|
|
343
|
+
const node = nodeRef.current
|
|
344
|
+
if (!node || !hotkey) return
|
|
345
|
+
const doc = node.ownerDocument
|
|
346
|
+
const focus = (event: KeyboardEvent) => {
|
|
347
|
+
if (
|
|
348
|
+
event.key !== 'F8' ||
|
|
349
|
+
event.altKey ||
|
|
350
|
+
event.ctrlKey ||
|
|
351
|
+
event.metaKey ||
|
|
352
|
+
event.shiftKey ||
|
|
353
|
+
event.defaultPrevented ||
|
|
354
|
+
event.isComposing
|
|
355
|
+
)
|
|
356
|
+
return
|
|
357
|
+
const modal = Array.from(doc.querySelectorAll('dialog:modal')).at(-1)
|
|
358
|
+
if (modal && !modal.contains(node)) return
|
|
359
|
+
const toast = node.querySelector<HTMLElement>('[data-slot~="toast"][data-state="open"]')
|
|
360
|
+
if (!toast || !toast.checkVisibility()) return
|
|
361
|
+
event.preventDefault()
|
|
362
|
+
toast.focus({ preventScroll: true })
|
|
363
|
+
}
|
|
364
|
+
doc.addEventListener('keydown', focus)
|
|
365
|
+
return () => doc.removeEventListener('keydown', focus)
|
|
366
|
+
}, [hotkey])
|
|
367
|
+
return (
|
|
368
|
+
<section
|
|
369
|
+
{...nativeProps}
|
|
370
|
+
ref={host}
|
|
371
|
+
aria-label={props['aria-labelledby'] ? undefined : (props['aria-label'] ?? 'Notifications')}
|
|
372
|
+
aria-keyshortcuts={hotkey ? 'F8' : undefined}
|
|
373
|
+
tabIndex={-1}
|
|
374
|
+
data-slot={joinIds('toast-viewport', props['data-slot'])}
|
|
375
|
+
className={cn(
|
|
376
|
+
'pointer-events-none fixed right-[var(--rad-toast-inset,1rem)] bottom-[var(--rad-toast-inset,1rem)] z-[var(--rad-toast-z-index,50)] grid min-w-0 w-[var(--rad-toast-width,24rem)] max-w-[calc(100vw-2rem)] max-h-[calc(100dvh-2rem)] overflow-visible',
|
|
377
|
+
className
|
|
378
|
+
)}
|
|
379
|
+
/>
|
|
380
|
+
)
|
|
381
|
+
}
|
package/src/actionStyles.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { cva } from 'class-variance-authority'
|
|
2
2
|
|
|
3
|
-
// Palette and readable ink are shared by
|
|
3
|
+
// Palette and readable ink are shared by actions, text links and inline feedback. Ink is
|
|
4
4
|
// mixed toward the scope foreground so pale warning/accent fills stay readable
|
|
5
5
|
// on an unfilled surface in either mode. Neutral preserves the quiet palette.
|
|
6
6
|
const tones = {
|
package/src/cn.ts
CHANGED
|
@@ -9,6 +9,9 @@ const roleClasses = (prefix: string, leaf: string) =>
|
|
|
9
9
|
export const cn: CnFunction = createCn({
|
|
10
10
|
extend: {
|
|
11
11
|
classGroups: {
|
|
12
|
+
right: ['right-(--rad-toast-inset)'],
|
|
13
|
+
bottom: ['bottom-(--rad-toast-inset)'],
|
|
14
|
+
z: ['z-(--rad-toast-z-index)'],
|
|
12
15
|
'font-family': [
|
|
13
16
|
...roleClasses('font', 'family'),
|
|
14
17
|
'font-(--rad-combobox-font-family)',
|
|
@@ -17,6 +20,8 @@ export const cn: CnFunction = createCn({
|
|
|
17
20
|
'font-(--rad-tabs-font-family)'
|
|
18
21
|
],
|
|
19
22
|
'font-weight': [
|
|
23
|
+
'font-(--rad-toast-title-weight)',
|
|
24
|
+
'font-(--rad-alert-title-weight)',
|
|
20
25
|
'font-(--rad-tooltip-font-weight)',
|
|
21
26
|
...roleClasses('font', 'weight'),
|
|
22
27
|
'font-(--rad-combobox-font-weight)',
|
|
@@ -25,6 +30,10 @@ export const cn: CnFunction = createCn({
|
|
|
25
30
|
'font-(--rad-tabs-font-weight)'
|
|
26
31
|
],
|
|
27
32
|
'font-size': [
|
|
33
|
+
'text-(--rad-toast-title-size)',
|
|
34
|
+
'text-(--rad-toast-description-size)',
|
|
35
|
+
'text-(--rad-alert-title-size)',
|
|
36
|
+
'text-(--rad-alert-description-size)',
|
|
28
37
|
'text-(--rad-tooltip-font-size)',
|
|
29
38
|
...roleClasses('text', 'size'),
|
|
30
39
|
...roleClasses('text', 'fluid-size'),
|
|
@@ -42,6 +51,7 @@ export const cn: CnFunction = createCn({
|
|
|
42
51
|
'h-(--rad-slider-length)'
|
|
43
52
|
],
|
|
44
53
|
w: [
|
|
54
|
+
'w-(--rad-toast-width)',
|
|
45
55
|
'w-(--rad-menu-width)',
|
|
46
56
|
'w-(--rad-popover-width)',
|
|
47
57
|
'w-(--rad-switch-width)',
|
|
@@ -51,6 +61,8 @@ export const cn: CnFunction = createCn({
|
|
|
51
61
|
'max-w': ['max-w-(--rad-tooltip-max-width)'],
|
|
52
62
|
size: ['size-(--rad-slider-thumb-size)', 'size-(--rad-slider-track-size)'],
|
|
53
63
|
gap: [
|
|
64
|
+
'gap-(--rad-toast-gap)',
|
|
65
|
+
'gap-(--rad-alert-gap)',
|
|
54
66
|
'gap-(--rad-popover-gap)',
|
|
55
67
|
'gap-(--rad-switch-gap)',
|
|
56
68
|
'gap-(--rad-tabs-gap)',
|
|
@@ -65,6 +77,8 @@ export const cn: CnFunction = createCn({
|
|
|
65
77
|
'px-(--rad-tabs-trigger-padding)'
|
|
66
78
|
],
|
|
67
79
|
p: [
|
|
80
|
+
'p-(--rad-toast-padding)',
|
|
81
|
+
'p-(--rad-alert-padding)',
|
|
68
82
|
'p-(--rad-menu-padding)',
|
|
69
83
|
'p-(--rad-menu-item-padding)',
|
|
70
84
|
'p-(--rad-tooltip-padding)',
|
|
@@ -75,6 +89,8 @@ export const cn: CnFunction = createCn({
|
|
|
75
89
|
'p-(--rad-tabs-content-padding)'
|
|
76
90
|
],
|
|
77
91
|
rounded: [
|
|
92
|
+
'rounded-(--rad-toast-radius)',
|
|
93
|
+
'rounded-(--rad-alert-radius)',
|
|
78
94
|
'rounded-(--rad-menu-radius)',
|
|
79
95
|
'rounded-(--rad-menu-item-radius)',
|
|
80
96
|
'rounded-(--rad-tooltip-radius)',
|
|
@@ -95,6 +111,8 @@ export const cn: CnFunction = createCn({
|
|
|
95
111
|
'rounded-(--rad-tabs-content-radius)'
|
|
96
112
|
],
|
|
97
113
|
'border-w': [
|
|
114
|
+
'border-(--rad-toast-border-width)',
|
|
115
|
+
'border-(--rad-alert-border-width)',
|
|
98
116
|
'border-(--rad-menu-border-width)',
|
|
99
117
|
'border-(--rad-tooltip-border-width)',
|
|
100
118
|
'border-(--rad-popover-border-width)',
|
|
@@ -106,6 +124,8 @@ export const cn: CnFunction = createCn({
|
|
|
106
124
|
'border-(--rad-tabs-trigger-border-width)'
|
|
107
125
|
],
|
|
108
126
|
'border-style': [
|
|
127
|
+
'border-(--rad-toast-border-style)',
|
|
128
|
+
'border-(--rad-alert-border-style)',
|
|
109
129
|
'border-(--rad-menu-border-style)',
|
|
110
130
|
'border-(--rad-tooltip-border-style)',
|
|
111
131
|
'border-(--rad-popover-border-style)',
|
|
@@ -115,6 +135,8 @@ export const cn: CnFunction = createCn({
|
|
|
115
135
|
'border-(--rad-switch-border-style)'
|
|
116
136
|
],
|
|
117
137
|
shadow: [
|
|
138
|
+
'shadow-(--rad-toast-shadow)',
|
|
139
|
+
'shadow-(--rad-alert-shadow)',
|
|
118
140
|
'shadow-(--rad-menu-shadow)',
|
|
119
141
|
'shadow-(--rad-tooltip-shadow)',
|
|
120
142
|
'shadow-(--rad-popover-shadow)',
|
package/src/index.ts
CHANGED
|
@@ -252,3 +252,36 @@ export type {
|
|
|
252
252
|
MenuPortalProps,
|
|
253
253
|
MenuRenderProps
|
|
254
254
|
} from './Menu.js'
|
|
255
|
+
|
|
256
|
+
export { Alert, AlertTitle, AlertDescription } from './Alert.js'
|
|
257
|
+
export type {
|
|
258
|
+
AlertProps,
|
|
259
|
+
AlertTitleProps,
|
|
260
|
+
AlertDescriptionProps,
|
|
261
|
+
AlertAnnouncement,
|
|
262
|
+
AlertTone,
|
|
263
|
+
AlertHeadingLevel
|
|
264
|
+
} from './Alert.js'
|
|
265
|
+
|
|
266
|
+
export {
|
|
267
|
+
ToastProvider,
|
|
268
|
+
ToastViewport,
|
|
269
|
+
Toast,
|
|
270
|
+
ToastTitle,
|
|
271
|
+
ToastDescription,
|
|
272
|
+
ToastAction,
|
|
273
|
+
ToastClose,
|
|
274
|
+
useToast
|
|
275
|
+
} from './Toast.js'
|
|
276
|
+
export type {
|
|
277
|
+
ToastProviderProps,
|
|
278
|
+
ToastViewportProps,
|
|
279
|
+
ToastProps,
|
|
280
|
+
ToastState,
|
|
281
|
+
ToastAnnouncement,
|
|
282
|
+
ToastTitleProps,
|
|
283
|
+
ToastDescriptionProps,
|
|
284
|
+
ToastActionProps,
|
|
285
|
+
ToastCloseProps,
|
|
286
|
+
ToastRenderProps
|
|
287
|
+
} from './Toast.js'
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export function validateToastDuration(value: number) {
|
|
2
|
+
if (!Number.isFinite(value) || value < 0 || value > 2_147_483_647)
|
|
3
|
+
throw new TypeError('Toast duration must be between 0 and 2147483647 milliseconds.')
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
/** A paused interval retains its remaining time; an elapsed interval fires once. */
|
|
7
|
+
export function createToastTimer(
|
|
8
|
+
duration: number,
|
|
9
|
+
clock: {
|
|
10
|
+
now(): number
|
|
11
|
+
set(callback: () => void, delay: number): number
|
|
12
|
+
clear(id: number): void
|
|
13
|
+
},
|
|
14
|
+
elapsed: () => void
|
|
15
|
+
) {
|
|
16
|
+
validateToastDuration(duration)
|
|
17
|
+
let remaining = duration
|
|
18
|
+
let deadline = 0
|
|
19
|
+
let timer: number | undefined
|
|
20
|
+
let ended = false
|
|
21
|
+
const pause = () => {
|
|
22
|
+
if (timer === undefined) return
|
|
23
|
+
clock.clear(timer)
|
|
24
|
+
timer = undefined
|
|
25
|
+
remaining = Math.max(0, deadline - clock.now())
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
pause,
|
|
29
|
+
resume() {
|
|
30
|
+
if (!duration || ended || timer !== undefined) return
|
|
31
|
+
deadline = clock.now() + remaining
|
|
32
|
+
timer = clock.set(() => {
|
|
33
|
+
timer = undefined
|
|
34
|
+
ended = true
|
|
35
|
+
elapsed()
|
|
36
|
+
}, remaining)
|
|
37
|
+
},
|
|
38
|
+
destroy() {
|
|
39
|
+
pause()
|
|
40
|
+
ended = true
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|