@cognitap/radiant 0.0.9 → 0.0.11
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 +9 -2
- package/dist/library/Slider.d.ts +13 -0
- package/dist/library/Slider.d.ts.map +1 -0
- package/dist/library/Slider.js +77 -0
- package/dist/library/Slider.js.map +1 -0
- package/dist/library/Switch.d.ts +15 -0
- package/dist/library/Switch.d.ts.map +1 -0
- package/dist/library/Switch.js +81 -0
- package/dist/library/Switch.js.map +1 -0
- package/dist/library/cn.d.ts.map +1 -1
- package/dist/library/cn.js +44 -10
- package/dist/library/cn.js.map +1 -1
- package/dist/library/index.d.ts +4 -0
- package/dist/library/index.d.ts.map +1 -1
- package/dist/library/index.js +2 -0
- package/dist/library/index.js.map +1 -1
- package/dist/library/internal/switchDrag.d.ts +9 -0
- package/dist/library/internal/switchDrag.d.ts.map +1 -0
- package/dist/library/internal/switchDrag.js +170 -0
- package/dist/library/internal/switchDrag.js.map +1 -0
- package/dist/library/react/useSwitchDrag.d.ts +7 -0
- package/dist/library/react/useSwitchDrag.d.ts.map +1 -0
- package/dist/library/react/useSwitchDrag.js +34 -0
- package/dist/library/react/useSwitchDrag.js.map +1 -0
- package/dist/library/styles.css +1 -1
- package/dist/library/themes/components.d.ts +47 -0
- package/dist/library/themes/components.d.ts.map +1 -1
- package/dist/library/themes/components.js +47 -0
- package/dist/library/themes/components.js.map +1 -1
- package/docs/architecture.md +17 -15
- package/docs/theming.md +10 -10
- package/package.json +3 -5
- package/src/Slider.tsx +142 -0
- package/src/Switch.tsx +174 -0
- package/src/cn.ts +44 -10
- package/src/index.ts +4 -0
- package/src/internal/switchDrag.ts +188 -0
- package/src/react/useSwitchDrag.ts +49 -0
- package/src/themes/components.ts +47 -0
package/src/cn.ts
CHANGED
|
@@ -23,9 +23,18 @@ export const cn: CnFunction = createCn({
|
|
|
23
23
|
...roleClasses('text', 'size'),
|
|
24
24
|
...roleClasses('text', 'fluid-size'),
|
|
25
25
|
'text-(--rad-select-font-size)',
|
|
26
|
-
'text-(--rad-textarea-font-size)'
|
|
26
|
+
'text-(--rad-textarea-font-size)',
|
|
27
|
+
'text-(--rad-switch-label-size)'
|
|
27
28
|
],
|
|
28
|
-
h: [
|
|
29
|
+
h: [
|
|
30
|
+
'h-(--rad-select-height)',
|
|
31
|
+
'h-(--rad-switch-height)',
|
|
32
|
+
'h-(--rad-slider-height)',
|
|
33
|
+
'h-(--rad-slider-length)'
|
|
34
|
+
],
|
|
35
|
+
w: ['w-(--rad-switch-width)', 'w-(--rad-slider-height)', 'w-(--rad-slider-length)'],
|
|
36
|
+
size: ['size-(--rad-slider-thumb-size)', 'size-(--rad-slider-track-size)'],
|
|
37
|
+
gap: ['gap-(--rad-switch-gap)'],
|
|
29
38
|
'min-h': ['min-h-(--rad-textarea-min-height)'],
|
|
30
39
|
py: ['py-(--rad-textarea-padding-block)'],
|
|
31
40
|
px: ['px-(--rad-select-padding)', 'px-(--rad-textarea-padding)'],
|
|
@@ -33,26 +42,51 @@ export const cn: CnFunction = createCn({
|
|
|
33
42
|
rounded: [
|
|
34
43
|
'rounded-(--rad-select-radius)',
|
|
35
44
|
'rounded-(--rad-textarea-radius)',
|
|
45
|
+
'rounded-(--rad-switch-radius)',
|
|
46
|
+
'rounded-(--rad-switch-thumb-radius)',
|
|
36
47
|
'rounded-(--rad-select-content-radius)',
|
|
37
|
-
'rounded-(--rad-select-item-radius)'
|
|
48
|
+
'rounded-(--rad-select-item-radius)',
|
|
49
|
+
'rounded-(--rad-slider-track-radius)',
|
|
50
|
+
'rounded-(--rad-slider-thumb-radius)'
|
|
51
|
+
],
|
|
52
|
+
'border-w': [
|
|
53
|
+
'border-(--rad-select-border-width)',
|
|
54
|
+
'border-(--rad-textarea-border-width)',
|
|
55
|
+
'border-(--rad-switch-border-width)',
|
|
56
|
+
'border-(--rad-slider-thumb-border-width)'
|
|
38
57
|
],
|
|
39
|
-
'border-w': ['border-(--rad-select-border-width)', 'border-(--rad-textarea-border-width)'],
|
|
40
58
|
'border-style': [
|
|
41
59
|
'border-(--rad-select-border-style)',
|
|
42
|
-
'border-(--rad-textarea-border-style)'
|
|
60
|
+
'border-(--rad-textarea-border-style)',
|
|
61
|
+
'border-(--rad-switch-border-style)'
|
|
43
62
|
],
|
|
44
63
|
shadow: [
|
|
45
64
|
'shadow-(--rad-select-shadow)',
|
|
46
65
|
'shadow-(--rad-select-content-shadow)',
|
|
47
|
-
'shadow-(--rad-textarea-shadow)'
|
|
66
|
+
'shadow-(--rad-textarea-shadow)',
|
|
67
|
+
'shadow-(--rad-slider-thumb-shadow)'
|
|
48
68
|
],
|
|
49
69
|
'corner-shape': [
|
|
50
70
|
'[corner-shape:var(--rad-select-corner-shape)]',
|
|
51
|
-
'[corner-shape:var(--rad-textarea-corner-shape)]'
|
|
71
|
+
'[corner-shape:var(--rad-textarea-corner-shape)]',
|
|
72
|
+
'[corner-shape:var(--rad-switch-corner-shape)]',
|
|
73
|
+
'[corner-shape:var(--rad-slider-corner-shape)]'
|
|
74
|
+
],
|
|
75
|
+
'outline-w': [
|
|
76
|
+
'outline-(--rad-textarea-focus-width)',
|
|
77
|
+
'outline-(--rad-switch-focus-width)',
|
|
78
|
+
'outline-(--rad-slider-focus-width)'
|
|
79
|
+
],
|
|
80
|
+
'outline-offset': [
|
|
81
|
+
'outline-offset-(--rad-textarea-focus-offset)',
|
|
82
|
+
'outline-offset-(--rad-switch-focus-offset)',
|
|
83
|
+
'outline-offset-(--rad-slider-focus-offset)'
|
|
84
|
+
],
|
|
85
|
+
'outline-style': [
|
|
86
|
+
'outline-(--rad-textarea-focus-style)',
|
|
87
|
+
'outline-(--rad-switch-focus-style)',
|
|
88
|
+
'outline-(--rad-slider-focus-style)'
|
|
52
89
|
],
|
|
53
|
-
'outline-w': ['outline-(--rad-textarea-focus-width)'],
|
|
54
|
-
'outline-offset': ['outline-offset-(--rad-textarea-focus-offset)'],
|
|
55
|
-
'outline-style': ['outline-(--rad-textarea-focus-style)'],
|
|
56
90
|
'backdrop-filter': ['[backdrop-filter:var(--rad-select-content-backdrop-filter)]'],
|
|
57
91
|
leading: roleClasses('leading', 'line-height'),
|
|
58
92
|
tracking: roleClasses('tracking', 'letter-spacing'),
|
package/src/index.ts
CHANGED
|
@@ -7,6 +7,8 @@ export { Input } from './Input.js'
|
|
|
7
7
|
export type { InputProps } from './Input.js'
|
|
8
8
|
export { Textarea } from './Textarea.js'
|
|
9
9
|
export type { TextareaProps } from './Textarea.js'
|
|
10
|
+
export { Slider } from './Slider.js'
|
|
11
|
+
export type { SliderProps } from './Slider.js'
|
|
10
12
|
export {
|
|
11
13
|
Select,
|
|
12
14
|
SelectTrigger,
|
|
@@ -34,6 +36,8 @@ export type {
|
|
|
34
36
|
SelectItemTextProps,
|
|
35
37
|
SelectGroupProps
|
|
36
38
|
} from './Select.js'
|
|
39
|
+
export { Switch } from './Switch.js'
|
|
40
|
+
export type { SwitchProps } from './Switch.js'
|
|
37
41
|
export { Checkbox } from './Checkbox.js'
|
|
38
42
|
export type { CheckboxProps } from './Checkbox.js'
|
|
39
43
|
export { Radio, RadioGroup } from './RadioGroup.js'
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
export interface SwitchDragBinding {
|
|
2
|
+
start(event: PointerEvent): void
|
|
3
|
+
escape(event: KeyboardEvent): boolean
|
|
4
|
+
reconcile(): void
|
|
5
|
+
dispose(): void
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
type Gesture = {
|
|
9
|
+
pointer: number
|
|
10
|
+
x: number
|
|
11
|
+
y: number
|
|
12
|
+
checked: boolean
|
|
13
|
+
direction: number
|
|
14
|
+
travel: number
|
|
15
|
+
cssTravel: number
|
|
16
|
+
progress: number
|
|
17
|
+
moved: boolean
|
|
18
|
+
transform: string
|
|
19
|
+
form: HTMLFormElement | null
|
|
20
|
+
cleanup(): void
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Preview on a private thumb wrapper; commit through the input's native activation. */
|
|
24
|
+
export function bindSwitchDrag(
|
|
25
|
+
input: HTMLInputElement,
|
|
26
|
+
rail: HTMLElement,
|
|
27
|
+
handle: HTMLElement
|
|
28
|
+
): SwitchDragBinding {
|
|
29
|
+
const document = input.ownerDocument
|
|
30
|
+
const view = document.defaultView!
|
|
31
|
+
let gesture: Gesture | null = null
|
|
32
|
+
let suppressClick = false
|
|
33
|
+
const blocked = () => input.matches(':disabled') || !input.isConnected
|
|
34
|
+
const metrics = () => ({
|
|
35
|
+
travel: Math.max(0, rail.getBoundingClientRect().width - handle.getBoundingClientRect().width),
|
|
36
|
+
cssTravel: Math.max(
|
|
37
|
+
0,
|
|
38
|
+
parseFloat(view.getComputedStyle(rail).width) -
|
|
39
|
+
parseFloat(view.getComputedStyle(handle).width)
|
|
40
|
+
),
|
|
41
|
+
direction: view.getComputedStyle(rail).direction === 'rtl' ? -1 : 1
|
|
42
|
+
})
|
|
43
|
+
function finish() {
|
|
44
|
+
const current = gesture
|
|
45
|
+
if (!current) return
|
|
46
|
+
gesture = null
|
|
47
|
+
suppressClick = current.moved
|
|
48
|
+
current.cleanup()
|
|
49
|
+
handle.style.transform = current.transform
|
|
50
|
+
input.removeAttribute('data-dragging')
|
|
51
|
+
if (input.hasPointerCapture(current.pointer)) input.releasePointerCapture(current.pointer)
|
|
52
|
+
return current
|
|
53
|
+
}
|
|
54
|
+
const cancel = () => {
|
|
55
|
+
finish()
|
|
56
|
+
}
|
|
57
|
+
function move(event: PointerEvent) {
|
|
58
|
+
const current = gesture
|
|
59
|
+
if (!current || current.pointer !== event.pointerId) return
|
|
60
|
+
if (event.defaultPrevented || blocked() || input.checked !== current.checked) {
|
|
61
|
+
cancel()
|
|
62
|
+
return
|
|
63
|
+
}
|
|
64
|
+
const dx = event.clientX - current.x
|
|
65
|
+
if (!current.moved && Math.abs(event.clientY - current.y) > Math.max(4, Math.abs(dx))) {
|
|
66
|
+
cancel()
|
|
67
|
+
return
|
|
68
|
+
}
|
|
69
|
+
if (!current.moved && Math.abs(dx) < 3) return
|
|
70
|
+
if (!current.moved) {
|
|
71
|
+
current.moved = true
|
|
72
|
+
input.focus({ preventScroll: true })
|
|
73
|
+
if (gesture !== current) return
|
|
74
|
+
input.setAttribute('data-dragging', '')
|
|
75
|
+
try {
|
|
76
|
+
input.setPointerCapture(current.pointer)
|
|
77
|
+
} catch {
|
|
78
|
+
/* Owner-document events retain outside release. */
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (gesture !== current) return
|
|
82
|
+
event.preventDefault()
|
|
83
|
+
current.progress = Math.max(
|
|
84
|
+
0,
|
|
85
|
+
Math.min(1, Number(current.checked) + (dx * current.direction) / current.travel)
|
|
86
|
+
)
|
|
87
|
+
handle.style.transform = `translateX(${(current.progress - Number(current.checked)) * current.cssTravel * current.direction}px)`
|
|
88
|
+
}
|
|
89
|
+
function up(event: PointerEvent) {
|
|
90
|
+
const current = gesture
|
|
91
|
+
if (!current || current.pointer !== event.pointerId) return
|
|
92
|
+
if (event.defaultPrevented || blocked() || input.checked !== current.checked) {
|
|
93
|
+
cancel()
|
|
94
|
+
return
|
|
95
|
+
}
|
|
96
|
+
// A release can contain a final coordinate without a preceding move sample.
|
|
97
|
+
if (current.moved) move(event)
|
|
98
|
+
const ended = finish()
|
|
99
|
+
if (!ended?.moved) return
|
|
100
|
+
const next = ended.progress === 0.5 ? ended.checked : ended.progress > 0.5
|
|
101
|
+
if (next !== input.checked) input.click()
|
|
102
|
+
}
|
|
103
|
+
function pointerCancelled(event: PointerEvent) {
|
|
104
|
+
if (gesture?.pointer === event.pointerId) cancel()
|
|
105
|
+
}
|
|
106
|
+
function click(event: MouseEvent) {
|
|
107
|
+
// Native keyboard/programmatic activation has detail 0, including our commit.
|
|
108
|
+
if (!suppressClick || event.detail === 0) return
|
|
109
|
+
suppressClick = false
|
|
110
|
+
event.preventDefault()
|
|
111
|
+
event.stopPropagation()
|
|
112
|
+
}
|
|
113
|
+
input.addEventListener('click', click, true)
|
|
114
|
+
return {
|
|
115
|
+
start(event) {
|
|
116
|
+
if (
|
|
117
|
+
event.defaultPrevented ||
|
|
118
|
+
event.button !== 0 ||
|
|
119
|
+
event.isPrimary === false ||
|
|
120
|
+
gesture ||
|
|
121
|
+
blocked()
|
|
122
|
+
)
|
|
123
|
+
return
|
|
124
|
+
suppressClick = false
|
|
125
|
+
const measured = metrics()
|
|
126
|
+
if (!(measured.travel > 0 && measured.cssTravel > 0)) return
|
|
127
|
+
const observer = new view.MutationObserver(() => {
|
|
128
|
+
if (blocked()) cancel()
|
|
129
|
+
})
|
|
130
|
+
observer.observe(input, { attributes: true, attributeFilter: ['disabled'] })
|
|
131
|
+
for (let parent = input.parentElement; parent; parent = parent.parentElement)
|
|
132
|
+
if (parent.tagName === 'FIELDSET')
|
|
133
|
+
observer.observe(parent, { attributes: true, attributeFilter: ['disabled'] })
|
|
134
|
+
const form = input.form
|
|
135
|
+
gesture = {
|
|
136
|
+
pointer: event.pointerId,
|
|
137
|
+
x: event.clientX,
|
|
138
|
+
y: event.clientY,
|
|
139
|
+
checked: input.checked,
|
|
140
|
+
...measured,
|
|
141
|
+
progress: Number(input.checked),
|
|
142
|
+
moved: false,
|
|
143
|
+
transform: handle.style.transform,
|
|
144
|
+
form,
|
|
145
|
+
cleanup() {
|
|
146
|
+
document.removeEventListener('pointermove', move)
|
|
147
|
+
document.removeEventListener('pointerup', up)
|
|
148
|
+
document.removeEventListener('pointercancel', pointerCancelled)
|
|
149
|
+
input.removeEventListener('lostpointercapture', pointerCancelled)
|
|
150
|
+
input.removeEventListener('blur', cancel)
|
|
151
|
+
form?.removeEventListener('reset', cancel)
|
|
152
|
+
view.removeEventListener('blur', cancel)
|
|
153
|
+
view.removeEventListener('resize', cancel)
|
|
154
|
+
observer.disconnect()
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
document.addEventListener('pointermove', move, { passive: false })
|
|
158
|
+
document.addEventListener('pointerup', up)
|
|
159
|
+
document.addEventListener('pointercancel', pointerCancelled)
|
|
160
|
+
input.addEventListener('lostpointercapture', pointerCancelled)
|
|
161
|
+
input.addEventListener('blur', cancel)
|
|
162
|
+
form?.addEventListener('reset', cancel)
|
|
163
|
+
view.addEventListener('blur', cancel)
|
|
164
|
+
view.addEventListener('resize', cancel)
|
|
165
|
+
},
|
|
166
|
+
escape(event) {
|
|
167
|
+
if (!gesture || event.defaultPrevented || event.key !== 'Escape') return false
|
|
168
|
+
cancel()
|
|
169
|
+
return true
|
|
170
|
+
},
|
|
171
|
+
reconcile() {
|
|
172
|
+
if (!gesture) return
|
|
173
|
+
const measured = metrics()
|
|
174
|
+
if (
|
|
175
|
+
blocked() ||
|
|
176
|
+
input.checked !== gesture.checked ||
|
|
177
|
+
input.form !== gesture.form ||
|
|
178
|
+
measured.direction !== gesture.direction ||
|
|
179
|
+
Math.abs(measured.travel - gesture.travel) > 0.5
|
|
180
|
+
)
|
|
181
|
+
cancel()
|
|
182
|
+
},
|
|
183
|
+
dispose() {
|
|
184
|
+
cancel()
|
|
185
|
+
input.removeEventListener('click', click, true)
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { useLayoutEffect, useRef, type RefObject } from 'react'
|
|
4
|
+
import { bindSwitchDrag, type SwitchDragBinding } from '../internal/switchDrag.js'
|
|
5
|
+
|
|
6
|
+
/** The input owns checked state; the private rail/handle own the transient drag. */
|
|
7
|
+
export function useSwitchDrag(
|
|
8
|
+
input: RefObject<HTMLInputElement | null>,
|
|
9
|
+
rail: RefObject<HTMLElement | null>,
|
|
10
|
+
handle: RefObject<HTMLElement | null>
|
|
11
|
+
) {
|
|
12
|
+
const binding = useRef<{
|
|
13
|
+
input: HTMLInputElement
|
|
14
|
+
rail: HTMLElement
|
|
15
|
+
handle: HTMLElement
|
|
16
|
+
control: SwitchDragBinding
|
|
17
|
+
} | null>(null)
|
|
18
|
+
useLayoutEffect(() => {
|
|
19
|
+
if (
|
|
20
|
+
binding.current &&
|
|
21
|
+
(binding.current.input !== input.current ||
|
|
22
|
+
binding.current.rail !== rail.current ||
|
|
23
|
+
binding.current.handle !== handle.current)
|
|
24
|
+
) {
|
|
25
|
+
binding.current.control.dispose()
|
|
26
|
+
binding.current = null
|
|
27
|
+
}
|
|
28
|
+
if (!binding.current && input.current && rail.current && handle.current) {
|
|
29
|
+
binding.current = {
|
|
30
|
+
input: input.current,
|
|
31
|
+
rail: rail.current,
|
|
32
|
+
handle: handle.current,
|
|
33
|
+
control: bindSwitchDrag(input.current, rail.current, handle.current)
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
binding.current?.control.reconcile()
|
|
37
|
+
})
|
|
38
|
+
useLayoutEffect(
|
|
39
|
+
() => () => {
|
|
40
|
+
binding.current?.control.dispose()
|
|
41
|
+
binding.current = null
|
|
42
|
+
},
|
|
43
|
+
[]
|
|
44
|
+
)
|
|
45
|
+
return {
|
|
46
|
+
start: (event: PointerEvent) => binding.current?.control.start(event),
|
|
47
|
+
escape: (event: KeyboardEvent) => binding.current?.control.escape(event) ?? false
|
|
48
|
+
}
|
|
49
|
+
}
|
package/src/themes/components.ts
CHANGED
|
@@ -118,6 +118,27 @@ export const componentRegistry = {
|
|
|
118
118
|
focusOffset: 'css',
|
|
119
119
|
focusStyle: 'css'
|
|
120
120
|
},
|
|
121
|
+
slider: {
|
|
122
|
+
height: 'css',
|
|
123
|
+
length: 'css',
|
|
124
|
+
trackSize: 'css',
|
|
125
|
+
trackRadius: 'css',
|
|
126
|
+
trackColor: 'css',
|
|
127
|
+
thumbSize: 'css',
|
|
128
|
+
thumbRadius: 'css',
|
|
129
|
+
thumbColor: 'css',
|
|
130
|
+
thumbBorderWidth: 'css',
|
|
131
|
+
thumbBorderColor: 'css',
|
|
132
|
+
thumbShadow: 'css',
|
|
133
|
+
cornerShape: 'css',
|
|
134
|
+
invalidTrack: 'css',
|
|
135
|
+
disabledTrack: 'css',
|
|
136
|
+
disabledThumb: 'css',
|
|
137
|
+
focusColor: 'css',
|
|
138
|
+
focusWidth: 'css',
|
|
139
|
+
focusOffset: 'css',
|
|
140
|
+
focusStyle: 'css'
|
|
141
|
+
},
|
|
121
142
|
select: {
|
|
122
143
|
height: 'css',
|
|
123
144
|
padding: 'css',
|
|
@@ -164,6 +185,32 @@ export const componentRegistry = {
|
|
|
164
185
|
focusOffset: 'css',
|
|
165
186
|
focusStyle: 'css'
|
|
166
187
|
},
|
|
188
|
+
switch: {
|
|
189
|
+
width: 'css',
|
|
190
|
+
height: 'css',
|
|
191
|
+
radius: 'css',
|
|
192
|
+
cornerShape: 'css',
|
|
193
|
+
borderWidth: 'css',
|
|
194
|
+
borderStyle: 'css',
|
|
195
|
+
borderColor: 'css',
|
|
196
|
+
background: 'css',
|
|
197
|
+
checkedBackground: 'css',
|
|
198
|
+
checkedBorder: 'css',
|
|
199
|
+
thumb: 'css',
|
|
200
|
+
checkedThumb: 'css',
|
|
201
|
+
thumbRadius: 'css',
|
|
202
|
+
invalidBorder: 'css',
|
|
203
|
+
gap: 'css',
|
|
204
|
+
labelColor: 'css',
|
|
205
|
+
labelSize: 'css',
|
|
206
|
+
disabledBorder: 'css',
|
|
207
|
+
disabledBackground: 'css',
|
|
208
|
+
disabledThumb: 'css',
|
|
209
|
+
focusColor: 'css',
|
|
210
|
+
focusWidth: 'css',
|
|
211
|
+
focusOffset: 'css',
|
|
212
|
+
focusStyle: 'css'
|
|
213
|
+
},
|
|
167
214
|
radio: {
|
|
168
215
|
size: 'css',
|
|
169
216
|
radius: 'css',
|