@versini/ui-togglegroup 8.2.4 โ 9.0.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/README.md +24 -19
- package/dist/components/index.d.ts +60 -7
- package/dist/components/index.js +343 -26
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
> An accessible React toggle button group component built with TypeScript and TailwindCSS.
|
|
7
7
|
|
|
8
|
-
The ToggleGroup component provides grouped toggle buttons for single-select usage with proper accessibility,
|
|
8
|
+
The ToggleGroup component provides grouped toggle buttons for single-select usage with proper accessibility, theming, sizing and focus management.
|
|
9
9
|
|
|
10
10
|
## Table of Contents
|
|
11
11
|
|
|
@@ -18,9 +18,9 @@ The ToggleGroup component provides grouped toggle buttons for single-select usag
|
|
|
18
18
|
## Features
|
|
19
19
|
|
|
20
20
|
- **๐ข Grouped Selection**: Manage a set of related options with single selection
|
|
21
|
-
- **โฟ Accessible**:
|
|
22
|
-
- **๐จ Theming & Sizes**:
|
|
23
|
-
- **โจ๏ธ Keyboard Friendly**: Arrow
|
|
21
|
+
- **โฟ Accessible**: `radiogroup` / `radio` semantics with roving focus, no third-party runtime dependencies
|
|
22
|
+
- **๐จ Theming & Sizes**: Inherits light/dark theming from the surrounding theme, plus `small | medium | large` sizes
|
|
23
|
+
- **โจ๏ธ Keyboard Friendly**: Arrow keys (both axes), `Home`/`End`/`PageUp`/`PageDown` move focus and skip disabled items; `Enter`/`Space` toggle
|
|
24
24
|
- **๐งช Type Safe**: Strongly typed props and context sharing
|
|
25
25
|
|
|
26
26
|
## Installation
|
|
@@ -84,10 +84,10 @@ function App() {
|
|
|
84
84
|
</ToggleGroup>
|
|
85
85
|
```
|
|
86
86
|
|
|
87
|
-
###
|
|
87
|
+
### Theme Picker
|
|
88
88
|
|
|
89
89
|
```tsx
|
|
90
|
-
<ToggleGroup
|
|
90
|
+
<ToggleGroup value={theme} onValueChange={setTheme}>
|
|
91
91
|
<ToggleGroupItem value="auto" />
|
|
92
92
|
<ToggleGroupItem value="light" />
|
|
93
93
|
<ToggleGroupItem value="dark" />
|
|
@@ -98,24 +98,29 @@ function App() {
|
|
|
98
98
|
|
|
99
99
|
### ToggleGroup Props
|
|
100
100
|
|
|
101
|
-
| Prop | Type
|
|
102
|
-
| --------------- |
|
|
103
|
-
| `value` | `string`
|
|
104
|
-
| `defaultValue` | `string`
|
|
105
|
-
| `onValueChange` | `(value: string) => void`
|
|
106
|
-
| `disabled` | `boolean`
|
|
107
|
-
| `
|
|
108
|
-
| `
|
|
109
|
-
| `className` | `string`
|
|
110
|
-
| `children` | `React.ReactNode`
|
|
101
|
+
| Prop | Type | Default | Description |
|
|
102
|
+
| --------------- | ---------------------------------- | ---------- | ----------------------------------------------------------------- |
|
|
103
|
+
| `value` | `string` | - | Current selected value (controlled). |
|
|
104
|
+
| `defaultValue` | `string` | - | Initial value (uncontrolled). |
|
|
105
|
+
| `onValueChange` | `(value: string) => void` | - | Fired when selection changes. Reports `""` when nothing is pressed. |
|
|
106
|
+
| `disabled` | `boolean` | `false` | Disable the entire group. |
|
|
107
|
+
| `size` | `"small" \| "medium" \| "large"` | `"medium"` | Visual size of items. |
|
|
108
|
+
| `flat` | `boolean` | `false` | Render a flat solid background instead of the gradient overlay. |
|
|
109
|
+
| `className` | `string` | - | Extra class(es) applied to container. |
|
|
110
|
+
| `children` | `React.ReactNode` | - | One or more `ToggleGroupItem` components. |
|
|
111
111
|
|
|
112
|
-
>
|
|
112
|
+
> Selecting the already-selected item clears the selection and reports an empty
|
|
113
|
+
> string, rather than being a no-op as in a plain radio group.
|
|
113
114
|
|
|
114
115
|
### ToggleGroupItem Props
|
|
115
116
|
|
|
116
117
|
| Prop | Type | Default | Description |
|
|
117
118
|
| ---------- | --------- | ------- | -------------------------------------- |
|
|
118
119
|
| `value` | `string` | - | Value represented by this toggle item. |
|
|
119
|
-
| `disabled` | `boolean` |
|
|
120
|
+
| `disabled` | `boolean` | `false` | Disable just this item. |
|
|
120
121
|
|
|
121
|
-
> Item content is the `value` string by default;
|
|
122
|
+
> Item content is the `value` string by default; pass `children` to customize it.
|
|
123
|
+
>
|
|
124
|
+
> `ToggleGroupItem` accepts all native `<button>` props except `value` and
|
|
125
|
+
> `type`. Extra props are forwarded to the underlying button, and `className` is
|
|
126
|
+
> merged with the component's own classes.
|
|
@@ -1,9 +1,38 @@
|
|
|
1
|
+
import type { ComponentPropsWithoutRef } from 'react';
|
|
1
2
|
import { JSX } from 'react';
|
|
2
|
-
import type
|
|
3
|
+
import type { ReactNode } from 'react';
|
|
3
4
|
|
|
4
5
|
declare type Size = "small" | "medium" | "large";
|
|
5
6
|
|
|
6
|
-
|
|
7
|
+
/**
|
|
8
|
+
* ToggleGroup root. Owns the pressed value (controlled or uncontrolled), the
|
|
9
|
+
* item registry, and the shared context.
|
|
10
|
+
*
|
|
11
|
+
* Invariants:
|
|
12
|
+
* 1. Unique values โ item `value`s must be unique within a group; duplicates
|
|
13
|
+
* are unsupported and dev-warn (no runtime de-dup).
|
|
14
|
+
* 2. Exactly one tabbable item โ `tabIndex=0` goes to the last focused item if
|
|
15
|
+
* it is still registered and enabled, else the pressed item, else the first
|
|
16
|
+
* enabled item, else the first; all others get `tabIndex=-1`. The group is
|
|
17
|
+
* therefore a single tab stop.
|
|
18
|
+
* 3. Deselectable โ pressing the already-pressed item clears the selection and
|
|
19
|
+
* reports an empty string, rather than being a no-op as in a radio group.
|
|
20
|
+
* 4. Focus without activation โ arrow keys move focus only; activation is left
|
|
21
|
+
* to the native button, so Enter/Space toggle the focused item.
|
|
22
|
+
* 5. Group disable โ the group's `disabled` prop disables every item; disabled
|
|
23
|
+
* buttons are not focusable and are skipped by keyboard navigation.
|
|
24
|
+
* 6. DOM-order traversal โ item order is resolved on read
|
|
25
|
+
* (compareDocumentPosition) so keyboard navigation matches visual order even
|
|
26
|
+
* after a keyed reorder, which does not remount the items.
|
|
27
|
+
* 7. Focus recovery โ if the item holding focus unmounts or becomes disabled,
|
|
28
|
+
* focus moves to the item that inherits the tab stop rather than falling to
|
|
29
|
+
* <body>.
|
|
30
|
+
*
|
|
31
|
+
*/
|
|
32
|
+
export declare const ToggleGroup: {
|
|
33
|
+
({ children, value, onValueChange, disabled, size, defaultValue, className, flat, }: ToggleGroupProps): JSX.Element;
|
|
34
|
+
displayName: string;
|
|
35
|
+
};
|
|
7
36
|
|
|
8
37
|
export declare const TOGGLEGROUP_CLASSNAME = "av-togglegroup";
|
|
9
38
|
|
|
@@ -11,9 +40,32 @@ export declare const TOGGLEGROUP_ITEM_CLASSNAME = "av-togglegroup-item";
|
|
|
11
40
|
|
|
12
41
|
export declare const TOGGLEGROUP_ITEM_WRAPPER_CLASSNAME = "av-togglegroup-item-wrapper";
|
|
13
42
|
|
|
14
|
-
export declare const ToggleGroupItem:
|
|
43
|
+
export declare const ToggleGroupItem: {
|
|
44
|
+
({ value, disabled, children, className, onClick, onFocus, onPointerDown, ...rest }: ToggleGroupItemProps): JSX.Element;
|
|
45
|
+
displayName: string;
|
|
46
|
+
};
|
|
15
47
|
|
|
16
|
-
|
|
48
|
+
/**
|
|
49
|
+
* `value` and `type` are owned by the component: `value` identifies the item
|
|
50
|
+
* within the group (a string, not the native button value) and the element is
|
|
51
|
+
* always rendered as `type="button"` so it never submits an enclosing form.
|
|
52
|
+
*/
|
|
53
|
+
declare type ToggleGroupItemProps = Omit<ComponentPropsWithoutRef<"button">, "value" | "type"> & {
|
|
54
|
+
/**
|
|
55
|
+
* A string value for the toggle group item. All items within a toggle group
|
|
56
|
+
* should use a unique value.
|
|
57
|
+
*/
|
|
58
|
+
value: string;
|
|
59
|
+
/**
|
|
60
|
+
* Whether or not the item is disabled. The group's own `disabled` prop
|
|
61
|
+
* disables every item regardless of this value.
|
|
62
|
+
*/
|
|
63
|
+
disabled?: boolean;
|
|
64
|
+
/**
|
|
65
|
+
* The content of the item. Defaults to `value` when omitted.
|
|
66
|
+
*/
|
|
67
|
+
children?: ReactNode;
|
|
68
|
+
};
|
|
17
69
|
|
|
18
70
|
declare type ToggleGroupProps = {
|
|
19
71
|
/**
|
|
@@ -29,7 +81,8 @@ declare type ToggleGroupProps = {
|
|
|
29
81
|
*/
|
|
30
82
|
defaultValue?: string;
|
|
31
83
|
/**
|
|
32
|
-
* Callback fired when the value changes.
|
|
84
|
+
* Callback fired when the value changes. Selecting the already-pressed item
|
|
85
|
+
* clears the selection and reports an empty string.
|
|
33
86
|
*/
|
|
34
87
|
onValueChange?: (value: string) => void;
|
|
35
88
|
/**
|
|
@@ -45,8 +98,8 @@ declare type ToggleGroupProps = {
|
|
|
45
98
|
*/
|
|
46
99
|
className?: string;
|
|
47
100
|
/**
|
|
48
|
-
* Whether to render the ToggleGroup with a flat solid background
|
|
49
|
-
*
|
|
101
|
+
* Whether to render the ToggleGroup with a flat solid background instead of
|
|
102
|
+
* the default Aqua-style gradient overlay.
|
|
50
103
|
* @default false
|
|
51
104
|
*/
|
|
52
105
|
flat?: boolean;
|
package/dist/components/index.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
@versini/ui-togglegroup
|
|
2
|
+
@versini/ui-togglegroup v9.0.0
|
|
3
3
|
ยฉ 2026 gizmette.com
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
7
|
-
import
|
|
7
|
+
import { useUncontrolled } from "@versini/ui-hooks/use-uncontrolled";
|
|
8
|
+
import { createContext, useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
|
|
8
9
|
import clsx from "clsx";
|
|
9
10
|
|
|
10
|
-
import * as __rspack_external__radix_ui_react_toggle_group_1cb63752 from "@radix-ui/react-toggle-group";
|
|
11
11
|
|
|
12
12
|
|
|
13
13
|
const TOGGLEGROUP_CLASSNAME = "av-togglegroup";
|
|
@@ -19,10 +19,86 @@ const TOGGLEGROUP_ITEM_CLASSNAME = "av-togglegroup-item";
|
|
|
19
19
|
|
|
20
20
|
|
|
21
21
|
|
|
22
|
-
const ToggleGroupContext =
|
|
22
|
+
/* v8 ignore start - default context values are fallbacks, never used directly */ const ToggleGroupContext = createContext({
|
|
23
23
|
size: "medium",
|
|
24
|
-
flat: false
|
|
25
|
-
|
|
24
|
+
flat: false,
|
|
25
|
+
value: "",
|
|
26
|
+
toggleValue: ()=>{},
|
|
27
|
+
disabled: false,
|
|
28
|
+
tabbableValue: undefined,
|
|
29
|
+
onItemFocus: ()=>{},
|
|
30
|
+
registerItem: ()=>{},
|
|
31
|
+
unregisterItem: ()=>{}
|
|
32
|
+
}); /* v8 ignore stop */
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* `useLayoutEffect` in the browser, `useEffect` on the server โ avoids React's
|
|
38
|
+
* "useLayoutEffect does nothing on the server" warning during SSR while keeping
|
|
39
|
+
* the synchronous, pre-paint timing in the browser.
|
|
40
|
+
*/ /* v8 ignore start - useEffect fallback only runs during SSR (window undefined) */ const useIsomorphicLayoutEffect = typeof window !== "undefined" ? useLayoutEffect : useEffect; /* v8 ignore stop */
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The group is laid out horizontally but does not constrain navigation to one
|
|
46
|
+
* axis, so both arrow pairs traverse it. PageUp/PageDown mirror Home/End.
|
|
47
|
+
*/ const MAP_KEY_TO_FOCUS_INTENT = {
|
|
48
|
+
ArrowLeft: "prev",
|
|
49
|
+
ArrowUp: "prev",
|
|
50
|
+
ArrowRight: "next",
|
|
51
|
+
ArrowDown: "next",
|
|
52
|
+
Home: "first",
|
|
53
|
+
PageUp: "first",
|
|
54
|
+
End: "last",
|
|
55
|
+
PageDown: "last"
|
|
56
|
+
};
|
|
57
|
+
const getTargetIndex = (intent, currentIndex, count)=>{
|
|
58
|
+
switch(intent){
|
|
59
|
+
case "first":
|
|
60
|
+
return 0;
|
|
61
|
+
case "last":
|
|
62
|
+
return count - 1;
|
|
63
|
+
/**
|
|
64
|
+
* Navigation wraps. With no item focused yet, stepping backward enters at the
|
|
65
|
+
* end and stepping forward enters at the start.
|
|
66
|
+
*/ case "prev":
|
|
67
|
+
return currentIndex < 0 ? count - 1 : (currentIndex - 1 + count) % count;
|
|
68
|
+
default:
|
|
69
|
+
return currentIndex < 0 ? 0 : (currentIndex + 1) % count;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Returns an onKeyDown handler for the group implementing roving focus: arrow
|
|
74
|
+
* keys on either axis with wrap and disabled-skip, plus
|
|
75
|
+
* Home/End/PageUp/PageDown. Focus moves only โ activation stays with the native
|
|
76
|
+
* button (Enter/Space fire a click), so arrowing across the group never changes
|
|
77
|
+
* the selection.
|
|
78
|
+
*/ const useToggleGroupKeyboard = ({ getItems })=>{
|
|
79
|
+
return useCallback((event)=>{
|
|
80
|
+
/**
|
|
81
|
+
* Shift+Tab leaves the group; every other modifier combination is left to
|
|
82
|
+
* the browser so shortcuts are not swallowed.
|
|
83
|
+
*/ if (event.metaKey || event.ctrlKey || event.altKey || event.shiftKey) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const intent = MAP_KEY_TO_FOCUS_INTENT[event.key];
|
|
87
|
+
if (intent === undefined) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
// Disabled buttons are not focusable, so they are never candidates.
|
|
91
|
+
const items = getItems().filter((item)=>!item.disabled);
|
|
92
|
+
if (items.length === 0) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
event.preventDefault();
|
|
96
|
+
const currentIndex = items.findIndex((item)=>item.element === document.activeElement);
|
|
97
|
+
items[getTargetIndex(intent, currentIndex, items.length)].element.focus();
|
|
98
|
+
}, [
|
|
99
|
+
getItems
|
|
100
|
+
]);
|
|
101
|
+
};
|
|
26
102
|
|
|
27
103
|
|
|
28
104
|
|
|
@@ -71,41 +147,280 @@ const getToggleGroupClasses = ({ className, size })=>{
|
|
|
71
147
|
|
|
72
148
|
|
|
73
149
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* ToggleGroup root. Owns the pressed value (controlled or uncontrolled), the
|
|
154
|
+
* item registry, and the shared context.
|
|
155
|
+
*
|
|
156
|
+
* Invariants:
|
|
157
|
+
* 1. Unique values โ item `value`s must be unique within a group; duplicates
|
|
158
|
+
* are unsupported and dev-warn (no runtime de-dup).
|
|
159
|
+
* 2. Exactly one tabbable item โ `tabIndex=0` goes to the last focused item if
|
|
160
|
+
* it is still registered and enabled, else the pressed item, else the first
|
|
161
|
+
* enabled item, else the first; all others get `tabIndex=-1`. The group is
|
|
162
|
+
* therefore a single tab stop.
|
|
163
|
+
* 3. Deselectable โ pressing the already-pressed item clears the selection and
|
|
164
|
+
* reports an empty string, rather than being a no-op as in a radio group.
|
|
165
|
+
* 4. Focus without activation โ arrow keys move focus only; activation is left
|
|
166
|
+
* to the native button, so Enter/Space toggle the focused item.
|
|
167
|
+
* 5. Group disable โ the group's `disabled` prop disables every item; disabled
|
|
168
|
+
* buttons are not focusable and are skipped by keyboard navigation.
|
|
169
|
+
* 6. DOM-order traversal โ item order is resolved on read
|
|
170
|
+
* (compareDocumentPosition) so keyboard navigation matches visual order even
|
|
171
|
+
* after a keyed reorder, which does not remount the items.
|
|
172
|
+
* 7. Focus recovery โ if the item holding focus unmounts or becomes disabled,
|
|
173
|
+
* focus moves to the item that inherits the tab stop rather than falling to
|
|
174
|
+
* <body>.
|
|
175
|
+
*
|
|
176
|
+
*/ const ToggleGroup = ({ children, value, onValueChange, disabled = false, size = "medium", defaultValue, className, flat = false })=>{
|
|
177
|
+
const [pressedValue, setPressedValue] = useUncontrolled({
|
|
178
|
+
value,
|
|
179
|
+
defaultValue,
|
|
180
|
+
/**
|
|
181
|
+
* Empty string, not undefined, is the "nothing pressed" state โ it is what
|
|
182
|
+
* deselecting reports, so the uncontrolled seed has to match.
|
|
183
|
+
*/ finalValue: "",
|
|
184
|
+
onChange: onValueChange
|
|
185
|
+
});
|
|
186
|
+
const itemsRef = useRef([]);
|
|
187
|
+
const focusWithinRef = useRef(false);
|
|
188
|
+
const [tabStopValue, setTabStopValue] = useState(undefined);
|
|
189
|
+
/**
|
|
190
|
+
* Bumped whenever an item registers or unregisters; it is a dependency of the
|
|
191
|
+
* context memo so consumers re-render and recompute the roving tabIndex from
|
|
192
|
+
* the (mutable) registry.
|
|
193
|
+
*/ const [registrationVersion, setRegistrationVersion] = useState(0);
|
|
194
|
+
const bumpVersion = useCallback(()=>setRegistrationVersion((version)=>version + 1), []);
|
|
195
|
+
/**
|
|
196
|
+
* Stable toggle setter (useUncontrolled returns a fresh setter each render in
|
|
197
|
+
* uncontrolled mode; refs keep `toggleValue`'s identity stable so the context
|
|
198
|
+
* memo is not invalidated on every render).
|
|
199
|
+
*/ const setPressedRef = useRef(setPressedValue);
|
|
200
|
+
setPressedRef.current = setPressedValue;
|
|
201
|
+
const pressedRef = useRef(pressedValue);
|
|
202
|
+
pressedRef.current = pressedValue;
|
|
203
|
+
const toggleValue = useCallback((next)=>{
|
|
204
|
+
// Invariant 3: pressing the pressed item clears the selection.
|
|
205
|
+
setPressedRef.current(pressedRef.current === next ? "" : next);
|
|
206
|
+
/**
|
|
207
|
+
* Claim the tab stop here rather than relying on focus alone: Safari and
|
|
208
|
+
* Firefox on macOS do not focus a button on click, and a controlled parent
|
|
209
|
+
* can change the value with no focus event at all.
|
|
210
|
+
*/ setTabStopValue(next);
|
|
211
|
+
}, []);
|
|
212
|
+
const getItems = useCallback(()=>itemsRef.current, []);
|
|
213
|
+
const registerItem = useCallback((registration)=>{
|
|
214
|
+
const existingIndex = itemsRef.current.findIndex((item)=>item.element === registration.element);
|
|
215
|
+
/* v8 ignore start - re-registration of the same element on re-render */ if (existingIndex >= 0) {
|
|
216
|
+
itemsRef.current[existingIndex] = registration;
|
|
217
|
+
bumpVersion();
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
/* v8 ignore stop */ if (itemsRef.current.some((item)=>item.value === registration.value)) {
|
|
221
|
+
console.warn(`[ui-togglegroup] Duplicate ToggleGroupItem value "${registration.value}". Item values must be unique within a ToggleGroup.`);
|
|
222
|
+
}
|
|
223
|
+
// Order is applied in getItems, so registration order does not matter.
|
|
224
|
+
itemsRef.current.push(registration);
|
|
225
|
+
bumpVersion();
|
|
226
|
+
}, [
|
|
227
|
+
bumpVersion
|
|
228
|
+
]);
|
|
229
|
+
const unregisterItem = useCallback((element)=>{
|
|
230
|
+
itemsRef.current = itemsRef.current.filter((item)=>item.element !== element);
|
|
231
|
+
bumpVersion();
|
|
232
|
+
}, [
|
|
233
|
+
bumpVersion
|
|
234
|
+
]);
|
|
235
|
+
const onItemFocus = useCallback((focused)=>{
|
|
236
|
+
setTabStopValue(focused);
|
|
237
|
+
}, []);
|
|
238
|
+
const handleKeyDown = useToggleGroupKeyboard({
|
|
239
|
+
getItems
|
|
240
|
+
});
|
|
241
|
+
const handleRootFocus = useCallback(()=>{
|
|
242
|
+
focusWithinRef.current = true;
|
|
243
|
+
}, []);
|
|
244
|
+
const handleRootBlur = useCallback((event)=>{
|
|
245
|
+
/**
|
|
246
|
+
* Keep the flag when focus is lost to <body> (e.g. the focused item unmounts)
|
|
247
|
+
* so recovery can restore it; only clear when focus genuinely moves to an
|
|
248
|
+
* element outside this group.
|
|
249
|
+
*/ if (event.relatedTarget && !event.currentTarget.contains(event.relatedTarget)) {
|
|
250
|
+
focusWithinRef.current = false;
|
|
251
|
+
}
|
|
252
|
+
}, []);
|
|
253
|
+
/**
|
|
254
|
+
* Invariant 6. Keep the registry in DOM order after every commit. React can
|
|
255
|
+
* reorder keyed items without remounting them, so the registration effect
|
|
256
|
+
* never re-runs and any order captured at registration time goes stale. This
|
|
257
|
+
* has to happen post-commit: during render the DOM still reflects the previous
|
|
258
|
+
* order, so comparing document positions there reads the old layout.
|
|
259
|
+
*/ useIsomorphicLayoutEffect(()=>{
|
|
260
|
+
const current = itemsRef.current;
|
|
261
|
+
const sorted = current.slice().sort((a, b)=>a.element.compareDocumentPosition(b.element) & Node.DOCUMENT_POSITION_FOLLOWING ? -1 : 1);
|
|
262
|
+
if (sorted.some((item, index)=>item !== current[index])) {
|
|
263
|
+
itemsRef.current = sorted;
|
|
264
|
+
bumpVersion();
|
|
265
|
+
}
|
|
78
266
|
});
|
|
79
|
-
|
|
267
|
+
/**
|
|
268
|
+
* Invariant 2. The last focused item keeps the tab stop so returning to the
|
|
269
|
+
* group lands where the user left it; before any focus, the pressed item does.
|
|
270
|
+
* The fallback therefore means first in visual order, matching traversal.
|
|
271
|
+
*/ const items = itemsRef.current;
|
|
272
|
+
const isTabbable = (candidate)=>candidate !== undefined && items.some((item)=>item.value === candidate && !item.disabled && !disabled);
|
|
273
|
+
let tabbableValue;
|
|
274
|
+
if (isTabbable(tabStopValue)) {
|
|
275
|
+
tabbableValue = tabStopValue;
|
|
276
|
+
} else if (isTabbable(pressedValue)) {
|
|
277
|
+
tabbableValue = pressedValue;
|
|
278
|
+
} else {
|
|
279
|
+
tabbableValue = (items.find((item)=>!item.disabled) ?? items[0])?.value;
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Invariant 7. When the item holding focus unmounts or becomes disabled the
|
|
283
|
+
* browser drops focus to <body>; move it to whichever item now owns the tab
|
|
284
|
+
* stop so a keyboard user is not ejected to the top of the document. Gated on
|
|
285
|
+
* the previous tab stop having actually gone away, so clicking on inert page
|
|
286
|
+
* background never pulls focus back into the group.
|
|
287
|
+
*/ useIsomorphicLayoutEffect(()=>{
|
|
288
|
+
if (tabStopValue === undefined || !focusWithinRef.current) {
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
const stillUsable = itemsRef.current.some((item)=>item.value === tabStopValue && !item.disabled);
|
|
292
|
+
if (stillUsable) {
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Recover from the two ways focus ends up stranded: the browser drops it to
|
|
297
|
+
* <body> when the focused element is removed, and it can be left sitting on
|
|
298
|
+
* an element that has just been disabled.
|
|
299
|
+
*/ const active = document.activeElement;
|
|
300
|
+
const stranded = active === document.body || itemsRef.current.some((item)=>item.element === active && item.disabled);
|
|
301
|
+
if (!stranded) {
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
const recovered = itemsRef.current.find((item)=>item.value === tabbableValue && !item.disabled);
|
|
305
|
+
if (recovered) {
|
|
306
|
+
recovered.element.focus();
|
|
307
|
+
setTabStopValue(recovered.value);
|
|
308
|
+
}
|
|
309
|
+
});
|
|
310
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: registrationVersion is an intentional tripwire. The memo reads the mutable item registry (a ref the linter cannot see), so it MUST recompute on every (un)registration; removing this dep reintroduces a stale roving tabIndex.
|
|
311
|
+
const contextValue = useMemo(()=>({
|
|
312
|
+
size,
|
|
313
|
+
flat,
|
|
314
|
+
value: pressedValue,
|
|
315
|
+
toggleValue,
|
|
316
|
+
disabled,
|
|
317
|
+
tabbableValue,
|
|
318
|
+
onItemFocus,
|
|
319
|
+
registerItem,
|
|
320
|
+
unregisterItem
|
|
321
|
+
}), [
|
|
80
322
|
size,
|
|
81
|
-
flat
|
|
82
|
-
|
|
323
|
+
flat,
|
|
324
|
+
pressedValue,
|
|
325
|
+
toggleValue,
|
|
326
|
+
disabled,
|
|
327
|
+
tabbableValue,
|
|
328
|
+
/**
|
|
329
|
+
* registrationVersion drives recompute of the roving tabIndex from the
|
|
330
|
+
* registry when an item (un)registers.
|
|
331
|
+
*/ registrationVersion,
|
|
332
|
+
onItemFocus,
|
|
333
|
+
registerItem,
|
|
334
|
+
unregisterItem
|
|
335
|
+
]);
|
|
83
336
|
return /*#__PURE__*/ jsx(ToggleGroupContext.Provider, {
|
|
84
337
|
value: contextValue,
|
|
85
|
-
children: /*#__PURE__*/ jsx(
|
|
86
|
-
|
|
87
|
-
className:
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
338
|
+
children: /*#__PURE__*/ jsx("div", {
|
|
339
|
+
role: "radiogroup",
|
|
340
|
+
className: getToggleGroupClasses({
|
|
341
|
+
className,
|
|
342
|
+
size
|
|
343
|
+
}),
|
|
344
|
+
onKeyDown: handleKeyDown,
|
|
345
|
+
onFocus: handleRootFocus,
|
|
346
|
+
onBlur: handleRootBlur,
|
|
92
347
|
children: children
|
|
93
348
|
})
|
|
94
349
|
});
|
|
95
350
|
};
|
|
96
|
-
|
|
97
|
-
|
|
351
|
+
ToggleGroup.displayName = "ToggleGroup";
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
const ToggleGroupItem = ({ value, disabled = false, children, className, onClick, onFocus, onPointerDown, ...rest })=>{
|
|
363
|
+
const { size, flat, value: pressedValue, toggleValue, disabled: groupDisabled, tabbableValue, onItemFocus, registerItem, unregisterItem } = useContext(ToggleGroupContext);
|
|
364
|
+
const buttonRef = useRef(null);
|
|
365
|
+
// Invariant 5: the group's `disabled` disables every item.
|
|
366
|
+
const isDisabled = groupDisabled || disabled;
|
|
367
|
+
/**
|
|
368
|
+
* Register in a layout effect (before paint) so the first render already sees
|
|
369
|
+
* a populated registry and the roving tabIndex settles without a flicker.
|
|
370
|
+
*/ useIsomorphicLayoutEffect(()=>{
|
|
371
|
+
const element = buttonRef.current;
|
|
372
|
+
/* v8 ignore start - ref is always set after mount */ if (!element) {
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
/* v8 ignore stop */ registerItem({
|
|
376
|
+
element,
|
|
377
|
+
value,
|
|
378
|
+
disabled: isDisabled
|
|
379
|
+
});
|
|
380
|
+
return ()=>{
|
|
381
|
+
unregisterItem(element);
|
|
382
|
+
};
|
|
383
|
+
}, [
|
|
384
|
+
value,
|
|
385
|
+
isDisabled,
|
|
386
|
+
registerItem,
|
|
387
|
+
unregisterItem
|
|
388
|
+
]);
|
|
98
389
|
const { itemClass, wrapperClass } = getToggleGroupItemClasses({
|
|
99
390
|
size,
|
|
100
391
|
flat
|
|
101
392
|
});
|
|
393
|
+
const pressed = pressedValue === value;
|
|
102
394
|
const label = children ?? value;
|
|
103
395
|
return /*#__PURE__*/ jsx("div", {
|
|
104
396
|
className: wrapperClass,
|
|
105
|
-
children: /*#__PURE__*/ jsx(
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
397
|
+
children: /*#__PURE__*/ jsx("button", {
|
|
398
|
+
...rest,
|
|
399
|
+
ref: buttonRef,
|
|
400
|
+
type: "button",
|
|
401
|
+
role: "radio",
|
|
402
|
+
"aria-checked": pressed,
|
|
403
|
+
"data-state": pressed ? "on" : "off",
|
|
404
|
+
"data-disabled": isDisabled ? "" : undefined,
|
|
405
|
+
disabled: isDisabled,
|
|
406
|
+
tabIndex: tabbableValue === value ? 0 : -1,
|
|
407
|
+
className: clsx(itemClass, className),
|
|
408
|
+
onClick: (event)=>{
|
|
409
|
+
onClick?.(event);
|
|
410
|
+
toggleValue(value);
|
|
411
|
+
},
|
|
412
|
+
onFocus: (event)=>{
|
|
413
|
+
onFocus?.(event);
|
|
414
|
+
onItemFocus(value);
|
|
415
|
+
},
|
|
416
|
+
/**
|
|
417
|
+
* Pointer interaction claims the tab stop even when the browser does not
|
|
418
|
+
* focus the button (Safari, Firefox on macOS) or when a consumer handler
|
|
419
|
+
* vetoes the click.
|
|
420
|
+
*/ onPointerDown: (event)=>{
|
|
421
|
+
onPointerDown?.(event);
|
|
422
|
+
onItemFocus(value);
|
|
423
|
+
},
|
|
109
424
|
children: /*#__PURE__*/ jsxs("span", {
|
|
110
425
|
className: "grid",
|
|
111
426
|
children: [
|
|
@@ -123,6 +438,8 @@ const ToggleGroupItem = ({ value, disabled, children })=>{
|
|
|
123
438
|
})
|
|
124
439
|
});
|
|
125
440
|
};
|
|
441
|
+
ToggleGroupItem.displayName = "ToggleGroupItem";
|
|
442
|
+
|
|
126
443
|
|
|
127
444
|
|
|
128
445
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@versini/ui-togglegroup",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "9.0.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Arno Versini",
|
|
6
6
|
"publishConfig": {
|
|
@@ -44,12 +44,12 @@
|
|
|
44
44
|
"@versini/ui-types": "10.1.0"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@
|
|
47
|
+
"@versini/ui-hooks": "6.2.0",
|
|
48
48
|
"clsx": "2.1.1",
|
|
49
49
|
"tailwindcss": "4.3.3"
|
|
50
50
|
},
|
|
51
51
|
"sideEffects": [
|
|
52
52
|
"**/*.css"
|
|
53
53
|
],
|
|
54
|
-
"gitHead": "
|
|
54
|
+
"gitHead": "ed6fc78a514eb1ea338beec03517238c2ef98311"
|
|
55
55
|
}
|