@cognitap/radiant 0.0.89 → 0.0.91

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.
Files changed (84) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/dist/library/components/Checkbox/Checkbox.d.ts.map +1 -1
  3. package/dist/library/components/Checkbox/Checkbox.js +2 -2
  4. package/dist/library/components/Checkbox/Checkbox.js.map +1 -1
  5. package/dist/library/components/Collapsible/Collapsible.js +1 -1
  6. package/dist/library/components/Collapsible/Collapsible.js.map +1 -1
  7. package/dist/library/components/DateField/DateField.d.ts +7 -0
  8. package/dist/library/components/DateField/DateField.d.ts.map +1 -0
  9. package/dist/library/components/DateField/DateField.js +15 -0
  10. package/dist/library/components/DateField/DateField.js.map +1 -0
  11. package/dist/library/components/DatePicker/DateEditor.d.ts +4 -2
  12. package/dist/library/components/DatePicker/DateEditor.d.ts.map +1 -1
  13. package/dist/library/components/DatePicker/DateEditor.js +3 -3
  14. package/dist/library/components/DatePicker/DateEditor.js.map +1 -1
  15. package/dist/library/components/InlineEdit/InlineEdit.d.ts +19 -0
  16. package/dist/library/components/InlineEdit/InlineEdit.d.ts.map +1 -0
  17. package/dist/library/components/InlineEdit/InlineEdit.js +73 -0
  18. package/dist/library/components/InlineEdit/InlineEdit.js.map +1 -0
  19. package/dist/library/components/InputGroup/InputGroup.js +1 -1
  20. package/dist/library/components/InputGroup/InputGroup.js.map +1 -1
  21. package/dist/library/components/ListBox/ListBox.d.ts +27 -0
  22. package/dist/library/components/ListBox/ListBox.d.ts.map +1 -0
  23. package/dist/library/components/ListBox/ListBox.js +166 -0
  24. package/dist/library/components/ListBox/ListBox.js.map +1 -0
  25. package/dist/library/components/NavLink/NavLink.d.ts.map +1 -1
  26. package/dist/library/components/NavLink/NavLink.js +1 -1
  27. package/dist/library/components/NavLink/NavLink.js.map +1 -1
  28. package/dist/library/components/RadioGroup/RadioGroup.d.ts.map +1 -1
  29. package/dist/library/components/RadioGroup/RadioGroup.js +3 -3
  30. package/dist/library/components/RadioGroup/RadioGroup.js.map +1 -1
  31. package/dist/library/components/RangeSlider/RangeSlider.d.ts +24 -0
  32. package/dist/library/components/RangeSlider/RangeSlider.d.ts.map +1 -0
  33. package/dist/library/components/RangeSlider/RangeSlider.js +109 -0
  34. package/dist/library/components/RangeSlider/RangeSlider.js.map +1 -0
  35. package/dist/library/components/ScrollArea/ScrollArea.d.ts.map +1 -1
  36. package/dist/library/components/ScrollArea/ScrollArea.js +4 -1
  37. package/dist/library/components/ScrollArea/ScrollArea.js.map +1 -1
  38. package/dist/library/components/Slider/Slider.d.ts +6 -1
  39. package/dist/library/components/Slider/Slider.d.ts.map +1 -1
  40. package/dist/library/components/Slider/Slider.js +16 -10
  41. package/dist/library/components/Slider/Slider.js.map +1 -1
  42. package/dist/library/components/Switch/Switch.d.ts.map +1 -1
  43. package/dist/library/components/Switch/Switch.js +2 -2
  44. package/dist/library/components/Switch/Switch.js.map +1 -1
  45. package/dist/library/components/TagsInput/TagsInput.d.ts.map +1 -1
  46. package/dist/library/components/TagsInput/TagsInput.js +1 -1
  47. package/dist/library/components/TagsInput/TagsInput.js.map +1 -1
  48. package/dist/library/components/TimeInput/TimeInput.d.ts.map +1 -1
  49. package/dist/library/components/TimeInput/TimeInput.js +28 -20
  50. package/dist/library/components/TimeInput/TimeInput.js.map +1 -1
  51. package/dist/library/components/Toast/Toast.js +1 -1
  52. package/dist/library/components/Toast/Toast.js.map +1 -1
  53. package/dist/library/components/ToggleGroup/ToggleGroup.js +1 -1
  54. package/dist/library/components/ToggleGroup/ToggleGroup.js.map +1 -1
  55. package/dist/library/index.d.ts +4 -0
  56. package/dist/library/index.d.ts.map +1 -1
  57. package/dist/library/index.js +4 -0
  58. package/dist/library/index.js.map +1 -1
  59. package/dist/library/internal/decimal.d.ts +2 -0
  60. package/dist/library/internal/decimal.d.ts.map +1 -1
  61. package/dist/library/internal/decimal.js +12 -7
  62. package/dist/library/internal/decimal.js.map +1 -1
  63. package/dist/library/styles.css +1 -1
  64. package/docs/architecture.md +7 -0
  65. package/package.json +1 -1
  66. package/src/components/Checkbox/Checkbox.tsx +2 -0
  67. package/src/components/Collapsible/Collapsible.tsx +1 -1
  68. package/src/components/DateField/DateField.tsx +32 -0
  69. package/src/components/DatePicker/DateEditor.tsx +16 -11
  70. package/src/components/InlineEdit/InlineEdit.tsx +172 -0
  71. package/src/components/InputGroup/InputGroup.tsx +2 -2
  72. package/src/components/ListBox/ListBox.tsx +318 -0
  73. package/src/components/NavLink/NavLink.tsx +1 -6
  74. package/src/components/RadioGroup/RadioGroup.tsx +3 -0
  75. package/src/components/RangeSlider/RangeSlider.tsx +245 -0
  76. package/src/components/ScrollArea/ScrollArea.tsx +5 -1
  77. package/src/components/Slider/Slider.tsx +28 -11
  78. package/src/components/Switch/Switch.tsx +2 -0
  79. package/src/components/TagsInput/TagsInput.tsx +1 -0
  80. package/src/components/TimeInput/TimeInput.tsx +100 -75
  81. package/src/components/Toast/Toast.tsx +1 -1
  82. package/src/components/ToggleGroup/ToggleGroup.tsx +1 -1
  83. package/src/index.ts +8 -0
  84. package/src/internal/decimal.ts +14 -7
@@ -75,6 +75,13 @@ Omitting the locale inherits formatting and matching from the closest provider.
75
75
 
76
76
  `NumberField` localizes its display and parsing. `NumberFormatter` preserves server text when browser locale data differs during hydration. Select and Menu use locale-aware prefix search. Combobox, Autocomplete and their composed families use locale-aware substring search. Built-in matching ignores case and distinguishes accents. Caller filters and text props still apply.
77
77
 
78
+ ### Right-to-left (RTL)
79
+
80
+ Direction-sensitive behavior reads computed CSS direction from the host or anchor, so components follow `LocaleProvider`, document `dir`, nested subtree overrides, and direct control `dir` props without separate RTL props. Logical CSS properties (`inset-inline-*`, `padding-inline-*`, `border-inline-*`, `rounded-s-*`/`rounded-e-*`, `text-start`) drive alignment, borders, indicators, and `ToastViewport` placement.
81
+
82
+ - **Verified coverage:** Horizontal arrow navigation, drag/pointer geometry, disclosure/branch chevron mirroring, logical alignment/positioning, horizontal and vertical scrolling, reading order, nested overrides, and SSR hydration are verified across overlays (`Popover`, `Tooltip`, `HoverCard`, `ContextMenu`, `Menu`, `Drawer`, `Toast`, `Tour`, `Select`, `Combobox`, `MultiSelect`, `Cascader`, `TreeSelect`), navigation (`Tabs`, `Toolbar`, `ToggleGroup`, `Menubar`, `NavigationMenu`, `Breadcrumb`, `Pagination`, `NavLink`, `AppShell`, `Stepper`, `Timeline`, `Tree`), controls (`Slider`, `RangeSlider`, `Rating`, `Calendar`, `DateField`, `DatePicker`, `DateRangePicker`, `TimeInput`, `ColorPicker`, `ColorInput`, `Switch`, `Checkbox`, `Radio`, `OTPField`, `TagsInput`, `NumberField`, `PasswordInput`, `FileInput`, `ButtonGroup`, `InputGroup`), and layout/data families (`Collapsible`, `Accordion`, `ScrollArea`, `Carousel`, `Resizable`, `DataTable`, `Indicator`, `CodeBlock`, `Charts`).
83
+ - **Intentional exceptions and limitations:** `TimeInput` keeps numeric `hour:minute[:second]` segments left-to-right while placing `dayPeriod` in the ambient flow. `CodeBlockViewport` defaults to `dir="ltr"` for source code and accepts an explicit `dir` override. `Popover`, `Tooltip`, and `HoverCard` keep physical `side` values (`'left' | 'right'`) while `align` (`'start' | 'end'`), `MenuSubContent`, `Drawer` (`'start' | 'end'`), and `Indicator` resolve logically. `Charts` preserve left-to-right SVG plot geometry while reversing horizontal keyboard stepping in RTL. Caller-supplied icons and `BreadcrumbSeparator` content are not mirrored automatically.
84
+
78
85
  ## Customization limits
79
86
 
80
87
  `render(props)` on button and link parts must return a `<button>` or `<a>`, or a component that forwards to one, with all props, the ref, and the children.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cognitap/radiant",
3
- "version": "0.0.89",
3
+ "version": "0.0.91",
4
4
  "description": "React interaction components and presentation primitives",
5
5
  "homepage": "https://radiant-docs.pages.dev",
6
6
  "bugs": {
@@ -121,6 +121,7 @@ export function Checkbox({
121
121
  })
122
122
  const controlNode = (
123
123
  <span
124
+ dir={!labelled ? props.dir : undefined}
124
125
  className={cn(
125
126
  'relative inline-grid w-fit shrink-0',
126
127
  indicatorPressClass,
@@ -186,6 +187,7 @@ export function Checkbox({
186
187
 
187
188
  return (
188
189
  <label
190
+ dir={props.dir}
189
191
  className={cn(rowClass, parts?.wrapper?.className)}
190
192
  style={parts?.wrapper?.style}
191
193
  data-slot="checkbox-wrapper"
@@ -56,7 +56,7 @@ const collapsibleTriggerVariants = cva([
56
56
  const collapsibleMarkerVariants = cva([
57
57
  'size-4 shrink-0',
58
58
  'transition-transform duration-[var(--rad-collapsible-duration,var(--rad-motion-disclosure-duration,150ms))] ease-out',
59
- '[details[open]>summary>&]:rotate-90',
59
+ 'rtl:rotate-180 [details[open]>summary>&]:rotate-90 rtl:[details[open]>summary>&]:rotate-90',
60
60
  'motion-reduce:transition-none'
61
61
  ])
62
62
 
@@ -0,0 +1,32 @@
1
+ 'use client'
2
+
3
+ import { useControllableState } from '../../react/useControllableState.js'
4
+ import type { ValueOwner } from '../../react/useNativeOwner.js'
5
+ import { DateEditor, type DateEditorControlProps } from '../DatePicker/DateEditor.js'
6
+
7
+ export type DateFieldProps = DateEditorControlProps &
8
+ ValueOwner<string | null> & {
9
+ onValueChange?: (iso: string | null) => void
10
+ }
11
+
12
+ export function DateField({ value, defaultValue, onValueChange, ...control }: DateFieldProps) {
13
+ const owner = useControllableState({
14
+ name: 'DateField value',
15
+ value,
16
+ defaultValue,
17
+ initialValue: null,
18
+ onChange: onValueChange
19
+ })
20
+
21
+ return (
22
+ <DateEditor
23
+ {...control}
24
+ componentName="DateField"
25
+ slot="date-field"
26
+ revision={0}
27
+ value={owner.value}
28
+ onValueChange={owner.request}
29
+ onFormReset={owner.reset}
30
+ />
31
+ )
32
+ }
@@ -18,7 +18,7 @@ import { useLocale } from '../../react/useLocale.js'
18
18
  import { dateSegments, type Segment, type Segments } from './dateSegments.js'
19
19
  import type { CalendarProps } from '../Calendar/Calendar.js'
20
20
 
21
- type EditorProps = Pick<
21
+ export type DateEditorControlProps = Pick<
22
22
  ComponentProps<'input'>,
23
23
  | 'id'
24
24
  | 'ref'
@@ -33,15 +33,19 @@ type EditorProps = Pick<
33
33
  | 'aria-invalid'
34
34
  | 'className'
35
35
  > &
36
- Pick<CalendarProps, 'controlSize' | 'locale' | 'calendar' | 'min' | 'max' | 'isDateDisabled'> & {
37
- value: string | null
38
- onValueChange: (date: string | null) => void
39
- onFormReset?: () => void
40
- revision: number
41
- slot: string
42
- children?: ReactNode
43
- }
36
+ Pick<CalendarProps, 'controlSize' | 'locale' | 'calendar' | 'min' | 'max' | 'isDateDisabled'>
37
+
38
+ type EditorProps = DateEditorControlProps & {
39
+ componentName?: 'DatePicker' | 'DateField'
40
+ value: string | null
41
+ onValueChange: (date: string | null) => void
42
+ onFormReset?: () => void
43
+ revision: number
44
+ slot: string
45
+ children?: ReactNode
46
+ }
44
47
  export function DateEditor({
48
+ componentName = 'DatePicker',
45
49
  value,
46
50
  onValueChange,
47
51
  onFormReset,
@@ -65,10 +69,11 @@ export function DateEditor({
65
69
  const control = useFieldControl(props)
66
70
  const labelledBy = props['aria-labelledby'] ?? field?.labelId
67
71
  const label = props['aria-label']
68
- requireAccessibleName('DatePicker', { 'aria-label': label, 'aria-labelledby': labelledBy })
72
+ requireAccessibleName(componentName, { 'aria-label': label, 'aria-labelledby': labelledBy })
69
73
  if (min !== undefined) civilTime(min)
70
74
  if (max !== undefined) civilTime(max)
71
- if (min && max && min > max) throw new RangeError('DatePicker min must be on or before max.')
75
+ if (min && max && min > max)
76
+ throw new RangeError(componentName + ' min must be on or before max.')
72
77
 
73
78
  const language = useLocale(locale)
74
79
  const format = useMemo(() => dateSegments(language, calendar), [language, calendar])
@@ -0,0 +1,172 @@
1
+ 'use client'
2
+
3
+ import { useLayoutEffect, useRef, useState, type ComponentProps } from 'react'
4
+ import { cn } from 'cn'
5
+ import { requireAccessibleName } from '../../internal/accessibleName.js'
6
+ import { joinIds } from '../../internal/ids.js'
7
+ import type { ControlSize, PartStyle } from '../../presentation.js'
8
+ import { useControllableState } from '../../react/useControllableState.js'
9
+ import type { ValueOwner } from '../../react/useNativeOwner.js'
10
+ import { Button } from '../Button/Button.js'
11
+ import { Field, useField } from '../Field/Field.js'
12
+ import { Input } from '../Input/Input.js'
13
+
14
+ export type InlineEditProps = Omit<
15
+ ComponentProps<'div'>,
16
+ 'children' | 'style' | 'value' | 'defaultValue' | 'onChange' | 'dangerouslySetInnerHTML'
17
+ > &
18
+ ValueOwner<string> & {
19
+ inputLabel: string
20
+ editLabel?: string
21
+ saveLabel?: string
22
+ cancelLabel?: string
23
+ placeholder?: string
24
+ disabled?: boolean
25
+ controlSize?: ControlSize
26
+ /** Return an error to keep the draft open and focus the input. */
27
+ validate?: (draft: string) => string | undefined
28
+ onValueChange?: (value: string) => void
29
+ style?: PartStyle['style']
30
+ 'data-slot'?: string
31
+ }
32
+
33
+ type Draft = { text: string; error?: string; requested?: string }
34
+
35
+ export function InlineEdit({
36
+ value,
37
+ defaultValue,
38
+ onValueChange,
39
+ inputLabel,
40
+ editLabel = 'Edit',
41
+ saveLabel = 'Save',
42
+ cancelLabel = 'Cancel',
43
+ placeholder,
44
+ disabled: disabledProp,
45
+ controlSize = 'medium',
46
+ validate,
47
+ className,
48
+ 'data-slot': slot,
49
+ ...props
50
+ }: InlineEditProps) {
51
+ requireAccessibleName('InlineEdit input', { 'aria-label': inputLabel })
52
+ requireAccessibleName('InlineEdit edit action', { 'aria-label': editLabel })
53
+ requireAccessibleName('InlineEdit save action', { 'aria-label': saveLabel })
54
+ requireAccessibleName('InlineEdit cancel action', { 'aria-label': cancelLabel })
55
+ const field = useField()
56
+ const disabled = disabledProp || field?.disabled
57
+ const owner = useControllableState({
58
+ name: 'InlineEdit value',
59
+ value,
60
+ defaultValue,
61
+ initialValue: '',
62
+ onChange: onValueChange
63
+ })
64
+ const [draft, setDraft] = useState<Draft | null>(null)
65
+ const input = useRef<HTMLInputElement>(null)
66
+ const trigger = useRef<HTMLButtonElement>(null)
67
+ const wasEditing = useRef(false)
68
+ const accepted = draft?.requested !== undefined && draft.requested === owner.value
69
+ if (draft && (accepted || disabled)) setDraft(null)
70
+ const editing = draft !== null && !accepted && !disabled
71
+
72
+ useLayoutEffect(() => {
73
+ if (editing) {
74
+ input.current?.focus()
75
+ input.current?.select()
76
+ } else if (wasEditing.current && !disabled) {
77
+ trigger.current?.focus()
78
+ }
79
+ wasEditing.current = editing
80
+ }, [editing, disabled])
81
+
82
+ function save() {
83
+ if (!draft) return
84
+ const error = validate?.(draft.text)
85
+ if (error) {
86
+ setDraft({ text: draft.text, error })
87
+ input.current?.focus()
88
+ return
89
+ }
90
+ if (owner.controlled && draft.text !== owner.value) {
91
+ setDraft({ text: draft.text, requested: draft.text })
92
+ } else {
93
+ setDraft(null)
94
+ }
95
+ owner.request(draft.text)
96
+ }
97
+
98
+ return (
99
+ <div
100
+ {...props}
101
+ className={cn('grid min-w-0 gap-2', className)}
102
+ data-slot={joinIds('inline-edit', slot)}
103
+ >
104
+ {editing ? (
105
+ <>
106
+ <Field
107
+ label={inputLabel}
108
+ error={draft.error}
109
+ parts={{ label: { className: 'sr-only' } }}
110
+ >
111
+ <Input
112
+ ref={input}
113
+ controlSize={controlSize}
114
+ data-slot="inline-edit-input"
115
+ placeholder={placeholder}
116
+ value={draft.text}
117
+ onChange={event => setDraft({ text: event.currentTarget.value })}
118
+ onKeyDown={event => {
119
+ if (event.nativeEvent.isComposing) return
120
+ if (event.key === 'Enter' || event.key === 'Escape') {
121
+ event.preventDefault()
122
+ event.stopPropagation()
123
+ if (event.key === 'Enter') save()
124
+ else setDraft(null)
125
+ }
126
+ }}
127
+ />
128
+ </Field>
129
+ <div className="flex flex-wrap gap-2">
130
+ <Button
131
+ controlSize={controlSize}
132
+ type="button"
133
+ data-slot="inline-edit-save"
134
+ onClick={save}
135
+ >
136
+ {saveLabel}
137
+ </Button>
138
+ <Button
139
+ controlSize={controlSize}
140
+ type="button"
141
+ variant="outline"
142
+ data-slot="inline-edit-cancel"
143
+ onClick={() => setDraft(null)}
144
+ >
145
+ {cancelLabel}
146
+ </Button>
147
+ </div>
148
+ </>
149
+ ) : (
150
+ <div className="flex min-w-0 flex-wrap items-center gap-2">
151
+ <span
152
+ data-slot="inline-edit-value"
153
+ className="min-w-0 break-words font-[family-name:var(--rad-typography-roles-ui-body-family,system-ui,sans-serif)]"
154
+ >
155
+ {owner.value || placeholder}
156
+ </span>
157
+ <Button
158
+ ref={trigger}
159
+ type="button"
160
+ variant="outline"
161
+ controlSize={controlSize}
162
+ disabled={disabled}
163
+ data-slot="inline-edit-trigger"
164
+ onClick={() => setDraft({ text: owner.value })}
165
+ >
166
+ {editLabel}
167
+ </Button>
168
+ </div>
169
+ )}
170
+ </div>
171
+ )
172
+ }
@@ -64,8 +64,8 @@ export function InputGroup({
64
64
  'font-[family-name:var(--rad-typography-roles-ui-body-family,system-ui,sans-serif)] text-[length:var(--_rad-control-font-size)] leading-[var(--rad-typography-roles-ui-body-line-height,1.5)]',
65
65
  focusWithinRingClass,
66
66
  'focus-within:[--_rad-focus-color:var(--rad-input-group-focus-color)] focus-within:[--_rad-focus-width:var(--rad-input-group-focus-width)] focus-within:[--_rad-focus-offset:var(--rad-input-group-focus-offset)] focus-within:[--_rad-focus-style:var(--rad-input-group-focus-style)]',
67
- 'has-[>[data-invalid]]:[border-color:var(--rad-input-group-invalid-border,var(--rad-color-invalid,oklch(0.56_0.19_25)))]',
68
- 'has-[>input:disabled]:cursor-not-allowed has-[>textarea:disabled]:cursor-not-allowed has-[>input:disabled]:bg-[var(--rad-color-disabled-background,oklch(0.96_0.005_286))] has-[>textarea:disabled]:bg-[var(--rad-color-disabled-background,oklch(0.96_0.005_286))]',
67
+ 'has-[>[data-invalid],[data-slot~=input-group-input][data-invalid]]:[border-color:var(--rad-input-group-invalid-border,var(--rad-color-invalid,oklch(0.56_0.19_25)))]',
68
+ 'has-[>input:disabled,[data-slot~=input-group-input]:disabled]:cursor-not-allowed has-[>textarea:disabled]:cursor-not-allowed has-[>input:disabled,[data-slot~=input-group-input]:disabled]:bg-[var(--rad-color-disabled-background,oklch(0.96_0.005_286))] has-[>textarea:disabled]:bg-[var(--rad-color-disabled-background,oklch(0.96_0.005_286))]',
69
69
  'forced-colors:border-[CanvasText] forced-colors:bg-[Canvas] forced-colors:text-[CanvasText]',
70
70
  className
71
71
  )}
@@ -0,0 +1,318 @@
1
+ 'use client'
2
+
3
+ import { useId, useRef, useState, type ComponentProps, type KeyboardEvent } from 'react'
4
+ import { cn } from 'cn'
5
+ import { requireAccessibleName } from '../../internal/accessibleName.js'
6
+ import { composeAction } from '../../internal/events.js'
7
+ import { joinIds } from '../../internal/ids.js'
8
+ import { matchSelectText, moveSelectFocus } from '../../internal/select.js'
9
+ import {
10
+ isItemOpen,
11
+ sameSelection,
12
+ nextSelection,
13
+ type Selection
14
+ } from '../../internal/selection.js'
15
+ import type { ControlSize, PartStyle } from '../../presentation.js'
16
+ import { useComposedRefs } from '../../react/useComposedRefs.js'
17
+ import { useControllableState } from '../../react/useControllableState.js'
18
+ import { useFormReset } from '../../react/useFormReset.js'
19
+ import { useLocaleMatcher } from '../../react/useLocale.js'
20
+ import type { ValueOwner } from '../../react/useNativeOwner.js'
21
+ import { useField, useFieldControl } from '../Field/Field.js'
22
+ import type { SelectOption } from '../Select/Select.js'
23
+ import { controlBorderClass, controlMetrics } from '../shared/controlStyles.js'
24
+ import { focusRingClass } from '../shared/focusStyles.js'
25
+ import { scrollbarClass } from '../shared/scrollbarStyles.js'
26
+
27
+ export type ListBoxOption = SelectOption
28
+
29
+ export type ListBoxProps = Omit<
30
+ ComponentProps<'div'>,
31
+ | 'children'
32
+ | 'style'
33
+ | 'role'
34
+ | 'tabIndex'
35
+ | 'defaultValue'
36
+ | 'value'
37
+ | 'onChange'
38
+ | 'aria-activedescendant'
39
+ | 'aria-multiselectable'
40
+ | 'dangerouslySetInnerHTML'
41
+ > & {
42
+ items: readonly ListBoxOption[]
43
+ name?: string
44
+ form?: string
45
+ required?: boolean
46
+ disabled?: boolean
47
+ controlSize?: ControlSize
48
+ style?: PartStyle['style']
49
+ parts?: { option?: PartStyle; indicator?: PartStyle }
50
+ 'data-slot'?: string
51
+ } & (
52
+ | ({ type?: 'single'; onValueChange?: (value: string | null) => void } & ValueOwner<
53
+ string | null
54
+ >)
55
+ | ({ type: 'multiple'; onValueChange?: (value: string[]) => void } & ValueOwner<string[]>)
56
+ )
57
+
58
+ const listClass = [
59
+ 'group/list-box grid min-w-0 gap-1 overflow-auto p-1',
60
+ controlBorderClass,
61
+ scrollbarClass,
62
+ 'bg-[var(--rad-control-field-background,var(--rad-color-surface,white))] text-[var(--rad-color-foreground)]',
63
+ 'font-[family-name:var(--rad-typography-roles-ui-body-family,system-ui,sans-serif)] text-[length:var(--_rad-control-font-size)]',
64
+ 'data-invalid:[border-color:var(--rad-color-invalid)] data-disabled:opacity-[var(--rad-state-disabled-opacity,0.5)]',
65
+ 'forced-colors:bg-[Canvas] forced-colors:text-[CanvasText] forced-colors:border-[CanvasText]',
66
+ focusRingClass
67
+ ].join(' ')
68
+
69
+ const optionClass = [
70
+ 'flex min-h-[var(--_rad-control-height)] min-w-0 items-center justify-between gap-2 px-[var(--_rad-control-padding)] py-2',
71
+ 'rounded-[var(--rad-control-radius,0.5rem)] [corner-shape:var(--rad-control-corner-shape,round)]',
72
+ 'cursor-default data-disabled:opacity-[var(--rad-state-disabled-opacity,0.5)]',
73
+ 'data-selected:bg-[var(--rad-control-selected-background,transparent)] data-selected:text-[var(--rad-control-selected-foreground,currentColor)]',
74
+ 'data-selected:[box-shadow:inset_0_0_0_var(--rad-control-border-width,1px)_var(--rad-control-selected-border-color,transparent)]',
75
+ 'group-focus-visible/list-box:data-active:outline-2 group-focus-visible/list-box:data-active:-outline-offset-2 group-focus-visible/list-box:data-active:outline-[var(--rad-color-focus)]',
76
+ 'forced-colors:data-selected:bg-[Highlight] forced-colors:data-selected:text-[HighlightText]',
77
+ 'forced-colors:group-focus-visible/list-box:data-active:outline-[CanvasText] forced-colors:data-disabled:text-[GrayText]'
78
+ ].join(' ')
79
+
80
+ export function ListBox(props: ListBoxProps) {
81
+ const {
82
+ items,
83
+ name,
84
+ form,
85
+ disabled,
86
+ required,
87
+ controlSize = 'medium',
88
+ id,
89
+ ref,
90
+ className,
91
+ parts,
92
+ type = 'single',
93
+ value,
94
+ defaultValue,
95
+ onValueChange: _onValueChange,
96
+ onKeyDown,
97
+ onClick,
98
+ 'aria-label': label,
99
+ 'aria-labelledby': labelledBy,
100
+ 'aria-describedby': describedBy,
101
+ 'aria-invalid': invalid,
102
+ 'data-slot': slot,
103
+ ...hostProps
104
+ } = props
105
+ const multiple = type === 'multiple'
106
+ const [initialType] = useState(type)
107
+ if (type !== initialType) throw new TypeError('ListBox needs a new key to change selection mode.')
108
+
109
+ const ids = useId()
110
+ const host = useRef<HTMLDivElement>(null)
111
+ const hostRef = useComposedRefs(host, ref)
112
+ const select = useRef<HTMLSelectElement>(null)
113
+ const matcher = useLocaleMatcher()
114
+ const search = useRef({ text: '', at: 0, matcher })
115
+ const field = useField()
116
+ const control = useFieldControl({
117
+ disabled,
118
+ required,
119
+ 'aria-describedby': describedBy,
120
+ 'aria-invalid': invalid
121
+ })
122
+ const listLabel = labelledBy ?? field?.labelId
123
+ requireAccessibleName('ListBox', { 'aria-label': label, 'aria-labelledby': listLabel })
124
+
125
+ const entries = items.map(item => ({
126
+ ...item,
127
+ textValue: item.textValue ?? (typeof item.label === 'string' ? item.label : '')
128
+ }))
129
+ if (
130
+ new Set(entries.map(item => item.value)).size !== items.length ||
131
+ entries.some(item => typeof item.value !== 'string' || !item.value || !item.textValue?.trim())
132
+ ) {
133
+ throw new TypeError('ListBox items need distinct, nonempty string values and readable labels.')
134
+ }
135
+ const owner = useControllableState<Selection>({
136
+ name: 'ListBox value',
137
+ value,
138
+ defaultValue,
139
+ initialValue: multiple ? [] : null,
140
+ equal: sameSelection,
141
+ onChange: next => {
142
+ if (Array.isArray(next)) {
143
+ if (props.type === 'multiple') props.onValueChange?.(next)
144
+ } else if (props.type !== 'multiple') {
145
+ props.onValueChange?.(next)
146
+ }
147
+ }
148
+ })
149
+ const selected = (item: string) => isItemOpen(owner.value, item)
150
+ const [focus, setFocus] = useState<string>()
151
+ const activeIndex = entries.findIndex(item => !item.disabled && item.value === focus)
152
+ const selectedIndex = entries.findIndex(item => !item.disabled && selected(item.value))
153
+ const index =
154
+ activeIndex >= 0
155
+ ? activeIndex
156
+ : selectedIndex >= 0
157
+ ? selectedIndex
158
+ : entries.findIndex(item => !item.disabled)
159
+ const active = entries[index]?.value
160
+ const optionId = (idx: number) => ids + '-option-' + idx
161
+ const nativeValue = multiple
162
+ ? entries.filter(item => selected(item.value)).map(item => item.value)
163
+ : (entries.find(item => selected(item.value))?.value ?? '')
164
+
165
+ const initialValue = useRef(owner.value)
166
+ useFormReset(select, () => {
167
+ owner.reset()
168
+ setFocus(undefined)
169
+ const resetValue = owner.controlled ? owner.value : initialValue.current
170
+ const hasSelection = entries.some(item => isItemOpen(resetValue, item.value))
171
+ for (const option of select.current?.options ?? []) {
172
+ option.selected =
173
+ isItemOpen(resetValue, option.value) || (option.value === '' && !hasSelection)
174
+ }
175
+ })
176
+
177
+ function move(next: string | undefined) {
178
+ if (next === undefined) return
179
+ setFocus(next)
180
+ const nextIndex = entries.findIndex(item => item.value === next)
181
+ host.current?.ownerDocument
182
+ .getElementById(optionId(nextIndex))
183
+ ?.scrollIntoView({ block: 'nearest' })
184
+ }
185
+
186
+ function choose(item: string) {
187
+ owner.request(nextSelection(owner.value, item, !multiple || !selected(item)))
188
+ }
189
+
190
+ function navigate(event: KeyboardEvent<HTMLDivElement>) {
191
+ if (event.nativeEvent.isComposing || event.altKey || event.ctrlKey || event.metaKey) return
192
+ if (event.key === 'Enter' || event.key === ' ') {
193
+ event.preventDefault()
194
+ if (active !== undefined) choose(active)
195
+ } else if (['ArrowDown', 'ArrowUp', 'Home', 'End'].includes(event.key)) {
196
+ event.preventDefault()
197
+ move(moveSelectFocus(entries, active ?? null, event.key))
198
+ } else if (matcher.isSingleKey(event.key)) {
199
+ event.preventDefault()
200
+ const now = event.timeStamp
201
+ const previous = search.current
202
+ const query =
203
+ (previous.matcher === matcher && now - previous.at < 1000 ? previous.text : '') + event.key
204
+ search.current = { text: query, at: now, matcher }
205
+ move(matchSelectText(entries, query, active ?? null, matcher))
206
+ }
207
+ }
208
+
209
+ const unavailable = () => !!(control.disabled || select.current?.matches(':disabled'))
210
+ return (
211
+ <>
212
+ <div
213
+ {...hostProps}
214
+ ref={hostRef}
215
+ id={id}
216
+ // oxlint-disable-next-line jsx-a11y/prefer-tag-over-role -- The native bridge cannot present rich labels.
217
+ role="listbox"
218
+ tabIndex={control.disabled ? -1 : 0}
219
+ aria-label={label}
220
+ aria-labelledby={listLabel}
221
+ aria-describedby={control['aria-describedby']}
222
+ aria-invalid={control['aria-invalid']}
223
+ aria-required={control['aria-required']}
224
+ aria-multiselectable={multiple || undefined}
225
+ aria-activedescendant={index >= 0 ? optionId(index) : undefined}
226
+ aria-disabled={control.disabled || undefined}
227
+ data-invalid={control['data-invalid']}
228
+ data-disabled={control.disabled ? '' : undefined}
229
+ data-slot={joinIds('list-box', slot)}
230
+ className={cn(listClass, controlMetrics({ controlSize }), className)}
231
+ onKeyDown={event => composeAction(onKeyDown, navigate, unavailable)(event)}
232
+ onClick={event =>
233
+ composeAction(
234
+ onClick,
235
+ event => {
236
+ const option = (event.target as HTMLElement).closest<HTMLElement>(
237
+ '[data-list-box-option]'
238
+ )
239
+ const item = entries[Number(option?.dataset.listBoxOption)]
240
+ if (!item || item.disabled) return
241
+ host.current?.focus({ preventScroll: true })
242
+ setFocus(item.value)
243
+ choose(item.value)
244
+ },
245
+ unavailable
246
+ )(event)
247
+ }
248
+ >
249
+ {entries.map((item, idx) => (
250
+ <div
251
+ key={item.value}
252
+ id={optionId(idx)}
253
+ // oxlint-disable-next-line jsx-a11y/prefer-tag-over-role -- The corresponding native option lives in the form bridge.
254
+ role="option"
255
+ aria-selected={selected(item.value)}
256
+ aria-disabled={item.disabled || undefined}
257
+ data-list-box-option={idx}
258
+ data-slot="list-box-option"
259
+ data-active={index === idx ? '' : undefined}
260
+ data-selected={selected(item.value) ? '' : undefined}
261
+ data-disabled={item.disabled ? '' : undefined}
262
+ className={cn(optionClass, parts?.option?.className)}
263
+ style={parts?.option?.style}
264
+ >
265
+ <div className="min-w-0 break-words">{item.label}</div>
266
+ <span
267
+ aria-hidden="true"
268
+ data-slot="list-box-indicator"
269
+ className={cn('w-4 shrink-0 text-center', parts?.indicator?.className)}
270
+ style={parts?.indicator?.style}
271
+ >
272
+ {selected(item.value) ? '✓' : ''}
273
+ </span>
274
+ </div>
275
+ ))}
276
+ </div>
277
+ <select
278
+ ref={select}
279
+ id={control.id}
280
+ name={name}
281
+ form={form}
282
+ multiple={multiple}
283
+ required={control.required}
284
+ disabled={control.disabled}
285
+ aria-hidden="true"
286
+ tabIndex={-1}
287
+ className="sr-only"
288
+ data-slot="list-box-native"
289
+ value={nativeValue}
290
+ onChange={event => {
291
+ if (unavailable()) return
292
+ const native = event.currentTarget
293
+ owner.request(
294
+ multiple
295
+ ? Array.from(native.selectedOptions, option => option.value)
296
+ : native.value || null
297
+ )
298
+ }}
299
+ onFocus={() => host.current?.focus({ preventScroll: true })}
300
+ onInvalid={event => {
301
+ event.preventDefault()
302
+ host.current?.focus()
303
+ }}
304
+ >
305
+ {!multiple && <option value="">No selection</option>}
306
+ {entries.map(item => (
307
+ <option
308
+ key={item.value}
309
+ value={item.value}
310
+ disabled={item.disabled}
311
+ >
312
+ {item.textValue}
313
+ </option>
314
+ ))}
315
+ </select>
316
+ </>
317
+ )
318
+ }
@@ -160,12 +160,7 @@ export function NavLinkGroupTrigger({
160
160
  {...native}
161
161
  data-slot={joinIds('nav-link-group-trigger', slot)}
162
162
  data-control-size={controlSize}
163
- className={cn(
164
- controlMetrics({ controlSize }),
165
- itemClass,
166
- 'rtl:[&>svg]:rotate-180 rtl:data-[state=open]:[&>svg]:rotate-90',
167
- className
168
- )}
163
+ className={cn(controlMetrics({ controlSize }), itemClass, className)}
169
164
  >
170
165
  <Content {...{ icon, description, parts, children }} />
171
166
  </CollapsibleTrigger>
@@ -232,6 +232,7 @@ export function Radio({
232
232
  }, [controlled, selected])
233
233
  const controlNode = (
234
234
  <span
235
+ dir={!labelled && !described ? props.dir : undefined}
235
236
  className={cn(
236
237
  'relative inline-grid shrink-0',
237
238
  indicatorPressClass,
@@ -273,6 +274,7 @@ export function Radio({
273
274
 
274
275
  const row = labelled ? (
275
276
  <label
277
+ dir={!described ? props.dir : undefined}
276
278
  className={cn(rowClass, !described && parts?.wrapper?.className)}
277
279
  style={!described ? parts?.wrapper?.style : undefined}
278
280
  data-slot={!described ? 'radio-wrapper' : undefined}
@@ -294,6 +296,7 @@ export function Radio({
294
296
 
295
297
  return (
296
298
  <div
299
+ dir={props.dir}
297
300
  className={cn('grid min-w-0 max-w-full justify-items-start', parts?.wrapper?.className)}
298
301
  style={parts?.wrapper?.style}
299
302
  data-slot="radio-wrapper"