@cognitap/radiant 0.0.82 → 0.0.84
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 +33 -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/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/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/DatePicker/DatePicker.d.ts +3 -1
- package/dist/library/components/DatePicker/DatePicker.d.ts.map +1 -1
- package/dist/library/components/DatePicker/DatePicker.js +2 -2
- package/dist/library/components/DatePicker/DatePicker.js.map +1 -1
- package/dist/library/components/DateRangePicker/DateRangePicker.d.ts +4 -1
- package/dist/library/components/DateRangePicker/DateRangePicker.d.ts.map +1 -1
- package/dist/library/components/DateRangePicker/DateRangePicker.js +7 -4
- package/dist/library/components/DateRangePicker/DateRangePicker.js.map +1 -1
- 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/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/TimeInput/TimeInput.d.ts +4 -1
- package/dist/library/components/TimeInput/TimeInput.d.ts.map +1 -1
- package/dist/library/components/TimeInput/TimeInput.js +2 -2
- package/dist/library/components/TimeInput/TimeInput.js.map +1 -1
- 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/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 +10 -0
- package/dist/library/index.d.ts.map +1 -1
- package/dist/library/index.js +9 -0
- package/dist/library/index.js.map +1 -1
- 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/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/ColorInput/ColorInput.tsx +191 -0
- package/src/components/ColorPicker/ColorArea.tsx +98 -0
- package/src/components/ColorPicker/ColorPicker.tsx +283 -0
- package/src/components/DataTable/DataTable.tsx +384 -0
- package/src/components/DatePicker/DatePicker.tsx +4 -0
- package/src/components/DateRangePicker/DateRangePicker.tsx +11 -3
- 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/Stepper/Stepper.tsx +306 -0
- package/src/components/TimeInput/TimeInput.tsx +5 -0
- package/src/components/Timeline/Timeline.tsx +139 -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 +35 -0
- package/src/react/useFormReset.ts +4 -1
|
@@ -19,16 +19,15 @@ import type { DistributiveOmit } from '../../internal/types.js'
|
|
|
19
19
|
import { cn } from 'cn'
|
|
20
20
|
import { joinIds } from '../../internal/ids.js'
|
|
21
21
|
import { controlMetrics, itemGroupLabelClass } from '../shared/controlStyles.js'
|
|
22
|
-
import { matchSelectText, moveSelectFocus
|
|
23
|
-
import { observeAnchorLocale, syncPopupLocale } from '../../internal/popupLocale.js'
|
|
22
|
+
import { matchSelectText, moveSelectFocus } from '../../internal/select.js'
|
|
24
23
|
import { useComposedRefs } from '../../react/useComposedRefs.js'
|
|
25
24
|
import { useControllableState } from '../../react/useControllableState.js'
|
|
26
25
|
import { useLocaleMatcher } from '../../react/useLocale.js'
|
|
27
|
-
import { useTransitionHost } from '../../motion/host.js'
|
|
28
26
|
import type { StateOwner, ValueOwner } from '../../react/useNativeOwner.js'
|
|
29
27
|
import type { ControlSize } from '../../presentation.js'
|
|
30
28
|
import type { ThemeStyle } from '../../themes/defineTheme.js'
|
|
31
|
-
import {
|
|
29
|
+
import { selectItemClass, selectTriggerClass } from './selectStyles.js'
|
|
30
|
+
import { SelectPopup, type SelectContentProps } from './SelectPopup.js'
|
|
32
31
|
import { scrollbarClass } from '../shared/scrollbarStyles.js'
|
|
33
32
|
|
|
34
33
|
export type SelectOption = { value: string; disabled?: boolean } & (
|
|
@@ -460,202 +459,31 @@ export function SelectPortal({ children }: { children?: ReactNode }) {
|
|
|
460
459
|
return select.trigger?.parentElement ? createPortal(children, select.trigger.parentElement) : null
|
|
461
460
|
}
|
|
462
461
|
|
|
463
|
-
export type SelectContentProps
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
sideOffset?: number
|
|
467
|
-
collisionPadding?: number
|
|
468
|
-
motion?: 'inherit' | 'off'
|
|
469
|
-
'data-slot'?: string
|
|
470
|
-
}
|
|
471
|
-
export function SelectContent({
|
|
472
|
-
children,
|
|
473
|
-
position = 'item-aligned',
|
|
474
|
-
sideOffset = 4,
|
|
475
|
-
collisionPadding = 8,
|
|
476
|
-
motion = 'inherit',
|
|
477
|
-
className,
|
|
478
|
-
onKeyDown,
|
|
479
|
-
ref,
|
|
480
|
-
...props
|
|
481
|
-
}: SelectContentProps) {
|
|
462
|
+
export type { SelectContentProps } from './SelectPopup.js'
|
|
463
|
+
|
|
464
|
+
export function SelectContent({ children, onKeyDown, ...props }: SelectContentProps) {
|
|
482
465
|
const select = useSelectContext()
|
|
483
|
-
const { lang, dir } = props
|
|
484
|
-
const mergedRef = useComposedRefs(select.content, ref)
|
|
485
|
-
const {
|
|
486
|
-
ref: transitionRef,
|
|
487
|
-
active: motionActive,
|
|
488
|
-
off: motionOff
|
|
489
|
-
} = useTransitionHost('popup', mergedRef, motion === 'off')
|
|
490
|
-
const latest = useRef(select)
|
|
491
|
-
useLayoutEffect(() => {
|
|
492
|
-
latest.current = select
|
|
493
|
-
})
|
|
494
|
-
const wasOpen = useRef(false)
|
|
495
|
-
useLayoutEffect(() => {
|
|
496
|
-
const state = latest.current
|
|
497
|
-
const node = state.content.current
|
|
498
|
-
const trigger = state.trigger
|
|
499
|
-
if (!node || !trigger) return
|
|
500
|
-
const doc = trigger.ownerDocument
|
|
501
|
-
const win = doc.defaultView!
|
|
502
|
-
if (!state.open) {
|
|
503
|
-
if (
|
|
504
|
-
wasOpen.current &&
|
|
505
|
-
latest.current.restoreFocus.current &&
|
|
506
|
-
node.contains(doc.activeElement)
|
|
507
|
-
)
|
|
508
|
-
trigger.focus({ preventScroll: true })
|
|
509
|
-
if (node.matches(':popover-open')) node.hidePopover()
|
|
510
|
-
wasOpen.current = false
|
|
511
|
-
return
|
|
512
|
-
}
|
|
513
|
-
const seedWidth = () => {
|
|
514
|
-
const available =
|
|
515
|
-
(win.visualViewport?.width ?? win.innerWidth) - 2 * Math.max(0, collisionPadding)
|
|
516
|
-
const width = `${Math.min(trigger.getBoundingClientRect().width, Math.max(0, available))}px`
|
|
517
|
-
if (node.style.getPropertyValue('--_rad-select-width') !== width)
|
|
518
|
-
node.style.setProperty('--_rad-select-width', width)
|
|
519
|
-
}
|
|
520
|
-
// Top-layer auto layout is viewport-sized. Establish the anchor width before it can paint.
|
|
521
|
-
seedWidth()
|
|
522
|
-
syncPopupLocale(trigger, node, { lang, dir })
|
|
523
|
-
node.showPopover()
|
|
524
|
-
const place = () => {
|
|
525
|
-
if (!node.isConnected || !node.matches(':popover-open')) return
|
|
526
|
-
const rtl = syncPopupLocale(trigger, node, { lang, dir })
|
|
527
|
-
seedWidth()
|
|
528
|
-
const viewport = win.visualViewport
|
|
529
|
-
const selected = Array.from(
|
|
530
|
-
node.querySelectorAll<HTMLElement>('[data-slot~="select-item"]')
|
|
531
|
-
).find(item => item.dataset.value === latest.current.value)
|
|
532
|
-
const rect = trigger.getBoundingClientRect()
|
|
533
|
-
const computed = win.getComputedStyle(node)
|
|
534
|
-
const viewportNode = node.querySelector<HTMLElement>('[data-slot~="select-viewport"]')
|
|
535
|
-
const naturalHeight = viewportNode
|
|
536
|
-
? viewportNode.scrollHeight +
|
|
537
|
-
parseFloat(computed.paddingTop) +
|
|
538
|
-
parseFloat(computed.paddingBottom)
|
|
539
|
-
: node.scrollHeight
|
|
540
|
-
const next = positionSelect({
|
|
541
|
-
trigger: rect,
|
|
542
|
-
width: node.getBoundingClientRect().width,
|
|
543
|
-
height:
|
|
544
|
-
naturalHeight +
|
|
545
|
-
parseFloat(computed.borderTopWidth) +
|
|
546
|
-
parseFloat(computed.borderBottomWidth),
|
|
547
|
-
selectedOffset: selected ? selected.offsetTop + selected.offsetHeight / 2 : 24,
|
|
548
|
-
viewport: {
|
|
549
|
-
top: viewport?.offsetTop ?? 0,
|
|
550
|
-
left: viewport?.offsetLeft ?? 0,
|
|
551
|
-
width: viewport?.width ?? win.innerWidth,
|
|
552
|
-
height: viewport?.height ?? win.innerHeight
|
|
553
|
-
},
|
|
554
|
-
padding: Math.max(0, collisionPadding),
|
|
555
|
-
offset: sideOffset,
|
|
556
|
-
aligned: position === 'item-aligned',
|
|
557
|
-
rtl
|
|
558
|
-
})
|
|
559
|
-
for (const [key, value] of Object.entries({
|
|
560
|
-
top: next.top,
|
|
561
|
-
left: next.left,
|
|
562
|
-
width: next.width,
|
|
563
|
-
'max-height': next.maxHeight
|
|
564
|
-
}))
|
|
565
|
-
if (node.style.getPropertyValue(`--_rad-select-${key}`) !== `${value}px`)
|
|
566
|
-
node.style.setProperty(`--_rad-select-${key}`, `${value}px`)
|
|
567
|
-
}
|
|
568
|
-
place()
|
|
569
|
-
const active = doc.activeElement as HTMLElement | null
|
|
570
|
-
if (
|
|
571
|
-
!wasOpen.current ||
|
|
572
|
-
active === doc.body ||
|
|
573
|
-
active?.getAttribute('aria-disabled') === 'true'
|
|
574
|
-
) {
|
|
575
|
-
const first =
|
|
576
|
-
state.items.find(item => item.value === state.value && !item.disabled) ??
|
|
577
|
-
state.items.find(item => !item.disabled)
|
|
578
|
-
if (first) state.focus(first.value)
|
|
579
|
-
else node.focus()
|
|
580
|
-
}
|
|
581
|
-
wasOpen.current = true
|
|
582
|
-
const outside = (event: PointerEvent) => {
|
|
583
|
-
const target = event.target
|
|
584
|
-
if (!(target instanceof win.Node) || node.contains(target) || trigger.contains(target)) return
|
|
585
|
-
latest.current.close(false)
|
|
586
|
-
}
|
|
587
|
-
const focusOut = (event: FocusEvent) => {
|
|
588
|
-
const target = event.target
|
|
589
|
-
if (target === doc.body || target === doc.documentElement) return
|
|
590
|
-
if (target instanceof win.Node && !node.contains(target) && !trigger.contains(target))
|
|
591
|
-
latest.current.close(false)
|
|
592
|
-
}
|
|
593
|
-
const scroll = (event: Event) => {
|
|
594
|
-
if (!(event.target instanceof win.Node) || !node.contains(event.target)) place()
|
|
595
|
-
}
|
|
596
|
-
let frame = 0
|
|
597
|
-
const schedule = () => {
|
|
598
|
-
if (!frame)
|
|
599
|
-
frame = win.requestAnimationFrame(() => {
|
|
600
|
-
frame = 0
|
|
601
|
-
place()
|
|
602
|
-
})
|
|
603
|
-
}
|
|
604
|
-
const observer = new win.ResizeObserver(schedule)
|
|
605
|
-
const mutations = new win.MutationObserver(schedule)
|
|
606
|
-
mutations.observe(node, { childList: true, subtree: true, characterData: true })
|
|
607
|
-
observeAnchorLocale(mutations, trigger)
|
|
608
|
-
observer.observe(trigger)
|
|
609
|
-
observer.observe(node)
|
|
610
|
-
doc.addEventListener('pointerdown', outside, true)
|
|
611
|
-
doc.addEventListener('focusin', focusOut)
|
|
612
|
-
doc.addEventListener('scroll', scroll, true)
|
|
613
|
-
win.addEventListener('resize', place)
|
|
614
|
-
win.visualViewport?.addEventListener('resize', place)
|
|
615
|
-
win.visualViewport?.addEventListener('scroll', place)
|
|
616
|
-
return () => {
|
|
617
|
-
observer.disconnect()
|
|
618
|
-
mutations.disconnect()
|
|
619
|
-
win.cancelAnimationFrame(frame)
|
|
620
|
-
doc.removeEventListener('pointerdown', outside, true)
|
|
621
|
-
doc.removeEventListener('focusin', focusOut)
|
|
622
|
-
doc.removeEventListener('scroll', scroll, true)
|
|
623
|
-
win.removeEventListener('resize', place)
|
|
624
|
-
win.visualViewport?.removeEventListener('resize', place)
|
|
625
|
-
win.visualViewport?.removeEventListener('scroll', place)
|
|
626
|
-
}
|
|
627
|
-
}, [select.open, select.trigger, select.items, position, sideOffset, collisionPadding, lang, dir])
|
|
628
|
-
useLayoutEffect(() => {
|
|
629
|
-
const node = select.content.current
|
|
630
|
-
const trigger = select.trigger
|
|
631
|
-
return () => {
|
|
632
|
-
if (node?.contains(node.ownerDocument.activeElement) && trigger?.isConnected)
|
|
633
|
-
trigger.focus({ preventScroll: true })
|
|
634
|
-
}
|
|
635
|
-
}, [select.content, select.trigger])
|
|
636
466
|
return (
|
|
637
|
-
<
|
|
467
|
+
<SelectPopup
|
|
638
468
|
{...props}
|
|
639
|
-
|
|
469
|
+
state={{
|
|
470
|
+
...select,
|
|
471
|
+
collection: select.items,
|
|
472
|
+
focusInitial(node) {
|
|
473
|
+
const first =
|
|
474
|
+
select.items.find(item => item.value === select.value && !item.disabled) ??
|
|
475
|
+
select.items.find(item => !item.disabled)
|
|
476
|
+
if (first) select.focus(first.value)
|
|
477
|
+
else node.focus()
|
|
478
|
+
}
|
|
479
|
+
}}
|
|
480
|
+
controlSize={select.controlSize}
|
|
640
481
|
id={select.contentId}
|
|
641
|
-
popover="manual"
|
|
642
482
|
role="listbox"
|
|
643
483
|
tabIndex={-1}
|
|
644
484
|
aria-labelledby={
|
|
645
485
|
props['aria-label'] ? undefined : (select.labelId ?? select.trigger?.id ?? select.triggerId)
|
|
646
486
|
}
|
|
647
|
-
className={cn(
|
|
648
|
-
selectContentClass,
|
|
649
|
-
controlMetrics({ controlSize: select.controlSize }),
|
|
650
|
-
motionActive &&
|
|
651
|
-
!motionOff &&
|
|
652
|
-
'opacity-[var(--_rad-popup-enter,0)] motion-reduce:opacity-100',
|
|
653
|
-
className
|
|
654
|
-
)}
|
|
655
|
-
data-slot={joinIds('select-content', props['data-slot'])}
|
|
656
|
-
data-state={select.open ? 'open' : 'closed'}
|
|
657
|
-
data-motion={motion}
|
|
658
|
-
data-position={position}
|
|
659
487
|
onKeyDown={event => {
|
|
660
488
|
onKeyDown?.(event)
|
|
661
489
|
if (event.defaultPrevented) return
|
|
@@ -695,7 +523,7 @@ export function SelectContent({
|
|
|
695
523
|
))}
|
|
696
524
|
</SelectViewport>
|
|
697
525
|
)}
|
|
698
|
-
</
|
|
526
|
+
</SelectPopup>
|
|
699
527
|
)
|
|
700
528
|
}
|
|
701
529
|
|
|
@@ -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)
|