@charcuterie/ui 4.27.3 → 4.28.1
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/dist/Accordion/AccordionSection.d.ts.map +1 -1
- package/dist/Accordion/AccordionSection.js +43 -4
- package/dist/Accordion/AccordionSection.js.map +1 -1
- package/dist/Combobox/Combobox.d.ts.map +1 -1
- package/dist/Combobox/Combobox.js +3 -3
- package/dist/Combobox/Combobox.js.map +1 -1
- package/dist/DatePicker/DatePicker.d.ts.map +1 -1
- package/dist/DatePicker/DatePicker.js +3 -3
- package/dist/DatePicker/DatePicker.js.map +1 -1
- package/dist/Dialog/Dialog.d.ts +7 -1
- package/dist/Dialog/Dialog.d.ts.map +1 -1
- package/dist/Dialog/Dialog.js +2 -2
- package/dist/Dialog/Dialog.js.map +1 -1
- package/dist/GroupedList/GroupedList.d.ts +9 -2
- package/dist/GroupedList/GroupedList.d.ts.map +1 -1
- package/dist/GroupedList/GroupedList.js +39 -5
- package/dist/GroupedList/GroupedList.js.map +1 -1
- package/dist/GroupedList/GroupedListRow.d.ts +4 -1
- package/dist/GroupedList/GroupedListRow.d.ts.map +1 -1
- package/dist/GroupedList/GroupedListRow.js +3 -3
- package/dist/GroupedList/GroupedListRow.js.map +1 -1
- package/dist/GroupedList/GroupedListRows.d.ts +16 -0
- package/dist/GroupedList/GroupedListRows.d.ts.map +1 -0
- package/dist/GroupedList/GroupedListRows.js +17 -0
- package/dist/GroupedList/GroupedListRows.js.map +1 -0
- package/dist/Listbox/Listbox.d.ts.map +1 -1
- package/dist/Listbox/Listbox.js +6 -4
- package/dist/Listbox/Listbox.js.map +1 -1
- package/dist/Menu/Menu.d.ts.map +1 -1
- package/dist/Menu/Menu.js +6 -4
- package/dist/Menu/Menu.js.map +1 -1
- package/dist/Modal/Modal.d.ts +7 -1
- package/dist/Modal/Modal.d.ts.map +1 -1
- package/dist/Modal/Modal.js +1 -1
- package/dist/Modal/Modal.js.map +1 -1
- package/dist/Motion/useLastPresentValue.d.ts +21 -0
- package/dist/Motion/useLastPresentValue.d.ts.map +1 -0
- package/dist/Motion/useLastPresentValue.js +30 -0
- package/dist/Motion/useLastPresentValue.js.map +1 -0
- package/dist/Motion/useListMotion.d.ts +111 -0
- package/dist/Motion/useListMotion.d.ts.map +1 -0
- package/dist/Motion/useListMotion.js +358 -0
- package/dist/Motion/useListMotion.js.map +1 -0
- package/dist/Motion/usePresence.d.ts +81 -0
- package/dist/Motion/usePresence.d.ts.map +1 -0
- package/dist/Motion/usePresence.js +131 -0
- package/dist/Motion/usePresence.js.map +1 -0
- package/dist/Motion/useValueChangeMotion.d.ts +52 -0
- package/dist/Motion/useValueChangeMotion.d.ts.map +1 -0
- package/dist/Motion/useValueChangeMotion.js +87 -0
- package/dist/Motion/useValueChangeMotion.js.map +1 -0
- package/dist/Overlay/OverlayPanel.d.ts +18 -1
- package/dist/Overlay/OverlayPanel.d.ts.map +1 -1
- package/dist/Overlay/OverlayPanel.js +83 -11
- package/dist/Overlay/OverlayPanel.js.map +1 -1
- package/dist/Overlay/SharedBackdrop.d.ts +3 -0
- package/dist/Overlay/SharedBackdrop.d.ts.map +1 -1
- package/dist/Overlay/SharedBackdrop.js +8 -2
- package/dist/Overlay/SharedBackdrop.js.map +1 -1
- package/dist/Overlay/focusHistory.d.ts +16 -0
- package/dist/Overlay/focusHistory.d.ts.map +1 -0
- package/dist/Overlay/focusHistory.js +110 -0
- package/dist/Overlay/focusHistory.js.map +1 -0
- package/dist/Overlay/useAnchoredOverlay.d.ts +17 -3
- package/dist/Overlay/useAnchoredOverlay.d.ts.map +1 -1
- package/dist/Overlay/useAnchoredOverlay.js +23 -5
- package/dist/Overlay/useAnchoredOverlay.js.map +1 -1
- package/dist/Popover/Popover.d.ts.map +1 -1
- package/dist/Popover/Popover.js +4 -2
- package/dist/Popover/Popover.js.map +1 -1
- package/dist/ReorderList/ReorderList.d.ts +6 -0
- package/dist/ReorderList/ReorderList.d.ts.map +1 -1
- package/dist/ReorderList/ReorderList.js +23 -6
- package/dist/ReorderList/ReorderList.js.map +1 -1
- package/dist/TableViewport/TableViewport.d.ts.map +1 -1
- package/dist/TableViewport/TableViewport.js +9 -1
- package/dist/TableViewport/TableViewport.js.map +1 -1
- package/dist/Tooltip/Tooltip.d.ts.map +1 -1
- package/dist/Tooltip/Tooltip.js +16 -13
- package/dist/Tooltip/Tooltip.js.map +1 -1
- package/dist/index.d.ts +22 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -1
- package/dist/styles.css +191 -1
- package/package.json +6 -6
- package/src/Accordion/AccordionSection.tsx +63 -4
- package/src/Combobox/Combobox.tsx +8 -2
- package/src/DatePicker/DatePicker.tsx +7 -1
- package/src/Dialog/Dialog.tsx +8 -0
- package/src/GroupedList/GroupedList.mdx +3 -2
- package/src/GroupedList/GroupedList.test.tsx +283 -0
- package/src/GroupedList/GroupedList.tsx +88 -44
- package/src/GroupedList/GroupedListRow.tsx +8 -0
- package/src/GroupedList/GroupedListRows.tsx +51 -0
- package/src/Listbox/Listbox.tsx +9 -1
- package/src/Menu/Menu.tsx +9 -1
- package/src/Modal/Modal.tsx +8 -0
- package/src/Motion/useLastPresentValue.test.tsx +66 -0
- package/src/Motion/useLastPresentValue.ts +38 -0
- package/src/Motion/useListMotion.test.tsx +527 -0
- package/src/Motion/useListMotion.ts +674 -0
- package/src/Motion/usePresence.test.tsx +223 -0
- package/src/Motion/usePresence.ts +252 -0
- package/src/Motion/useValueChangeMotion.test.tsx +131 -0
- package/src/Motion/useValueChangeMotion.ts +110 -0
- package/src/Overlay/OverlayPanel.server.test.ts +30 -0
- package/src/Overlay/OverlayPanel.tsx +146 -14
- package/src/Overlay/SharedBackdrop.tsx +17 -2
- package/src/Overlay/focusHistory.ts +152 -0
- package/src/Overlay/overlayReturnFocus.storyHelpers.tsx +242 -0
- package/src/Overlay/overlayReturnFocus.test.tsx +255 -0
- package/src/Overlay/useAnchoredOverlay.ts +56 -4
- package/src/Popover/Popover.tsx +9 -1
- package/src/ReorderList/ReorderList.test.tsx +76 -0
- package/src/ReorderList/ReorderList.tsx +86 -48
- package/src/TableViewport/TableViewport.tsx +4 -0
- package/src/Tooltip/Tooltip.tsx +32 -4
- package/src/index.ts +30 -0
- package/src/styles.css +191 -1
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { useUniqueId } from "@charcuterie/logic"
|
|
2
2
|
import type { IntentName } from "@charcuterie/tokens"
|
|
3
3
|
import type { ReactNode } from "react"
|
|
4
|
+
import { useCallback, useState } from "react"
|
|
4
5
|
|
|
6
|
+
import { useListMotion } from "../Motion/useListMotion.ts"
|
|
5
7
|
import { toClassName } from "../toClassName.ts"
|
|
6
|
-
import {
|
|
8
|
+
import { GroupedListRows } from "./GroupedListRows.tsx"
|
|
7
9
|
import { STICKY_HEADING_CLASS } from "./groupedListStickyHeading.ts"
|
|
8
10
|
|
|
9
11
|
export type GroupedListHeadingLevel = 2 | 3 | 4 | 5 | 6
|
|
@@ -58,13 +60,20 @@ export type GroupedListItem = {
|
|
|
58
60
|
* it lasts, usually by clearing it on a timer.
|
|
59
61
|
*
|
|
60
62
|
* **To pulse a row that is already highlighted again, change its
|
|
61
|
-
* `
|
|
62
|
-
*
|
|
63
|
+
* `highlightKey`**, not its `key`. Rows enter, leave, and slide
|
|
64
|
+
* with motion, so a new `key` is a new row: the old one would leave
|
|
65
|
+
* while a copy of it entered. A new `highlightKey` starts only the
|
|
66
|
+
* ring's pulse from the beginning; a scan count is the usual value.
|
|
63
67
|
*
|
|
64
68
|
* Under `prefers-reduced-motion` and on ePaper the ring does not
|
|
65
69
|
* pulse; it is simply there.
|
|
66
70
|
*/
|
|
67
71
|
isHighlighted?: boolean
|
|
72
|
+
/**
|
|
73
|
+
* Changing it restarts the highlight ring's pulse. See
|
|
74
|
+
* `isHighlighted`.
|
|
75
|
+
*/
|
|
76
|
+
highlightKey?: number | string
|
|
68
77
|
/**
|
|
69
78
|
* `true` draws the row receded — a sunken surface and a quieter
|
|
70
79
|
* label — for an entry that exists but is switched off. It stays
|
|
@@ -245,6 +254,35 @@ export const GroupedList = ({
|
|
|
245
254
|
(group) => group.items.length > 0,
|
|
246
255
|
)
|
|
247
256
|
|
|
257
|
+
// Groups move like their rows do. A group an action creates (the
|
|
258
|
+
// first scan of a day) enters with its first row, and a group an
|
|
259
|
+
// action empties leaves with its last row, instead of either one
|
|
260
|
+
// appearing or vanishing in a frame.
|
|
261
|
+
//
|
|
262
|
+
// The row keys are the layout key: a row added to or removed from
|
|
263
|
+
// one group changes its height, and the groups below it slide to
|
|
264
|
+
// their new places with it.
|
|
265
|
+
//
|
|
266
|
+
// A row that finishes leaving closes its gap one render after its
|
|
267
|
+
// key went away, so the rows report it, and that report is part of
|
|
268
|
+
// the layout key too.
|
|
269
|
+
const [rowExitCount, setRowExitCount] = useState(0)
|
|
270
|
+
|
|
271
|
+
const onRowExitEnd = useCallback(() => {
|
|
272
|
+
setRowExitCount((count) => count + 1)
|
|
273
|
+
}, [])
|
|
274
|
+
|
|
275
|
+
const { entries: groupEntries } = useListMotion({
|
|
276
|
+
getKey: (group: GroupedListGroup) => group.key,
|
|
277
|
+
items: shownGroups,
|
|
278
|
+
layoutKey: JSON.stringify([
|
|
279
|
+
shownGroups.map((group) =>
|
|
280
|
+
group.items.map((item) => item.key),
|
|
281
|
+
),
|
|
282
|
+
rowExitCount,
|
|
283
|
+
]),
|
|
284
|
+
})
|
|
285
|
+
|
|
248
286
|
const Heading = HEADING_TAG[headingLevel]
|
|
249
287
|
|
|
250
288
|
return (
|
|
@@ -255,58 +293,64 @@ export const GroupedList = ({
|
|
|
255
293
|
className,
|
|
256
294
|
)}
|
|
257
295
|
>
|
|
258
|
-
{
|
|
296
|
+
{groupEntries.length === 0
|
|
259
297
|
? (emptyState ?? null)
|
|
260
|
-
:
|
|
261
|
-
|
|
298
|
+
: groupEntries.map(
|
|
299
|
+
(
|
|
300
|
+
{ item: group, key, motionProps },
|
|
301
|
+
position,
|
|
302
|
+
) => {
|
|
303
|
+
// By place in what is drawn, so a leaving group and the
|
|
304
|
+
// group now at its old index never share an id.
|
|
305
|
+
const headingId = `${baseId}-heading-${position}`
|
|
262
306
|
|
|
263
|
-
|
|
264
|
-
<div
|
|
265
|
-
className={toClassName(
|
|
266
|
-
"flex flex-col",
|
|
267
|
-
isHeadingSticky ? "gap-0" : "gap-2",
|
|
268
|
-
)}
|
|
269
|
-
key={group.key}
|
|
270
|
-
>
|
|
307
|
+
return (
|
|
271
308
|
<div
|
|
309
|
+
{...motionProps}
|
|
272
310
|
className={toClassName(
|
|
273
|
-
"flex flex-
|
|
274
|
-
isHeadingSticky
|
|
311
|
+
"flex flex-col",
|
|
312
|
+
isHeadingSticky ? "gap-0" : "gap-2",
|
|
313
|
+
motionProps.className,
|
|
275
314
|
)}
|
|
315
|
+
key={key}
|
|
276
316
|
>
|
|
277
|
-
<
|
|
317
|
+
<div
|
|
278
318
|
className={toClassName(
|
|
279
|
-
"
|
|
280
|
-
|
|
319
|
+
"flex flex-wrap items-baseline gap-x-3 gap-y-0.5",
|
|
320
|
+
isHeadingSticky &&
|
|
321
|
+
STICKY_HEADING_CLASS,
|
|
281
322
|
)}
|
|
282
|
-
id={headingId}
|
|
283
323
|
>
|
|
284
|
-
|
|
285
|
-
|
|
324
|
+
<Heading
|
|
325
|
+
className={toClassName(
|
|
326
|
+
"m-0 min-w-0 wrap-anywhere font-semibold text-content-primary",
|
|
327
|
+
size === "sm"
|
|
328
|
+
? "text-sm"
|
|
329
|
+
: "text-md",
|
|
330
|
+
)}
|
|
331
|
+
id={headingId}
|
|
332
|
+
>
|
|
333
|
+
{group.heading}
|
|
334
|
+
</Heading>
|
|
286
335
|
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
336
|
+
{group.summary ? (
|
|
337
|
+
<span className="min-w-0 wrap-anywhere text-content-secondary text-sm">
|
|
338
|
+
{group.summary}
|
|
339
|
+
</span>
|
|
340
|
+
) : null}
|
|
341
|
+
</div>
|
|
293
342
|
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
))}
|
|
306
|
-
</ul>
|
|
307
|
-
</div>
|
|
308
|
-
)
|
|
309
|
-
})}
|
|
343
|
+
<GroupedListRows
|
|
344
|
+
headingId={headingId}
|
|
345
|
+
onExitEnd={onRowExitEnd}
|
|
346
|
+
isTextTruncated={isTextTruncated}
|
|
347
|
+
items={group.items}
|
|
348
|
+
size={size}
|
|
349
|
+
/>
|
|
350
|
+
</div>
|
|
351
|
+
)
|
|
352
|
+
},
|
|
353
|
+
)}
|
|
310
354
|
</div>
|
|
311
355
|
)
|
|
312
356
|
}
|
|
@@ -2,6 +2,7 @@ import type { IntentName } from "@charcuterie/tokens"
|
|
|
2
2
|
import type { ReactNode } from "react"
|
|
3
3
|
|
|
4
4
|
import { useIsTextClipped } from "../Badge/useIsTextClipped.ts"
|
|
5
|
+
import type { ListMotionProps } from "../Motion/useListMotion.ts"
|
|
5
6
|
import { toClassName } from "../toClassName.ts"
|
|
6
7
|
import type {
|
|
7
8
|
GroupedListItem,
|
|
@@ -11,6 +12,8 @@ import type {
|
|
|
11
12
|
export type GroupedListRowProps = {
|
|
12
13
|
isTextTruncated: boolean
|
|
13
14
|
item: GroupedListItem
|
|
15
|
+
/** From `useListMotion`, so an added row enters and a removed one leaves. */
|
|
16
|
+
motionProps?: ListMotionProps
|
|
14
17
|
size: GroupedListSize
|
|
15
18
|
}
|
|
16
19
|
|
|
@@ -97,6 +100,7 @@ const SELECTABLE_ROW_CLASS =
|
|
|
97
100
|
export const GroupedListRow = ({
|
|
98
101
|
isTextTruncated,
|
|
99
102
|
item,
|
|
103
|
+
motionProps,
|
|
100
104
|
size,
|
|
101
105
|
}: GroupedListRowProps): ReactNode => {
|
|
102
106
|
const [labelRef, clippedLabel] = useIsTextClipped()
|
|
@@ -125,12 +129,14 @@ export const GroupedListRow = ({
|
|
|
125
129
|
|
|
126
130
|
return (
|
|
127
131
|
<li
|
|
132
|
+
{...motionProps}
|
|
128
133
|
className={toClassName(
|
|
129
134
|
"flex flex-wrap items-center gap-x-3 gap-y-2",
|
|
130
135
|
ROW_SIZE_CLASS[size],
|
|
131
136
|
isSelectable && SELECTABLE_ROW_CLASS,
|
|
132
137
|
isHighlighted && "relative",
|
|
133
138
|
isMuted && "bg-surface-sunken",
|
|
139
|
+
motionProps?.className,
|
|
134
140
|
)}
|
|
135
141
|
>
|
|
136
142
|
{isSelectable ? (
|
|
@@ -231,6 +237,8 @@ export const GroupedListRow = ({
|
|
|
231
237
|
{isHighlighted ? (
|
|
232
238
|
<span
|
|
233
239
|
aria-hidden
|
|
240
|
+
// A new key is a new ring, and a new ring pulses again.
|
|
241
|
+
key={item.highlightKey}
|
|
234
242
|
className={toClassName(
|
|
235
243
|
"charcuterie-highlight pointer-events-none absolute inset-0 inset-ring-2",
|
|
236
244
|
HIGHLIGHT_RING_CLASS[highlightIntent],
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { ReactNode } from "react"
|
|
2
|
+
|
|
3
|
+
import { useListMotion } from "../Motion/useListMotion.ts"
|
|
4
|
+
import type {
|
|
5
|
+
GroupedListItem,
|
|
6
|
+
GroupedListSize,
|
|
7
|
+
} from "./GroupedList.tsx"
|
|
8
|
+
import { GroupedListRow } from "./GroupedListRow.tsx"
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* One group's rows, in their own component so each group can hold the
|
|
12
|
+
* `useListMotion` hook: an added row enters, a removed one leaves, and
|
|
13
|
+
* the rest slide, which a `map` over groups cannot do inline.
|
|
14
|
+
*/
|
|
15
|
+
export const GroupedListRows = ({
|
|
16
|
+
headingId,
|
|
17
|
+
isTextTruncated,
|
|
18
|
+
items,
|
|
19
|
+
onExitEnd,
|
|
20
|
+
size,
|
|
21
|
+
}: {
|
|
22
|
+
headingId: string
|
|
23
|
+
isTextTruncated: boolean
|
|
24
|
+
/** A row finished leaving; the groups below are about to move. */
|
|
25
|
+
onExitEnd: () => void
|
|
26
|
+
items: readonly GroupedListItem[]
|
|
27
|
+
size: GroupedListSize
|
|
28
|
+
}): ReactNode => {
|
|
29
|
+
const { entries } = useListMotion({
|
|
30
|
+
getKey: (item: GroupedListItem) => item.key,
|
|
31
|
+
items,
|
|
32
|
+
onExitEnd,
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
return (
|
|
36
|
+
<ul
|
|
37
|
+
aria-labelledby={headingId}
|
|
38
|
+
className="m-0 flex list-none flex-col divide-y divide-border-subtle overflow-hidden rounded-md border border-border-subtle bg-surface-raised p-0"
|
|
39
|
+
>
|
|
40
|
+
{entries.map(({ item, key, motionProps }) => (
|
|
41
|
+
<GroupedListRow
|
|
42
|
+
isTextTruncated={isTextTruncated}
|
|
43
|
+
item={item}
|
|
44
|
+
key={key}
|
|
45
|
+
motionProps={motionProps}
|
|
46
|
+
size={size}
|
|
47
|
+
/>
|
|
48
|
+
))}
|
|
49
|
+
</ul>
|
|
50
|
+
)
|
|
51
|
+
}
|
package/src/Listbox/Listbox.tsx
CHANGED
|
@@ -122,6 +122,9 @@ export const Listbox = ({
|
|
|
122
122
|
context,
|
|
123
123
|
floatingStyles,
|
|
124
124
|
getFloatingProps,
|
|
125
|
+
isExiting,
|
|
126
|
+
isMounted,
|
|
127
|
+
motionClassName,
|
|
125
128
|
setFloating,
|
|
126
129
|
triggerId,
|
|
127
130
|
} = useAnchoredOverlay({
|
|
@@ -204,10 +207,12 @@ export const Listbox = ({
|
|
|
204
207
|
<>
|
|
205
208
|
{clonedTrigger}
|
|
206
209
|
|
|
207
|
-
{
|
|
210
|
+
{isMounted ? (
|
|
208
211
|
<FloatingPortal>
|
|
209
212
|
<FloatingFocusManager
|
|
210
213
|
context={context}
|
|
214
|
+
// A leaving panel hands focus back at once.
|
|
215
|
+
disabled={isExiting}
|
|
211
216
|
initialFocus={-1}
|
|
212
217
|
modal={false}
|
|
213
218
|
>
|
|
@@ -223,6 +228,7 @@ export const Listbox = ({
|
|
|
223
228
|
// middleware (`maxHeightPx`), written as an inline
|
|
224
229
|
// style; this scrolls within it.
|
|
225
230
|
"z-[var(--layer-modal)] flex min-w-48 flex-col gap-0.5 overflow-auto p-1",
|
|
231
|
+
motionClassName,
|
|
226
232
|
className,
|
|
227
233
|
)}
|
|
228
234
|
onKeyDown={(keyEvent) => {
|
|
@@ -267,6 +273,8 @@ export const Listbox = ({
|
|
|
267
273
|
runTypeahead(keyEvent.key)
|
|
268
274
|
}
|
|
269
275
|
}}
|
|
276
|
+
aria-hidden={isExiting ? true : undefined}
|
|
277
|
+
inert={isExiting ? true : undefined}
|
|
270
278
|
ref={setFloating}
|
|
271
279
|
// Duplicated from `getFloatingProps()` on purpose, and
|
|
272
280
|
// the two cannot differ — `useRole(context, { role:
|
package/src/Menu/Menu.tsx
CHANGED
|
@@ -296,6 +296,9 @@ export const Menu = ({
|
|
|
296
296
|
context,
|
|
297
297
|
floatingStyles,
|
|
298
298
|
getFloatingProps,
|
|
299
|
+
isExiting,
|
|
300
|
+
isMounted,
|
|
301
|
+
motionClassName,
|
|
299
302
|
setFloating,
|
|
300
303
|
triggerId,
|
|
301
304
|
} = useAnchoredOverlay({
|
|
@@ -444,10 +447,12 @@ export const Menu = ({
|
|
|
444
447
|
<>
|
|
445
448
|
{clonedTrigger}
|
|
446
449
|
|
|
447
|
-
{
|
|
450
|
+
{isMounted ? (
|
|
448
451
|
<FloatingPortal>
|
|
449
452
|
<FloatingFocusManager
|
|
450
453
|
context={context}
|
|
454
|
+
// A leaving panel hands focus back at once.
|
|
455
|
+
disabled={isExiting}
|
|
451
456
|
// `false`, so Tab leaves the menu and lands on the next
|
|
452
457
|
// control in the page — which is what closes it, through
|
|
453
458
|
// `useDismiss`. A trapped menu is a dialog.
|
|
@@ -478,6 +483,7 @@ export const Menu = ({
|
|
|
478
483
|
// written straight onto the element by the `size`
|
|
479
484
|
// middleware; this scrolls inside it.
|
|
480
485
|
"charcuterie-scrollbar z-[var(--layer-modal)] flex min-w-48 flex-col gap-0.5 overflow-y-auto p-1",
|
|
486
|
+
motionClassName,
|
|
481
487
|
className,
|
|
482
488
|
)}
|
|
483
489
|
onKeyDown={(keyEvent) => {
|
|
@@ -501,6 +507,8 @@ export const Menu = ({
|
|
|
501
507
|
command()
|
|
502
508
|
}
|
|
503
509
|
}}
|
|
510
|
+
aria-hidden={isExiting ? true : undefined}
|
|
511
|
+
inert={isExiting ? true : undefined}
|
|
504
512
|
ref={setFloating}
|
|
505
513
|
// Duplicated from `getFloatingProps()` on purpose, and
|
|
506
514
|
// the two cannot differ — `useRole(context, { role:
|
package/src/Modal/Modal.tsx
CHANGED
|
@@ -12,6 +12,12 @@ export type ModalProps = {
|
|
|
12
12
|
* it the focus manager takes the first tabbable element — the
|
|
13
13
|
* Close button on anything with chrome. */
|
|
14
14
|
initialFocus?: RefObject<HTMLElement | null>
|
|
15
|
+
/**
|
|
16
|
+
* `false` only for a caller that animates the panel itself, as
|
|
17
|
+
* `TableViewport` does when it grows a table out of the page. Every
|
|
18
|
+
* other panel keeps the library's enter and exit motion.
|
|
19
|
+
*/
|
|
20
|
+
hasMotion?: boolean
|
|
15
21
|
/** Escape and a backdrop press both close. */
|
|
16
22
|
isDismissable?: boolean
|
|
17
23
|
isVisible: boolean
|
|
@@ -63,6 +69,7 @@ export const Modal = ({
|
|
|
63
69
|
"aria-labelledby": ariaLabelledBy,
|
|
64
70
|
children,
|
|
65
71
|
className,
|
|
72
|
+
hasMotion = true,
|
|
66
73
|
initialFocus,
|
|
67
74
|
isDismissable = true,
|
|
68
75
|
isVisible,
|
|
@@ -73,6 +80,7 @@ export const Modal = ({
|
|
|
73
80
|
aria-label={ariaLabel}
|
|
74
81
|
aria-labelledby={ariaLabelledBy}
|
|
75
82
|
className={toClassName(MODAL_BOX_CLASS, className)}
|
|
83
|
+
hasMotion={hasMotion}
|
|
76
84
|
initialFocus={initialFocus}
|
|
77
85
|
isDismissable={isDismissable}
|
|
78
86
|
isVisible={isVisible}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { ReactNode } from "react"
|
|
2
|
+
import { useState } from "react"
|
|
3
|
+
import { createRoot } from "react-dom/client"
|
|
4
|
+
import { expect, userEvent, waitFor } from "storybook/test"
|
|
5
|
+
import { test } from "vitest"
|
|
6
|
+
|
|
7
|
+
import { useLastPresentValue } from "./useLastPresentValue.ts"
|
|
8
|
+
|
|
9
|
+
const mount = (node: ReactNode) => {
|
|
10
|
+
const host = document.createElement("div")
|
|
11
|
+
|
|
12
|
+
document.body.append(host)
|
|
13
|
+
|
|
14
|
+
createRoot(host).render(node)
|
|
15
|
+
|
|
16
|
+
return host
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const Selection = (): ReactNode => {
|
|
20
|
+
const [selected, setSelected] = useState<string | null>(
|
|
21
|
+
null,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
const shown = useLastPresentValue(selected)
|
|
25
|
+
|
|
26
|
+
return (
|
|
27
|
+
<div>
|
|
28
|
+
<button
|
|
29
|
+
onClick={() => {
|
|
30
|
+
setSelected((previous) =>
|
|
31
|
+
previous === null ? "First" : null,
|
|
32
|
+
)
|
|
33
|
+
}}
|
|
34
|
+
type="button"
|
|
35
|
+
>
|
|
36
|
+
Toggle
|
|
37
|
+
</button>
|
|
38
|
+
|
|
39
|
+
<output>{shown ?? "none"}</output>
|
|
40
|
+
</div>
|
|
41
|
+
)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
test("the last present value is held after the value empties", async () => {
|
|
45
|
+
const host = mount(<Selection />)
|
|
46
|
+
|
|
47
|
+
const button = await waitFor(() => {
|
|
48
|
+
const found = host.querySelector("button")
|
|
49
|
+
|
|
50
|
+
expect(found).not.toBeNull()
|
|
51
|
+
|
|
52
|
+
return found as HTMLButtonElement
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
const output = host.querySelector("output") as HTMLElement
|
|
56
|
+
|
|
57
|
+
expect(output.textContent).toBe("none")
|
|
58
|
+
|
|
59
|
+
await userEvent.click(button)
|
|
60
|
+
|
|
61
|
+
expect(output.textContent).toBe("First")
|
|
62
|
+
|
|
63
|
+
await userEvent.click(button)
|
|
64
|
+
|
|
65
|
+
expect(output.textContent).toBe("First")
|
|
66
|
+
})
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { useState } from "react"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The value, or — once it becomes `null` or `undefined` — the last
|
|
5
|
+
* value it had. For an overlay whose content comes from a selection:
|
|
6
|
+
*
|
|
7
|
+
* ```tsx
|
|
8
|
+
* const editingCard = cards.find((card) => card.id === editingId)
|
|
9
|
+
* const shownCard = useLastPresentValue(editingCard)
|
|
10
|
+
*
|
|
11
|
+
* <Dialog isVisible={editingCard !== undefined} heading={`Edit ${shownCard?.name}`}>
|
|
12
|
+
* {shownCard ? <CardForm card={shownCard} /> : null}
|
|
13
|
+
* </Dialog>
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* Closing clears the selection in the same render that starts the
|
|
17
|
+
* dialog's exit, so without this the dialog spends its exit animation
|
|
18
|
+
* empty — it shrinks to a heading as it fades, which reads as a glitch
|
|
19
|
+
* rather than as closing. Visibility still follows the live value; only
|
|
20
|
+
* the content is held.
|
|
21
|
+
*/
|
|
22
|
+
export const useLastPresentValue = <Value>(
|
|
23
|
+
value: Value | null | undefined,
|
|
24
|
+
): Value | undefined => {
|
|
25
|
+
const [lastPresent, setLastPresent] = useState<
|
|
26
|
+
Value | undefined
|
|
27
|
+
>(value ?? undefined)
|
|
28
|
+
|
|
29
|
+
if (
|
|
30
|
+
value !== null &&
|
|
31
|
+
value !== undefined &&
|
|
32
|
+
!Object.is(value, lastPresent)
|
|
33
|
+
) {
|
|
34
|
+
setLastPresent(value)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return value ?? lastPresent
|
|
38
|
+
}
|