@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
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
/* oxlint-disable jsx-a11y/prefer-tag-over-role -- A non-modal dialog uses the native Popover top layer. */
|
|
4
|
+
import {
|
|
5
|
+
createContext,
|
|
6
|
+
useContext,
|
|
7
|
+
useLayoutEffect,
|
|
8
|
+
useRef,
|
|
9
|
+
useState,
|
|
10
|
+
type ComponentProps,
|
|
11
|
+
type ReactNode,
|
|
12
|
+
type RefObject
|
|
13
|
+
} from 'react'
|
|
14
|
+
import { cn } from 'cn'
|
|
15
|
+
import { composeAction } from '../../internal/events.js'
|
|
16
|
+
import { joinIds } from '../../internal/ids.js'
|
|
17
|
+
import type { PartStyle } from '../../presentation.js'
|
|
18
|
+
import { useComposedRefs } from '../../react/useComposedRefs.js'
|
|
19
|
+
import { useControllableState } from '../../react/useControllableState.js'
|
|
20
|
+
import type { StateOwner } from '../../react/useNativeOwner.js'
|
|
21
|
+
import { Button } from '../Button/Button.js'
|
|
22
|
+
import {
|
|
23
|
+
Popover,
|
|
24
|
+
PopoverContentRoot,
|
|
25
|
+
usePopoverContext,
|
|
26
|
+
type PopoverAlign,
|
|
27
|
+
type PopoverSide,
|
|
28
|
+
type PopoverTriggerProps
|
|
29
|
+
} from '../Popover/Popover.js'
|
|
30
|
+
|
|
31
|
+
export type TourStep = {
|
|
32
|
+
/** A CSS selector within this Tour. */
|
|
33
|
+
target: string
|
|
34
|
+
title: string
|
|
35
|
+
description?: ReactNode
|
|
36
|
+
side?: PopoverSide
|
|
37
|
+
align?: PopoverAlign
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export type TourProps = Omit<ComponentProps<'div'>, 'style'> &
|
|
41
|
+
StateOwner<'open', boolean> & {
|
|
42
|
+
steps: readonly TourStep[]
|
|
43
|
+
onOpenChange?: (open: boolean) => void
|
|
44
|
+
backLabel?: string
|
|
45
|
+
nextLabel?: string
|
|
46
|
+
skipLabel?: string
|
|
47
|
+
doneLabel?: string
|
|
48
|
+
formatProgress?: (step: number, total: number) => string
|
|
49
|
+
motion?: 'inherit' | 'off'
|
|
50
|
+
style?: PartStyle['style']
|
|
51
|
+
parts?: { popup?: PartStyle }
|
|
52
|
+
'data-slot'?: string
|
|
53
|
+
}
|
|
54
|
+
export type TourTriggerProps = PopoverTriggerProps
|
|
55
|
+
|
|
56
|
+
const TourContext = createContext<{
|
|
57
|
+
open: boolean
|
|
58
|
+
launch: (opener: HTMLElement) => void
|
|
59
|
+
} | null>(null)
|
|
60
|
+
|
|
61
|
+
type ReadyStep = { index: number; node: HTMLElement }
|
|
62
|
+
|
|
63
|
+
export function Tour({
|
|
64
|
+
steps,
|
|
65
|
+
open,
|
|
66
|
+
defaultOpen,
|
|
67
|
+
onOpenChange,
|
|
68
|
+
backLabel = 'Back',
|
|
69
|
+
nextLabel = 'Next',
|
|
70
|
+
skipLabel = 'Skip tour',
|
|
71
|
+
doneLabel = 'Done',
|
|
72
|
+
formatProgress = (step, total) => `Step ${step} of ${total}`,
|
|
73
|
+
motion,
|
|
74
|
+
parts,
|
|
75
|
+
className,
|
|
76
|
+
children,
|
|
77
|
+
ref,
|
|
78
|
+
...props
|
|
79
|
+
}: TourProps) {
|
|
80
|
+
const state = useControllableState({
|
|
81
|
+
name: 'Tour',
|
|
82
|
+
value: open,
|
|
83
|
+
defaultValue: defaultOpen,
|
|
84
|
+
initialValue: false,
|
|
85
|
+
onChange: onOpenChange
|
|
86
|
+
})
|
|
87
|
+
const host = useRef<HTMLDivElement>(null)
|
|
88
|
+
const hostRef = useComposedRefs(host, ref)
|
|
89
|
+
const opener = useRef<HTMLElement | null>(null)
|
|
90
|
+
const [requested, setRequested] = useState(0)
|
|
91
|
+
const [ready, setReady] = useState<ReadyStep[] | null>(null)
|
|
92
|
+
const notifiedEmpty = useRef(false)
|
|
93
|
+
const isOpen = state.value
|
|
94
|
+
const [wasOpen, setWasOpen] = useState(isOpen)
|
|
95
|
+
if (wasOpen !== isOpen) {
|
|
96
|
+
setWasOpen(isOpen)
|
|
97
|
+
if (isOpen) {
|
|
98
|
+
setReady(null)
|
|
99
|
+
setRequested(0)
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
useLayoutEffect(() => {
|
|
104
|
+
if (!isOpen) {
|
|
105
|
+
opener.current = null
|
|
106
|
+
notifiedEmpty.current = false
|
|
107
|
+
return
|
|
108
|
+
}
|
|
109
|
+
const root = host.current!
|
|
110
|
+
const doc = root.ownerDocument
|
|
111
|
+
const win = doc.defaultView!
|
|
112
|
+
if (!opener.current && doc.activeElement instanceof win.HTMLElement) {
|
|
113
|
+
opener.current = doc.activeElement
|
|
114
|
+
}
|
|
115
|
+
let frame = 0
|
|
116
|
+
const refresh = () => {
|
|
117
|
+
frame = 0
|
|
118
|
+
const matches = steps.flatMap(({ target }, index) => {
|
|
119
|
+
let node: Element | null
|
|
120
|
+
try {
|
|
121
|
+
node = root.querySelector(target)
|
|
122
|
+
} catch {
|
|
123
|
+
return []
|
|
124
|
+
}
|
|
125
|
+
return node instanceof win.HTMLElement &&
|
|
126
|
+
node.checkVisibility({ checkVisibilityCSS: true, checkOpacity: true })
|
|
127
|
+
? [{ index, node }]
|
|
128
|
+
: []
|
|
129
|
+
})
|
|
130
|
+
setReady(current =>
|
|
131
|
+
current?.length === matches.length &&
|
|
132
|
+
current.every(
|
|
133
|
+
(entry, index) =>
|
|
134
|
+
entry.index === matches[index].index && entry.node === matches[index].node
|
|
135
|
+
)
|
|
136
|
+
? current
|
|
137
|
+
: matches
|
|
138
|
+
)
|
|
139
|
+
}
|
|
140
|
+
const schedule = () => {
|
|
141
|
+
if (!frame) frame = win.requestAnimationFrame(refresh)
|
|
142
|
+
}
|
|
143
|
+
const observer = new win.MutationObserver(schedule)
|
|
144
|
+
observer.observe(root, {
|
|
145
|
+
subtree: true,
|
|
146
|
+
childList: true,
|
|
147
|
+
attributes: true,
|
|
148
|
+
attributeFilter: ['class', 'id', 'style', 'hidden', 'open', 'data-state']
|
|
149
|
+
})
|
|
150
|
+
root.addEventListener('toggle', schedule, true)
|
|
151
|
+
win.addEventListener('resize', schedule)
|
|
152
|
+
schedule()
|
|
153
|
+
return () => {
|
|
154
|
+
observer.disconnect()
|
|
155
|
+
root.removeEventListener('toggle', schedule, true)
|
|
156
|
+
win.removeEventListener('resize', schedule)
|
|
157
|
+
win.cancelAnimationFrame(frame)
|
|
158
|
+
}
|
|
159
|
+
}, [isOpen, steps])
|
|
160
|
+
|
|
161
|
+
const firstAvailable = ready?.findIndex(item => item.index >= requested) ?? -1
|
|
162
|
+
const position = firstAvailable === -1 ? (ready?.length ?? 0) - 1 : firstAvailable
|
|
163
|
+
const current = ready?.[position]
|
|
164
|
+
const step = current && steps[current.index]
|
|
165
|
+
const empty = ready?.length === 0
|
|
166
|
+
useLayoutEffect(() => {
|
|
167
|
+
if (isOpen && empty && !notifiedEmpty.current) {
|
|
168
|
+
notifiedEmpty.current = true
|
|
169
|
+
state.request(false)
|
|
170
|
+
} else if (!empty) notifiedEmpty.current = false
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
return (
|
|
174
|
+
<TourContext.Provider
|
|
175
|
+
value={{
|
|
176
|
+
open: isOpen && !!step,
|
|
177
|
+
launch: node => {
|
|
178
|
+
if (isOpen) return
|
|
179
|
+
opener.current = node
|
|
180
|
+
state.request(true)
|
|
181
|
+
}
|
|
182
|
+
}}
|
|
183
|
+
>
|
|
184
|
+
<Popover
|
|
185
|
+
open={isOpen && !!step}
|
|
186
|
+
onOpenChange={state.request}
|
|
187
|
+
>
|
|
188
|
+
<div
|
|
189
|
+
{...props}
|
|
190
|
+
ref={hostRef}
|
|
191
|
+
data-slot={joinIds('tour', props['data-slot'])}
|
|
192
|
+
className={cn(
|
|
193
|
+
'min-w-0 [&_[data-rad-tour-target]]:[outline:2px_solid_var(--rad-color-focus)] [&_[data-rad-tour-target]]:outline-offset-4 forced-colors:[&_[data-rad-tour-target]]:outline-[Highlight]',
|
|
194
|
+
className
|
|
195
|
+
)}
|
|
196
|
+
>
|
|
197
|
+
{children}
|
|
198
|
+
<TourSurface
|
|
199
|
+
anchor={isOpen && step ? current.node : null}
|
|
200
|
+
step={step}
|
|
201
|
+
stepIndex={current?.index}
|
|
202
|
+
opener={opener}
|
|
203
|
+
motion={motion}
|
|
204
|
+
part={parts?.popup}
|
|
205
|
+
>
|
|
206
|
+
{step && (
|
|
207
|
+
<>
|
|
208
|
+
<p
|
|
209
|
+
data-slot="tour-progress"
|
|
210
|
+
className="text-xs [color:var(--rad-color-muted-foreground)]"
|
|
211
|
+
>
|
|
212
|
+
{formatProgress(position + 1, ready!.length)}
|
|
213
|
+
</p>
|
|
214
|
+
<p className="text-sm font-semibold">{step.title}</p>
|
|
215
|
+
{step.description && <div className="text-sm">{step.description}</div>}
|
|
216
|
+
<div
|
|
217
|
+
className="flex flex-wrap gap-2"
|
|
218
|
+
data-slot="tour-actions"
|
|
219
|
+
>
|
|
220
|
+
<Button
|
|
221
|
+
controlSize="small"
|
|
222
|
+
variant="ghost"
|
|
223
|
+
onClick={() => state.request(false)}
|
|
224
|
+
>
|
|
225
|
+
{skipLabel}
|
|
226
|
+
</Button>
|
|
227
|
+
{position > 0 && (
|
|
228
|
+
<Button
|
|
229
|
+
controlSize="small"
|
|
230
|
+
variant="outline"
|
|
231
|
+
onClick={() => setRequested(ready![position - 1].index)}
|
|
232
|
+
>
|
|
233
|
+
{backLabel}
|
|
234
|
+
</Button>
|
|
235
|
+
)}
|
|
236
|
+
<Button
|
|
237
|
+
controlSize="small"
|
|
238
|
+
onClick={() => {
|
|
239
|
+
const next = ready![position + 1]
|
|
240
|
+
if (next) setRequested(next.index)
|
|
241
|
+
else state.request(false)
|
|
242
|
+
}}
|
|
243
|
+
>
|
|
244
|
+
{position === ready!.length - 1 ? doneLabel : nextLabel}
|
|
245
|
+
</Button>
|
|
246
|
+
</div>
|
|
247
|
+
</>
|
|
248
|
+
)}
|
|
249
|
+
</TourSurface>
|
|
250
|
+
</div>
|
|
251
|
+
</Popover>
|
|
252
|
+
</TourContext.Provider>
|
|
253
|
+
)
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function TourSurface({
|
|
257
|
+
anchor,
|
|
258
|
+
step,
|
|
259
|
+
stepIndex,
|
|
260
|
+
opener,
|
|
261
|
+
motion,
|
|
262
|
+
part,
|
|
263
|
+
children
|
|
264
|
+
}: {
|
|
265
|
+
anchor: HTMLElement | null
|
|
266
|
+
step?: TourStep
|
|
267
|
+
stepIndex?: number
|
|
268
|
+
opener: RefObject<HTMLElement | null>
|
|
269
|
+
motion?: TourProps['motion']
|
|
270
|
+
part?: PartStyle
|
|
271
|
+
children: ReactNode
|
|
272
|
+
}) {
|
|
273
|
+
const { setTrigger } = usePopoverContext()
|
|
274
|
+
const popup = useRef<HTMLDivElement>(null)
|
|
275
|
+
const description = useRef<HTMLDivElement>(null)
|
|
276
|
+
useLayoutEffect(() => {
|
|
277
|
+
setTrigger(anchor)
|
|
278
|
+
if (!anchor) return
|
|
279
|
+
anchor.scrollIntoView({ block: 'nearest', inline: 'nearest', behavior: 'instant' })
|
|
280
|
+
anchor.setAttribute('data-rad-tour-target', '')
|
|
281
|
+
return () => anchor.removeAttribute('data-rad-tour-target')
|
|
282
|
+
}, [anchor, setTrigger])
|
|
283
|
+
useLayoutEffect(() => {
|
|
284
|
+
if (anchor && popup.current?.matches(':popover-open')) {
|
|
285
|
+
description.current?.focus({ preventScroll: true })
|
|
286
|
+
}
|
|
287
|
+
}, [anchor, stepIndex])
|
|
288
|
+
|
|
289
|
+
return (
|
|
290
|
+
<PopoverContentRoot
|
|
291
|
+
ref={popup}
|
|
292
|
+
data-slot="tour-popup"
|
|
293
|
+
role="dialog"
|
|
294
|
+
aria-label={step?.title}
|
|
295
|
+
side={step?.side}
|
|
296
|
+
align={step?.align}
|
|
297
|
+
motion={motion}
|
|
298
|
+
initialFocusRef={description}
|
|
299
|
+
returnFocusRef={opener}
|
|
300
|
+
className={cn('w-max max-w-[min(19rem,var(--_rad-popover-max-width))]', part?.className)}
|
|
301
|
+
style={part?.style}
|
|
302
|
+
>
|
|
303
|
+
<div
|
|
304
|
+
ref={description}
|
|
305
|
+
tabIndex={-1}
|
|
306
|
+
className="grid gap-2 focus:outline-none"
|
|
307
|
+
>
|
|
308
|
+
{children}
|
|
309
|
+
</div>
|
|
310
|
+
</PopoverContentRoot>
|
|
311
|
+
)
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
export function TourTrigger({ onClick, ...props }: TourTriggerProps) {
|
|
315
|
+
const tour = useContext(TourContext)
|
|
316
|
+
const { contentId } = usePopoverContext()
|
|
317
|
+
if (!tour) throw new Error('TourTrigger must render inside Tour.')
|
|
318
|
+
return (
|
|
319
|
+
<Button
|
|
320
|
+
{...props}
|
|
321
|
+
type="button"
|
|
322
|
+
pressed={undefined}
|
|
323
|
+
defaultPressed={undefined}
|
|
324
|
+
onPressedChange={undefined}
|
|
325
|
+
aria-pressed={undefined}
|
|
326
|
+
popoverTarget={undefined}
|
|
327
|
+
popoverTargetAction={undefined}
|
|
328
|
+
data-slot={joinIds('tour-trigger', props['data-slot'])}
|
|
329
|
+
aria-controls={contentId}
|
|
330
|
+
aria-expanded={tour.open}
|
|
331
|
+
aria-haspopup="dialog"
|
|
332
|
+
onClick={composeAction(onClick, event => tour.launch(event.currentTarget))}
|
|
333
|
+
/>
|
|
334
|
+
)
|
|
335
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -868,3 +868,18 @@ export {
|
|
|
868
868
|
export type { ColorFormat } from './color/colorValue.js'
|
|
869
869
|
|
|
870
870
|
export { ColorInput, type ColorInputProps } from './components/ColorInput/ColorInput.js'
|
|
871
|
+
|
|
872
|
+
export {
|
|
873
|
+
Dropzone,
|
|
874
|
+
type DropzoneProps,
|
|
875
|
+
type DropzoneSelection,
|
|
876
|
+
type DropzoneRejection
|
|
877
|
+
} from './components/Dropzone/Dropzone.js'
|
|
878
|
+
export { Tour, TourTrigger } from './components/Tour/Tour.js'
|
|
879
|
+
export type { TourProps, TourStep, TourTriggerProps } from './components/Tour/Tour.js'
|
|
880
|
+
export { CopyButton } from './components/CopyButton/CopyButton.js'
|
|
881
|
+
export type { CopyButtonProps, CopyButtonRenderProps } from './components/CopyButton/CopyButton.js'
|
|
882
|
+
export { Spoiler } from './components/Spoiler/Spoiler.js'
|
|
883
|
+
export type { SpoilerProps } from './components/Spoiler/Spoiler.js'
|
|
884
|
+
export { Highlight } from './components/Highlight/Highlight.js'
|
|
885
|
+
export type { HighlightProps } from './components/Highlight/Highlight.js'
|
|
@@ -13,9 +13,10 @@ export function bindDisclosureMotion(host: HTMLElement, initial: MotionProfile):
|
|
|
13
13
|
)
|
|
14
14
|
const measure = () =>
|
|
15
15
|
contents().reduce((sum, child) => sum + child.getBoundingClientRect().height, 0)
|
|
16
|
+
const preview = () => Number(node.getAttribute('data-rad-disclosure-preview') ?? 0)
|
|
16
17
|
const previous = node.style.getPropertyValue('--_rad-spring-details-height')
|
|
17
18
|
const spring = createSpring(
|
|
18
|
-
node.open ? measure() :
|
|
19
|
+
node.open ? measure() : preview(),
|
|
19
20
|
resolveMotion(profile, 'disclosure', query.matches)
|
|
20
21
|
)
|
|
21
22
|
const saved = new Map<HTMLElement, { inert: boolean; hidden: string | null }>()
|
|
@@ -44,7 +45,7 @@ export function bindDisclosureMotion(host: HTMLElement, initial: MotionProfile):
|
|
|
44
45
|
child.setAttribute('aria-hidden', 'true')
|
|
45
46
|
} else restore(child)
|
|
46
47
|
}
|
|
47
|
-
spring.to(node.open ? measure() :
|
|
48
|
+
spring.to(node.open ? measure() : preview())
|
|
48
49
|
}
|
|
49
50
|
function restore(child: HTMLElement) {
|
|
50
51
|
const prior = saved.get(child)
|
|
@@ -55,7 +56,11 @@ export function bindDisclosureMotion(host: HTMLElement, initial: MotionProfile):
|
|
|
55
56
|
saved.delete(child)
|
|
56
57
|
}
|
|
57
58
|
const observer = new MutationObserver(sync)
|
|
58
|
-
observer.observe(node, {
|
|
59
|
+
observer.observe(node, {
|
|
60
|
+
attributes: true,
|
|
61
|
+
attributeFilter: ['open', 'data-rad-disclosure-preview'],
|
|
62
|
+
childList: true
|
|
63
|
+
})
|
|
59
64
|
const resize = new ResizeObserver(sync)
|
|
60
65
|
function update(next: MotionProfile) {
|
|
61
66
|
profile = next
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { useEffect, useRef, useState } from 'react'
|
|
4
|
+
|
|
5
|
+
type Phase = 'idle' | 'pending' | 'success' | 'error'
|
|
6
|
+
type Feedback = { source: string; phase: Phase }
|
|
7
|
+
|
|
8
|
+
export function useClipboardCopy(source: string, resetDelay?: number) {
|
|
9
|
+
const [feedback, setFeedback] = useState<Feedback>({ source, phase: 'idle' })
|
|
10
|
+
const [pending, setPending] = useState(false)
|
|
11
|
+
const busy = useRef(false)
|
|
12
|
+
const operation = useRef(0)
|
|
13
|
+
const cancelReset = useRef<() => void>(() => {})
|
|
14
|
+
// Forget feedback for replaced source while an already requested browser write completes.
|
|
15
|
+
if (feedback.source !== source) setFeedback({ source, phase: 'idle' })
|
|
16
|
+
|
|
17
|
+
useEffect(
|
|
18
|
+
() => () => {
|
|
19
|
+
operation.current += 1
|
|
20
|
+
busy.current = false
|
|
21
|
+
cancelReset.current()
|
|
22
|
+
},
|
|
23
|
+
[]
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
async function copy(win: Window | null) {
|
|
27
|
+
if (busy.current) return
|
|
28
|
+
const request = ++operation.current
|
|
29
|
+
busy.current = true
|
|
30
|
+
cancelReset.current()
|
|
31
|
+
setPending(true)
|
|
32
|
+
setFeedback({ source, phase: 'pending' })
|
|
33
|
+
let phase: Phase = 'success'
|
|
34
|
+
try {
|
|
35
|
+
if (!win?.navigator.clipboard?.writeText) throw new Error('Clipboard writing is unavailable.')
|
|
36
|
+
await win.navigator.clipboard.writeText(source)
|
|
37
|
+
} catch {
|
|
38
|
+
phase = 'error'
|
|
39
|
+
}
|
|
40
|
+
if (request !== operation.current) return
|
|
41
|
+
busy.current = false
|
|
42
|
+
setPending(false)
|
|
43
|
+
const result: Feedback = { source, phase }
|
|
44
|
+
setFeedback(current => (current.source === source ? result : current))
|
|
45
|
+
if (phase === 'success' && win && resetDelay !== undefined) {
|
|
46
|
+
const id = win.setTimeout(() => {
|
|
47
|
+
setFeedback(current => (current === result ? { source, phase: 'idle' } : current))
|
|
48
|
+
}, resetDelay)
|
|
49
|
+
cancelReset.current = () => win.clearTimeout(id)
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return { copy, pending, phase: feedback.source === source ? feedback.phase : 'idle' }
|
|
54
|
+
}
|
|
@@ -229,8 +229,9 @@ export function usePopoverSurface<Node extends HTMLElement>(
|
|
|
229
229
|
win.visualViewport?.removeEventListener('scroll', schedule)
|
|
230
230
|
stack.splice(stack.indexOf(node), 1)
|
|
231
231
|
if (!stack.length) layers.delete(doc)
|
|
232
|
-
|
|
233
|
-
|
|
232
|
+
const returnTarget = focusTarget()
|
|
233
|
+
if (shouldRestore() && node.contains(doc.activeElement) && returnTarget.isConnected)
|
|
234
|
+
returnTarget.focus({ preventScroll: true })
|
|
234
235
|
if (node.matches(':popover-open')) node.hidePopover()
|
|
235
236
|
reposition.current = () => {}
|
|
236
237
|
}
|