@cognitap/radiant 0.0.58 → 0.0.60
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 +8 -0
- package/dist/library/CodeBlock.d.ts +41 -0
- package/dist/library/CodeBlock.d.ts.map +1 -0
- package/dist/library/CodeBlock.js +85 -0
- package/dist/library/CodeBlock.js.map +1 -0
- package/dist/library/InlineCode.d.ts +9 -0
- package/dist/library/InlineCode.d.ts.map +1 -0
- package/dist/library/InlineCode.js +8 -0
- package/dist/library/InlineCode.js.map +1 -0
- package/dist/library/Input.d.ts +1 -1
- package/dist/library/Input.d.ts.map +1 -1
- package/dist/library/Input.js +2 -2
- package/dist/library/Input.js.map +1 -1
- package/dist/library/InputGroup.d.ts +1 -1
- package/dist/library/InputGroup.d.ts.map +1 -1
- package/dist/library/InputGroup.js.map +1 -1
- package/dist/library/NumberField.d.ts +25 -0
- package/dist/library/NumberField.d.ts.map +1 -0
- package/dist/library/NumberField.js +160 -0
- package/dist/library/NumberField.js.map +1 -0
- package/dist/library/Separator.d.ts +2 -1
- package/dist/library/Separator.d.ts.map +1 -1
- package/dist/library/Separator.js.map +1 -1
- package/dist/library/cn.d.ts.map +1 -1
- package/dist/library/cn.js +19 -3
- package/dist/library/cn.js.map +1 -1
- package/dist/library/index.d.ts +3 -0
- package/dist/library/index.d.ts.map +1 -1
- package/dist/library/index.js +3 -0
- package/dist/library/index.js.map +1 -1
- package/dist/library/internal/numberField.d.ts +21 -0
- package/dist/library/internal/numberField.d.ts.map +1 -0
- package/dist/library/internal/numberField.js +158 -0
- package/dist/library/internal/numberField.js.map +1 -0
- package/dist/library/styles.css +1 -1
- package/dist/library/themes/components.d.ts +27 -0
- package/dist/library/themes/components.d.ts.map +1 -1
- package/dist/library/themes/components.js +25 -0
- package/dist/library/themes/components.js.map +1 -1
- package/docs/architecture.md +6 -8
- package/package.json +1 -1
- package/src/CodeBlock.tsx +249 -0
- package/src/InlineCode.tsx +25 -0
- package/src/Input.tsx +3 -3
- package/src/InputGroup.tsx +1 -1
- package/src/NumberField.tsx +302 -0
- package/src/Separator.tsx +2 -1
- package/src/cn.ts +19 -3
- package/src/index.ts +17 -0
- package/src/internal/numberField.ts +193 -0
- package/src/themes/components.ts +25 -0
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
/* oxlint-disable jsx-a11y/prefer-tag-over-role -- Locale currency and percent editing requires a native text input with spinbutton semantics. */
|
|
4
|
+
|
|
5
|
+
import { useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react'
|
|
6
|
+
import type { InputProps } from './Input.js'
|
|
7
|
+
import { InputGroup, InputGroupAddon, InputGroupInputRoot } from './InputGroup.js'
|
|
8
|
+
import { Button } from './Button.js'
|
|
9
|
+
import { useFieldControl } from './Field.js'
|
|
10
|
+
import { cn } from './cn.js'
|
|
11
|
+
import type { PartStyle } from './presentation.js'
|
|
12
|
+
import type { DistributiveOmit } from './internal/types.js'
|
|
13
|
+
import { requireAccessibleName } from './internal/accessibleName.js'
|
|
14
|
+
import {
|
|
15
|
+
addNumberStep,
|
|
16
|
+
clampNumber,
|
|
17
|
+
createNumberAdapter,
|
|
18
|
+
requireFiniteNumber,
|
|
19
|
+
type NumberFieldFormatOptions
|
|
20
|
+
} from './internal/numberField.js'
|
|
21
|
+
import type { ValueOwner } from './react/useNativeOwner.js'
|
|
22
|
+
import { useControllableState } from './react/useControllableState.js'
|
|
23
|
+
import { useComposedRefs } from './react/useComposedRefs.js'
|
|
24
|
+
export type { NumberFieldFormatOptions } from './internal/numberField.js'
|
|
25
|
+
|
|
26
|
+
export type NumberFieldProps = DistributiveOmit<
|
|
27
|
+
InputProps,
|
|
28
|
+
| 'value'
|
|
29
|
+
| 'defaultValue'
|
|
30
|
+
| 'type'
|
|
31
|
+
| 'children'
|
|
32
|
+
| 'min'
|
|
33
|
+
| 'max'
|
|
34
|
+
| 'step'
|
|
35
|
+
| 'role'
|
|
36
|
+
| 'aria-valuenow'
|
|
37
|
+
| 'aria-valuemin'
|
|
38
|
+
| 'aria-valuemax'
|
|
39
|
+
| 'aria-valuetext'
|
|
40
|
+
> &
|
|
41
|
+
ValueOwner<number | null> & {
|
|
42
|
+
onValueChange?: (value: number | null) => void
|
|
43
|
+
min?: number
|
|
44
|
+
max?: number
|
|
45
|
+
step?: number
|
|
46
|
+
largeStep?: number
|
|
47
|
+
locale?: string
|
|
48
|
+
formatOptions?: NumberFieldFormatOptions
|
|
49
|
+
incrementLabel?: string
|
|
50
|
+
decrementLabel?: string
|
|
51
|
+
parts?: { wrapper?: PartStyle; increment?: PartStyle; decrement?: PartStyle }
|
|
52
|
+
}
|
|
53
|
+
/** One editable Input host and one raw native form value. Label with Field or a native label. */
|
|
54
|
+
export function NumberField({
|
|
55
|
+
value,
|
|
56
|
+
defaultValue,
|
|
57
|
+
onValueChange,
|
|
58
|
+
min,
|
|
59
|
+
max,
|
|
60
|
+
step = 1,
|
|
61
|
+
largeStep = step * 10,
|
|
62
|
+
locale = 'en-US',
|
|
63
|
+
formatOptions,
|
|
64
|
+
incrementLabel = 'Increase value',
|
|
65
|
+
decrementLabel = 'Decrease value',
|
|
66
|
+
controlSize = 'medium',
|
|
67
|
+
parts,
|
|
68
|
+
ref,
|
|
69
|
+
name,
|
|
70
|
+
form,
|
|
71
|
+
readOnly,
|
|
72
|
+
onChange,
|
|
73
|
+
onBlur,
|
|
74
|
+
onKeyDown,
|
|
75
|
+
onCompositionStart,
|
|
76
|
+
onCompositionEnd,
|
|
77
|
+
...props
|
|
78
|
+
}: NumberFieldProps) {
|
|
79
|
+
for (const [name, number] of [
|
|
80
|
+
['value', value],
|
|
81
|
+
['defaultValue', defaultValue],
|
|
82
|
+
['min', min],
|
|
83
|
+
['max', max]
|
|
84
|
+
] as const)
|
|
85
|
+
if (number !== undefined)
|
|
86
|
+
requireFiniteNumber(
|
|
87
|
+
'NumberField ' + name,
|
|
88
|
+
number,
|
|
89
|
+
name === 'value' || name === 'defaultValue'
|
|
90
|
+
)
|
|
91
|
+
requireFiniteNumber('NumberField step', step)
|
|
92
|
+
requireFiniteNumber('NumberField largeStep', largeStep)
|
|
93
|
+
if (step <= 0 || largeStep <= 0 || (min !== undefined && max !== undefined && min > max))
|
|
94
|
+
throw new TypeError('NumberField requires positive steps and min <= max.')
|
|
95
|
+
requireAccessibleName('NumberField increment', { 'aria-label': incrementLabel })
|
|
96
|
+
requireAccessibleName('NumberField decrement', { 'aria-label': decrementLabel })
|
|
97
|
+
const state = useControllableState({
|
|
98
|
+
name: 'NumberField',
|
|
99
|
+
value,
|
|
100
|
+
defaultValue,
|
|
101
|
+
initialValue: null,
|
|
102
|
+
onChange: onValueChange,
|
|
103
|
+
equal: (a, b) => a === b
|
|
104
|
+
})
|
|
105
|
+
const control = useFieldControl(props),
|
|
106
|
+
input = useRef<HTMLInputElement>(null),
|
|
107
|
+
composed = useComposedRefs(input, ref)
|
|
108
|
+
const adapter = useMemo(() => createNumberAdapter(locale, formatOptions), [locale, formatOptions])
|
|
109
|
+
const [draft, setDraft] = useState<{ value: number | null; text: string; key: string } | null>(
|
|
110
|
+
null
|
|
111
|
+
)
|
|
112
|
+
const composing = useRef(false)
|
|
113
|
+
if (draft && (draft.value !== state.value || draft.key !== adapter.key)) setDraft(null)
|
|
114
|
+
const active = draft && draft.value === state.value && draft.key === adapter.key
|
|
115
|
+
const text = active ? draft.text : adapter.format(state.value)
|
|
116
|
+
const parsed = adapter.parse(text)
|
|
117
|
+
const invalid =
|
|
118
|
+
parsed === undefined ||
|
|
119
|
+
(state.value !== null &&
|
|
120
|
+
((min !== undefined && state.value < min) || (max !== undefined && state.value > max)))
|
|
121
|
+
const latest = useRef({ state, adapter })
|
|
122
|
+
useLayoutEffect(() => {
|
|
123
|
+
latest.current = { state, adapter }
|
|
124
|
+
input.current?.setCustomValidity(
|
|
125
|
+
composing.current
|
|
126
|
+
? 'Finish entering the number.'
|
|
127
|
+
: invalid
|
|
128
|
+
? 'Enter a valid number within the allowed range.'
|
|
129
|
+
: ''
|
|
130
|
+
)
|
|
131
|
+
})
|
|
132
|
+
useEffect(() => {
|
|
133
|
+
const node = input.current,
|
|
134
|
+
view = node?.ownerDocument.defaultView
|
|
135
|
+
if (!node || !view) return
|
|
136
|
+
const roots = new Set([node.ownerDocument, node.getRootNode()])
|
|
137
|
+
let timer: number | undefined
|
|
138
|
+
const reset = (event: Event) => {
|
|
139
|
+
if (event.target !== node.form) return
|
|
140
|
+
view.clearTimeout(timer)
|
|
141
|
+
timer = view.setTimeout(() => {
|
|
142
|
+
if (event.defaultPrevented || !node.isConnected) return
|
|
143
|
+
composing.current = false
|
|
144
|
+
latest.current.state.reset()
|
|
145
|
+
setDraft(null)
|
|
146
|
+
}, 0)
|
|
147
|
+
}
|
|
148
|
+
for (const root of roots) root.addEventListener('reset', reset, true)
|
|
149
|
+
return () => {
|
|
150
|
+
for (const root of roots) root.removeEventListener('reset', reset, true)
|
|
151
|
+
view.clearTimeout(timer)
|
|
152
|
+
}
|
|
153
|
+
}, [])
|
|
154
|
+
function edit(text: string) {
|
|
155
|
+
const next = adapter.parse(text)
|
|
156
|
+
setDraft({ text, value: next === undefined ? state.value : next, key: adapter.key })
|
|
157
|
+
if (next !== undefined) state.request(next)
|
|
158
|
+
}
|
|
159
|
+
function move(amount: number) {
|
|
160
|
+
const node = input.current
|
|
161
|
+
if (!node || node.matches(':disabled') || readOnly) return
|
|
162
|
+
const next = addNumberStep(
|
|
163
|
+
(active && parsed !== undefined ? parsed : state.value) ?? 0,
|
|
164
|
+
amount,
|
|
165
|
+
min,
|
|
166
|
+
max
|
|
167
|
+
)
|
|
168
|
+
state.request(next)
|
|
169
|
+
setDraft(null)
|
|
170
|
+
node.focus({ preventScroll: true })
|
|
171
|
+
}
|
|
172
|
+
const atMinimum =
|
|
173
|
+
state.value !== null && addNumberStep(state.value, -step, min, max) === state.value
|
|
174
|
+
const atMaximum =
|
|
175
|
+
state.value !== null && addNumberStep(state.value, step, min, max) === state.value
|
|
176
|
+
const actionClass =
|
|
177
|
+
'h-[calc(var(--_rad-control-height)_-_2*var(--rad-input-group-border-width,var(--rad-control-border-width,1px)))] min-h-0 shrink-0 px-2'
|
|
178
|
+
return (
|
|
179
|
+
<InputGroup
|
|
180
|
+
controlSize={controlSize}
|
|
181
|
+
dir={props.dir}
|
|
182
|
+
data-slot="number-field-wrapper"
|
|
183
|
+
className={cn('gap-[var(--rad-number-field-gap,0.5rem)]', parts?.wrapper?.className)}
|
|
184
|
+
style={parts?.wrapper?.style}
|
|
185
|
+
>
|
|
186
|
+
<InputGroupInputRoot
|
|
187
|
+
{...props}
|
|
188
|
+
{...control}
|
|
189
|
+
ref={composed}
|
|
190
|
+
form={form}
|
|
191
|
+
readOnly={readOnly}
|
|
192
|
+
family="number-field"
|
|
193
|
+
type="text"
|
|
194
|
+
role="spinbutton"
|
|
195
|
+
value={text}
|
|
196
|
+
controlSize={controlSize}
|
|
197
|
+
inputMode={props.inputMode ?? 'decimal'}
|
|
198
|
+
spellCheck={props.spellCheck ?? false}
|
|
199
|
+
aria-valuenow={
|
|
200
|
+
parsed === undefined || parsed === null ? undefined : (state.value ?? undefined)
|
|
201
|
+
}
|
|
202
|
+
aria-valuemin={min}
|
|
203
|
+
aria-valuemax={max}
|
|
204
|
+
aria-valuetext={text || undefined}
|
|
205
|
+
aria-invalid={control['aria-invalid'] || (invalid ? true : undefined)}
|
|
206
|
+
onChange={event => {
|
|
207
|
+
onChange?.(event)
|
|
208
|
+
if (readOnly || event.currentTarget.matches(':disabled')) return
|
|
209
|
+
if (composing.current)
|
|
210
|
+
setDraft({ value: state.value, text: event.currentTarget.value, key: adapter.key })
|
|
211
|
+
else edit(event.currentTarget.value)
|
|
212
|
+
}}
|
|
213
|
+
onBlur={event => {
|
|
214
|
+
onBlur?.(event)
|
|
215
|
+
if (readOnly || event.currentTarget.matches(':disabled')) return
|
|
216
|
+
const current = active ? adapter.parse(event.currentTarget.value) : state.value
|
|
217
|
+
if (current !== undefined && current !== null)
|
|
218
|
+
state.request(clampNumber(current, min, max))
|
|
219
|
+
// Unparseable drafts remain editable and block native submission.
|
|
220
|
+
if (current !== undefined) setDraft(null)
|
|
221
|
+
}}
|
|
222
|
+
onKeyDown={event => {
|
|
223
|
+
onKeyDown?.(event)
|
|
224
|
+
if (
|
|
225
|
+
event.defaultPrevented ||
|
|
226
|
+
event.nativeEvent.isComposing ||
|
|
227
|
+
composing.current ||
|
|
228
|
+
event.altKey ||
|
|
229
|
+
event.ctrlKey ||
|
|
230
|
+
event.metaKey ||
|
|
231
|
+
event.shiftKey ||
|
|
232
|
+
readOnly ||
|
|
233
|
+
event.currentTarget.matches(':disabled')
|
|
234
|
+
)
|
|
235
|
+
return
|
|
236
|
+
const amount =
|
|
237
|
+
event.key === 'ArrowUp'
|
|
238
|
+
? step
|
|
239
|
+
: event.key === 'ArrowDown'
|
|
240
|
+
? -step
|
|
241
|
+
: event.key === 'PageUp'
|
|
242
|
+
? largeStep
|
|
243
|
+
: event.key === 'PageDown'
|
|
244
|
+
? -largeStep
|
|
245
|
+
: undefined
|
|
246
|
+
if (amount === undefined) return
|
|
247
|
+
event.preventDefault()
|
|
248
|
+
move(amount)
|
|
249
|
+
}}
|
|
250
|
+
onCompositionStart={event => {
|
|
251
|
+
onCompositionStart?.(event)
|
|
252
|
+
composing.current = true
|
|
253
|
+
}}
|
|
254
|
+
onCompositionEnd={event => {
|
|
255
|
+
onCompositionEnd?.(event)
|
|
256
|
+
composing.current = false
|
|
257
|
+
if (!readOnly && !event.currentTarget.matches(':disabled'))
|
|
258
|
+
edit(event.currentTarget.value)
|
|
259
|
+
}}
|
|
260
|
+
/>
|
|
261
|
+
<input
|
|
262
|
+
type="hidden"
|
|
263
|
+
name={name}
|
|
264
|
+
form={form}
|
|
265
|
+
disabled={control.disabled}
|
|
266
|
+
value={state.value === null ? '' : String(state.value)}
|
|
267
|
+
data-slot="number-field-value"
|
|
268
|
+
/>
|
|
269
|
+
<InputGroupAddon data-slot="number-field-actions">
|
|
270
|
+
{([-1, 1] as const).map(sign => (
|
|
271
|
+
<Button
|
|
272
|
+
key={sign}
|
|
273
|
+
type="button"
|
|
274
|
+
tabIndex={-1}
|
|
275
|
+
variant="ghost"
|
|
276
|
+
tone="neutral"
|
|
277
|
+
motion="off"
|
|
278
|
+
controlSize={controlSize}
|
|
279
|
+
aria-label={sign === 1 ? incrementLabel : decrementLabel}
|
|
280
|
+
aria-controls={control.id}
|
|
281
|
+
disabled={control.disabled || readOnly || (sign === 1 ? atMaximum : atMinimum)}
|
|
282
|
+
data-slot={sign === 1 ? 'number-field-increment' : 'number-field-decrement'}
|
|
283
|
+
className={cn(
|
|
284
|
+
actionClass,
|
|
285
|
+
(sign === 1 ? parts?.increment : parts?.decrement)?.className
|
|
286
|
+
)}
|
|
287
|
+
style={(sign === 1 ? parts?.increment : parts?.decrement)?.style}
|
|
288
|
+
onPointerDown={event => {
|
|
289
|
+
if (event.button === 0) {
|
|
290
|
+
event.preventDefault()
|
|
291
|
+
input.current?.focus({ preventScroll: true })
|
|
292
|
+
}
|
|
293
|
+
}}
|
|
294
|
+
onClick={() => move(sign * step)}
|
|
295
|
+
>
|
|
296
|
+
{sign === 1 ? '+' : '−'}
|
|
297
|
+
</Button>
|
|
298
|
+
))}
|
|
299
|
+
</InputGroupAddon>
|
|
300
|
+
</InputGroup>
|
|
301
|
+
)
|
|
302
|
+
}
|
package/src/Separator.tsx
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { ComponentProps } from 'react'
|
|
2
2
|
import { cn } from './cn.js'
|
|
3
3
|
import { joinIds } from './internal/ids.js'
|
|
4
|
+
import type { Orientation } from './presentation.js'
|
|
4
5
|
import type { ThemeStyle } from './themes/defineTheme.js'
|
|
5
6
|
|
|
6
7
|
export type SeparatorProps = Omit<
|
|
@@ -15,7 +16,7 @@ export type SeparatorProps = Omit<
|
|
|
15
16
|
> & {
|
|
16
17
|
/** Hide purely visual divisions from assistive technology. */
|
|
17
18
|
decorative?: boolean
|
|
18
|
-
orientation?:
|
|
19
|
+
orientation?: Orientation
|
|
19
20
|
style?: ThemeStyle
|
|
20
21
|
'data-slot'?: string
|
|
21
22
|
}
|
package/src/cn.ts
CHANGED
|
@@ -9,7 +9,10 @@ const roleClasses = (prefix: string, leaf: string) =>
|
|
|
9
9
|
export const cn: CnFunction = createCn({
|
|
10
10
|
extend: {
|
|
11
11
|
classGroups: {
|
|
12
|
-
'border-w-b': [
|
|
12
|
+
'border-w-b': [
|
|
13
|
+
'border-b-(--rad-code-block-border-width)',
|
|
14
|
+
'border-b-(--rad-table-border-width)'
|
|
15
|
+
],
|
|
13
16
|
'border-w-t': ['border-t-(--rad-separator-thickness)'],
|
|
14
17
|
'border-w-s': ['border-s-(--rad-separator-thickness)'],
|
|
15
18
|
my: ['my-(--rad-separator-spacing)'],
|
|
@@ -40,6 +43,8 @@ export const cn: CnFunction = createCn({
|
|
|
40
43
|
'font-(--rad-tabs-font-weight)'
|
|
41
44
|
],
|
|
42
45
|
'font-size': [
|
|
46
|
+
'text-(--rad-inline-code-font-size)',
|
|
47
|
+
'text-(--rad-code-block-font-size)',
|
|
43
48
|
'text-(--rad-empty-state-title-size)',
|
|
44
49
|
'text-(--rad-empty-state-description-size)',
|
|
45
50
|
'text-(--rad-nav-link-font-size)',
|
|
@@ -91,7 +96,7 @@ export const cn: CnFunction = createCn({
|
|
|
91
96
|
'w-(--rad-slider-length)'
|
|
92
97
|
],
|
|
93
98
|
'min-w': ['min-w-(--rad-pagination-item-min-width)'],
|
|
94
|
-
'max-h': ['max-h-(--rad-command-results-max-height)'],
|
|
99
|
+
'max-h': ['max-h-(--rad-code-block-max-height)', 'max-h-(--rad-command-results-max-height)'],
|
|
95
100
|
'max-w': ['max-w-(--rad-tooltip-max-width)'],
|
|
96
101
|
size: [
|
|
97
102
|
'size-(--rad-empty-state-icon-size)',
|
|
@@ -106,10 +111,12 @@ export const cn: CnFunction = createCn({
|
|
|
106
111
|
],
|
|
107
112
|
mb: ['mb-(--rad-multi-select-gap)', 'mb-(--rad-fieldset-gap)'],
|
|
108
113
|
gap: [
|
|
114
|
+
'gap-(--rad-code-block-header-gap)',
|
|
109
115
|
'gap-(--rad-empty-state-gap)',
|
|
110
116
|
'gap-(--rad-empty-state-actions-gap)',
|
|
111
117
|
'gap-(--rad-nav-link-gap)',
|
|
112
118
|
'gap-(--rad-password-input-gap)',
|
|
119
|
+
'gap-(--rad-number-field-gap)',
|
|
113
120
|
'gap-(--rad-input-group-gap)',
|
|
114
121
|
'gap-(--rad-kbd-gap)',
|
|
115
122
|
'gap-(--rad-otp-field-gap)',
|
|
@@ -137,6 +144,7 @@ export const cn: CnFunction = createCn({
|
|
|
137
144
|
],
|
|
138
145
|
ps: ['ps-(--rad-nav-link-indent)'],
|
|
139
146
|
py: [
|
|
147
|
+
'py-(--rad-inline-code-padding-block)',
|
|
140
148
|
'py-(--rad-nav-link-padding-block)',
|
|
141
149
|
'py-(--rad-kbd-padding-block)',
|
|
142
150
|
'py-(--rad-table-padding-block)',
|
|
@@ -144,6 +152,8 @@ export const cn: CnFunction = createCn({
|
|
|
144
152
|
'py-(--rad-badge-padding-block)'
|
|
145
153
|
],
|
|
146
154
|
px: [
|
|
155
|
+
'px-(--rad-inline-code-padding-inline)',
|
|
156
|
+
'px-(--rad-code-block-padding)',
|
|
147
157
|
'px-(--rad-nav-link-padding-inline)',
|
|
148
158
|
'px-(--rad-native-select-padding)',
|
|
149
159
|
'px-(--rad-input-group-padding-inline)',
|
|
@@ -156,6 +166,8 @@ export const cn: CnFunction = createCn({
|
|
|
156
166
|
'px-(--rad-tabs-trigger-padding)'
|
|
157
167
|
],
|
|
158
168
|
p: [
|
|
169
|
+
'p-(--rad-code-block-padding)',
|
|
170
|
+
'p-(--rad-code-block-header-padding)',
|
|
159
171
|
'p-(--rad-empty-state-padding)',
|
|
160
172
|
'p-(--rad-menubar-padding)',
|
|
161
173
|
'p-(--rad-toolbar-padding)',
|
|
@@ -172,6 +184,8 @@ export const cn: CnFunction = createCn({
|
|
|
172
184
|
'p-(--rad-tabs-content-padding)'
|
|
173
185
|
],
|
|
174
186
|
rounded: [
|
|
187
|
+
'rounded-(--rad-code-block-radius)',
|
|
188
|
+
'rounded-(--rad-inline-code-radius)',
|
|
175
189
|
'rounded-(--rad-empty-state-radius)',
|
|
176
190
|
'rounded-(--rad-nav-link-radius)',
|
|
177
191
|
'rounded-(--rad-meter-radius)',
|
|
@@ -212,6 +226,7 @@ export const cn: CnFunction = createCn({
|
|
|
212
226
|
'rounded-(--rad-tabs-content-radius)'
|
|
213
227
|
],
|
|
214
228
|
'border-w': [
|
|
229
|
+
'border-(--rad-code-block-border-width)',
|
|
215
230
|
'border-(--rad-empty-state-border-width)',
|
|
216
231
|
'border-(--rad-meter-border-width)',
|
|
217
232
|
'border-(--rad-native-select-border-width)',
|
|
@@ -314,7 +329,8 @@ export const cn: CnFunction = createCn({
|
|
|
314
329
|
'[backdrop-filter:var(--rad-combobox-content-backdrop-filter)]',
|
|
315
330
|
'[backdrop-filter:var(--rad-select-content-backdrop-filter)]'
|
|
316
331
|
],
|
|
317
|
-
leading: roleClasses('leading', 'line-height'),
|
|
332
|
+
leading: ['leading-(--rad-code-block-line-height)', ...roleClasses('leading', 'line-height')],
|
|
333
|
+
pe: ['pe-(--rad-code-block-number-gap)'],
|
|
318
334
|
tracking: roleClasses('tracking', 'letter-spacing'),
|
|
319
335
|
'max-inline-size': roleClasses('max-inline', 'measure'),
|
|
320
336
|
mbs: [
|
package/src/index.ts
CHANGED
|
@@ -655,3 +655,20 @@ export {
|
|
|
655
655
|
type EmptyStateActionsProps
|
|
656
656
|
} from './EmptyState.js'
|
|
657
657
|
export type { HeadingLevel } from './internal/headings.js'
|
|
658
|
+
|
|
659
|
+
export { InlineCode, type InlineCodeProps } from './InlineCode.js'
|
|
660
|
+
export {
|
|
661
|
+
CodeBlock,
|
|
662
|
+
CodeBlockHeader,
|
|
663
|
+
CodeBlockViewport,
|
|
664
|
+
CodeBlockContent,
|
|
665
|
+
CodeBlockCopy,
|
|
666
|
+
type CodeBlockProps,
|
|
667
|
+
type CodeBlockHeaderProps,
|
|
668
|
+
type CodeBlockViewportProps,
|
|
669
|
+
type CodeBlockContentProps,
|
|
670
|
+
type CodeBlockCopyProps,
|
|
671
|
+
type CodeBlockRenderProps
|
|
672
|
+
} from './CodeBlock.js'
|
|
673
|
+
|
|
674
|
+
export { NumberField, type NumberFieldProps, type NumberFieldFormatOptions } from './NumberField.js'
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
/** A bounded editable decimal adapter; Intl supplies display and locale symbols. */
|
|
2
|
+
export type NumberFieldFormatOptions = {
|
|
3
|
+
style?: 'decimal' | 'percent' | 'currency'
|
|
4
|
+
currency?: string
|
|
5
|
+
currencyDisplay?: 'symbol' | 'narrowSymbol' | 'code'
|
|
6
|
+
currencySign?: 'standard' | 'accounting'
|
|
7
|
+
minimumFractionDigits?: number
|
|
8
|
+
maximumFractionDigits?: number
|
|
9
|
+
useGrouping?: boolean
|
|
10
|
+
signDisplay?: 'auto' | 'always' | 'exceptZero'
|
|
11
|
+
}
|
|
12
|
+
const clean = (text: string) =>
|
|
13
|
+
text.replace(/[\u061c\u200e\u200f]/g, '').replace(/[\u00a0\u202f]/g, ' ')
|
|
14
|
+
const numeric = new Set(['integer', 'group', 'decimal', 'fraction'])
|
|
15
|
+
export function createNumberAdapter(locale: string, options: NumberFieldFormatOptions = {}) {
|
|
16
|
+
const allowed = new Set([
|
|
17
|
+
'style',
|
|
18
|
+
'currency',
|
|
19
|
+
'currencyDisplay',
|
|
20
|
+
'currencySign',
|
|
21
|
+
'minimumFractionDigits',
|
|
22
|
+
'maximumFractionDigits',
|
|
23
|
+
'useGrouping',
|
|
24
|
+
'signDisplay'
|
|
25
|
+
])
|
|
26
|
+
if (
|
|
27
|
+
Object.keys(options).some(key => !allowed.has(key)) ||
|
|
28
|
+
(options.style !== undefined && !['decimal', 'percent', 'currency'].includes(options.style)) ||
|
|
29
|
+
(options.currencyDisplay !== undefined &&
|
|
30
|
+
!['symbol', 'narrowSymbol', 'code'].includes(options.currencyDisplay)) ||
|
|
31
|
+
(options.signDisplay !== undefined &&
|
|
32
|
+
!['auto', 'always', 'exceptZero'].includes(options.signDisplay))
|
|
33
|
+
)
|
|
34
|
+
throw new TypeError('NumberField supports standard decimal, percent and currency formatting.')
|
|
35
|
+
const formatter = new Intl.NumberFormat(locale, { maximumFractionDigits: 20, ...options })
|
|
36
|
+
const resolved = formatter.resolvedOptions()
|
|
37
|
+
const decimal = new Intl.NumberFormat(locale, {
|
|
38
|
+
numberingSystem: resolved.numberingSystem,
|
|
39
|
+
maximumFractionDigits: 20
|
|
40
|
+
})
|
|
41
|
+
const digits = new Map<string, string>()
|
|
42
|
+
const digitFormatter = new Intl.NumberFormat(locale, {
|
|
43
|
+
numberingSystem: resolved.numberingSystem,
|
|
44
|
+
useGrouping: false
|
|
45
|
+
})
|
|
46
|
+
for (let index = 0; index < 10; index++) digits.set(digitFormatter.format(index), String(index))
|
|
47
|
+
const ascii = (text: string) =>
|
|
48
|
+
Array.from(clean(text), digit => digits.get(digit) ?? digit).join('')
|
|
49
|
+
const samples = decimal.formatToParts(123456789012345.6)
|
|
50
|
+
const group = clean(samples.find(part => part.type === 'group')?.value ?? ',')
|
|
51
|
+
const separator = clean(samples.find(part => part.type === 'decimal')?.value ?? '.')
|
|
52
|
+
const groups = samples
|
|
53
|
+
.filter(part => part.type === 'integer')
|
|
54
|
+
.map(part => ascii(part.value).length)
|
|
55
|
+
const primary = groups.at(-1) ?? 3,
|
|
56
|
+
secondary = groups.at(-2) ?? primary
|
|
57
|
+
const affixes = [1, -1, 0].map(value => {
|
|
58
|
+
const parts = formatter.formatToParts(value)
|
|
59
|
+
const first = parts.findIndex(part => numeric.has(part.type))
|
|
60
|
+
const last = parts.length - 1 - [...parts].reverse().findIndex(part => numeric.has(part.type))
|
|
61
|
+
return {
|
|
62
|
+
prefix: clean(
|
|
63
|
+
parts
|
|
64
|
+
.slice(0, first)
|
|
65
|
+
.map(part => part.value)
|
|
66
|
+
.join('')
|
|
67
|
+
),
|
|
68
|
+
suffix: clean(
|
|
69
|
+
parts
|
|
70
|
+
.slice(last + 1)
|
|
71
|
+
.map(part => part.value)
|
|
72
|
+
.join('')
|
|
73
|
+
),
|
|
74
|
+
sign: value < 0 ? -1 : 1
|
|
75
|
+
}
|
|
76
|
+
})
|
|
77
|
+
const plus = new Intl.NumberFormat(locale, {
|
|
78
|
+
maximumFractionDigits: 20,
|
|
79
|
+
...options,
|
|
80
|
+
signDisplay: 'always'
|
|
81
|
+
}).formatToParts(1)
|
|
82
|
+
affixes.push({
|
|
83
|
+
prefix: clean(
|
|
84
|
+
plus
|
|
85
|
+
.slice(
|
|
86
|
+
0,
|
|
87
|
+
plus.findIndex(part => numeric.has(part.type))
|
|
88
|
+
)
|
|
89
|
+
.map(part => part.value)
|
|
90
|
+
.join('')
|
|
91
|
+
),
|
|
92
|
+
suffix: clean(
|
|
93
|
+
plus
|
|
94
|
+
.slice(plus.length - 1 - [...plus].reverse().findIndex(part => numeric.has(part.type)) + 1)
|
|
95
|
+
.map(part => part.value)
|
|
96
|
+
.join('')
|
|
97
|
+
),
|
|
98
|
+
sign: 1
|
|
99
|
+
})
|
|
100
|
+
const minus = clean(
|
|
101
|
+
decimal.formatToParts(-1).find(part => part.type === 'minusSign')?.value ?? '-'
|
|
102
|
+
)
|
|
103
|
+
function bodyNumber(body: string): number | undefined {
|
|
104
|
+
body = ascii(body)
|
|
105
|
+
const split = body.split(separator)
|
|
106
|
+
if (split.length > 2) return
|
|
107
|
+
const whole = split[0]!,
|
|
108
|
+
fraction = split[1]
|
|
109
|
+
if (fraction !== undefined && !/^\d*$/.test(fraction)) return
|
|
110
|
+
const pieces = whole.split(group)
|
|
111
|
+
if (
|
|
112
|
+
pieces.some(piece => !/^\d+$/.test(piece)) &&
|
|
113
|
+
!(whole === '' && fraction && /^\d+$/.test(fraction))
|
|
114
|
+
)
|
|
115
|
+
return
|
|
116
|
+
if (
|
|
117
|
+
pieces.length > 1 &&
|
|
118
|
+
(pieces[0]!.length < 1 ||
|
|
119
|
+
pieces[0]!.length > secondary ||
|
|
120
|
+
pieces.at(-1)!.length !== primary ||
|
|
121
|
+
pieces.slice(1, -1).some(piece => piece.length !== secondary))
|
|
122
|
+
)
|
|
123
|
+
return
|
|
124
|
+
// Shift the decimal before conversion, avoiding percentage division tails or intermediate overflow.
|
|
125
|
+
const value = Number(
|
|
126
|
+
pieces.join('') +
|
|
127
|
+
(fraction === undefined ? '' : '.' + fraction) +
|
|
128
|
+
(resolved.style === 'percent' ? 'e-2' : '')
|
|
129
|
+
)
|
|
130
|
+
return Number.isFinite(value) ? value : undefined
|
|
131
|
+
}
|
|
132
|
+
return {
|
|
133
|
+
key: JSON.stringify(resolved),
|
|
134
|
+
format: (value: number | null) => (value === null ? '' : formatter.format(value)),
|
|
135
|
+
parse(text: string): number | null | undefined {
|
|
136
|
+
text = clean(text).trim()
|
|
137
|
+
if (text === '') return null
|
|
138
|
+
const candidates: { body: string; sign: number }[] = []
|
|
139
|
+
for (const { prefix, suffix, sign } of affixes)
|
|
140
|
+
if (
|
|
141
|
+
text.startsWith(prefix) &&
|
|
142
|
+
text.endsWith(suffix) &&
|
|
143
|
+
text.length > prefix.length + suffix.length
|
|
144
|
+
)
|
|
145
|
+
candidates.push({
|
|
146
|
+
body: text.slice(prefix.length, suffix.length ? -suffix.length : undefined),
|
|
147
|
+
sign
|
|
148
|
+
})
|
|
149
|
+
const negative = text.startsWith(minus) || text.startsWith('-')
|
|
150
|
+
const signed = negative || text.startsWith('+')
|
|
151
|
+
candidates.push({
|
|
152
|
+
body: signed
|
|
153
|
+
? text.slice((negative && text.startsWith(minus) ? minus : text[0]!).length)
|
|
154
|
+
: text,
|
|
155
|
+
sign: negative ? -1 : 1
|
|
156
|
+
})
|
|
157
|
+
for (const { body, sign } of candidates) {
|
|
158
|
+
const parsed = bodyNumber(body)
|
|
159
|
+
if (parsed !== undefined) {
|
|
160
|
+
const value = sign * parsed
|
|
161
|
+
if (Number.isFinite(value)) return value === 0 ? 0 : value
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return undefined
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
export function requireFiniteNumber(name: string, value: unknown, nullable = false) {
|
|
169
|
+
if (nullable && value === null) return
|
|
170
|
+
if (typeof value !== 'number' || !Number.isFinite(value))
|
|
171
|
+
throw new TypeError(name + ' must be finite.')
|
|
172
|
+
}
|
|
173
|
+
export const clampNumber = (value: number, min?: number, max?: number) =>
|
|
174
|
+
Math.min(max ?? Infinity, Math.max(min ?? -Infinity, value))
|
|
175
|
+
/** Add decimal coefficients before converting back to the public JavaScript number. */
|
|
176
|
+
export function addNumberStep(value: number, amount: number, min?: number, max?: number) {
|
|
177
|
+
const parts = (number: number) => {
|
|
178
|
+
const [mantissa, exponent = '0'] = number.toString().split('e')
|
|
179
|
+
const fraction = mantissa!.split('.')[1]?.length ?? 0
|
|
180
|
+
return {
|
|
181
|
+
coefficient: BigInt(mantissa!.replace('.', '')),
|
|
182
|
+
exponent: Number(exponent) - fraction
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
const a = parts(value),
|
|
186
|
+
b = parts(amount),
|
|
187
|
+
exponent = Math.min(a.exponent, b.exponent)
|
|
188
|
+
const coefficient =
|
|
189
|
+
a.coefficient * 10n ** BigInt(a.exponent - exponent) +
|
|
190
|
+
b.coefficient * 10n ** BigInt(b.exponent - exponent)
|
|
191
|
+
const next = clampNumber(Number(coefficient.toString() + 'e' + exponent), min, max)
|
|
192
|
+
return Number.isFinite(next) ? next : value
|
|
193
|
+
}
|
package/src/themes/components.ts
CHANGED
|
@@ -2,6 +2,30 @@ import { kebabCase } from './tokens.js'
|
|
|
2
2
|
|
|
3
3
|
/** Explicit component slots; omitted slots reset to CSS fallback semantics. */
|
|
4
4
|
export const componentRegistry = {
|
|
5
|
+
inlineCode: {
|
|
6
|
+
radius: 'css',
|
|
7
|
+
paddingInline: 'css',
|
|
8
|
+
paddingBlock: 'css',
|
|
9
|
+
background: 'css',
|
|
10
|
+
color: 'css',
|
|
11
|
+
fontSize: 'css'
|
|
12
|
+
},
|
|
13
|
+
codeBlock: {
|
|
14
|
+
radius: 'css',
|
|
15
|
+
borderWidth: 'css',
|
|
16
|
+
borderColor: 'css',
|
|
17
|
+
background: 'css',
|
|
18
|
+
color: 'css',
|
|
19
|
+
padding: 'css',
|
|
20
|
+
headerPadding: 'css',
|
|
21
|
+
headerGap: 'css',
|
|
22
|
+
maxHeight: 'css',
|
|
23
|
+
fontSize: 'css',
|
|
24
|
+
lineHeight: 'css',
|
|
25
|
+
numberGap: 'css',
|
|
26
|
+
numberColor: 'css',
|
|
27
|
+
statusColor: 'css'
|
|
28
|
+
},
|
|
5
29
|
emptyState: {
|
|
6
30
|
padding: 'css',
|
|
7
31
|
gap: 'css',
|
|
@@ -39,6 +63,7 @@ export const componentRegistry = {
|
|
|
39
63
|
focusWidth: 'css',
|
|
40
64
|
focusOffset: 'css'
|
|
41
65
|
},
|
|
66
|
+
numberField: { gap: 'css' },
|
|
42
67
|
passwordInput: { gap: 'css' },
|
|
43
68
|
meter: {
|
|
44
69
|
height: 'css',
|