@delacour/react-native-ui 0.1.0-alpha.20260925060619 → 0.1.0-alpha.20260925060916
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/package.json +3 -2
- package/src/components/toggle-button/AGENTS.md +106 -0
- package/src/components/toggle-button/index.ts +30 -0
- package/src/components/toggle-button/toggle-button-group.tsx +156 -0
- package/src/components/toggle-button/toggle-button-label.tsx +19 -0
- package/src/components/toggle-button/toggle-button.context.tsx +130 -0
- package/src/components/toggle-button/toggle-button.tsx +172 -0
- package/src/components/toggle-button/toggle-button.variants.test.ts +189 -0
- package/src/components/toggle-button/toggle-button.variants.ts +160 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@delacour/react-native-ui",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.20260925060916",
|
|
4
4
|
"description": "React Native UI components — Uniwind, Reanimated, Gesture Handler",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -58,6 +58,7 @@
|
|
|
58
58
|
"./switch": "./src/components/switch/index.ts",
|
|
59
59
|
"./tabs": "./src/components/tabs/index.ts",
|
|
60
60
|
"./text": "./src/components/text/index.ts",
|
|
61
|
+
"./toggle-button": "./src/components/toggle-button/index.ts",
|
|
61
62
|
"./expo/navigation-theme": "./src/expo/navigation-theme.tsx",
|
|
62
63
|
"./hooks/use-controllable-state": "./src/hooks/use-controllable-state.ts",
|
|
63
64
|
"./hooks/use-keyboard-state-sync": "./src/hooks/use-keyboard-state-sync.tsx",
|
|
@@ -86,7 +87,7 @@
|
|
|
86
87
|
},
|
|
87
88
|
"peerDependencies": {
|
|
88
89
|
"@central-icons-react-native/round-outlined-radius-1-stroke-1.5": "^1.1",
|
|
89
|
-
"@delacour/react-native-charts": "0.1.0-alpha.
|
|
90
|
+
"@delacour/react-native-charts": "0.1.0-alpha.20260925060916",
|
|
90
91
|
"@gorhom/bottom-sheet": "^5.2.8",
|
|
91
92
|
"@legendapp/list": ">=3.3",
|
|
92
93
|
"expo-linear-gradient": ">=15",
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# ToggleButton
|
|
2
|
+
|
|
3
|
+
A button that stays pressed — on its own, or as one of a group that owns the
|
|
4
|
+
selection. Compound root plus `ToggleButton.Label` and `ToggleButton.Group`.
|
|
5
|
+
|
|
6
|
+
`import { ToggleButton } from "@delacour/react-native-ui/toggle-button";`
|
|
7
|
+
|
|
8
|
+
## Files
|
|
9
|
+
|
|
10
|
+
| File | What it holds |
|
|
11
|
+
| --- | --- |
|
|
12
|
+
| `index.ts` | → `@delacour/react-native-ui/toggle-button` |
|
|
13
|
+
| `toggle-button.tsx` | Root + the `Object.assign` compound surface |
|
|
14
|
+
| `toggle-button-label.tsx` | `ToggleButton.Label` |
|
|
15
|
+
| `toggle-button-group.tsx` | `ToggleButton.Group`, which owns the selection |
|
|
16
|
+
| `toggle-button.context.tsx` | `ToggleButtonContext` and `ToggleButtonGroupContext`, with their hooks |
|
|
17
|
+
| `toggle-button.variants.ts` | The appearance table, the group's `tv()` and three pure resolvers |
|
|
18
|
+
| `toggle-button.variants.test.ts` | |
|
|
19
|
+
|
|
20
|
+
There is no `toggle-button.types.ts`: every prop type here has exactly one
|
|
21
|
+
consumer, so each lives beside the component it types. See
|
|
22
|
+
[Compound component layout](../../../AGENTS.md#compound-component-layout).
|
|
23
|
+
|
|
24
|
+
## Design
|
|
25
|
+
|
|
26
|
+
- **Variants**: `default`, `outline`, `ghost`. **Sizes**: the button's own —
|
|
27
|
+
`sm`, `md`, `lg` and the square `icon-sm`, `icon-md`, `icon-lg`. **Selection
|
|
28
|
+
modes**, on the group: `multiple` (default) and `single`. **Layouts**, on the
|
|
29
|
+
group: `attached` (default) and `detached`. **Orientations**, on the group:
|
|
30
|
+
`horizontal` and `vertical`.
|
|
31
|
+
- **A toggle is a `Button`, and owns no paint.** Each state names a button
|
|
32
|
+
variant — `TOGGLE_BUTTON_APPEARANCE` — and the toggle renders a `Button` with
|
|
33
|
+
whichever one applies. The fill, the border, the label colour, the colour a
|
|
34
|
+
composed `Icon` inherits, the corner, the loading spinner and the press
|
|
35
|
+
feedback all come from the button, so there is no second table of colours
|
|
36
|
+
here to drift from it. Every button variant draws the same `border` box, so
|
|
37
|
+
switching state never resizes the toggle. A test asserts every entry names a
|
|
38
|
+
real button variant and that `on` never equals `off`.
|
|
39
|
+
- **No state is destructive.** A pressed toggle is a choice, not a warning; the
|
|
40
|
+
appearance table never names `destructive` or `destructive-soft`, and a test
|
|
41
|
+
holds it there.
|
|
42
|
+
- **A square is a size, not a flag.** `size="icon-md"` is the button's own
|
|
43
|
+
square step — there is no `isIconOnly`. Always pair one with an
|
|
44
|
+
`accessibilityLabel`.
|
|
45
|
+
- **The state is announced, not only drawn.** `resolveToggleButtonAccessibility`
|
|
46
|
+
makes a standalone toggle, or a member of a `multiple` group, a
|
|
47
|
+
`togglebutton` with `accessibilityState.checked` — read out as on or off. A
|
|
48
|
+
member of a `single` group is a `radio` with `accessibilityState.selected`,
|
|
49
|
+
and that group is a `radiogroup`: announcing an either-or option as a toggle
|
|
50
|
+
promises that pressing it again turns it off, which a group with
|
|
51
|
+
`isSelectionRequired` refuses to do. A `multiple` group has no role, the same
|
|
52
|
+
call `Button.Group` makes.
|
|
53
|
+
- **The toggle keeps the role, the state and the press handler.**
|
|
54
|
+
`accessibilityRole`, `accessibilityState` and `variant` are `Omit`ted from its
|
|
55
|
+
props, and the three it computes are set *after* the caller's spread, so
|
|
56
|
+
nothing passed in can make the announced state and the drawn state disagree.
|
|
57
|
+
A caller's `onPress` still runs — first, before the state moves, so a
|
|
58
|
+
handler reading the state it closed over sees what the toggle was when
|
|
59
|
+
pressed.
|
|
60
|
+
- **A toggle works with or without a group.** On its own it is `isSelected` +
|
|
61
|
+
`onSelected`, or uncontrolled from `defaultSelected`, through
|
|
62
|
+
`useControllableState`. Inside a `ToggleButton.Group` its `value` identifies
|
|
63
|
+
it, the group owns the state, and its own `isSelected` is ignored. The hook is
|
|
64
|
+
still called in a group — a hook may not sit behind a branch — and its state
|
|
65
|
+
is never read. A grouped toggle with no `value` can never be selected, and
|
|
66
|
+
warns in development.
|
|
67
|
+
- **Children may be a function of the state.** `{({ isSelected }) => …}` is
|
|
68
|
+
for content that changes with the toggle — a label that reads *Saved* once
|
|
69
|
+
pressed, a glyph that swaps. `useToggleButton()` answers the same question
|
|
70
|
+
for a custom child that would rather read context.
|
|
71
|
+
- **`ToggleButton.Label` is a `Button.Label` with a part check.** The colour is
|
|
72
|
+
already right — the toggle hands its button the variant for the state it is
|
|
73
|
+
in — so the part adds nothing but an error that names the toggle when it is
|
|
74
|
+
rendered outside one.
|
|
75
|
+
|
|
76
|
+
## ToggleButton.Group
|
|
77
|
+
|
|
78
|
+
- **State is one array of the members' `value`s**: `selected` + `onSelected`,
|
|
79
|
+
or `defaultSelected` alone. `resolveToggleSelection` is the whole decision
|
|
80
|
+
and is pure: `multiple` adds or removes the one pressed, keeping press order;
|
|
81
|
+
`single` replaces the selection with it; a re-press in `single` clears it.
|
|
82
|
+
- **`isSelectionRequired` keeps an answer on the board.** The last selection
|
|
83
|
+
cannot be cleared, in either mode. A segmented choice like text alignment
|
|
84
|
+
always has one.
|
|
85
|
+
- **A press that changes nothing notifies nobody.** The resolver returns
|
|
86
|
+
`null` rather than the same list back, so `onSelected` is never called with a
|
|
87
|
+
value the caller already holds — the rule `Radio.Group` keeps for a re-press
|
|
88
|
+
of the current option.
|
|
89
|
+
- **A controlled `single` group handed several values collapses** to the one
|
|
90
|
+
pressed rather than carrying the others along.
|
|
91
|
+
- **`attached` is a `Button.Group`, not a copy of one.** The toggles join into
|
|
92
|
+
one run with the button's own corner squaring, hairline seam and fade-not-scale
|
|
93
|
+
feedback, and `Button.Group.Separator` works between two of them. Restating
|
|
94
|
+
any of that here is how the two runs would stop matching. An attached group
|
|
95
|
+
therefore owns the size *step* outright — controls of different heights do not
|
|
96
|
+
join — while a square member stays square (`resolveGroupedButtonSize`).
|
|
97
|
+
- **`detached` is a wrapping row, or a column, spaced by a gap** — the one thing
|
|
98
|
+
an attached run must never have. It is the shape for a row of filters. The
|
|
99
|
+
classes are compound cells on `layout` × `orientation`, and an attached group
|
|
100
|
+
emits none of them.
|
|
101
|
+
- **The axes are defaults, not overrides.** `variant`, `size`, `isDisabled` and
|
|
102
|
+
`haptic` are published raw, so a member's own value wins and one option can
|
|
103
|
+
disable itself inside a group that did not — `own ?? group ?? default`, the
|
|
104
|
+
`Checkbox.Group` ladder.
|
|
105
|
+
- **The group paints nothing.** A disabled group publishes `isDisabled` and each
|
|
106
|
+
member fades itself; a group fading too would compound the two.
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export { ToggleButton, type ToggleButtonProps, type ToggleButtonRenderState } from "./toggle-button";
|
|
2
|
+
export {
|
|
3
|
+
type ToggleButtonContextValue,
|
|
4
|
+
type ToggleButtonGroupContextValue,
|
|
5
|
+
ToggleButtonGroupProvider,
|
|
6
|
+
ToggleButtonProvider,
|
|
7
|
+
useToggleButton,
|
|
8
|
+
useToggleButtonContext,
|
|
9
|
+
useToggleButtonGroup,
|
|
10
|
+
useToggleButtonGroupContext,
|
|
11
|
+
} from "./toggle-button.context";
|
|
12
|
+
export {
|
|
13
|
+
resolveToggleButtonAccessibility,
|
|
14
|
+
resolveToggleButtonVariant,
|
|
15
|
+
resolveToggleSelection,
|
|
16
|
+
TOGGLE_BUTTON_APPEARANCE,
|
|
17
|
+
TOGGLE_BUTTON_GROUP_LAYOUTS,
|
|
18
|
+
TOGGLE_BUTTON_GROUP_ORIENTATIONS,
|
|
19
|
+
TOGGLE_BUTTON_SELECTION_MODES,
|
|
20
|
+
TOGGLE_BUTTON_VARIANTS,
|
|
21
|
+
type ToggleButtonAccessibility,
|
|
22
|
+
type ToggleButtonGroupLayout,
|
|
23
|
+
type ToggleButtonGroupOrientation,
|
|
24
|
+
type ToggleButtonSelectionMode,
|
|
25
|
+
type ToggleButtonVariant,
|
|
26
|
+
type ToggleButtonVariantProps,
|
|
27
|
+
toggleButtonVariants,
|
|
28
|
+
} from "./toggle-button.variants";
|
|
29
|
+
export type { ToggleButtonGroupProps } from "./toggle-button-group";
|
|
30
|
+
export type { ToggleButtonLabelProps } from "./toggle-button-label";
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { type ReactElement, type ReactNode, useCallback, useMemo } from "react";
|
|
2
|
+
import { View, type ViewProps } from "react-native";
|
|
3
|
+
import { useControllableState } from "../../hooks/use-controllable-state";
|
|
4
|
+
import { Button } from "../button";
|
|
5
|
+
import type { ButtonSize } from "../button/button.variants";
|
|
6
|
+
import type { HapticFeedback, PressableFeedback } from "../pressable";
|
|
7
|
+
import { type ToggleButtonGroupContextValue, ToggleButtonGroupProvider } from "./toggle-button.context";
|
|
8
|
+
import {
|
|
9
|
+
resolveToggleSelection,
|
|
10
|
+
type ToggleButtonGroupLayout,
|
|
11
|
+
type ToggleButtonGroupOrientation,
|
|
12
|
+
type ToggleButtonSelectionMode,
|
|
13
|
+
type ToggleButtonVariant,
|
|
14
|
+
toggleButtonVariants,
|
|
15
|
+
} from "./toggle-button.variants";
|
|
16
|
+
|
|
17
|
+
export type ToggleButtonGroupProps = Omit<ViewProps, "children"> & {
|
|
18
|
+
/** `multiple` marks independently; `single` is an either-or choice. Default `multiple`. */
|
|
19
|
+
selectionMode?: ToggleButtonSelectionMode;
|
|
20
|
+
/** The `value` of every selected toggle. Controlled. */
|
|
21
|
+
selected?: string[];
|
|
22
|
+
/** The values selected to begin with, while uncontrolled. */
|
|
23
|
+
defaultSelected?: string[];
|
|
24
|
+
/** Called with the whole new list. Never called for a press that changes nothing. */
|
|
25
|
+
onSelected?: (selected: string[]) => void;
|
|
26
|
+
/** Refuse to clear the last selection — a re-press of it does nothing. */
|
|
27
|
+
isSelectionRequired?: boolean;
|
|
28
|
+
/** `attached` joins the toggles into one run; `detached` spaces them apart. Default `attached`. */
|
|
29
|
+
layout?: ToggleButtonGroupLayout;
|
|
30
|
+
/** Which way the toggles run. Default `horizontal`. */
|
|
31
|
+
orientation?: ToggleButtonGroupOrientation;
|
|
32
|
+
/** A default every toggle takes unless it names its own. */
|
|
33
|
+
variant?: ToggleButtonVariant;
|
|
34
|
+
/** Shared by every toggle. An attached group owns the step outright. */
|
|
35
|
+
size?: ButtonSize;
|
|
36
|
+
/** Disables every toggle that does not say otherwise. */
|
|
37
|
+
isDisabled?: boolean;
|
|
38
|
+
/** Haptic for every toggle that does not name its own. */
|
|
39
|
+
haptic?: false | HapticFeedback;
|
|
40
|
+
/** Press treatment for every toggle. Unset, an attached toggle fades rather than scaling. */
|
|
41
|
+
feedback?: PressableFeedback;
|
|
42
|
+
className?: string;
|
|
43
|
+
children?: ReactNode;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
function ToggleButtonGroupRoot({
|
|
47
|
+
selectionMode = "multiple",
|
|
48
|
+
selected,
|
|
49
|
+
defaultSelected = EMPTY,
|
|
50
|
+
onSelected,
|
|
51
|
+
isSelectionRequired = false,
|
|
52
|
+
layout = "attached",
|
|
53
|
+
orientation = "horizontal",
|
|
54
|
+
variant,
|
|
55
|
+
size,
|
|
56
|
+
isDisabled,
|
|
57
|
+
haptic,
|
|
58
|
+
feedback,
|
|
59
|
+
className,
|
|
60
|
+
children,
|
|
61
|
+
...props
|
|
62
|
+
}: ToggleButtonGroupProps): ReactElement {
|
|
63
|
+
const [values, setValues] = useControllableState<string[]>({
|
|
64
|
+
defaultValue: defaultSelected,
|
|
65
|
+
onChange: onSelected,
|
|
66
|
+
value: selected,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const toggle = useCallback(
|
|
70
|
+
(value: string) => {
|
|
71
|
+
const next = resolveToggleSelection({ current: values, isSelectionRequired, selectionMode, value });
|
|
72
|
+
if (next !== null) setValues(next);
|
|
73
|
+
},
|
|
74
|
+
[isSelectionRequired, selectionMode, setValues, values]
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
const context = useMemo<ToggleButtonGroupContextValue>(
|
|
78
|
+
() => ({ haptic, isDisabled, layout, orientation, selected: values, selectionMode, size, toggle, variant }),
|
|
79
|
+
[haptic, isDisabled, layout, orientation, selectionMode, size, toggle, values, variant]
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
// A single-choice group is a radio group, and says so. A set of independent
|
|
83
|
+
// marks is a container with no action of its own, so it announces nothing —
|
|
84
|
+
// the same call `Button.Group` makes.
|
|
85
|
+
const accessibilityRole = selectionMode === "single" ? "radiogroup" : undefined;
|
|
86
|
+
|
|
87
|
+
return (
|
|
88
|
+
<ToggleButtonGroupProvider value={context}>
|
|
89
|
+
{layout === "attached" ? (
|
|
90
|
+
<Button.Group
|
|
91
|
+
accessibilityRole={accessibilityRole}
|
|
92
|
+
className={className}
|
|
93
|
+
feedback={feedback}
|
|
94
|
+
orientation={orientation}
|
|
95
|
+
size={size}
|
|
96
|
+
{...props}
|
|
97
|
+
>
|
|
98
|
+
{children}
|
|
99
|
+
</Button.Group>
|
|
100
|
+
) : (
|
|
101
|
+
<View
|
|
102
|
+
accessibilityRole={accessibilityRole}
|
|
103
|
+
className={toggleButtonVariants({ layout, orientation }).group({ className })}
|
|
104
|
+
{...props}
|
|
105
|
+
>
|
|
106
|
+
{children}
|
|
107
|
+
</View>
|
|
108
|
+
)}
|
|
109
|
+
</ToggleButtonGroupProvider>
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* A stable empty list, so an uncontrolled group does not seed its state from a
|
|
115
|
+
* fresh array on every render.
|
|
116
|
+
*/
|
|
117
|
+
const EMPTY: string[] = [];
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Toggles sharing one selection.
|
|
121
|
+
*
|
|
122
|
+
* State is one array of the members' `value`s, controlled with `selected` and
|
|
123
|
+
* `onSelected` or held by the group from `defaultSelected`. `selectionMode`
|
|
124
|
+
* decides what a press does: `multiple` adds or removes the one pressed, while
|
|
125
|
+
* `single` replaces the selection with it. `isSelectionRequired` keeps the last
|
|
126
|
+
* one from being cleared — a segmented choice always has an answer.
|
|
127
|
+
*
|
|
128
|
+
* `attached` — the default — renders a `Button.Group`, so the toggles join into
|
|
129
|
+
* one run with shared corners and a hairline seam, and `Button.Group.Separator`
|
|
130
|
+
* works between them. `detached` spaces them apart and wraps, for a row of
|
|
131
|
+
* filters.
|
|
132
|
+
*
|
|
133
|
+
* `variant`, `size`, `isDisabled` and `haptic` are defaults a member may
|
|
134
|
+
* override, so one option can disable itself. An attached group owns the size
|
|
135
|
+
* step outright, because controls of different heights do not join; a square
|
|
136
|
+
* member stays square.
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* <ToggleButton.Group defaultSelected={["bold"]} variant="ghost">
|
|
140
|
+
* <ToggleButton accessibilityLabel="Bold" size="icon-md" value="bold">
|
|
141
|
+
* <Icon icon={IconBold} />
|
|
142
|
+
* </ToggleButton>
|
|
143
|
+
* <ToggleButton accessibilityLabel="Italic" size="icon-md" value="italic">
|
|
144
|
+
* <Icon icon={IconItalic} />
|
|
145
|
+
* </ToggleButton>
|
|
146
|
+
* </ToggleButton.Group>
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* <ToggleButton.Group layout="detached" onSelected={setFilters} selected={filters} size="sm" variant="outline">
|
|
150
|
+
* <ToggleButton value="open">Open</ToggleButton>
|
|
151
|
+
* <ToggleButton value="closed">Closed</ToggleButton>
|
|
152
|
+
* </ToggleButton.Group>
|
|
153
|
+
*/
|
|
154
|
+
export const ToggleButtonGroup = Object.assign(ToggleButtonGroupRoot, {
|
|
155
|
+
displayName: "DelacourUI.ToggleButton.Group",
|
|
156
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ReactElement } from "react";
|
|
2
|
+
import { Button, type ButtonLabelProps } from "../button";
|
|
3
|
+
import { useToggleButtonPart } from "./toggle-button.context";
|
|
4
|
+
|
|
5
|
+
export type ToggleButtonLabelProps = ButtonLabelProps;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The toggle's text.
|
|
9
|
+
*
|
|
10
|
+
* A `Button.Label`, and nothing more: the toggle hands its button the variant
|
|
11
|
+
* for the state it is in, so the label already reads the right colour off the
|
|
12
|
+
* button's context. A part of its own anyway, so a label placed outside a toggle
|
|
13
|
+
* fails with a message that names the toggle rather than the button.
|
|
14
|
+
*/
|
|
15
|
+
export function ToggleButtonLabel(props: ToggleButtonLabelProps): ReactElement {
|
|
16
|
+
useToggleButtonPart("ToggleButton.Label");
|
|
17
|
+
return <Button.Label {...props} />;
|
|
18
|
+
}
|
|
19
|
+
ToggleButtonLabel.displayName = "DelacourUI.ToggleButton.Label";
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { createContext, type ReactElement, type ReactNode, use } from "react";
|
|
2
|
+
import type { ButtonSize } from "../button/button.variants";
|
|
3
|
+
import type { HapticFeedback } from "../pressable";
|
|
4
|
+
import type {
|
|
5
|
+
ToggleButtonGroupLayout,
|
|
6
|
+
ToggleButtonGroupOrientation,
|
|
7
|
+
ToggleButtonSelectionMode,
|
|
8
|
+
ToggleButtonVariant,
|
|
9
|
+
} from "./toggle-button.variants";
|
|
10
|
+
|
|
11
|
+
export type ToggleButtonContextValue = {
|
|
12
|
+
/** Whether the toggle is pressed in right now. */
|
|
13
|
+
isSelected: boolean;
|
|
14
|
+
/** Whether the toggle is disabled, once its group has had its say. */
|
|
15
|
+
isDisabled: boolean;
|
|
16
|
+
/** The toggle's own variant, not the button variant it resolves to. */
|
|
17
|
+
variant: ToggleButtonVariant;
|
|
18
|
+
/** The size the toggle asked for, or `undefined` to take the button's default. */
|
|
19
|
+
size: ButtonSize | undefined;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const ToggleButtonContext = createContext<ToggleButtonContextValue | null>(null);
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Supplies the enclosing toggle's state to its subtree.
|
|
26
|
+
*
|
|
27
|
+
* In a leaf of its own, importing nothing but types, so a part can read it
|
|
28
|
+
* without importing `./toggle-button` and closing a cycle (package AGENTS.md
|
|
29
|
+
* rule 3).
|
|
30
|
+
*/
|
|
31
|
+
export function ToggleButtonProvider({
|
|
32
|
+
value,
|
|
33
|
+
children,
|
|
34
|
+
}: {
|
|
35
|
+
value: ToggleButtonContextValue;
|
|
36
|
+
children: ReactNode;
|
|
37
|
+
}): ReactElement {
|
|
38
|
+
return <ToggleButtonContext value={value}>{children}</ToggleButtonContext>;
|
|
39
|
+
}
|
|
40
|
+
ToggleButtonProvider.displayName = "DelacourUI.ToggleButton.Provider";
|
|
41
|
+
|
|
42
|
+
/** The enclosing toggle's state, or null outside a `<ToggleButton>`. */
|
|
43
|
+
export function useToggleButtonContext(): ToggleButtonContextValue | null {
|
|
44
|
+
return use(ToggleButtonContext);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Reads the enclosing toggle's state.
|
|
49
|
+
*
|
|
50
|
+
* For a custom child that changes with the toggle — a glyph that fills when
|
|
51
|
+
* selected. Throws outside a `<ToggleButton>` — use
|
|
52
|
+
* {@link useToggleButtonContext} where the toggle is optional.
|
|
53
|
+
*/
|
|
54
|
+
export function useToggleButton(): ToggleButtonContextValue {
|
|
55
|
+
const context = useToggleButtonContext();
|
|
56
|
+
if (!context) {
|
|
57
|
+
throw new Error("useToggleButton must be called inside a <ToggleButton>.");
|
|
58
|
+
}
|
|
59
|
+
return context;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The enclosing toggle's state, for a compound part that cannot work without it.
|
|
64
|
+
*
|
|
65
|
+
* Internal: deliberately not re-exported from `index.ts`.
|
|
66
|
+
*/
|
|
67
|
+
export function useToggleButtonPart(component: string): ToggleButtonContextValue {
|
|
68
|
+
const context = useToggleButtonContext();
|
|
69
|
+
if (!context) {
|
|
70
|
+
throw new Error(`${component} must be rendered inside a <ToggleButton>.`);
|
|
71
|
+
}
|
|
72
|
+
return context;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export type ToggleButtonGroupContextValue = {
|
|
76
|
+
/** The `value` of every selected member. */
|
|
77
|
+
selected: readonly string[];
|
|
78
|
+
/** Presses the member called `value`, as the selection mode decides. */
|
|
79
|
+
toggle: (value: string) => void;
|
|
80
|
+
selectionMode: ToggleButtonSelectionMode;
|
|
81
|
+
layout: ToggleButtonGroupLayout;
|
|
82
|
+
orientation: ToggleButtonGroupOrientation;
|
|
83
|
+
/** A default a member may override. */
|
|
84
|
+
variant?: ToggleButtonVariant;
|
|
85
|
+
/** A default a member may override. An attached group owns the step outright. */
|
|
86
|
+
size?: ButtonSize;
|
|
87
|
+
/** A default a member may override, so one option can disable itself. */
|
|
88
|
+
isDisabled?: boolean;
|
|
89
|
+
/** A default a member may override. */
|
|
90
|
+
haptic?: false | HapticFeedback;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const ToggleButtonGroupContext = createContext<ToggleButtonGroupContextValue | null>(null);
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Supplies a group's selection and shared axes to its members.
|
|
97
|
+
*
|
|
98
|
+
* The optional axes are published raw, so `undefined` means "the group said
|
|
99
|
+
* nothing" and a member's `??` ladder can see past it.
|
|
100
|
+
*/
|
|
101
|
+
export function ToggleButtonGroupProvider({
|
|
102
|
+
value,
|
|
103
|
+
children,
|
|
104
|
+
}: {
|
|
105
|
+
value: ToggleButtonGroupContextValue;
|
|
106
|
+
children: ReactNode;
|
|
107
|
+
}): ReactElement {
|
|
108
|
+
return <ToggleButtonGroupContext value={value}>{children}</ToggleButtonGroupContext>;
|
|
109
|
+
}
|
|
110
|
+
ToggleButtonGroupProvider.displayName = "DelacourUI.ToggleButton.Group.Provider";
|
|
111
|
+
|
|
112
|
+
/** The enclosing group, or null for a toggle standing on its own. */
|
|
113
|
+
export function useToggleButtonGroupContext(): ToggleButtonGroupContextValue | null {
|
|
114
|
+
return use(ToggleButtonGroupContext);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Reads the enclosing group's selection and axes.
|
|
119
|
+
*
|
|
120
|
+
* Throws outside a `<ToggleButton.Group>` — use
|
|
121
|
+
* {@link useToggleButtonGroupContext} where the group is optional, as a
|
|
122
|
+
* `ToggleButton` itself does.
|
|
123
|
+
*/
|
|
124
|
+
export function useToggleButtonGroup(): ToggleButtonGroupContextValue {
|
|
125
|
+
const context = useToggleButtonGroupContext();
|
|
126
|
+
if (!context) {
|
|
127
|
+
throw new Error("useToggleButtonGroup must be called inside a <ToggleButton.Group>.");
|
|
128
|
+
}
|
|
129
|
+
return context;
|
|
130
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { type ReactElement, type ReactNode, useCallback, useMemo } from "react";
|
|
2
|
+
import { useControllableState } from "../../hooks/use-controllable-state";
|
|
3
|
+
import { Button, type ButtonProps } from "../button";
|
|
4
|
+
import {
|
|
5
|
+
type ToggleButtonContextValue,
|
|
6
|
+
ToggleButtonProvider,
|
|
7
|
+
useToggleButtonGroupContext,
|
|
8
|
+
} from "./toggle-button.context";
|
|
9
|
+
import {
|
|
10
|
+
resolveToggleButtonAccessibility,
|
|
11
|
+
resolveToggleButtonVariant,
|
|
12
|
+
type ToggleButtonVariant,
|
|
13
|
+
} from "./toggle-button.variants";
|
|
14
|
+
import { ToggleButtonGroup } from "./toggle-button-group";
|
|
15
|
+
import { ToggleButtonLabel } from "./toggle-button-label";
|
|
16
|
+
|
|
17
|
+
/** What a render-function child is handed. */
|
|
18
|
+
export type ToggleButtonRenderState = {
|
|
19
|
+
isSelected: boolean;
|
|
20
|
+
isDisabled: boolean;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export type ToggleButtonProps = Omit<
|
|
24
|
+
ButtonProps,
|
|
25
|
+
"accessibilityRole" | "accessibilityState" | "children" | "variant"
|
|
26
|
+
> & {
|
|
27
|
+
/** Identifies the toggle inside a `ToggleButton.Group`. Required there. */
|
|
28
|
+
value?: string;
|
|
29
|
+
/** How the toggle paints itself, off and on. Inherited from a group when not given. */
|
|
30
|
+
variant?: ToggleButtonVariant;
|
|
31
|
+
/** Controlled selection. Ignored inside a group, which owns the state. */
|
|
32
|
+
isSelected?: boolean;
|
|
33
|
+
/** Starting state while uncontrolled and outside a group. */
|
|
34
|
+
defaultSelected?: boolean;
|
|
35
|
+
/** Called with the new state on every press. Not called inside a group — the group's `onSelected` is. */
|
|
36
|
+
onSelected?: (isSelected: boolean) => void;
|
|
37
|
+
/** Content, or a function of the toggle's state for content that changes with it. */
|
|
38
|
+
children?: ReactNode | ((state: ToggleButtonRenderState) => ReactNode);
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
function ToggleButtonRoot({
|
|
42
|
+
value,
|
|
43
|
+
variant,
|
|
44
|
+
size,
|
|
45
|
+
isSelected,
|
|
46
|
+
defaultSelected = false,
|
|
47
|
+
onSelected,
|
|
48
|
+
isDisabled,
|
|
49
|
+
haptic,
|
|
50
|
+
onPress,
|
|
51
|
+
children,
|
|
52
|
+
...props
|
|
53
|
+
}: ToggleButtonProps): ReactElement {
|
|
54
|
+
const group = useToggleButtonGroupContext();
|
|
55
|
+
|
|
56
|
+
// Called unconditionally, grouped or not: a hook may not sit behind a branch.
|
|
57
|
+
// Inside a group its state is simply never read.
|
|
58
|
+
const [ownSelected, setOwnSelected] = useControllableState<boolean>({
|
|
59
|
+
defaultValue: defaultSelected,
|
|
60
|
+
onChange: onSelected,
|
|
61
|
+
value: isSelected,
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
if (process.env.NODE_ENV !== "production" && group && value === undefined) {
|
|
65
|
+
console.warn(
|
|
66
|
+
"ToggleButton: a <ToggleButton> inside a <ToggleButton.Group> needs a `value`, or it can never be selected."
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const resolvedIsSelected = group ? value !== undefined && group.selected.includes(value) : ownSelected;
|
|
71
|
+
const resolvedVariant = variant ?? group?.variant ?? "default";
|
|
72
|
+
const resolvedSize = size ?? group?.size;
|
|
73
|
+
const resolvedIsDisabled = isDisabled ?? group?.isDisabled ?? false;
|
|
74
|
+
|
|
75
|
+
// The caller's `onPress` runs first, then the state moves — so a handler
|
|
76
|
+
// reading the state it closed over sees what the toggle was when pressed.
|
|
77
|
+
const handlePress = useCallback(() => {
|
|
78
|
+
onPress?.();
|
|
79
|
+
if (group) {
|
|
80
|
+
if (value !== undefined) group.toggle(value);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
setOwnSelected(!ownSelected);
|
|
84
|
+
}, [group, onPress, ownSelected, setOwnSelected, value]);
|
|
85
|
+
|
|
86
|
+
const context = useMemo<ToggleButtonContextValue>(
|
|
87
|
+
() => ({
|
|
88
|
+
isDisabled: resolvedIsDisabled,
|
|
89
|
+
isSelected: resolvedIsSelected,
|
|
90
|
+
size: resolvedSize,
|
|
91
|
+
variant: resolvedVariant,
|
|
92
|
+
}),
|
|
93
|
+
[resolvedIsDisabled, resolvedIsSelected, resolvedSize, resolvedVariant]
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
const accessibility = resolveToggleButtonAccessibility(group?.selectionMode ?? null, resolvedIsSelected);
|
|
97
|
+
|
|
98
|
+
const content =
|
|
99
|
+
typeof children === "function"
|
|
100
|
+
? children({ isDisabled: resolvedIsDisabled, isSelected: resolvedIsSelected })
|
|
101
|
+
: children;
|
|
102
|
+
|
|
103
|
+
// The role, state and press handler are set after the caller's props on
|
|
104
|
+
// purpose: they are what keeps the announced state and the drawn state one
|
|
105
|
+
// and the same, so nothing passed in may pull them apart.
|
|
106
|
+
return (
|
|
107
|
+
<ToggleButtonProvider value={context}>
|
|
108
|
+
<Button
|
|
109
|
+
{...props}
|
|
110
|
+
accessibilityRole={accessibility.accessibilityRole}
|
|
111
|
+
accessibilityState={accessibility.accessibilityState}
|
|
112
|
+
haptic={haptic ?? group?.haptic}
|
|
113
|
+
isDisabled={resolvedIsDisabled}
|
|
114
|
+
onPress={handlePress}
|
|
115
|
+
size={resolvedSize}
|
|
116
|
+
variant={resolveToggleButtonVariant(resolvedVariant, resolvedIsSelected)}
|
|
117
|
+
>
|
|
118
|
+
{content}
|
|
119
|
+
</Button>
|
|
120
|
+
</ToggleButtonProvider>
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* A button that stays pressed — on its own, or as one of a group.
|
|
126
|
+
*
|
|
127
|
+
* It is a `Button`, and draws itself with one: each state names a button
|
|
128
|
+
* variant (`TOGGLE_BUTTON_APPEARANCE`), so the fill, the label colour and the
|
|
129
|
+
* colour a composed `Icon` inherits all come off the button's own tables. Sizes
|
|
130
|
+
* are the button's, square `icon-*` steps included, and so are `haptic`,
|
|
131
|
+
* `feedback`, `isLoading` and the rest.
|
|
132
|
+
*
|
|
133
|
+
* On its own it is controlled with `isSelected` and `onSelected`, or
|
|
134
|
+
* uncontrolled from `defaultSelected`. Inside a `ToggleButton.Group` its `value`
|
|
135
|
+
* identifies it and the group owns the state.
|
|
136
|
+
*
|
|
137
|
+
* The state is announced, not only drawn: a toggle is a `togglebutton` that is
|
|
138
|
+
* checked or not, and a member of a `single` group is a `radio` that is selected
|
|
139
|
+
* or not. A square toggle holds no text, so give it an `accessibilityLabel`.
|
|
140
|
+
*
|
|
141
|
+
* Children may be a function of the state, for content that changes with it.
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* <ToggleButton defaultSelected>Follow</ToggleButton>
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* <ToggleButton isSelected={saved} onSelected={setSaved} variant="outline">
|
|
148
|
+
* {({ isSelected }) => (
|
|
149
|
+
* <>
|
|
150
|
+
* <Icon icon={isSelected ? IconBookmarkCheck : IconBookmark} />
|
|
151
|
+
* <ToggleButton.Label>{isSelected ? "Saved" : "Save"}</ToggleButton.Label>
|
|
152
|
+
* </>
|
|
153
|
+
* )}
|
|
154
|
+
* </ToggleButton>
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* <ToggleButton.Group selectionMode="single" defaultSelected={["left"]} isSelectionRequired>
|
|
158
|
+
* <ToggleButton accessibilityLabel="Align left" size="icon-md" value="left">
|
|
159
|
+
* <Icon icon={IconAlignmentLeft} />
|
|
160
|
+
* </ToggleButton>
|
|
161
|
+
* <ToggleButton accessibilityLabel="Align centre" size="icon-md" value="center">
|
|
162
|
+
* <Icon icon={IconAlignmentCenter} />
|
|
163
|
+
* </ToggleButton>
|
|
164
|
+
* </ToggleButton.Group>
|
|
165
|
+
*/
|
|
166
|
+
export const ToggleButton = Object.assign(ToggleButtonRoot, {
|
|
167
|
+
/** The toggle's text. Picks its colour from the state the toggle is in. */
|
|
168
|
+
Label: ToggleButtonLabel,
|
|
169
|
+
/** Toggles sharing one selection, joined into a run or spaced apart. */
|
|
170
|
+
Group: ToggleButtonGroup,
|
|
171
|
+
displayName: "DelacourUI.ToggleButton",
|
|
172
|
+
});
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { BUTTON_VARIANTS } from "../button/button.variants";
|
|
3
|
+
import {
|
|
4
|
+
resolveToggleButtonAccessibility,
|
|
5
|
+
resolveToggleButtonVariant,
|
|
6
|
+
resolveToggleSelection,
|
|
7
|
+
TOGGLE_BUTTON_APPEARANCE,
|
|
8
|
+
TOGGLE_BUTTON_GROUP_LAYOUTS,
|
|
9
|
+
TOGGLE_BUTTON_SELECTION_MODES,
|
|
10
|
+
TOGGLE_BUTTON_VARIANTS,
|
|
11
|
+
toggleButtonVariants,
|
|
12
|
+
} from "./toggle-button.variants";
|
|
13
|
+
|
|
14
|
+
describe("TOGGLE_BUTTON_APPEARANCE", () => {
|
|
15
|
+
test("covers every variant, in both states", () => {
|
|
16
|
+
expect(Object.keys(TOGGLE_BUTTON_APPEARANCE).sort()).toEqual([...TOGGLE_BUTTON_VARIANTS].sort());
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
test("names only button variants, so the button's own paint and foreground apply", () => {
|
|
20
|
+
for (const variant of TOGGLE_BUTTON_VARIANTS) {
|
|
21
|
+
expect(BUTTON_VARIANTS).toContain(TOGGLE_BUTTON_APPEARANCE[variant].off);
|
|
22
|
+
expect(BUTTON_VARIANTS).toContain(TOGGLE_BUTTON_APPEARANCE[variant].on);
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test("a selected button never looks like an unselected one", () => {
|
|
27
|
+
for (const variant of TOGGLE_BUTTON_VARIANTS) {
|
|
28
|
+
expect(TOGGLE_BUTTON_APPEARANCE[variant].on).not.toBe(TOGGLE_BUTTON_APPEARANCE[variant].off);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test("never paints a toggle destructive", () => {
|
|
33
|
+
for (const variant of TOGGLE_BUTTON_VARIANTS) {
|
|
34
|
+
for (const state of ["on", "off"] as const) {
|
|
35
|
+
expect(TOGGLE_BUTTON_APPEARANCE[variant][state].startsWith("destructive")).toBe(false);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
describe("resolveToggleButtonVariant", () => {
|
|
42
|
+
test("reads the appearance table", () => {
|
|
43
|
+
expect(resolveToggleButtonVariant("default", false)).toBe("secondary");
|
|
44
|
+
expect(resolveToggleButtonVariant("default", true)).toBe("primary");
|
|
45
|
+
expect(resolveToggleButtonVariant("outline", false)).toBe("outline");
|
|
46
|
+
expect(resolveToggleButtonVariant("outline", true)).toBe("primary");
|
|
47
|
+
expect(resolveToggleButtonVariant("ghost", false)).toBe("ghost");
|
|
48
|
+
expect(resolveToggleButtonVariant("ghost", true)).toBe("secondary");
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
describe("resolveToggleSelection — multiple", () => {
|
|
53
|
+
const mode = "multiple" as const;
|
|
54
|
+
|
|
55
|
+
test("adds a value that was not selected, keeping press order", () => {
|
|
56
|
+
expect(resolveToggleSelection({ current: ["bold"], selectionMode: mode, value: "italic" })).toEqual([
|
|
57
|
+
"bold",
|
|
58
|
+
"italic",
|
|
59
|
+
]);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test("removes a value that was selected", () => {
|
|
63
|
+
expect(resolveToggleSelection({ current: ["bold", "italic"], selectionMode: mode, value: "bold" })).toEqual([
|
|
64
|
+
"italic",
|
|
65
|
+
]);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test("clears the last value when a selection is not required", () => {
|
|
69
|
+
expect(resolveToggleSelection({ current: ["bold"], selectionMode: mode, value: "bold" })).toEqual([]);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test("refuses to clear the last value when a selection is required", () => {
|
|
73
|
+
expect(
|
|
74
|
+
resolveToggleSelection({ current: ["bold"], isSelectionRequired: true, selectionMode: mode, value: "bold" })
|
|
75
|
+
).toBeNull();
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test("still removes one of several when a selection is required", () => {
|
|
79
|
+
expect(
|
|
80
|
+
resolveToggleSelection({
|
|
81
|
+
current: ["bold", "italic"],
|
|
82
|
+
isSelectionRequired: true,
|
|
83
|
+
selectionMode: mode,
|
|
84
|
+
value: "bold",
|
|
85
|
+
})
|
|
86
|
+
).toEqual(["italic"]);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test("does not mutate the list it was given", () => {
|
|
90
|
+
const current = ["bold"];
|
|
91
|
+
resolveToggleSelection({ current, selectionMode: mode, value: "italic" });
|
|
92
|
+
resolveToggleSelection({ current, selectionMode: mode, value: "bold" });
|
|
93
|
+
expect(current).toEqual(["bold"]);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
describe("resolveToggleSelection — single", () => {
|
|
98
|
+
const mode = "single" as const;
|
|
99
|
+
|
|
100
|
+
test("replaces the selection with the pressed value", () => {
|
|
101
|
+
expect(resolveToggleSelection({ current: ["left"], selectionMode: mode, value: "center" })).toEqual(["center"]);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test("selects from nothing", () => {
|
|
105
|
+
expect(resolveToggleSelection({ current: [], selectionMode: mode, value: "center" })).toEqual(["center"]);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test("a re-press clears the selection when one is not required", () => {
|
|
109
|
+
expect(resolveToggleSelection({ current: ["left"], selectionMode: mode, value: "left" })).toEqual([]);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test("a re-press is no change when a selection is required", () => {
|
|
113
|
+
expect(
|
|
114
|
+
resolveToggleSelection({ current: ["left"], isSelectionRequired: true, selectionMode: mode, value: "left" })
|
|
115
|
+
).toBeNull();
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test("collapses a controlled list that arrived with several values", () => {
|
|
119
|
+
expect(resolveToggleSelection({ current: ["left", "right"], selectionMode: mode, value: "center" })).toEqual([
|
|
120
|
+
"center",
|
|
121
|
+
]);
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
describe("resolveToggleButtonAccessibility", () => {
|
|
126
|
+
test("a standalone button is a toggle button that is checked or not", () => {
|
|
127
|
+
expect(resolveToggleButtonAccessibility(null, true)).toEqual({
|
|
128
|
+
accessibilityRole: "togglebutton",
|
|
129
|
+
accessibilityState: { checked: true },
|
|
130
|
+
});
|
|
131
|
+
expect(resolveToggleButtonAccessibility(null, false)).toEqual({
|
|
132
|
+
accessibilityRole: "togglebutton",
|
|
133
|
+
accessibilityState: { checked: false },
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
test("a member of a multiple-selection group is still a toggle button", () => {
|
|
138
|
+
expect(resolveToggleButtonAccessibility("multiple", true)).toEqual({
|
|
139
|
+
accessibilityRole: "togglebutton",
|
|
140
|
+
accessibilityState: { checked: true },
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
test("a member of a single-selection group is a radio, selected or not", () => {
|
|
145
|
+
expect(resolveToggleButtonAccessibility("single", true)).toEqual({
|
|
146
|
+
accessibilityRole: "radio",
|
|
147
|
+
accessibilityState: { selected: true },
|
|
148
|
+
});
|
|
149
|
+
expect(resolveToggleButtonAccessibility("single", false)).toEqual({
|
|
150
|
+
accessibilityRole: "radio",
|
|
151
|
+
accessibilityState: { selected: false },
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
describe("toggleButtonVariants", () => {
|
|
157
|
+
test("exposes both selection modes and both layouts", () => {
|
|
158
|
+
expect([...TOGGLE_BUTTON_SELECTION_MODES]).toEqual(["single", "multiple"]);
|
|
159
|
+
expect([...TOGGLE_BUTTON_GROUP_LAYOUTS]).toEqual(["attached", "detached"]);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
test("an attached group adds nothing — Button.Group draws the run", () => {
|
|
163
|
+
// `tv` returns undefined rather than "" for a slot with no classes.
|
|
164
|
+
expect(toggleButtonVariants({ layout: "attached", orientation: "horizontal" }).group() ?? "").toBe("");
|
|
165
|
+
expect(toggleButtonVariants({ layout: "attached", orientation: "vertical" }).group() ?? "").toBe("");
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
test("a detached horizontal group wraps with a gap", () => {
|
|
169
|
+
const group = toggleButtonVariants({ layout: "detached", orientation: "horizontal" }).group();
|
|
170
|
+
expect(group).toContain("flex-row");
|
|
171
|
+
expect(group).toContain("flex-wrap");
|
|
172
|
+
expect(group).toContain("gap-2");
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
test("a detached vertical group stacks with a gap", () => {
|
|
176
|
+
const group = toggleButtonVariants({ layout: "detached", orientation: "vertical" }).group();
|
|
177
|
+
expect(group).toContain("flex-col");
|
|
178
|
+
expect(group).toContain("gap-2");
|
|
179
|
+
expect(group).not.toContain("flex-row");
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
test("a caller's className merges over the group's own", () => {
|
|
183
|
+
const group = toggleButtonVariants({ layout: "detached", orientation: "horizontal" }).group({
|
|
184
|
+
className: "gap-4",
|
|
185
|
+
});
|
|
186
|
+
expect(group).toContain("gap-4");
|
|
187
|
+
expect(group).not.toContain("gap-2");
|
|
188
|
+
});
|
|
189
|
+
});
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import type { VariantProps } from "tailwind-variants";
|
|
2
|
+
import { tv } from "../../lib/tv";
|
|
3
|
+
import type { ButtonVariant } from "../button/button.variants";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* How a toggle paints itself, off and on.
|
|
7
|
+
*
|
|
8
|
+
* `default` is a quiet fill that turns into the action colour; `outline` is a
|
|
9
|
+
* bordered button that fills; `ghost` is bare until pressed and then takes the
|
|
10
|
+
* quiet fill, which is what a toolbar of formatting marks wants.
|
|
11
|
+
*/
|
|
12
|
+
export const TOGGLE_BUTTON_VARIANTS = ["default", "outline", "ghost"] as const;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* `multiple` is a set of independent marks — bold *and* italic. `single` is an
|
|
16
|
+
* either-or choice, where picking one clears the last.
|
|
17
|
+
*/
|
|
18
|
+
export const TOGGLE_BUTTON_SELECTION_MODES = ["single", "multiple"] as const;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* `attached` joins the members into one run through `Button.Group`, sharing a
|
|
22
|
+
* corner and a seam. `detached` spaces them apart and wraps — a row of filters.
|
|
23
|
+
*/
|
|
24
|
+
export const TOGGLE_BUTTON_GROUP_LAYOUTS = ["attached", "detached"] as const;
|
|
25
|
+
|
|
26
|
+
export const TOGGLE_BUTTON_GROUP_ORIENTATIONS = ["horizontal", "vertical"] as const;
|
|
27
|
+
|
|
28
|
+
export type ToggleButtonVariant = (typeof TOGGLE_BUTTON_VARIANTS)[number];
|
|
29
|
+
export type ToggleButtonSelectionMode = (typeof TOGGLE_BUTTON_SELECTION_MODES)[number];
|
|
30
|
+
export type ToggleButtonGroupLayout = (typeof TOGGLE_BUTTON_GROUP_LAYOUTS)[number];
|
|
31
|
+
export type ToggleButtonGroupOrientation = (typeof TOGGLE_BUTTON_GROUP_ORIENTATIONS)[number];
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The `Button` variant each toggle variant draws with, off and on.
|
|
35
|
+
*
|
|
36
|
+
* A toggle owns no paint of its own. Every state is a button variant, so the
|
|
37
|
+
* fill, the border, the label colour and the colour a composed `Icon` inherits
|
|
38
|
+
* all come off `buttonVariants` and `BUTTON_FOREGROUND_TOKEN` — there is no
|
|
39
|
+
* second table of colours here to drift from the button's. Every variant the
|
|
40
|
+
* button already has draws with the same `border` box, so switching state never
|
|
41
|
+
* resizes the toggle.
|
|
42
|
+
*
|
|
43
|
+
* No state is ever destructive: a pressed toggle is a choice, not a warning.
|
|
44
|
+
*/
|
|
45
|
+
export const TOGGLE_BUTTON_APPEARANCE: Record<ToggleButtonVariant, { off: ButtonVariant; on: ButtonVariant }> = {
|
|
46
|
+
default: { off: "secondary", on: "primary" },
|
|
47
|
+
outline: { off: "outline", on: "primary" },
|
|
48
|
+
ghost: { off: "ghost", on: "secondary" },
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Styling for the group's box. The toggle itself is a `Button` and carries none.
|
|
53
|
+
*
|
|
54
|
+
* An `attached` group adds nothing: it renders a `Button.Group`, which already
|
|
55
|
+
* owns the run's direction, its corners and its seams. A `detached` group is a
|
|
56
|
+
* plain wrapping row, or a column, spaced by a gap — the one thing an attached
|
|
57
|
+
* run must never have.
|
|
58
|
+
*
|
|
59
|
+
* Free of React Native imports so it stays unit-testable. See AGENTS.md.
|
|
60
|
+
*/
|
|
61
|
+
export const toggleButtonVariants = tv({
|
|
62
|
+
slots: {
|
|
63
|
+
group: "",
|
|
64
|
+
},
|
|
65
|
+
variants: {
|
|
66
|
+
layout: { attached: {}, detached: {} },
|
|
67
|
+
orientation: { horizontal: {}, vertical: {} },
|
|
68
|
+
},
|
|
69
|
+
compoundVariants: [
|
|
70
|
+
{ layout: "detached", orientation: "horizontal", class: { group: "flex-row flex-wrap gap-2" } },
|
|
71
|
+
{ layout: "detached", orientation: "vertical", class: { group: "flex-col gap-2" } },
|
|
72
|
+
],
|
|
73
|
+
defaultVariants: {
|
|
74
|
+
layout: "attached",
|
|
75
|
+
orientation: "horizontal",
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The `Button` variant a toggle draws with right now.
|
|
81
|
+
*
|
|
82
|
+
* Pure, so the whole matrix is reachable from `bun test`. See AGENTS.md.
|
|
83
|
+
*/
|
|
84
|
+
export function resolveToggleButtonVariant(variant: ToggleButtonVariant, isSelected: boolean): ButtonVariant {
|
|
85
|
+
return TOGGLE_BUTTON_APPEARANCE[variant][isSelected ? "on" : "off"];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The group's next selection after `value` is pressed, or `null` for no change.
|
|
90
|
+
*
|
|
91
|
+
* `null` rather than the same list back, so the group never re-notifies a caller
|
|
92
|
+
* with a value it already holds — the same rule `Radio.Group` keeps for a
|
|
93
|
+
* re-press of the current option.
|
|
94
|
+
*
|
|
95
|
+
* - `multiple` adds or removes `value`, keeping the order things were pressed in.
|
|
96
|
+
* - `single` replaces the selection with `value`. A re-press of the selected one
|
|
97
|
+
* clears it, unless `isSelectionRequired`, when it is no change at all — a
|
|
98
|
+
* segmented choice like text alignment always has an answer.
|
|
99
|
+
* - `isSelectionRequired` also refuses to empty a `multiple` group, so the last
|
|
100
|
+
* mark cannot be taken off.
|
|
101
|
+
*
|
|
102
|
+
* A controlled `single` group handed several values collapses to the one pressed
|
|
103
|
+
* rather than carrying the others along.
|
|
104
|
+
*
|
|
105
|
+
* Pure, so the whole matrix is reachable from `bun test`. See AGENTS.md.
|
|
106
|
+
*/
|
|
107
|
+
export function resolveToggleSelection({
|
|
108
|
+
current,
|
|
109
|
+
value,
|
|
110
|
+
selectionMode,
|
|
111
|
+
isSelectionRequired = false,
|
|
112
|
+
}: {
|
|
113
|
+
current: readonly string[];
|
|
114
|
+
value: string;
|
|
115
|
+
selectionMode: ToggleButtonSelectionMode;
|
|
116
|
+
isSelectionRequired?: boolean;
|
|
117
|
+
}): string[] | null {
|
|
118
|
+
const isSelected = current.includes(value);
|
|
119
|
+
|
|
120
|
+
if (selectionMode === "single") {
|
|
121
|
+
if (!isSelected) return [value];
|
|
122
|
+
if (isSelectionRequired) return null;
|
|
123
|
+
return [];
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (!isSelected) return [...current, value];
|
|
127
|
+
const next = current.filter((entry) => entry !== value);
|
|
128
|
+
if (isSelectionRequired && next.length === 0) return null;
|
|
129
|
+
return next;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** A toggle's announced role — see {@link resolveToggleButtonAccessibility}. */
|
|
133
|
+
export type ToggleButtonAccessibility =
|
|
134
|
+
| { accessibilityRole: "togglebutton"; accessibilityState: { checked: boolean } }
|
|
135
|
+
| { accessibilityRole: "radio"; accessibilityState: { selected: boolean } };
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* What a screen reader is told a toggle is.
|
|
139
|
+
*
|
|
140
|
+
* A toggle standing alone, or one of a set of independent marks, is a
|
|
141
|
+
* `togglebutton` that is checked or not — read out as on or off. A member of a
|
|
142
|
+
* `single` group is a choice among options, which is a radio: announcing it as a
|
|
143
|
+
* toggle would promise that pressing it again turns it off, and in a group that
|
|
144
|
+
* requires a selection it does not.
|
|
145
|
+
*
|
|
146
|
+
* `null` is "not in a group".
|
|
147
|
+
*
|
|
148
|
+
* Pure, so the whole matrix is reachable from `bun test`. See AGENTS.md.
|
|
149
|
+
*/
|
|
150
|
+
export function resolveToggleButtonAccessibility(
|
|
151
|
+
selectionMode: ToggleButtonSelectionMode | null,
|
|
152
|
+
isSelected: boolean
|
|
153
|
+
): ToggleButtonAccessibility {
|
|
154
|
+
if (selectionMode === "single") {
|
|
155
|
+
return { accessibilityRole: "radio", accessibilityState: { selected: isSelected } };
|
|
156
|
+
}
|
|
157
|
+
return { accessibilityRole: "togglebutton", accessibilityState: { checked: isSelected } };
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export type ToggleButtonVariantProps = VariantProps<typeof toggleButtonVariants>;
|