@cognitap/radiant 0.0.83 → 0.0.85
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 +36 -0
- package/dist/library/color/colorValue.d.ts +8 -0
- package/dist/library/color/colorValue.d.ts.map +1 -0
- package/dist/library/color/colorValue.js +87 -0
- package/dist/library/color/colorValue.js.map +1 -0
- package/dist/library/color/oklch.d.ts +2 -0
- package/dist/library/color/oklch.d.ts.map +1 -1
- package/dist/library/color/oklch.js +27 -11
- package/dist/library/color/oklch.js.map +1 -1
- package/dist/library/components/Cascader/Cascader.d.ts +25 -0
- package/dist/library/components/Cascader/Cascader.d.ts.map +1 -0
- package/dist/library/components/Cascader/Cascader.js +154 -0
- package/dist/library/components/Cascader/Cascader.js.map +1 -0
- package/dist/library/components/Cascader/CascaderColumn.d.ts +25 -0
- package/dist/library/components/Cascader/CascaderColumn.d.ts.map +1 -0
- package/dist/library/components/Cascader/CascaderColumn.js +80 -0
- package/dist/library/components/Cascader/CascaderColumn.js.map +1 -0
- package/dist/library/components/CodeBlock/CodeBlock.d.ts.map +1 -1
- package/dist/library/components/CodeBlock/CodeBlock.js +3 -37
- package/dist/library/components/CodeBlock/CodeBlock.js.map +1 -1
- package/dist/library/components/Collapsible/Collapsible.d.ts +5 -1
- package/dist/library/components/Collapsible/Collapsible.d.ts.map +1 -1
- package/dist/library/components/Collapsible/Collapsible.js +13 -3
- package/dist/library/components/Collapsible/Collapsible.js.map +1 -1
- package/dist/library/components/ColorInput/ColorInput.d.ts +22 -0
- package/dist/library/components/ColorInput/ColorInput.d.ts.map +1 -0
- package/dist/library/components/ColorInput/ColorInput.js +63 -0
- package/dist/library/components/ColorInput/ColorInput.js.map +1 -0
- package/dist/library/components/ColorPicker/ColorArea.d.ts +11 -0
- package/dist/library/components/ColorPicker/ColorArea.d.ts.map +1 -0
- package/dist/library/components/ColorPicker/ColorArea.js +58 -0
- package/dist/library/components/ColorPicker/ColorArea.js.map +1 -0
- package/dist/library/components/ColorPicker/ColorPicker.d.ts +37 -0
- package/dist/library/components/ColorPicker/ColorPicker.d.ts.map +1 -0
- package/dist/library/components/ColorPicker/ColorPicker.js +135 -0
- package/dist/library/components/ColorPicker/ColorPicker.js.map +1 -0
- package/dist/library/components/CopyButton/CopyButton.d.ts +17 -0
- package/dist/library/components/CopyButton/CopyButton.d.ts.map +1 -0
- package/dist/library/components/CopyButton/CopyButton.js +22 -0
- package/dist/library/components/CopyButton/CopyButton.js.map +1 -0
- package/dist/library/components/DataTable/DataTable.d.ts +47 -0
- package/dist/library/components/DataTable/DataTable.d.ts.map +1 -0
- package/dist/library/components/DataTable/DataTable.js +146 -0
- package/dist/library/components/DataTable/DataTable.js.map +1 -0
- package/dist/library/components/Dropzone/Dropzone.d.ts +26 -0
- package/dist/library/components/Dropzone/Dropzone.d.ts.map +1 -0
- package/dist/library/components/Dropzone/Dropzone.js +118 -0
- package/dist/library/components/Dropzone/Dropzone.js.map +1 -0
- package/dist/library/components/Highlight/Highlight.d.ts +15 -0
- package/dist/library/components/Highlight/Highlight.d.ts.map +1 -0
- package/dist/library/components/Highlight/Highlight.js +69 -0
- package/dist/library/components/Highlight/Highlight.js.map +1 -0
- package/dist/library/components/Rating/Rating.d.ts +21 -0
- package/dist/library/components/Rating/Rating.d.ts.map +1 -0
- package/dist/library/components/Rating/Rating.js +81 -0
- package/dist/library/components/Rating/Rating.js.map +1 -0
- package/dist/library/components/Select/Select.d.ts +3 -9
- package/dist/library/components/Select/Select.d.ts.map +1 -1
- package/dist/library/components/Select/Select.js +16 -156
- package/dist/library/components/Select/Select.js.map +1 -1
- package/dist/library/components/Select/SelectPopup.d.ts +29 -0
- package/dist/library/components/Select/SelectPopup.d.ts.map +1 -0
- package/dist/library/components/Select/SelectPopup.js +179 -0
- package/dist/library/components/Select/SelectPopup.js.map +1 -0
- package/dist/library/components/Slider/Slider.d.ts +2 -2
- package/dist/library/components/Slider/Slider.d.ts.map +1 -1
- package/dist/library/components/Slider/Slider.js +44 -3
- package/dist/library/components/Slider/Slider.js.map +1 -1
- package/dist/library/components/Spoiler/Spoiler.d.ts +15 -0
- package/dist/library/components/Spoiler/Spoiler.d.ts.map +1 -0
- package/dist/library/components/Spoiler/Spoiler.js +34 -0
- package/dist/library/components/Spoiler/Spoiler.js.map +1 -0
- package/dist/library/components/Stepper/Stepper.d.ts +48 -0
- package/dist/library/components/Stepper/Stepper.d.ts.map +1 -0
- package/dist/library/components/Stepper/Stepper.js +81 -0
- package/dist/library/components/Stepper/Stepper.js.map +1 -0
- package/dist/library/components/Timeline/Timeline.d.ts +30 -0
- package/dist/library/components/Timeline/Timeline.d.ts.map +1 -0
- package/dist/library/components/Timeline/Timeline.js +15 -0
- package/dist/library/components/Timeline/Timeline.js.map +1 -0
- package/dist/library/components/Tour/Tour.d.ts +31 -0
- package/dist/library/components/Tour/Tour.d.ts.map +1 -0
- package/dist/library/components/Tour/Tour.js +145 -0
- package/dist/library/components/Tour/Tour.js.map +1 -0
- package/dist/library/components/Tree/Tree.d.ts +48 -0
- package/dist/library/components/Tree/Tree.d.ts.map +1 -0
- package/dist/library/components/Tree/Tree.js +298 -0
- package/dist/library/components/Tree/Tree.js.map +1 -0
- package/dist/library/components/Tree/treeModel.d.ts +43 -0
- package/dist/library/components/Tree/treeModel.d.ts.map +1 -0
- package/dist/library/components/Tree/treeModel.js +83 -0
- package/dist/library/components/Tree/treeModel.js.map +1 -0
- package/dist/library/components/Tree/useTreeLoading.d.ts +6 -0
- package/dist/library/components/Tree/useTreeLoading.d.ts.map +1 -0
- package/dist/library/components/Tree/useTreeLoading.js +69 -0
- package/dist/library/components/Tree/useTreeLoading.js.map +1 -0
- package/dist/library/components/TreeSelect/TreeSelect.d.ts +29 -0
- package/dist/library/components/TreeSelect/TreeSelect.d.ts.map +1 -0
- package/dist/library/components/TreeSelect/TreeSelect.js +163 -0
- package/dist/library/components/TreeSelect/TreeSelect.js.map +1 -0
- package/dist/library/index.d.ts +19 -0
- package/dist/library/index.d.ts.map +1 -1
- package/dist/library/index.js +14 -0
- package/dist/library/index.js.map +1 -1
- package/dist/library/motion/transitions.d.ts.map +1 -1
- package/dist/library/motion/transitions.js +8 -3
- package/dist/library/motion/transitions.js.map +1 -1
- package/dist/library/react/useClipboardCopy.d.ts +8 -0
- package/dist/library/react/useClipboardCopy.d.ts.map +1 -0
- package/dist/library/react/useClipboardCopy.js +49 -0
- package/dist/library/react/useClipboardCopy.js.map +1 -0
- package/dist/library/react/useFormReset.d.ts +1 -1
- package/dist/library/react/useFormReset.d.ts.map +1 -1
- package/dist/library/react/useFormReset.js.map +1 -1
- package/dist/library/react/usePopoverSurface.d.ts.map +1 -1
- package/dist/library/react/usePopoverSurface.js +3 -2
- package/dist/library/react/usePopoverSurface.js.map +1 -1
- package/dist/library/styles.css +1 -1
- package/package.json +1 -1
- package/src/color/colorValue.ts +89 -0
- package/src/color/oklch.ts +33 -14
- package/src/components/Cascader/Cascader.tsx +315 -0
- package/src/components/Cascader/CascaderColumn.tsx +203 -0
- package/src/components/CodeBlock/CodeBlock.tsx +3 -45
- package/src/components/Collapsible/Collapsible.tsx +22 -4
- package/src/components/ColorInput/ColorInput.tsx +191 -0
- package/src/components/ColorPicker/ColorArea.tsx +98 -0
- package/src/components/ColorPicker/ColorPicker.tsx +283 -0
- package/src/components/CopyButton/CopyButton.tsx +75 -0
- package/src/components/DataTable/DataTable.tsx +384 -0
- package/src/components/Dropzone/Dropzone.tsx +230 -0
- package/src/components/Highlight/Highlight.tsx +112 -0
- package/src/components/Rating/Rating.tsx +202 -0
- package/src/components/Select/Select.tsx +20 -192
- package/src/components/Select/SelectPopup.tsx +246 -0
- package/src/components/Slider/Slider.tsx +51 -2
- package/src/components/Spoiler/Spoiler.tsx +96 -0
- package/src/components/Stepper/Stepper.tsx +306 -0
- package/src/components/Timeline/Timeline.tsx +139 -0
- package/src/components/Tour/Tour.tsx +335 -0
- package/src/components/Tree/Tree.tsx +530 -0
- package/src/components/Tree/treeModel.ts +110 -0
- package/src/components/Tree/useTreeLoading.ts +73 -0
- package/src/components/TreeSelect/TreeSelect.tsx +326 -0
- package/src/index.ts +50 -0
- package/src/motion/transitions.ts +8 -3
- package/src/react/useClipboardCopy.ts +54 -0
- package/src/react/useFormReset.ts +4 -1
- package/src/react/usePopoverSurface.ts +3 -2
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { useLayoutEffect, useRef, type ComponentProps, type RefObject } from 'react'
|
|
4
|
+
import { cn } from 'cn'
|
|
5
|
+
import { joinIds } from '../../internal/ids.js'
|
|
6
|
+
import { positionSelect } from '../../internal/select.js'
|
|
7
|
+
import { observeAnchorLocale, syncPopupLocale } from '../../internal/popupLocale.js'
|
|
8
|
+
import { useComposedRefs } from '../../react/useComposedRefs.js'
|
|
9
|
+
import { useTransitionHost } from '../../motion/host.js'
|
|
10
|
+
import type { ControlSize } from '../../presentation.js'
|
|
11
|
+
import type { ThemeStyle } from '../../themes/defineTheme.js'
|
|
12
|
+
import { controlMetrics } from '../shared/controlStyles.js'
|
|
13
|
+
import { selectContentClass } from './selectStyles.js'
|
|
14
|
+
|
|
15
|
+
type SelectPopupState = {
|
|
16
|
+
open: boolean
|
|
17
|
+
trigger: HTMLButtonElement | null
|
|
18
|
+
content: RefObject<HTMLDivElement | null>
|
|
19
|
+
restoreFocus: RefObject<boolean>
|
|
20
|
+
value?: string | null
|
|
21
|
+
collection?: readonly unknown[]
|
|
22
|
+
close(restore?: boolean): void
|
|
23
|
+
focusInitial(node: HTMLDivElement): void
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Shared Select top layer. The owner defines its roles, selection and keyboard model. */
|
|
27
|
+
export type SelectContentProps = Omit<ComponentProps<'div'>, 'style' | 'popover'> & {
|
|
28
|
+
style?: ThemeStyle
|
|
29
|
+
position?: 'item-aligned' | 'popper'
|
|
30
|
+
sideOffset?: number
|
|
31
|
+
collisionPadding?: number
|
|
32
|
+
motion?: 'inherit' | 'off'
|
|
33
|
+
'data-slot'?: string
|
|
34
|
+
}
|
|
35
|
+
export function SelectPopup({
|
|
36
|
+
state: select,
|
|
37
|
+
controlSize,
|
|
38
|
+
children,
|
|
39
|
+
position = 'item-aligned',
|
|
40
|
+
width = 'trigger',
|
|
41
|
+
sideOffset = 4,
|
|
42
|
+
collisionPadding = 8,
|
|
43
|
+
motion = 'inherit',
|
|
44
|
+
className,
|
|
45
|
+
ref,
|
|
46
|
+
...props
|
|
47
|
+
}: SelectContentProps & {
|
|
48
|
+
state: SelectPopupState
|
|
49
|
+
controlSize: ControlSize
|
|
50
|
+
width?: 'trigger' | 'content'
|
|
51
|
+
}) {
|
|
52
|
+
const { lang, dir } = props
|
|
53
|
+
const mergedRef = useComposedRefs(select.content, ref)
|
|
54
|
+
const {
|
|
55
|
+
ref: transitionRef,
|
|
56
|
+
active: motionActive,
|
|
57
|
+
off: motionOff
|
|
58
|
+
} = useTransitionHost('popup', mergedRef, motion === 'off')
|
|
59
|
+
const latest = useRef(select)
|
|
60
|
+
useLayoutEffect(() => {
|
|
61
|
+
latest.current = select
|
|
62
|
+
})
|
|
63
|
+
const wasOpen = useRef(false)
|
|
64
|
+
useLayoutEffect(() => {
|
|
65
|
+
const state = latest.current
|
|
66
|
+
const node = state.content.current
|
|
67
|
+
const trigger = state.trigger
|
|
68
|
+
if (!node || !trigger) return
|
|
69
|
+
const doc = trigger.ownerDocument
|
|
70
|
+
const win = doc.defaultView!
|
|
71
|
+
if (!state.open) {
|
|
72
|
+
if (
|
|
73
|
+
wasOpen.current &&
|
|
74
|
+
latest.current.restoreFocus.current &&
|
|
75
|
+
node.contains(doc.activeElement)
|
|
76
|
+
)
|
|
77
|
+
trigger.focus({ preventScroll: true })
|
|
78
|
+
if (node.matches(':popover-open')) node.hidePopover()
|
|
79
|
+
wasOpen.current = false
|
|
80
|
+
return
|
|
81
|
+
}
|
|
82
|
+
const seedWidth = () => {
|
|
83
|
+
const available = Math.max(
|
|
84
|
+
0,
|
|
85
|
+
(win.visualViewport?.width ?? win.innerWidth) - 2 * Math.max(0, collisionPadding)
|
|
86
|
+
)
|
|
87
|
+
const anchor = `${Math.min(trigger.getBoundingClientRect().width, available)}px`
|
|
88
|
+
const widthProperty =
|
|
89
|
+
width === 'content' ? '--_rad-select-anchor-width' : '--_rad-select-width'
|
|
90
|
+
if (node.style.getPropertyValue(widthProperty) !== anchor)
|
|
91
|
+
node.style.setProperty(widthProperty, anchor)
|
|
92
|
+
if (width === 'content') {
|
|
93
|
+
const limit = `${available}px`
|
|
94
|
+
if (node.style.getPropertyValue('--_rad-select-available-width') !== limit)
|
|
95
|
+
node.style.setProperty('--_rad-select-available-width', limit)
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
// Top-layer auto layout is viewport-sized. Establish the anchor width before it can paint.
|
|
99
|
+
seedWidth()
|
|
100
|
+
syncPopupLocale(trigger, node, { lang, dir })
|
|
101
|
+
node.showPopover()
|
|
102
|
+
const place = () => {
|
|
103
|
+
if (!node.isConnected || !node.matches(':popover-open')) return
|
|
104
|
+
const rtl = syncPopupLocale(trigger, node, { lang, dir })
|
|
105
|
+
seedWidth()
|
|
106
|
+
const viewport = win.visualViewport
|
|
107
|
+
const selected =
|
|
108
|
+
position === 'item-aligned'
|
|
109
|
+
? Array.from(node.querySelectorAll<HTMLElement>('[data-slot~="select-item"]')).find(
|
|
110
|
+
item => item.dataset.value === latest.current.value
|
|
111
|
+
)
|
|
112
|
+
: undefined
|
|
113
|
+
const rect = trigger.getBoundingClientRect()
|
|
114
|
+
const computed = win.getComputedStyle(node)
|
|
115
|
+
const viewportNode = node.querySelector<HTMLElement>('[data-slot~="select-viewport"]')
|
|
116
|
+
const naturalHeight = viewportNode
|
|
117
|
+
? viewportNode.scrollHeight +
|
|
118
|
+
parseFloat(computed.paddingTop) +
|
|
119
|
+
parseFloat(computed.paddingBottom)
|
|
120
|
+
: node.scrollHeight
|
|
121
|
+
const next = positionSelect({
|
|
122
|
+
trigger: rect,
|
|
123
|
+
width: node.getBoundingClientRect().width,
|
|
124
|
+
height:
|
|
125
|
+
naturalHeight +
|
|
126
|
+
parseFloat(computed.borderTopWidth) +
|
|
127
|
+
parseFloat(computed.borderBottomWidth),
|
|
128
|
+
selectedOffset: selected ? selected.offsetTop + selected.offsetHeight / 2 : 24,
|
|
129
|
+
viewport: {
|
|
130
|
+
top: viewport?.offsetTop ?? 0,
|
|
131
|
+
left: viewport?.offsetLeft ?? 0,
|
|
132
|
+
width: viewport?.width ?? win.innerWidth,
|
|
133
|
+
height: viewport?.height ?? win.innerHeight
|
|
134
|
+
},
|
|
135
|
+
padding: Math.max(0, collisionPadding),
|
|
136
|
+
offset: sideOffset,
|
|
137
|
+
aligned: position === 'item-aligned',
|
|
138
|
+
rtl
|
|
139
|
+
})
|
|
140
|
+
for (const [key, value] of Object.entries({
|
|
141
|
+
top: next.top,
|
|
142
|
+
left: next.left,
|
|
143
|
+
width: next.width,
|
|
144
|
+
'max-height': next.maxHeight
|
|
145
|
+
}))
|
|
146
|
+
if (node.style.getPropertyValue(`--_rad-select-${key}`) !== `${value}px`)
|
|
147
|
+
node.style.setProperty(`--_rad-select-${key}`, `${value}px`)
|
|
148
|
+
}
|
|
149
|
+
place()
|
|
150
|
+
const active = doc.activeElement as HTMLElement | null
|
|
151
|
+
if (
|
|
152
|
+
!wasOpen.current ||
|
|
153
|
+
active === doc.body ||
|
|
154
|
+
active?.getAttribute('aria-disabled') === 'true'
|
|
155
|
+
) {
|
|
156
|
+
state.focusInitial(node)
|
|
157
|
+
}
|
|
158
|
+
wasOpen.current = true
|
|
159
|
+
const outside = (event: PointerEvent) => {
|
|
160
|
+
const target = event.target
|
|
161
|
+
if (!(target instanceof win.Node) || node.contains(target) || trigger.contains(target)) return
|
|
162
|
+
latest.current.close(false)
|
|
163
|
+
}
|
|
164
|
+
const focusOut = (event: FocusEvent) => {
|
|
165
|
+
const target = event.target
|
|
166
|
+
if (target === doc.body || target === doc.documentElement) return
|
|
167
|
+
if (target instanceof win.Node && !node.contains(target) && !trigger.contains(target))
|
|
168
|
+
latest.current.close(false)
|
|
169
|
+
}
|
|
170
|
+
const scroll = (event: Event) => {
|
|
171
|
+
if (!(event.target instanceof win.Node) || !node.contains(event.target)) place()
|
|
172
|
+
}
|
|
173
|
+
let frame = 0
|
|
174
|
+
const schedule = () => {
|
|
175
|
+
if (!frame)
|
|
176
|
+
frame = win.requestAnimationFrame(() => {
|
|
177
|
+
frame = 0
|
|
178
|
+
place()
|
|
179
|
+
})
|
|
180
|
+
}
|
|
181
|
+
const observer = new win.ResizeObserver(schedule)
|
|
182
|
+
const mutations = new win.MutationObserver(schedule)
|
|
183
|
+
mutations.observe(node, { childList: true, subtree: true, characterData: true })
|
|
184
|
+
observeAnchorLocale(mutations, trigger)
|
|
185
|
+
observer.observe(trigger)
|
|
186
|
+
observer.observe(node)
|
|
187
|
+
doc.addEventListener('pointerdown', outside, true)
|
|
188
|
+
doc.addEventListener('focusin', focusOut)
|
|
189
|
+
doc.addEventListener('scroll', scroll, true)
|
|
190
|
+
win.addEventListener('resize', place)
|
|
191
|
+
win.visualViewport?.addEventListener('resize', place)
|
|
192
|
+
win.visualViewport?.addEventListener('scroll', place)
|
|
193
|
+
return () => {
|
|
194
|
+
observer.disconnect()
|
|
195
|
+
mutations.disconnect()
|
|
196
|
+
win.cancelAnimationFrame(frame)
|
|
197
|
+
doc.removeEventListener('pointerdown', outside, true)
|
|
198
|
+
doc.removeEventListener('focusin', focusOut)
|
|
199
|
+
doc.removeEventListener('scroll', scroll, true)
|
|
200
|
+
win.removeEventListener('resize', place)
|
|
201
|
+
win.visualViewport?.removeEventListener('resize', place)
|
|
202
|
+
win.visualViewport?.removeEventListener('scroll', place)
|
|
203
|
+
}
|
|
204
|
+
}, [
|
|
205
|
+
select.open,
|
|
206
|
+
select.trigger,
|
|
207
|
+
select.collection,
|
|
208
|
+
position,
|
|
209
|
+
width,
|
|
210
|
+
sideOffset,
|
|
211
|
+
collisionPadding,
|
|
212
|
+
lang,
|
|
213
|
+
dir
|
|
214
|
+
])
|
|
215
|
+
useLayoutEffect(() => {
|
|
216
|
+
const node = select.content.current
|
|
217
|
+
const trigger = select.trigger
|
|
218
|
+
return () => {
|
|
219
|
+
if (node?.contains(node.ownerDocument.activeElement) && trigger?.isConnected)
|
|
220
|
+
trigger.focus({ preventScroll: true })
|
|
221
|
+
}
|
|
222
|
+
}, [select.content, select.trigger])
|
|
223
|
+
return (
|
|
224
|
+
<div
|
|
225
|
+
{...props}
|
|
226
|
+
ref={transitionRef}
|
|
227
|
+
popover="manual"
|
|
228
|
+
className={cn(
|
|
229
|
+
selectContentClass,
|
|
230
|
+
controlMetrics({ controlSize }),
|
|
231
|
+
width === 'content' &&
|
|
232
|
+
'w-max min-w-[var(--_rad-select-anchor-width,0px)] max-w-[var(--_rad-select-available-width,100vw)]',
|
|
233
|
+
motionActive &&
|
|
234
|
+
!motionOff &&
|
|
235
|
+
'opacity-[var(--_rad-popup-enter,0)] motion-reduce:opacity-100',
|
|
236
|
+
className
|
|
237
|
+
)}
|
|
238
|
+
data-slot={joinIds('select-content', props['data-slot'])}
|
|
239
|
+
data-state={select.open ? 'open' : 'closed'}
|
|
240
|
+
data-motion={motion}
|
|
241
|
+
data-position={position}
|
|
242
|
+
>
|
|
243
|
+
{children}
|
|
244
|
+
</div>
|
|
245
|
+
)
|
|
246
|
+
}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
'use client'
|
|
2
2
|
|
|
3
|
-
import type { ComponentProps } from 'react'
|
|
3
|
+
import type { ComponentProps, KeyboardEvent } from 'react'
|
|
4
4
|
import { cn } from 'cn'
|
|
5
5
|
import { joinIds } from '../../internal/ids.js'
|
|
6
|
+
import { composeAction } from '../../internal/events.js'
|
|
7
|
+
import { addDecimal } from '../../internal/decimal.js'
|
|
6
8
|
import { useFieldControl } from '../Field/Field.js'
|
|
7
9
|
import { controlBorderClass, controlMetrics } from '../shared/controlStyles.js'
|
|
8
10
|
import { useNativeOwner, type ValueOwner } from '../../react/useNativeOwner.js'
|
|
@@ -103,12 +105,58 @@ export type SliderProps = Omit<
|
|
|
103
105
|
'data-slot'?: string
|
|
104
106
|
}
|
|
105
107
|
|
|
106
|
-
|
|
108
|
+
function rangeBound(attribute: string, fallback: number) {
|
|
109
|
+
const value = Number(attribute)
|
|
110
|
+
return /^-?(?:[0-9]+(?:\.[0-9]+)?|\.[0-9]+)(?:[eE][+-]?[0-9]+)?$/.test(attribute) &&
|
|
111
|
+
Number.isFinite(value)
|
|
112
|
+
? value
|
|
113
|
+
: fallback
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Horizontal RTL keys move the thumb in the pressed direction across browsers. */
|
|
117
|
+
function stepRtl(event: KeyboardEvent<HTMLInputElement>) {
|
|
118
|
+
const input = event.currentTarget
|
|
119
|
+
if (
|
|
120
|
+
(event.key !== 'ArrowLeft' && event.key !== 'ArrowRight') ||
|
|
121
|
+
event.altKey ||
|
|
122
|
+
event.ctrlKey ||
|
|
123
|
+
event.metaKey ||
|
|
124
|
+
event.nativeEvent.isComposing ||
|
|
125
|
+
input.matches(':disabled')
|
|
126
|
+
)
|
|
127
|
+
return
|
|
128
|
+
const view = input.ownerDocument.defaultView!
|
|
129
|
+
if (view.getComputedStyle(input).direction !== 'rtl') return
|
|
130
|
+
|
|
131
|
+
event.preventDefault()
|
|
132
|
+
const previous = input.value
|
|
133
|
+
const direction = event.key === 'ArrowLeft' ? 1 : -1
|
|
134
|
+
if (input.step.toLowerCase() === 'any') {
|
|
135
|
+
// Continuous ranges have no step for stepUp. Move one percent of the interval.
|
|
136
|
+
const min = rangeBound(input.min, 0)
|
|
137
|
+
const max = Math.max(min, rangeBound(input.max, 100))
|
|
138
|
+
const span = max - min
|
|
139
|
+
const step = Number.isFinite(span) ? span / 100 : max / 100 - min / 100
|
|
140
|
+
input.valueAsNumber = Math.max(
|
|
141
|
+
min,
|
|
142
|
+
Math.min(max, addDecimal(input.valueAsNumber, step * direction))
|
|
143
|
+
)
|
|
144
|
+
} else {
|
|
145
|
+
input.stepUp(direction)
|
|
146
|
+
}
|
|
147
|
+
if (input.value !== previous) {
|
|
148
|
+
input.dispatchEvent(new view.Event('input', { bubbles: true, composed: true }))
|
|
149
|
+
input.dispatchEvent(new view.Event('change', { bubbles: true }))
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** One native range input owns numeric selection, pointer input and forms. */
|
|
107
154
|
export function Slider({
|
|
108
155
|
className,
|
|
109
156
|
controlSize = 'medium',
|
|
110
157
|
orientation = 'horizontal',
|
|
111
158
|
onChange,
|
|
159
|
+
onKeyDown,
|
|
112
160
|
onValueChange,
|
|
113
161
|
id,
|
|
114
162
|
disabled,
|
|
@@ -138,6 +186,7 @@ export function Slider({
|
|
|
138
186
|
<input
|
|
139
187
|
{...nativeProps}
|
|
140
188
|
{...control}
|
|
189
|
+
onKeyDown={orientation === 'horizontal' ? composeAction(onKeyDown, stepRtl) : onKeyDown}
|
|
141
190
|
onChange={event => {
|
|
142
191
|
onChange?.(event)
|
|
143
192
|
onValueChange?.(event.currentTarget.valueAsNumber)
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { useLayoutEffect, useRef, useState } from 'react'
|
|
4
|
+
import { cn } from 'cn'
|
|
5
|
+
import { joinIds } from '../../internal/ids.js'
|
|
6
|
+
import type { DistributiveOmit } from '../../internal/types.js'
|
|
7
|
+
import type { PartStyle } from '../../presentation.js'
|
|
8
|
+
import { useControllableState } from '../../react/useControllableState.js'
|
|
9
|
+
import {
|
|
10
|
+
CollapsibleContent,
|
|
11
|
+
CollapsibleRoot,
|
|
12
|
+
CollapsibleTrigger,
|
|
13
|
+
type CollapsibleProps
|
|
14
|
+
} from '../Collapsible/Collapsible.js'
|
|
15
|
+
|
|
16
|
+
export type SpoilerProps = DistributiveOmit<CollapsibleProps, 'name' | 'onToggle'> & {
|
|
17
|
+
/** Maximum height of the collapsed preview in CSS pixels. */
|
|
18
|
+
maxHeight?: number
|
|
19
|
+
showMoreLabel?: string
|
|
20
|
+
showLessLabel?: string
|
|
21
|
+
parts?: { trigger?: PartStyle; content?: PartStyle }
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function Spoiler({
|
|
25
|
+
maxHeight = 96,
|
|
26
|
+
open,
|
|
27
|
+
defaultOpen,
|
|
28
|
+
onOpenChange,
|
|
29
|
+
showMoreLabel = 'Show more',
|
|
30
|
+
showLessLabel = 'Show less',
|
|
31
|
+
motion,
|
|
32
|
+
className,
|
|
33
|
+
parts,
|
|
34
|
+
children,
|
|
35
|
+
...props
|
|
36
|
+
}: SpoilerProps) {
|
|
37
|
+
if (!Number.isFinite(maxHeight) || maxHeight < 0) {
|
|
38
|
+
throw new RangeError('Spoiler maxHeight must be a nonnegative number of pixels.')
|
|
39
|
+
}
|
|
40
|
+
const state = useControllableState({
|
|
41
|
+
name: 'Spoiler',
|
|
42
|
+
value: open,
|
|
43
|
+
defaultValue: defaultOpen,
|
|
44
|
+
initialValue: false,
|
|
45
|
+
onChange: onOpenChange
|
|
46
|
+
})
|
|
47
|
+
const contentRef = useRef<HTMLDivElement>(null)
|
|
48
|
+
const [overflows, setOverflows] = useState<boolean | null>(null)
|
|
49
|
+
useLayoutEffect(() => {
|
|
50
|
+
const node = contentRef.current!
|
|
51
|
+
const win = node.ownerDocument.defaultView!
|
|
52
|
+
const measure = () => node.scrollHeight > maxHeight
|
|
53
|
+
// oxlint-disable-next-line react/set-state-in-effect -- Read external layout before showing a preview.
|
|
54
|
+
setOverflows(measure())
|
|
55
|
+
const observer = new win.ResizeObserver(() => setOverflows(measure()))
|
|
56
|
+
observer.observe(node)
|
|
57
|
+
return () => observer.disconnect()
|
|
58
|
+
}, [maxHeight])
|
|
59
|
+
const clipped = overflows === true && !state.value
|
|
60
|
+
|
|
61
|
+
return (
|
|
62
|
+
<CollapsibleRoot
|
|
63
|
+
{...props}
|
|
64
|
+
data-slot={joinIds('spoiler', props['data-slot'])}
|
|
65
|
+
className={cn('gap-0 [&::details-content]:row-start-1', className)}
|
|
66
|
+
open={!clipped}
|
|
67
|
+
onOpenChange={state.request}
|
|
68
|
+
motion={overflows === null ? 'off' : motion}
|
|
69
|
+
previewHeight={maxHeight}
|
|
70
|
+
>
|
|
71
|
+
<CollapsibleTrigger
|
|
72
|
+
data-slot="spoiler-trigger"
|
|
73
|
+
className={cn('row-start-2 py-2', parts?.trigger?.className, !overflows && 'hidden')}
|
|
74
|
+
style={parts?.trigger?.style}
|
|
75
|
+
>
|
|
76
|
+
{clipped ? showMoreLabel : showLessLabel}
|
|
77
|
+
</CollapsibleTrigger>
|
|
78
|
+
<CollapsibleContent
|
|
79
|
+
ref={contentRef}
|
|
80
|
+
data-slot="spoiler-content"
|
|
81
|
+
className={cn(
|
|
82
|
+
'flow-root pt-[var(--rad-collapsible-content-padding,var(--rad-collapsible-padding,var(--rad-surface-padding,1.5rem)))]',
|
|
83
|
+
parts?.content?.className
|
|
84
|
+
)}
|
|
85
|
+
style={parts?.content?.style}
|
|
86
|
+
>
|
|
87
|
+
<div
|
|
88
|
+
inert={clipped}
|
|
89
|
+
aria-hidden={clipped || undefined}
|
|
90
|
+
>
|
|
91
|
+
{children}
|
|
92
|
+
</div>
|
|
93
|
+
</CollapsibleContent>
|
|
94
|
+
</CollapsibleRoot>
|
|
95
|
+
)
|
|
96
|
+
}
|