@charcuterie/ui 2.4.1 → 2.5.0
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/Checkbox/Checkbox.d.ts +59 -0
- package/dist/Checkbox/Checkbox.d.ts.map +1 -0
- package/dist/Checkbox/Checkbox.js +61 -0
- package/dist/Checkbox/Checkbox.js.map +1 -0
- package/dist/Field/Field.d.ts.map +1 -1
- package/dist/Field/Field.js +12 -1
- package/dist/Field/Field.js.map +1 -1
- package/dist/Menu/Menu.d.ts +60 -2
- package/dist/Menu/Menu.d.ts.map +1 -1
- package/dist/Menu/Menu.js +58 -11
- package/dist/Menu/Menu.js.map +1 -1
- package/dist/RadioGroup/RadioGroup.d.ts +47 -0
- package/dist/RadioGroup/RadioGroup.d.ts.map +1 -0
- package/dist/RadioGroup/RadioGroup.js +114 -0
- package/dist/RadioGroup/RadioGroup.js.map +1 -0
- package/dist/RadioGroup/RadioGroupOption.d.ts +26 -0
- package/dist/RadioGroup/RadioGroupOption.d.ts.map +1 -0
- package/dist/RadioGroup/RadioGroupOption.js +52 -0
- package/dist/RadioGroup/RadioGroupOption.js.map +1 -0
- package/dist/Switch/Switch.d.ts +54 -0
- package/dist/Switch/Switch.d.ts.map +1 -0
- package/dist/Switch/Switch.js +94 -0
- package/dist/Switch/Switch.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/slotWiring.d.ts +9 -7
- package/dist/slotWiring.d.ts.map +1 -1
- package/dist/slotWiring.js +10 -70
- package/dist/slotWiring.js.map +1 -1
- package/package.json +2 -2
- package/src/Checkbox/Checkbox.mdx +58 -0
- package/src/Checkbox/Checkbox.stories.tsx +100 -0
- package/src/Checkbox/Checkbox.test.tsx +80 -0
- package/src/Checkbox/Checkbox.tsx +146 -0
- package/src/Combobox/Combobox.stories.tsx +19 -4
- package/src/Field/Field.stories.tsx +19 -0
- package/src/Field/Field.test.tsx +30 -2
- package/src/Field/Field.tsx +13 -1
- package/src/Menu/Menu.stories.tsx +80 -2
- package/src/Menu/Menu.test.tsx +102 -0
- package/src/Menu/Menu.tsx +156 -17
- package/src/RadioGroup/RadioGroup.mdx +58 -0
- package/src/RadioGroup/RadioGroup.stories.tsx +130 -0
- package/src/RadioGroup/RadioGroup.test.tsx +191 -0
- package/src/RadioGroup/RadioGroup.tsx +204 -0
- package/src/RadioGroup/RadioGroupOption.tsx +126 -0
- package/src/Switch/Switch.mdx +53 -0
- package/src/Switch/Switch.stories.tsx +94 -0
- package/src/Switch/Switch.test.tsx +86 -0
- package/src/Switch/Switch.tsx +162 -0
- package/src/index.ts +9 -0
- package/src/slotWiring.ts +15 -107
- package/src/sourceRules.test.ts +11 -1
- package/src/tailwindCandidates.test.ts +9 -7
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { composeStories } from "@storybook/react"
|
|
2
|
+
import { expect, userEvent, waitFor } from "storybook/test"
|
|
3
|
+
import { test } from "vitest"
|
|
4
|
+
|
|
5
|
+
import { expectNoAxeViolations } from "../expectNoAxeViolations.testHelpers.ts"
|
|
6
|
+
import { mountStory } from "../mountStory.testHelpers.ts"
|
|
7
|
+
import { expectAgentDrivable } from "../testing/index.ts"
|
|
8
|
+
import * as stories from "./Checkbox.stories.tsx"
|
|
9
|
+
|
|
10
|
+
const { AllStates, Default, Interactive } =
|
|
11
|
+
composeStories(stories)
|
|
12
|
+
|
|
13
|
+
test("it is a checkbox a screen reader can name", async () => {
|
|
14
|
+
const { canvas, canvasElement } =
|
|
15
|
+
await mountStory(Default)
|
|
16
|
+
|
|
17
|
+
const checkbox = expectAgentDrivable(canvas, {
|
|
18
|
+
name: "Delete originals after import",
|
|
19
|
+
role: "checkbox",
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
await expect(checkbox).not.toBeChecked()
|
|
23
|
+
|
|
24
|
+
await expectNoAxeViolations(canvasElement)
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
test("a press toggles it, and the label is part of the target", async () => {
|
|
28
|
+
const { canvas } = await mountStory(Interactive)
|
|
29
|
+
|
|
30
|
+
const checkbox = expectAgentDrivable(canvas, {
|
|
31
|
+
name: "Delete originals after import",
|
|
32
|
+
role: "checkbox",
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
// The label wraps the control, so a click on the text is a click
|
|
36
|
+
// on the box — which is the association `<label htmlFor>` gets
|
|
37
|
+
// wrong half the time in the fleet and this cannot.
|
|
38
|
+
await userEvent.click(checkbox)
|
|
39
|
+
|
|
40
|
+
await waitFor(() => {
|
|
41
|
+
expect(checkbox).toBeChecked()
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
await userEvent.click(checkbox)
|
|
45
|
+
|
|
46
|
+
await waitFor(() => {
|
|
47
|
+
expect(checkbox).not.toBeChecked()
|
|
48
|
+
})
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
test("a disabled box cannot be toggled", async () => {
|
|
52
|
+
const { canvas, canvasElement } =
|
|
53
|
+
await mountStory(AllStates)
|
|
54
|
+
|
|
55
|
+
const disabled = expectAgentDrivable(canvas, {
|
|
56
|
+
name: "Disabled",
|
|
57
|
+
role: "checkbox",
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
await expect(disabled).toBeDisabled()
|
|
61
|
+
|
|
62
|
+
await expect(disabled).not.toBeChecked()
|
|
63
|
+
|
|
64
|
+
await userEvent.click(disabled)
|
|
65
|
+
|
|
66
|
+
await expect(disabled).not.toBeChecked()
|
|
67
|
+
|
|
68
|
+
await expectNoAxeViolations(canvasElement)
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
test("isChecked decides the first render", async () => {
|
|
72
|
+
const { canvas } = await mountStory(AllStates)
|
|
73
|
+
|
|
74
|
+
await expect(
|
|
75
|
+
expectAgentDrivable(canvas, {
|
|
76
|
+
name: "Checked",
|
|
77
|
+
role: "checkbox",
|
|
78
|
+
}),
|
|
79
|
+
).toBeChecked()
|
|
80
|
+
})
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import type { ControlSize } from "@charcuterie/tokens"
|
|
2
|
+
import type { ChangeEvent, ReactNode } from "react"
|
|
3
|
+
|
|
4
|
+
import { FOCUS_RING_CLASS } from "../intentStyles.ts"
|
|
5
|
+
import { toClassName } from "../toClassName.ts"
|
|
6
|
+
|
|
7
|
+
export type CheckboxProps = {
|
|
8
|
+
className?: string
|
|
9
|
+
/**
|
|
10
|
+
* The caller's own id, when it has one — a stable target for an
|
|
11
|
+
* autofill hint or a deep link. Left to React's `useId` on the
|
|
12
|
+
* `<input>` otherwise, which is enough for the wrapping `<label>`.
|
|
13
|
+
*/
|
|
14
|
+
id?: string
|
|
15
|
+
/**
|
|
16
|
+
* **Initial** only. The `<input>` is the store from then on — the
|
|
17
|
+
* DOM owns a checkbox's checkedness, and a controlled `checked`
|
|
18
|
+
* prop is the thing this library refuses to have
|
|
19
|
+
* (`SegmentedControl` says the same about `selectedValue`).
|
|
20
|
+
*/
|
|
21
|
+
isChecked?: boolean
|
|
22
|
+
isDisabled?: boolean
|
|
23
|
+
/**
|
|
24
|
+
* The visible text beside the box, and the checkbox's accessible
|
|
25
|
+
* name — the `<label>` wraps the control, so there is no `for` to
|
|
26
|
+
* get wrong. A box with no label is a control a screen reader
|
|
27
|
+
* cannot announce, which is why this is required.
|
|
28
|
+
*/
|
|
29
|
+
label: ReactNode
|
|
30
|
+
onChange?: (isChecked: boolean) => void
|
|
31
|
+
size?: ControlSize
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const BOX_SIZE_CLASS: Record<ControlSize, string> = {
|
|
35
|
+
sm: "size-3.5",
|
|
36
|
+
md: "size-4",
|
|
37
|
+
lg: "size-5",
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const CHECK_SIZE_CLASS: Record<ControlSize, string> = {
|
|
41
|
+
sm: "size-2.5",
|
|
42
|
+
md: "size-3",
|
|
43
|
+
lg: "size-3.5",
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const TEXT_SIZE_CLASS: Record<ControlSize, string> = {
|
|
47
|
+
sm: "text-sm",
|
|
48
|
+
md: "text-md",
|
|
49
|
+
lg: "text-lg",
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* A single boolean, on or off, that owns nothing but itself.
|
|
54
|
+
*
|
|
55
|
+
* This is the primitive mux-magic's `BooleanField` hand-rolled in
|
|
56
|
+
* `bg-slate-700 accent-blue-500` because the library had no checkbox
|
|
57
|
+
* to reach for — palette colours with no light mode, the exact M6
|
|
58
|
+
* defect the token layer exists to delete. Here the box is
|
|
59
|
+
* `bg-surface-sunken` and the fill is `bg-intent-accent-solid`, so
|
|
60
|
+
* one control reads correctly in `daylight`, `midnight`, and on the
|
|
61
|
+
* kiosk with no per-app override.
|
|
62
|
+
*
|
|
63
|
+
* ### The `<label>` wraps the control
|
|
64
|
+
*
|
|
65
|
+
* A checkbox reads to the *left* of its text, which is why it is not
|
|
66
|
+
* a `Field` — `Field` stacks label above control in a `flex-col`,
|
|
67
|
+
* right for a text input and wrong for a boolean, and stacking every
|
|
68
|
+
* checkbox in a step card would double the card's height. Wrapping is
|
|
69
|
+
* also the one label association that needs no `for` at all, so there
|
|
70
|
+
* is no minted-then-dropped id to get wrong.
|
|
71
|
+
*
|
|
72
|
+
* ### The `<input>` is the store
|
|
73
|
+
*
|
|
74
|
+
* `defaultChecked`, not `checked`. The DOM owns a checkbox's state
|
|
75
|
+
* natively, so there is no second copy to keep in step and no
|
|
76
|
+
* controlled-prop round-trip — `onChange` reports the new state to a
|
|
77
|
+
* consumer that wants to persist it, and reading it back is the
|
|
78
|
+
* `<input>`'s job. `isChecked` seeds the first paint and nothing
|
|
79
|
+
* after.
|
|
80
|
+
*/
|
|
81
|
+
export const Checkbox = ({
|
|
82
|
+
className,
|
|
83
|
+
id,
|
|
84
|
+
isChecked = false,
|
|
85
|
+
isDisabled = false,
|
|
86
|
+
label,
|
|
87
|
+
onChange,
|
|
88
|
+
size = "md",
|
|
89
|
+
}: CheckboxProps): ReactNode => (
|
|
90
|
+
<label
|
|
91
|
+
className={toClassName(
|
|
92
|
+
"inline-flex cursor-pointer items-center gap-2 text-content-secondary select-none",
|
|
93
|
+
TEXT_SIZE_CLASS[size],
|
|
94
|
+
isDisabled &&
|
|
95
|
+
"cursor-not-allowed text-content-disabled",
|
|
96
|
+
className,
|
|
97
|
+
)}
|
|
98
|
+
>
|
|
99
|
+
<span className="relative grid shrink-0 place-items-center">
|
|
100
|
+
<input
|
|
101
|
+
className={toClassName(
|
|
102
|
+
"peer cursor-pointer appearance-none rounded-sm border border-border-strong bg-surface-sunken transition-colors duration-(--duration-fast) ease-standard",
|
|
103
|
+
"checked:border-intent-accent-solid checked:bg-intent-accent-solid",
|
|
104
|
+
// Disabled mutes the border and the cursor, and — unlike the
|
|
105
|
+
// shared `DISABLED_CLASS` — does *not* touch the background.
|
|
106
|
+
// The generic bundle repaints the box `surface-sunken`, which
|
|
107
|
+
// erased both the checked accent fill (white tick on grey)
|
|
108
|
+
// and, in a light scheme, the unchecked box entirely: a
|
|
109
|
+
// `border-subtle` outline on `surface-sunken` on
|
|
110
|
+
// `surface-base` is three near-identical creams. A disabled
|
|
111
|
+
// box is still a box the user has to see.
|
|
112
|
+
"disabled:cursor-not-allowed disabled:border-border-default",
|
|
113
|
+
BOX_SIZE_CLASS[size],
|
|
114
|
+
FOCUS_RING_CLASS,
|
|
115
|
+
)}
|
|
116
|
+
defaultChecked={isChecked}
|
|
117
|
+
disabled={isDisabled}
|
|
118
|
+
id={id}
|
|
119
|
+
onChange={(
|
|
120
|
+
event: ChangeEvent<HTMLInputElement>,
|
|
121
|
+
) => {
|
|
122
|
+
onChange?.(event.target.checked)
|
|
123
|
+
}}
|
|
124
|
+
type="checkbox"
|
|
125
|
+
/>
|
|
126
|
+
|
|
127
|
+
<svg
|
|
128
|
+
aria-hidden="true"
|
|
129
|
+
className={toClassName(
|
|
130
|
+
"pointer-events-none invisible absolute text-intent-accent-on-solid peer-checked:visible",
|
|
131
|
+
CHECK_SIZE_CLASS[size],
|
|
132
|
+
)}
|
|
133
|
+
fill="none"
|
|
134
|
+
stroke="currentColor"
|
|
135
|
+
strokeLinecap="round"
|
|
136
|
+
strokeLinejoin="round"
|
|
137
|
+
strokeWidth={3}
|
|
138
|
+
viewBox="0 0 24 24"
|
|
139
|
+
>
|
|
140
|
+
<path d="M20 6 9 17l-5-5" />
|
|
141
|
+
</svg>
|
|
142
|
+
</span>
|
|
143
|
+
|
|
144
|
+
<span>{label}</span>
|
|
145
|
+
</label>
|
|
146
|
+
)
|
|
@@ -88,11 +88,20 @@ const ComboboxHarness = ({
|
|
|
88
88
|
isVisible={isVisible}
|
|
89
89
|
onDismiss={hide}
|
|
90
90
|
onSelect={(value) => {
|
|
91
|
-
|
|
92
|
-
|
|
91
|
+
// Mirror the control's own selection model so the "Chosen"
|
|
92
|
+
// line never disagrees with the ticks in the list: multi-select
|
|
93
|
+
// accumulates/toggles, single-select replaces (one value, one
|
|
94
|
+
// ✓). Toggling into an array in single-select made the line
|
|
95
|
+
// claim several were chosen while only the last carried a tick.
|
|
96
|
+
setChosen((previous) => {
|
|
97
|
+
if (!isMultiple) {
|
|
98
|
+
return [value]
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return previous.includes(value)
|
|
93
102
|
? previous.filter((one) => one !== value)
|
|
94
|
-
: [...previous, value]
|
|
95
|
-
)
|
|
103
|
+
: [...previous, value]
|
|
104
|
+
})
|
|
96
105
|
}}
|
|
97
106
|
options={options}
|
|
98
107
|
trigger={
|
|
@@ -415,6 +424,11 @@ const MANY_LONG: ListboxItem[] = Array.from(
|
|
|
415
424
|
* so the list both virtualizes (over ~100 options) and wraps each row to
|
|
416
425
|
* two lines. Rows are measured, not pinned to the 36px estimate, so they
|
|
417
426
|
* lay out below one another instead of overlapping.
|
|
427
|
+
*
|
|
428
|
+
* Multi-select as well, so it exercises the case together: every picked
|
|
429
|
+
* row keeps its own ✓ (a virtualized row that scrolls out and back is
|
|
430
|
+
* re-rendered from `selected`, not remembered by the DOM), and the chips
|
|
431
|
+
* above the trigger stay in sync with the ticks.
|
|
418
432
|
*/
|
|
419
433
|
export const VirtualizedLongOptions: Story = {
|
|
420
434
|
args: {
|
|
@@ -428,6 +442,7 @@ export const VirtualizedLongOptions: Story = {
|
|
|
428
442
|
<ComboboxHarness
|
|
429
443
|
className="w-72"
|
|
430
444
|
isInitiallyVisible
|
|
445
|
+
isMultiple
|
|
431
446
|
options={MANY_LONG}
|
|
432
447
|
triggerLabel="Search folders"
|
|
433
448
|
/>
|
|
@@ -36,6 +36,25 @@ export const Default: Story = {
|
|
|
36
36
|
},
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
+
/**
|
|
40
|
+
* The control carries its own `id` — `rename-pattern`, the kind of
|
|
41
|
+
* stable id a deep link or a server-rendered error summary points at.
|
|
42
|
+
* The `Field` adopts it rather than minting a `-control` id over the
|
|
43
|
+
* top, so the `<label htmlFor>` and every outside-in reference agree.
|
|
44
|
+
*/
|
|
45
|
+
export const AdoptsChildId: Story = {
|
|
46
|
+
args: {
|
|
47
|
+
children: (
|
|
48
|
+
<input
|
|
49
|
+
className={TEXT_INPUT_CLASS}
|
|
50
|
+
id="rename-pattern"
|
|
51
|
+
type="text"
|
|
52
|
+
/>
|
|
53
|
+
),
|
|
54
|
+
label: "Rename pattern",
|
|
55
|
+
},
|
|
56
|
+
}
|
|
57
|
+
|
|
39
58
|
/**
|
|
40
59
|
* The control is a slot, not a hardcoded `<input>` — `Select` goes
|
|
41
60
|
* in the same hole, and so does an app's own control. The `id`,
|
package/src/Field/Field.test.tsx
CHANGED
|
@@ -7,8 +7,36 @@ import { mountStory } from "../mountStory.testHelpers.ts"
|
|
|
7
7
|
import { expectAgentDrivable } from "../testing/index.ts"
|
|
8
8
|
import * as stories from "./Field.stories.tsx"
|
|
9
9
|
|
|
10
|
-
const {
|
|
11
|
-
|
|
10
|
+
const {
|
|
11
|
+
AdoptsChildId,
|
|
12
|
+
AllStates,
|
|
13
|
+
AllVariants,
|
|
14
|
+
Default,
|
|
15
|
+
Group,
|
|
16
|
+
Nested,
|
|
17
|
+
} = composeStories(stories)
|
|
18
|
+
|
|
19
|
+
test("a control's own id is adopted, not overwritten", async () => {
|
|
20
|
+
const { canvas, canvasElement } =
|
|
21
|
+
await mountStory(AdoptsChildId)
|
|
22
|
+
|
|
23
|
+
// The child was written `<input id="rename-pattern" />`. The Field
|
|
24
|
+
// must keep that id — overwriting it with a minted `-control` id is
|
|
25
|
+
// what broke the outside-in references the id exists for — and the
|
|
26
|
+
// `<label htmlFor>` must agree, so the control is still findable by
|
|
27
|
+
// its label's text.
|
|
28
|
+
const control = expectAgentDrivable(canvas, {
|
|
29
|
+
name: "Rename pattern",
|
|
30
|
+
role: "textbox",
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
await expect(control).toHaveAttribute(
|
|
34
|
+
"id",
|
|
35
|
+
"rename-pattern",
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
await expectNoAxeViolations(canvasElement)
|
|
39
|
+
})
|
|
12
40
|
|
|
13
41
|
test("the label names the control it points at", async () => {
|
|
14
42
|
const { canvas, canvasElement } =
|
package/src/Field/Field.tsx
CHANGED
|
@@ -126,9 +126,21 @@ export const Field = ({
|
|
|
126
126
|
* link, a server-rendered error summary linking to the control.
|
|
127
127
|
* `useUniqueId` is still called: hooks are unconditional, and it
|
|
128
128
|
* costs one counter read.
|
|
129
|
+
*
|
|
130
|
+
* Precedence is `Field` prop → **the child's own `id`** →
|
|
131
|
+
* generated. The child's is read because a control written as
|
|
132
|
+
* `<input id="rename-pattern" />` puts the id where the author can
|
|
133
|
+
* see it, not on the `Field`; overwriting it with a minted
|
|
134
|
+
* `-control` id used to break exactly the outside-in references the
|
|
135
|
+
* `Field` prop exists to keep — a deep link, an autofill hint, a
|
|
136
|
+
* consumer's own selector. The `Field` prop still wins when both
|
|
137
|
+
* are set: it is the outer, later declaration, and last-writer-wins
|
|
138
|
+
* is the slot's rule for a value.
|
|
129
139
|
*/
|
|
140
|
+
const childId = (children.props as { id?: string }).id
|
|
141
|
+
|
|
130
142
|
const controlId =
|
|
131
|
-
receivedSlotProps.id ?? `${baseId}-control`
|
|
143
|
+
receivedSlotProps.id ?? childId ?? `${baseId}-control`
|
|
132
144
|
|
|
133
145
|
const descriptionId = `${baseId}-description`
|
|
134
146
|
|
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
UndoIcon,
|
|
15
15
|
} from "../icons.storyHelpers.tsx"
|
|
16
16
|
import { Tooltip } from "../Tooltip/Tooltip.tsx"
|
|
17
|
-
import type { MenuItem } from "./Menu.tsx"
|
|
17
|
+
import type { MenuEntry, MenuItem } from "./Menu.tsx"
|
|
18
18
|
import { Menu } from "./Menu.tsx"
|
|
19
19
|
|
|
20
20
|
const noop = () => undefined
|
|
@@ -41,18 +41,50 @@ const BAY_ACTIONS: MenuItem[] = [
|
|
|
41
41
|
},
|
|
42
42
|
]
|
|
43
43
|
|
|
44
|
+
const GROUPED_ACTIONS: MenuEntry[] = [
|
|
45
|
+
{
|
|
46
|
+
items: [
|
|
47
|
+
{
|
|
48
|
+
key: "retry",
|
|
49
|
+
label: "Retry title",
|
|
50
|
+
onSelect: noop,
|
|
51
|
+
},
|
|
52
|
+
{ key: "skip", label: "Skip title", onSelect: noop },
|
|
53
|
+
],
|
|
54
|
+
key: "disc",
|
|
55
|
+
label: "Disc",
|
|
56
|
+
type: "group",
|
|
57
|
+
},
|
|
58
|
+
{ key: "sep", type: "separator" },
|
|
59
|
+
{
|
|
60
|
+
items: [
|
|
61
|
+
{
|
|
62
|
+
icon: <SettingsIcon />,
|
|
63
|
+
key: "eject",
|
|
64
|
+
label: "Eject disc",
|
|
65
|
+
onSelect: noop,
|
|
66
|
+
},
|
|
67
|
+
],
|
|
68
|
+
key: "danger",
|
|
69
|
+
label: "Danger",
|
|
70
|
+
type: "group",
|
|
71
|
+
},
|
|
72
|
+
]
|
|
73
|
+
|
|
44
74
|
/**
|
|
45
75
|
* A menu's visibility belongs to the app, exactly like `Popover`'s —
|
|
46
76
|
* so the stories hold it in a `useVisibility`, which is what a
|
|
47
77
|
* consumer writes too.
|
|
48
78
|
*/
|
|
49
79
|
const MenuHarness = ({
|
|
80
|
+
emptyState,
|
|
50
81
|
isInitiallyVisible = false,
|
|
51
82
|
items = BAY_ACTIONS,
|
|
52
83
|
triggerLabel,
|
|
53
84
|
}: {
|
|
85
|
+
emptyState?: ReactNode
|
|
54
86
|
isInitiallyVisible?: boolean
|
|
55
|
-
items?:
|
|
87
|
+
items?: MenuEntry[]
|
|
56
88
|
triggerLabel: string
|
|
57
89
|
}): ReactNode => {
|
|
58
90
|
const { hide, isVisible, toggle } = useVisibility({
|
|
@@ -61,6 +93,7 @@ const MenuHarness = ({
|
|
|
61
93
|
|
|
62
94
|
return (
|
|
63
95
|
<Menu
|
|
96
|
+
emptyState={emptyState}
|
|
64
97
|
isVisible={isVisible}
|
|
65
98
|
items={items}
|
|
66
99
|
onDismiss={hide}
|
|
@@ -226,3 +259,48 @@ export const Interactive: Story = {
|
|
|
226
259
|
<MenuHarness triggerLabel="Open bay 4 menu" />
|
|
227
260
|
),
|
|
228
261
|
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* `items` is a union — a `group` (`role="group"`, named by its
|
|
265
|
+
* label) and a `separator` (`role="separator"`) opt in by their
|
|
266
|
+
* `type`, and a bare item still works unchanged. Neither the
|
|
267
|
+
* separator nor the group headings register with the roving group,
|
|
268
|
+
* so arrow-down goes Retry → Skip → Eject, straight past both
|
|
269
|
+
* headings and the rule between them.
|
|
270
|
+
*/
|
|
271
|
+
export const Grouped: Story = {
|
|
272
|
+
args: {
|
|
273
|
+
isVisible: false,
|
|
274
|
+
onDismiss: noop,
|
|
275
|
+
trigger: <Button appearance="outline">Actions</Button>,
|
|
276
|
+
},
|
|
277
|
+
render: () => (
|
|
278
|
+
<MenuHarness
|
|
279
|
+
isInitiallyVisible
|
|
280
|
+
items={GROUPED_ACTIONS}
|
|
281
|
+
triggerLabel="Bay 6"
|
|
282
|
+
/>
|
|
283
|
+
),
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* No actions to show. The `emptyState` renders as a **disabled**
|
|
288
|
+
* `menuitem` — a `role="menu"` must own one — so it is announced but
|
|
289
|
+
* takes no tab stop and no roving-focus membership, and a keyboard
|
|
290
|
+
* user can still Escape out of it.
|
|
291
|
+
*/
|
|
292
|
+
export const Empty: Story = {
|
|
293
|
+
args: {
|
|
294
|
+
isVisible: false,
|
|
295
|
+
onDismiss: noop,
|
|
296
|
+
trigger: <Button appearance="outline">Actions</Button>,
|
|
297
|
+
},
|
|
298
|
+
render: () => (
|
|
299
|
+
<MenuHarness
|
|
300
|
+
emptyState="No actions available"
|
|
301
|
+
isInitiallyVisible
|
|
302
|
+
items={[]}
|
|
303
|
+
triggerLabel="Bay 7"
|
|
304
|
+
/>
|
|
305
|
+
),
|
|
306
|
+
}
|
package/src/Menu/Menu.test.tsx
CHANGED
|
@@ -10,6 +10,8 @@ import * as stories from "./Menu.stories.tsx"
|
|
|
10
10
|
const {
|
|
11
11
|
AllStates,
|
|
12
12
|
AllVariants,
|
|
13
|
+
Empty,
|
|
14
|
+
Grouped,
|
|
13
15
|
Interactive,
|
|
14
16
|
SharedTrigger,
|
|
15
17
|
} = composeStories(stories)
|
|
@@ -320,3 +322,103 @@ test("the tooltip sharing that trigger is anchored to it too", async () => {
|
|
|
320
322
|
)
|
|
321
323
|
})
|
|
322
324
|
})
|
|
325
|
+
|
|
326
|
+
test("a group is a named group and a separator is a separator", async () => {
|
|
327
|
+
const { body } = await mountStory(Grouped)
|
|
328
|
+
|
|
329
|
+
const menu = expectAgentDrivable(body, {
|
|
330
|
+
name: "Bay 6",
|
|
331
|
+
role: "menu",
|
|
332
|
+
})
|
|
333
|
+
|
|
334
|
+
// Each group is a `role="group"` named by its heading, so a screen
|
|
335
|
+
// reader announces "Danger, group" before its members.
|
|
336
|
+
expectAgentDrivable(body, {
|
|
337
|
+
name: "Disc",
|
|
338
|
+
role: "group",
|
|
339
|
+
})
|
|
340
|
+
|
|
341
|
+
expectAgentDrivable(body, {
|
|
342
|
+
name: "Danger",
|
|
343
|
+
role: "group",
|
|
344
|
+
})
|
|
345
|
+
|
|
346
|
+
// A native `<hr>`, which carries the implicit `role="separator"`.
|
|
347
|
+
await expect(menu.querySelectorAll("hr")).toHaveLength(1)
|
|
348
|
+
|
|
349
|
+
await expect(body.getAllByRole("separator")).toHaveLength(
|
|
350
|
+
1,
|
|
351
|
+
)
|
|
352
|
+
|
|
353
|
+
await expectNoAxeViolations(menu)
|
|
354
|
+
})
|
|
355
|
+
|
|
356
|
+
test("arrow keys skip group headings and separators", async () => {
|
|
357
|
+
const { body } = await mountStory(Grouped)
|
|
358
|
+
|
|
359
|
+
const retry = expectAgentDrivable(body, {
|
|
360
|
+
name: "Retry title",
|
|
361
|
+
role: "menuitem",
|
|
362
|
+
})
|
|
363
|
+
|
|
364
|
+
const skip = expectAgentDrivable(body, {
|
|
365
|
+
name: "Skip title",
|
|
366
|
+
role: "menuitem",
|
|
367
|
+
})
|
|
368
|
+
|
|
369
|
+
const eject = expectAgentDrivable(body, {
|
|
370
|
+
name: "Eject disc",
|
|
371
|
+
role: "menuitem",
|
|
372
|
+
})
|
|
373
|
+
|
|
374
|
+
// Opening lands on the first item, and arrow-down walks the items
|
|
375
|
+
// in DOM order across the group boundary — the heading and the
|
|
376
|
+
// separator register nothing, so there is nothing between Skip
|
|
377
|
+
// (end of group one) and Eject (start of group two) to stop on.
|
|
378
|
+
await waitFor(() => {
|
|
379
|
+
expect(retry).toHaveFocus()
|
|
380
|
+
})
|
|
381
|
+
|
|
382
|
+
await userEvent.keyboard("{ArrowDown}")
|
|
383
|
+
|
|
384
|
+
await waitFor(() => {
|
|
385
|
+
expect(skip).toHaveFocus()
|
|
386
|
+
})
|
|
387
|
+
|
|
388
|
+
await userEvent.keyboard("{ArrowDown}")
|
|
389
|
+
|
|
390
|
+
await waitFor(() => {
|
|
391
|
+
expect(eject).toHaveFocus()
|
|
392
|
+
})
|
|
393
|
+
})
|
|
394
|
+
|
|
395
|
+
test("an empty menu shows a disabled note, not an actionable item", async () => {
|
|
396
|
+
const { body } = await mountStory(Empty)
|
|
397
|
+
|
|
398
|
+
const menu = expectAgentDrivable(body, {
|
|
399
|
+
name: "Bay 7",
|
|
400
|
+
role: "menu",
|
|
401
|
+
})
|
|
402
|
+
|
|
403
|
+
await expect(menu).toHaveTextContent(
|
|
404
|
+
"No actions available",
|
|
405
|
+
)
|
|
406
|
+
|
|
407
|
+
// The note is the menu's one `menuitem` — a `role="menu"` must own
|
|
408
|
+
// one — but it is `aria-disabled` and carries no tab stop, so a
|
|
409
|
+
// keyboard user is told the menu is empty and cannot land on it.
|
|
410
|
+
const items = menu.querySelectorAll<HTMLElement>(
|
|
411
|
+
'[role="menuitem"]',
|
|
412
|
+
)
|
|
413
|
+
|
|
414
|
+
await expect(items).toHaveLength(1)
|
|
415
|
+
|
|
416
|
+
await expect(items[0]).toHaveAttribute(
|
|
417
|
+
"aria-disabled",
|
|
418
|
+
"true",
|
|
419
|
+
)
|
|
420
|
+
|
|
421
|
+
await expect(items[0]?.tabIndex).toBe(-1)
|
|
422
|
+
|
|
423
|
+
await expectNoAxeViolations(menu)
|
|
424
|
+
})
|