@cognitap/radiant 0.0.84 → 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 +12 -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/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/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/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/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/index.d.ts +9 -0
- package/dist/library/index.d.ts.map +1 -1
- package/dist/library/index.js +5 -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/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/components/CodeBlock/CodeBlock.tsx +3 -45
- package/src/components/Collapsible/Collapsible.tsx +22 -4
- package/src/components/CopyButton/CopyButton.tsx +75 -0
- package/src/components/Dropzone/Dropzone.tsx +230 -0
- package/src/components/Highlight/Highlight.tsx +112 -0
- package/src/components/Spoiler/Spoiler.tsx +96 -0
- package/src/components/Tour/Tour.tsx +335 -0
- package/src/index.ts +15 -0
- package/src/motion/transitions.ts +8 -3
- package/src/react/useClipboardCopy.ts +54 -0
- package/src/react/usePopoverSurface.ts +3 -2
package/package.json
CHANGED
|
@@ -1,14 +1,6 @@
|
|
|
1
1
|
'use client'
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
createContext,
|
|
5
|
-
useContext,
|
|
6
|
-
useEffect,
|
|
7
|
-
useId,
|
|
8
|
-
useRef,
|
|
9
|
-
useState,
|
|
10
|
-
type ComponentProps
|
|
11
|
-
} from 'react'
|
|
3
|
+
import { createContext, useContext, useId, type ComponentProps } from 'react'
|
|
12
4
|
import { Button, type ButtonProps, type ButtonRenderProps } from '../Button/Button.js'
|
|
13
5
|
import { ScrollArea, type ScrollAreaProps } from '../ScrollArea/ScrollArea.js'
|
|
14
6
|
import { cn } from 'cn'
|
|
@@ -16,10 +8,9 @@ import { joinIds } from '../../internal/ids.js'
|
|
|
16
8
|
import { composeAction } from '../../internal/events.js'
|
|
17
9
|
import type { DistributiveOmit, WithoutToggle } from '../../internal/types.js'
|
|
18
10
|
import type { PartStyle } from '../../presentation.js'
|
|
11
|
+
import { useClipboardCopy } from '../../react/useClipboardCopy.js'
|
|
19
12
|
import type { ThemeStyle } from '../../themes/defineTheme.js'
|
|
20
13
|
|
|
21
|
-
type Phase = 'idle' | 'pending' | 'success' | 'error'
|
|
22
|
-
type Feedback = { source: string; phase: Phase }
|
|
23
14
|
type Native<Host extends 'div' | 'pre'> = Omit<ComponentProps<Host>, 'style'> & {
|
|
24
15
|
style?: ThemeStyle
|
|
25
16
|
'data-slot'?: string
|
|
@@ -68,41 +59,8 @@ export function CodeBlock({
|
|
|
68
59
|
...props
|
|
69
60
|
}: CodeBlockProps) {
|
|
70
61
|
if (typeof code !== 'string') throw new TypeError('CodeBlock code must be a string.')
|
|
71
|
-
const [feedback, setFeedback] = useState<Feedback>({ source: code, phase: 'idle' })
|
|
72
|
-
const [pending, setPending] = useState(false)
|
|
73
|
-
const busy = useRef(false),
|
|
74
|
-
mounted = useRef(true),
|
|
75
|
-
operation = useRef(0)
|
|
76
62
|
const statusId = useId()
|
|
77
|
-
|
|
78
|
-
if (feedback.source !== code) setFeedback({ source: code, phase: 'idle' })
|
|
79
|
-
useEffect(() => {
|
|
80
|
-
mounted.current = true
|
|
81
|
-
return () => {
|
|
82
|
-
mounted.current = false
|
|
83
|
-
operation.current += 1
|
|
84
|
-
}
|
|
85
|
-
}, [])
|
|
86
|
-
const copy = async (win: Window | null) => {
|
|
87
|
-
if (busy.current) return
|
|
88
|
-
const source = code,
|
|
89
|
-
request = ++operation.current
|
|
90
|
-
busy.current = true
|
|
91
|
-
setPending(true)
|
|
92
|
-
setFeedback({ source, phase: 'pending' })
|
|
93
|
-
let phase: Phase = 'success'
|
|
94
|
-
try {
|
|
95
|
-
if (!win?.navigator.clipboard?.writeText) throw new Error('Clipboard writing is unavailable.')
|
|
96
|
-
await win.navigator.clipboard.writeText(source)
|
|
97
|
-
} catch {
|
|
98
|
-
phase = 'error'
|
|
99
|
-
}
|
|
100
|
-
if (!mounted.current || request !== operation.current) return
|
|
101
|
-
busy.current = false
|
|
102
|
-
setPending(false)
|
|
103
|
-
setFeedback(current => (current.source === source ? { source, phase } : current))
|
|
104
|
-
}
|
|
105
|
-
const phase = feedback.source === code ? feedback.phase : 'idle'
|
|
63
|
+
const { copy, pending, phase } = useClipboardCopy(code)
|
|
106
64
|
const message =
|
|
107
65
|
phase === 'pending'
|
|
108
66
|
? loadingText
|
|
@@ -28,11 +28,12 @@ const collapsibleVariants = cva([
|
|
|
28
28
|
'[color:var(--rad-collapsible-color,var(--rad-color-foreground,oklch(0.21_0.006_286)))]',
|
|
29
29
|
'[box-shadow:var(--rad-collapsible-shadow,var(--rad-surface-shadow,none))] [backdrop-filter:var(--rad-surface-backdrop-filter,none)]',
|
|
30
30
|
'[&::details-content]:overflow-hidden',
|
|
31
|
+
'[--_rad-disclosure-preview:0px]',
|
|
31
32
|
'[&::details-content]:[interpolate-size:allow-keywords]',
|
|
32
33
|
'[&::details-content]:transition-[height]',
|
|
33
34
|
'[&::details-content]:duration-[var(--rad-collapsible-duration,var(--rad-motion-disclosure-duration,150ms))]',
|
|
34
35
|
'[&::details-content]:ease-out',
|
|
35
|
-
'[&:not([open])::details-content]:h-
|
|
36
|
+
'[&:not([open])::details-content]:h-[var(--_rad-disclosure-preview,0px)]',
|
|
36
37
|
'open:[&::details-content]:h-auto',
|
|
37
38
|
'motion-reduce:[&::details-content]:transition-none',
|
|
38
39
|
'forced-colors:border-[CanvasText] forced-colors:bg-[Canvas] forced-colors:text-[CanvasText]'
|
|
@@ -108,7 +109,12 @@ function useCollapsibleContext() {
|
|
|
108
109
|
return collapsible
|
|
109
110
|
}
|
|
110
111
|
|
|
111
|
-
export function Collapsible({
|
|
112
|
+
export function Collapsible(props: CollapsibleProps) {
|
|
113
|
+
return <CollapsibleRoot {...props} />
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Private collapsed preview seam for Spoiler. */
|
|
117
|
+
export function CollapsibleRoot({
|
|
112
118
|
open,
|
|
113
119
|
defaultOpen,
|
|
114
120
|
motion = 'inherit',
|
|
@@ -117,8 +123,10 @@ export function Collapsible({
|
|
|
117
123
|
onToggle,
|
|
118
124
|
className,
|
|
119
125
|
children,
|
|
126
|
+
previewHeight,
|
|
127
|
+
style,
|
|
120
128
|
...props
|
|
121
|
-
}: CollapsibleProps) {
|
|
129
|
+
}: CollapsibleProps & { previewHeight?: number }) {
|
|
122
130
|
const node = useRef<HTMLDetailsElement>(null)
|
|
123
131
|
const nativeRef = useComposedRefs(node, ref)
|
|
124
132
|
const transition = useTransitionHost<HTMLDetailsElement>(
|
|
@@ -160,11 +168,21 @@ export function Collapsible({
|
|
|
160
168
|
collapsibleVariants(),
|
|
161
169
|
transition.active &&
|
|
162
170
|
transition.ready &&
|
|
163
|
-
'[&::details-content]:[content-visibility:visible] [&::details-content]:transition-none [&:not([open])::details-content]:h-[var(--_rad-spring-details-height,0px)] open:[&::details-content]:h-[var(--_rad-spring-details-height,auto)]',
|
|
171
|
+
'[&::details-content]:[content-visibility:visible] [&::details-content]:transition-none [&:not([open])::details-content]:h-[var(--_rad-spring-details-height,var(--_rad-disclosure-preview,0px))] open:[&::details-content]:h-[var(--_rad-spring-details-height,auto)]',
|
|
172
|
+
previewHeight !== undefined && '[&::details-content]:[content-visibility:visible]',
|
|
164
173
|
motionOff && '[&::details-content]:transition-none [&>summary>svg]:transition-none',
|
|
165
174
|
className
|
|
166
175
|
)}
|
|
167
176
|
data-slot={joinIds('collapsible', props['data-slot'])}
|
|
177
|
+
data-rad-disclosure-preview={previewHeight}
|
|
178
|
+
style={
|
|
179
|
+
previewHeight === undefined
|
|
180
|
+
? style
|
|
181
|
+
: {
|
|
182
|
+
...style,
|
|
183
|
+
'--_rad-disclosure-preview': `${previewHeight}px`
|
|
184
|
+
}
|
|
185
|
+
}
|
|
168
186
|
data-state={isOpen ? 'open' : 'closed'}
|
|
169
187
|
open={isOpen}
|
|
170
188
|
onToggle={event => {
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { useId } from 'react'
|
|
4
|
+
import { cn } from 'cn'
|
|
5
|
+
import { composeAction } from '../../internal/events.js'
|
|
6
|
+
import { joinIds } from '../../internal/ids.js'
|
|
7
|
+
import type { DistributiveOmit, WithoutToggle } from '../../internal/types.js'
|
|
8
|
+
import type { PartStyle } from '../../presentation.js'
|
|
9
|
+
import { useClipboardCopy } from '../../react/useClipboardCopy.js'
|
|
10
|
+
import { Button, type ButtonProps, type ButtonRenderProps } from '../Button/Button.js'
|
|
11
|
+
|
|
12
|
+
export type CopyButtonRenderProps = ButtonRenderProps
|
|
13
|
+
export type CopyButtonProps = WithoutToggle<DistributiveOmit<ButtonProps, 'type' | 'value'>> & {
|
|
14
|
+
/** The exact text to write. */
|
|
15
|
+
value: string
|
|
16
|
+
/** Milliseconds to show a successful write before clearing the confirmation. */
|
|
17
|
+
resetDelay?: number
|
|
18
|
+
copiedText?: string
|
|
19
|
+
errorText?: string
|
|
20
|
+
parts?: { status?: PartStyle }
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function CopyButton({
|
|
24
|
+
value,
|
|
25
|
+
resetDelay = 2000,
|
|
26
|
+
copiedText = 'Copied.',
|
|
27
|
+
errorText = 'Could not copy. Try again or copy the text manually.',
|
|
28
|
+
onClick,
|
|
29
|
+
children = 'Copy',
|
|
30
|
+
pending: externalPending,
|
|
31
|
+
parts,
|
|
32
|
+
...props
|
|
33
|
+
}: CopyButtonProps) {
|
|
34
|
+
if (typeof value !== 'string') throw new TypeError('CopyButton value must be a string.')
|
|
35
|
+
if (!Number.isFinite(resetDelay) || resetDelay <= 0 || resetDelay > 2_147_483_647) {
|
|
36
|
+
throw new RangeError('CopyButton resetDelay must be a positive delay in milliseconds.')
|
|
37
|
+
}
|
|
38
|
+
const statusId = useId()
|
|
39
|
+
const { pending, phase, copy } = useClipboardCopy(value, resetDelay)
|
|
40
|
+
const message = phase === 'success' ? copiedText : phase === 'error' ? errorText : ''
|
|
41
|
+
|
|
42
|
+
return (
|
|
43
|
+
<>
|
|
44
|
+
<Button
|
|
45
|
+
{...props}
|
|
46
|
+
type="button"
|
|
47
|
+
pressed={undefined}
|
|
48
|
+
defaultPressed={undefined}
|
|
49
|
+
onPressedChange={undefined}
|
|
50
|
+
aria-pressed={undefined}
|
|
51
|
+
pending={externalPending || pending}
|
|
52
|
+
data-slot={joinIds('copy-button', props['data-slot'])}
|
|
53
|
+
aria-describedby={joinIds(props['aria-describedby'], statusId)}
|
|
54
|
+
onClick={composeAction(onClick, event => {
|
|
55
|
+
void copy(event.currentTarget.ownerDocument.defaultView)
|
|
56
|
+
})}
|
|
57
|
+
>
|
|
58
|
+
{children}
|
|
59
|
+
</Button>
|
|
60
|
+
<output
|
|
61
|
+
id={statusId}
|
|
62
|
+
data-slot="copy-button-status"
|
|
63
|
+
data-state={phase}
|
|
64
|
+
className={cn(
|
|
65
|
+
'block min-w-0 text-sm [overflow-wrap:anywhere] empty:absolute empty:size-px empty:overflow-hidden',
|
|
66
|
+
phase === 'error' && '[color:var(--rad-color-invalid)]',
|
|
67
|
+
parts?.status?.className
|
|
68
|
+
)}
|
|
69
|
+
style={parts?.status?.style}
|
|
70
|
+
>
|
|
71
|
+
{message}
|
|
72
|
+
</output>
|
|
73
|
+
</>
|
|
74
|
+
)
|
|
75
|
+
}
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { useId, useRef, useState, type DragEvent } from 'react'
|
|
4
|
+
import { cn } from 'cn'
|
|
5
|
+
import { joinIds } from '../../internal/ids.js'
|
|
6
|
+
import type { PartStyle } from '../../presentation.js'
|
|
7
|
+
import { useComposedRefs } from '../../react/useComposedRefs.js'
|
|
8
|
+
import { useFormReset } from '../../react/useFormReset.js'
|
|
9
|
+
import { useField } from '../Field/Field.js'
|
|
10
|
+
import { FileInput, type FileInputProps } from '../FileInput/FileInput.js'
|
|
11
|
+
import { focusWithinRingClass } from '../shared/focusStyles.js'
|
|
12
|
+
|
|
13
|
+
export type DropzoneRejection = {
|
|
14
|
+
file: File
|
|
15
|
+
reason: 'type' | 'size' | 'count'
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export type DropzoneSelection = {
|
|
19
|
+
accepted: readonly File[]
|
|
20
|
+
rejected: readonly DropzoneRejection[]
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type DropzoneProps = Omit<
|
|
24
|
+
FileInputProps,
|
|
25
|
+
'parts' | 'onChange' | 'onDrop' | 'onDragEnter' | 'onDragOver' | 'onDragLeave'
|
|
26
|
+
> & {
|
|
27
|
+
/** Per-file limit, in bytes. */
|
|
28
|
+
maxSize?: number
|
|
29
|
+
maxFiles?: number
|
|
30
|
+
description?: string
|
|
31
|
+
rejectionText?: (rejection: DropzoneRejection) => string
|
|
32
|
+
onFilesChange?: (selection: DropzoneSelection) => void
|
|
33
|
+
parts?: { label?: PartStyle; description?: PartStyle; status?: PartStyle }
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const reasons = {
|
|
37
|
+
type: 'Unsupported file type',
|
|
38
|
+
size: 'File is too large',
|
|
39
|
+
count: 'Too many files'
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function matchesAccept(file: File, tokens: string[]) {
|
|
43
|
+
if (!tokens.length) return true
|
|
44
|
+
const name = file.name.toLowerCase()
|
|
45
|
+
const type = file.type.toLowerCase()
|
|
46
|
+
return tokens.some(token =>
|
|
47
|
+
token.startsWith('.')
|
|
48
|
+
? name.endsWith(token)
|
|
49
|
+
: token.endsWith('/*')
|
|
50
|
+
? type.startsWith(token.slice(0, -1))
|
|
51
|
+
: type === token
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** A file input that accepts both file-picker and drop selections. */
|
|
56
|
+
export function Dropzone({
|
|
57
|
+
accept,
|
|
58
|
+
multiple,
|
|
59
|
+
maxSize,
|
|
60
|
+
maxFiles,
|
|
61
|
+
chooseLabel = multiple ? 'Choose files or drop them here' : 'Choose a file or drop it here',
|
|
62
|
+
description,
|
|
63
|
+
emptyText,
|
|
64
|
+
rejectionText = rejection => rejection.file.name + ' — ' + reasons[rejection.reason],
|
|
65
|
+
onFilesChange,
|
|
66
|
+
className,
|
|
67
|
+
style,
|
|
68
|
+
parts,
|
|
69
|
+
ref,
|
|
70
|
+
'data-slot': slot,
|
|
71
|
+
'aria-label': ariaLabel,
|
|
72
|
+
'aria-labelledby': labelledBy,
|
|
73
|
+
'aria-describedby': describedBy,
|
|
74
|
+
...props
|
|
75
|
+
}: DropzoneProps) {
|
|
76
|
+
if (maxSize !== undefined && (!Number.isFinite(maxSize) || maxSize < 0)) {
|
|
77
|
+
throw new RangeError('Dropzone maxSize must be a nonnegative number of bytes.')
|
|
78
|
+
}
|
|
79
|
+
if (maxFiles !== undefined && (!Number.isSafeInteger(maxFiles) || maxFiles < 1)) {
|
|
80
|
+
throw new RangeError('Dropzone maxFiles must be a positive integer.')
|
|
81
|
+
}
|
|
82
|
+
const field = useField()
|
|
83
|
+
const input = useRef<HTMLInputElement>(null)
|
|
84
|
+
const inputRef = useComposedRefs(input, ref)
|
|
85
|
+
const labelId = useId()
|
|
86
|
+
const descriptionId = useId()
|
|
87
|
+
const depth = useRef(0)
|
|
88
|
+
const [dragOver, setDragOver] = useState(false)
|
|
89
|
+
const [selection, setSelection] = useState<DropzoneSelection | null>(null)
|
|
90
|
+
useFormReset(input, () => setSelection(null))
|
|
91
|
+
const disabled = props.disabled || field?.disabled
|
|
92
|
+
const tokens = (accept ?? '')
|
|
93
|
+
.toLowerCase()
|
|
94
|
+
.split(',')
|
|
95
|
+
.map(token => token.trim())
|
|
96
|
+
.filter(Boolean)
|
|
97
|
+
|
|
98
|
+
function select(node: HTMLInputElement, files: readonly File[], dropped = false) {
|
|
99
|
+
const accepted: File[] = []
|
|
100
|
+
const rejected: DropzoneRejection[] = []
|
|
101
|
+
for (const file of files) {
|
|
102
|
+
if (!matchesAccept(file, tokens)) rejected.push({ file, reason: 'type' })
|
|
103
|
+
else if (maxSize !== undefined && file.size > maxSize) rejected.push({ file, reason: 'size' })
|
|
104
|
+
else if (accepted.length >= (multiple ? (maxFiles ?? Infinity) : 1))
|
|
105
|
+
rejected.push({ file, reason: 'count' })
|
|
106
|
+
else accepted.push(file)
|
|
107
|
+
}
|
|
108
|
+
if (dropped || rejected.length) {
|
|
109
|
+
const view = node.ownerDocument.defaultView!
|
|
110
|
+
const transfer = new view.DataTransfer()
|
|
111
|
+
for (const file of accepted) transfer.items.add(file)
|
|
112
|
+
node.files = transfer.files
|
|
113
|
+
}
|
|
114
|
+
const result = { accepted, rejected }
|
|
115
|
+
setSelection(result)
|
|
116
|
+
onFilesChange?.(result)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function filesIn(event: DragEvent<HTMLDivElement>) {
|
|
120
|
+
return event.dataTransfer.types.includes('Files') || event.dataTransfer.files.length > 0
|
|
121
|
+
}
|
|
122
|
+
const active = !disabled && dragOver
|
|
123
|
+
const state = active
|
|
124
|
+
? 'drag-over'
|
|
125
|
+
: selection?.rejected.length
|
|
126
|
+
? 'rejected'
|
|
127
|
+
: selection?.accepted.length
|
|
128
|
+
? 'accepted'
|
|
129
|
+
: 'idle'
|
|
130
|
+
|
|
131
|
+
return (
|
|
132
|
+
<div
|
|
133
|
+
data-slot={joinIds('dropzone', slot)}
|
|
134
|
+
data-state={state}
|
|
135
|
+
data-disabled={disabled ? '' : undefined}
|
|
136
|
+
className={cn(
|
|
137
|
+
'relative grid min-w-0 w-full gap-1 rounded-[var(--rad-control-radius,0.5rem)] [corner-shape:var(--rad-control-corner-shape,round)] border-2 border-dashed border-[var(--rad-color-border)] bg-[var(--rad-color-surface)] px-4 py-5 text-center [color:var(--rad-color-foreground)]',
|
|
138
|
+
'has-[input:enabled]:hover:bg-[var(--rad-color-neutral)] data-[state=drag-over]:border-[var(--rad-color-accent)] data-[state=drag-over]:bg-[var(--rad-color-neutral)] data-[state=rejected]:border-[var(--rad-color-invalid)] data-disabled:opacity-60 forced-colors:border-[CanvasText] forced-colors:data-[state=drag-over]:border-[Highlight]',
|
|
139
|
+
focusWithinRingClass,
|
|
140
|
+
className
|
|
141
|
+
)}
|
|
142
|
+
style={style}
|
|
143
|
+
onDragEnter={event => {
|
|
144
|
+
if (!filesIn(event)) return
|
|
145
|
+
event.preventDefault()
|
|
146
|
+
if (input.current?.matches(':enabled')) {
|
|
147
|
+
depth.current++
|
|
148
|
+
setDragOver(true)
|
|
149
|
+
}
|
|
150
|
+
}}
|
|
151
|
+
onDragOver={event => {
|
|
152
|
+
if (!filesIn(event)) return
|
|
153
|
+
event.preventDefault()
|
|
154
|
+
event.dataTransfer.dropEffect = input.current?.matches(':enabled') ? 'copy' : 'none'
|
|
155
|
+
}}
|
|
156
|
+
onDragLeave={() => {
|
|
157
|
+
depth.current = Math.max(0, depth.current - 1)
|
|
158
|
+
if (!depth.current) setDragOver(false)
|
|
159
|
+
}}
|
|
160
|
+
onDrop={event => {
|
|
161
|
+
depth.current = 0
|
|
162
|
+
setDragOver(false)
|
|
163
|
+
if (!filesIn(event)) return
|
|
164
|
+
event.preventDefault()
|
|
165
|
+
const node = input.current
|
|
166
|
+
if (node?.matches(':enabled')) select(node, Array.from(event.dataTransfer.files), true)
|
|
167
|
+
}}
|
|
168
|
+
>
|
|
169
|
+
<FileInput
|
|
170
|
+
{...props}
|
|
171
|
+
ref={inputRef}
|
|
172
|
+
multiple={multiple}
|
|
173
|
+
accept={accept}
|
|
174
|
+
aria-label={ariaLabel}
|
|
175
|
+
aria-labelledby={
|
|
176
|
+
labelledBy ??
|
|
177
|
+
(ariaLabel ? undefined : field?.layout === 'control' ? field.labelId : labelId)
|
|
178
|
+
}
|
|
179
|
+
aria-describedby={joinIds(describedBy, description ? descriptionId : undefined)}
|
|
180
|
+
onChange={event => select(event.currentTarget, Array.from(event.currentTarget.files ?? []))}
|
|
181
|
+
data-slot="dropzone-input"
|
|
182
|
+
className="disabled:cursor-not-allowed"
|
|
183
|
+
parts={{
|
|
184
|
+
wrapper: { className: 'absolute inset-0 z-10' },
|
|
185
|
+
trigger: { className: 'hidden' },
|
|
186
|
+
files: { className: 'hidden' }
|
|
187
|
+
}}
|
|
188
|
+
/>
|
|
189
|
+
<span
|
|
190
|
+
id={labelId}
|
|
191
|
+
data-slot="dropzone-label"
|
|
192
|
+
className={cn('text-sm font-semibold', parts?.label?.className)}
|
|
193
|
+
style={parts?.label?.style}
|
|
194
|
+
>
|
|
195
|
+
{chooseLabel}
|
|
196
|
+
</span>
|
|
197
|
+
{description && (
|
|
198
|
+
<span
|
|
199
|
+
id={descriptionId}
|
|
200
|
+
data-slot="dropzone-description"
|
|
201
|
+
className={cn(
|
|
202
|
+
'text-xs [color:var(--rad-color-muted-foreground)]',
|
|
203
|
+
parts?.description?.className
|
|
204
|
+
)}
|
|
205
|
+
style={parts?.description?.style}
|
|
206
|
+
>
|
|
207
|
+
{description}
|
|
208
|
+
</span>
|
|
209
|
+
)}
|
|
210
|
+
<output
|
|
211
|
+
data-slot="dropzone-status"
|
|
212
|
+
className={cn('grid gap-1 text-xs [overflow-wrap:anywhere]', parts?.status?.className)}
|
|
213
|
+
style={parts?.status?.style}
|
|
214
|
+
>
|
|
215
|
+
{selection?.accepted.map((file, index) => (
|
|
216
|
+
<span key={index}>{file.name}</span>
|
|
217
|
+
))}
|
|
218
|
+
{selection?.rejected.map((rejection, index) => (
|
|
219
|
+
<span
|
|
220
|
+
key={index}
|
|
221
|
+
className="[color:var(--rad-color-invalid)]"
|
|
222
|
+
>
|
|
223
|
+
{rejectionText(rejection)}
|
|
224
|
+
</span>
|
|
225
|
+
))}
|
|
226
|
+
{!selection && emptyText}
|
|
227
|
+
</output>
|
|
228
|
+
</div>
|
|
229
|
+
)
|
|
230
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { useMemo, type ComponentProps, type ReactNode } from 'react'
|
|
4
|
+
import { cn } from 'cn'
|
|
5
|
+
import { joinIds } from '../../internal/ids.js'
|
|
6
|
+
import { supportedLocale } from '../../internal/locale.js'
|
|
7
|
+
import type { PartStyle } from '../../presentation.js'
|
|
8
|
+
import { useLocale } from '../../react/useLocale.js'
|
|
9
|
+
|
|
10
|
+
export type HighlightProps = Omit<ComponentProps<'span'>, 'children' | 'style'> & {
|
|
11
|
+
text: string
|
|
12
|
+
query: string
|
|
13
|
+
caseSensitive?: boolean
|
|
14
|
+
ignoreDiacritics?: boolean
|
|
15
|
+
parts?: { mark?: PartStyle }
|
|
16
|
+
style?: PartStyle['style']
|
|
17
|
+
'data-slot'?: string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function findRanges(
|
|
21
|
+
text: string,
|
|
22
|
+
query: string,
|
|
23
|
+
segmenter: Intl.Segmenter,
|
|
24
|
+
caseSensitive: boolean,
|
|
25
|
+
ignoreDiacritics: boolean
|
|
26
|
+
) {
|
|
27
|
+
const ranges: { start: number; end: number }[] = []
|
|
28
|
+
if (!query || !text) return ranges
|
|
29
|
+
const locale = segmenter.resolvedOptions().locale
|
|
30
|
+
function fold(segment: string) {
|
|
31
|
+
const composed = segment.normalize()
|
|
32
|
+
const cased = caseSensitive
|
|
33
|
+
? composed
|
|
34
|
+
: composed.toLocaleLowerCase(locale).toLocaleUpperCase(locale)
|
|
35
|
+
const decomposed = cased.normalize('NFD')
|
|
36
|
+
return ignoreDiacritics ? decomposed.replace(/\p{Diacritic}/gu, '') : decomposed
|
|
37
|
+
}
|
|
38
|
+
const needle = Array.from(segmenter.segment(query), value => fold(value.segment)).join('')
|
|
39
|
+
if (!needle) return ranges
|
|
40
|
+
|
|
41
|
+
let folded = ''
|
|
42
|
+
const starts = new Map<number, number>()
|
|
43
|
+
const ends = new Map<number, number>()
|
|
44
|
+
for (const { index, segment } of segmenter.segment(text)) {
|
|
45
|
+
const key = fold(segment)
|
|
46
|
+
if (!key) continue
|
|
47
|
+
// Folding can change a grapheme's length. Both boundaries must map to the original text.
|
|
48
|
+
starts.set(folded.length, index)
|
|
49
|
+
folded += key
|
|
50
|
+
ends.set(folded.length, index + segment.length)
|
|
51
|
+
}
|
|
52
|
+
let cursor = 0
|
|
53
|
+
while (cursor < folded.length) {
|
|
54
|
+
const index = folded.indexOf(needle, cursor)
|
|
55
|
+
if (index < 0) break
|
|
56
|
+
const limit = index + needle.length
|
|
57
|
+
const start = starts.get(index)
|
|
58
|
+
const end = ends.get(limit)
|
|
59
|
+
if (start !== undefined && end !== undefined) {
|
|
60
|
+
ranges.push({ start, end })
|
|
61
|
+
cursor = limit
|
|
62
|
+
} else cursor = index + 1
|
|
63
|
+
}
|
|
64
|
+
return ranges
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function Highlight({
|
|
68
|
+
text,
|
|
69
|
+
query,
|
|
70
|
+
caseSensitive = false,
|
|
71
|
+
ignoreDiacritics = false,
|
|
72
|
+
parts,
|
|
73
|
+
...props
|
|
74
|
+
}: HighlightProps) {
|
|
75
|
+
const locale = useLocale()
|
|
76
|
+
const segmenter = useMemo(
|
|
77
|
+
() => new Intl.Segmenter(supportedLocale(Intl.Segmenter, locale)),
|
|
78
|
+
[locale]
|
|
79
|
+
)
|
|
80
|
+
const ranges = useMemo(
|
|
81
|
+
() => findRanges(text, query, segmenter, caseSensitive, ignoreDiacritics),
|
|
82
|
+
[text, query, segmenter, caseSensitive, ignoreDiacritics]
|
|
83
|
+
)
|
|
84
|
+
const content: ReactNode[] = []
|
|
85
|
+
let cursor = 0
|
|
86
|
+
for (const { start, end } of ranges) {
|
|
87
|
+
if (start > cursor) content.push(text.slice(cursor, start))
|
|
88
|
+
content.push(
|
|
89
|
+
<mark
|
|
90
|
+
key={start}
|
|
91
|
+
data-slot="highlight-mark"
|
|
92
|
+
className={cn(
|
|
93
|
+
'rounded-[min(0.125rem,var(--rad-control-radius,0.125rem))] bg-[var(--rad-color-accent)] [color:var(--rad-color-accent-foreground)] [box-decoration-break:clone] forced-colors:bg-[Highlight] forced-colors:text-[HighlightText]',
|
|
94
|
+
parts?.mark?.className
|
|
95
|
+
)}
|
|
96
|
+
style={parts?.mark?.style}
|
|
97
|
+
>
|
|
98
|
+
{text.slice(start, end)}
|
|
99
|
+
</mark>
|
|
100
|
+
)
|
|
101
|
+
cursor = end
|
|
102
|
+
}
|
|
103
|
+
if (cursor < text.length) content.push(text.slice(cursor))
|
|
104
|
+
return (
|
|
105
|
+
<span
|
|
106
|
+
{...props}
|
|
107
|
+
data-slot={joinIds('highlight', props['data-slot'])}
|
|
108
|
+
>
|
|
109
|
+
{content}
|
|
110
|
+
</span>
|
|
111
|
+
)
|
|
112
|
+
}
|
|
@@ -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
|
+
}
|