@mui/utils 9.0.0-beta.0 → 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.
@@ -1,31 +1,179 @@
1
1
  import * as React from 'react';
2
- export type UseRovingTabIndexOptions = {
3
- focusableIndex?: number | undefined;
2
+ export interface Item<Key = unknown> {
3
+ /**
4
+ * The logical id used to track the item across reorders and re-renders.
5
+ * Components such as `Tabs` use the tab value here, while `MenuItem` generates an internal id.
6
+ */
7
+ id: Key;
8
+ /**
9
+ * The item's current DOM element.
10
+ */
11
+ element: HTMLElement | null;
12
+ /**
13
+ * Whether the item ignores user interaction.
14
+ */
15
+ disabled: boolean;
16
+ /**
17
+ * Whether a disabled item should still be allowed to receive roving focus.
18
+ * `MenuList` uses this for its `disabledItemsFocusable` behavior.
19
+ */
20
+ focusableWhenDisabled: boolean;
21
+ /**
22
+ * An optional text string used for typeahead matching.
23
+ */
24
+ textValue?: string | undefined;
25
+ /**
26
+ * Whether the item is currently selected in the consumer component's own state model.
27
+ * `MenuList` uses this when `variant="selectedMenu"` to prefer the selected item by default,
28
+ * and `Tabs` sets this on the selected tab.
29
+ */
30
+ selected: boolean;
31
+ }
32
+ export interface UseRovingTabIndexParams<Key = unknown> {
33
+ /**
34
+ * The logical item id that should own `tabIndex=0`.
35
+ *
36
+ * Pass a concrete id when the consumer wants to drive roving focus explicitly.
37
+ * For example, `Tabs` uses the selected tab value here when focus is outside the list so
38
+ * that keyboard focus re-enters on the selected tab.
39
+ *
40
+ * `undefined` means "the hook should choose the active item using its default-item logic".
41
+ * `null` means "there is intentionally no preferred item", which also falls back to the
42
+ * default-item logic.
43
+ */
44
+ activeItemId?: Key | null | undefined;
45
+ /**
46
+ * Chooses the default item when `activeItemId` is not driving the tab stop directly.
47
+ *
48
+ * `MenuList` uses this to prefer the selected item when `variant="selectedMenu"`, then
49
+ * falls back to the first focusable item.
50
+ */
51
+ getDefaultActiveItemId?: ((items: Item<Key>[]) => Key | null) | undefined;
52
+ /**
53
+ * The axis used by arrow-key navigation.
54
+ */
4
55
  orientation: 'horizontal' | 'vertical';
56
+ /**
57
+ * Whether horizontal keyboard navigation should follow right-to-left semantics.
58
+ * Only affects horizontal lists.
59
+ * @default false
60
+ */
5
61
  isRtl?: boolean | undefined;
6
- shouldFocus?: ((element: HTMLElement | null) => boolean) | undefined;
7
- shouldWrap?: boolean | undefined;
8
- };
9
- type UseRovingTabIndexReturn = {
10
- getItemProps: (index: number, ref?: React.Ref<HTMLElement>) => {
11
- ref: (element: HTMLElement | null) => void;
12
- tabIndex: number;
13
- };
62
+ /**
63
+ * Filters items out of roving navigation without removing them from the registry.
64
+ * For example, `MenuList` uses this so disabled menu items can stay registered while still
65
+ * being skipped for roving focus unless `disabledItemsFocusable` is enabled.
66
+ */
67
+ isItemFocusable?: ((item: Item<Key>) => boolean) | undefined;
68
+ /**
69
+ * Whether keyboard navigation should wrap from the last item back to the first, and vice versa.
70
+ * @default true
71
+ */
72
+ wrap?: boolean | undefined;
73
+ }
74
+ export interface UseRovingTabIndexReturnValue<Key = unknown> {
75
+ /**
76
+ * The item id that currently owns `tabIndex=0` for this render.
77
+ */
78
+ activeItemId: Key | null;
79
+ /**
80
+ * Imperatively moves focus to the next matching item.
81
+ * Consumers such as `MenuList` use this for typeahead and other non-arrow-key navigation.
82
+ */
83
+ focusNext: (isItemFocusableOverride?: (item: Item<Key>) => boolean) => Key | null;
84
+ /**
85
+ * Returns the current active item record from a fresh registry snapshot.
86
+ */
87
+ getActiveItem: () => Item<Key> | null;
88
+ /**
89
+ * Returns the props that enable roving behavior on the container element.
90
+ *
91
+ * Spread these props onto the list or composite root element that should listen for focus
92
+ * and keyboard events.
93
+ */
14
94
  getContainerProps: (ref?: React.Ref<HTMLElement>) => {
95
+ /**
96
+ * Keeps the active item in sync when focus moves onto one of the registered items.
97
+ */
15
98
  onFocus: (event: React.FocusEvent<HTMLElement>) => void;
99
+ /**
100
+ * Handles arrow-key, Home, and End navigation for the roving set.
101
+ */
16
102
  onKeyDown: (event: React.KeyboardEvent<HTMLElement>) => void;
103
+ /**
104
+ * Merges the consumer ref with the internal container ref used by the hook.
105
+ */
17
106
  ref: (element: HTMLElement | null) => void;
18
107
  };
19
- focusNext: (shouldSkipFocusOverride?: (element: HTMLElement | null) => boolean) => number;
20
- };
108
+ /**
109
+ * Returns the current item registry.
110
+ * The map is keyed by item id and stores normalized item records.
111
+ */
112
+ getItemMap: () => Map<Key, Item<Key>>;
113
+ /**
114
+ * Reports whether the supplied item id currently owns `tabIndex=0`.
115
+ */
116
+ isItemActive: (itemId: Key) => boolean;
117
+ /**
118
+ * Registers or updates an item in the roving registry.
119
+ */
120
+ registerItem: (item: Item<Key>) => void;
121
+ /**
122
+ * Updates the current active item id.
123
+ */
124
+ setActiveItemId: (itemId: Key | null) => void;
125
+ /**
126
+ * Removes an item from the roving registry.
127
+ */
128
+ unregisterItem: (itemId: Key) => void;
129
+ }
130
+ export interface UseRovingTabIndexItemParams<Key = unknown> {
131
+ /**
132
+ * The logical id that will be used as the item's registry key.
133
+ */
134
+ id: Key;
135
+ /**
136
+ * The consumer's ref for the item's DOM element.
137
+ * The item hook merges this with its own registration ref callback.
138
+ */
139
+ ref?: React.Ref<HTMLElement> | undefined;
140
+ /**
141
+ * Whether the item ignores user interaction.
142
+ * @default false
143
+ */
144
+ disabled?: boolean | undefined;
145
+ /**
146
+ * Whether the item should still be focusable even when `disabled` is true.
147
+ * @default false
148
+ */
149
+ focusableWhenDisabled?: boolean | undefined;
150
+ /**
151
+ * An optional text string used for typeahead matching.
152
+ */
153
+ textValue?: string | undefined;
154
+ /**
155
+ * Whether the item is selected in the consumer component's state model.
156
+ * @default false
157
+ */
158
+ selected?: boolean | undefined;
159
+ }
160
+ export interface UseRovingTabIndexItemReturnValue {
161
+ /**
162
+ * The merged ref callback that registers the item element with the root registry.
163
+ */
164
+ ref: React.RefCallback<HTMLElement | null>;
165
+ /**
166
+ * The `tabIndex` that should be applied to the item element.
167
+ * The active item receives `0`; every other item receives `-1`.
168
+ */
169
+ tabIndex: number;
170
+ }
21
171
  /**
22
- * Provides roving tab index behavior for a container and its focusable children.
172
+ * Provides roving tab index behavior for a composite container and its focusable children.
23
173
  * This is useful for implementing keyboard navigation in components like menus, tabs, and lists.
24
174
  * The hook manages the focus state of child elements and provides props to be spread on both the container and the items.
25
175
  * The container will handle keyboard events to move focus between items based on the specified orientation and wrapping behavior.
26
- *
27
- * @param options - Configuration options for the roving tab index behavior, including orientation, initial focusable index, RTL support, and custom focus logic.
28
- * @returns An object containing `getItemProps` and `getContainerProps` functions to be spread on the respective elements, and a `focusNext` function to programmatically move focus to the next item.
29
176
  */
30
- export default function useRovingTabIndex(options: UseRovingTabIndexOptions): UseRovingTabIndexReturn;
31
- export {};
177
+ export declare function useRovingTabIndexRoot<Key = unknown>(params: UseRovingTabIndexParams<Key>): UseRovingTabIndexReturnValue<Key>;
178
+ export declare function useRovingTabIndexItem<Key = unknown>(params: UseRovingTabIndexItemParams<Key>): UseRovingTabIndexItemReturnValue;
179
+ export declare function isItemFocusable(item: Item<unknown>): boolean;
@@ -1,31 +1,179 @@
1
1
  import * as React from 'react';
2
- export type UseRovingTabIndexOptions = {
3
- focusableIndex?: number | undefined;
2
+ export interface Item<Key = unknown> {
3
+ /**
4
+ * The logical id used to track the item across reorders and re-renders.
5
+ * Components such as `Tabs` use the tab value here, while `MenuItem` generates an internal id.
6
+ */
7
+ id: Key;
8
+ /**
9
+ * The item's current DOM element.
10
+ */
11
+ element: HTMLElement | null;
12
+ /**
13
+ * Whether the item ignores user interaction.
14
+ */
15
+ disabled: boolean;
16
+ /**
17
+ * Whether a disabled item should still be allowed to receive roving focus.
18
+ * `MenuList` uses this for its `disabledItemsFocusable` behavior.
19
+ */
20
+ focusableWhenDisabled: boolean;
21
+ /**
22
+ * An optional text string used for typeahead matching.
23
+ */
24
+ textValue?: string | undefined;
25
+ /**
26
+ * Whether the item is currently selected in the consumer component's own state model.
27
+ * `MenuList` uses this when `variant="selectedMenu"` to prefer the selected item by default,
28
+ * and `Tabs` sets this on the selected tab.
29
+ */
30
+ selected: boolean;
31
+ }
32
+ export interface UseRovingTabIndexParams<Key = unknown> {
33
+ /**
34
+ * The logical item id that should own `tabIndex=0`.
35
+ *
36
+ * Pass a concrete id when the consumer wants to drive roving focus explicitly.
37
+ * For example, `Tabs` uses the selected tab value here when focus is outside the list so
38
+ * that keyboard focus re-enters on the selected tab.
39
+ *
40
+ * `undefined` means "the hook should choose the active item using its default-item logic".
41
+ * `null` means "there is intentionally no preferred item", which also falls back to the
42
+ * default-item logic.
43
+ */
44
+ activeItemId?: Key | null | undefined;
45
+ /**
46
+ * Chooses the default item when `activeItemId` is not driving the tab stop directly.
47
+ *
48
+ * `MenuList` uses this to prefer the selected item when `variant="selectedMenu"`, then
49
+ * falls back to the first focusable item.
50
+ */
51
+ getDefaultActiveItemId?: ((items: Item<Key>[]) => Key | null) | undefined;
52
+ /**
53
+ * The axis used by arrow-key navigation.
54
+ */
4
55
  orientation: 'horizontal' | 'vertical';
56
+ /**
57
+ * Whether horizontal keyboard navigation should follow right-to-left semantics.
58
+ * Only affects horizontal lists.
59
+ * @default false
60
+ */
5
61
  isRtl?: boolean | undefined;
6
- shouldFocus?: ((element: HTMLElement | null) => boolean) | undefined;
7
- shouldWrap?: boolean | undefined;
8
- };
9
- type UseRovingTabIndexReturn = {
10
- getItemProps: (index: number, ref?: React.Ref<HTMLElement>) => {
11
- ref: (element: HTMLElement | null) => void;
12
- tabIndex: number;
13
- };
62
+ /**
63
+ * Filters items out of roving navigation without removing them from the registry.
64
+ * For example, `MenuList` uses this so disabled menu items can stay registered while still
65
+ * being skipped for roving focus unless `disabledItemsFocusable` is enabled.
66
+ */
67
+ isItemFocusable?: ((item: Item<Key>) => boolean) | undefined;
68
+ /**
69
+ * Whether keyboard navigation should wrap from the last item back to the first, and vice versa.
70
+ * @default true
71
+ */
72
+ wrap?: boolean | undefined;
73
+ }
74
+ export interface UseRovingTabIndexReturnValue<Key = unknown> {
75
+ /**
76
+ * The item id that currently owns `tabIndex=0` for this render.
77
+ */
78
+ activeItemId: Key | null;
79
+ /**
80
+ * Imperatively moves focus to the next matching item.
81
+ * Consumers such as `MenuList` use this for typeahead and other non-arrow-key navigation.
82
+ */
83
+ focusNext: (isItemFocusableOverride?: (item: Item<Key>) => boolean) => Key | null;
84
+ /**
85
+ * Returns the current active item record from a fresh registry snapshot.
86
+ */
87
+ getActiveItem: () => Item<Key> | null;
88
+ /**
89
+ * Returns the props that enable roving behavior on the container element.
90
+ *
91
+ * Spread these props onto the list or composite root element that should listen for focus
92
+ * and keyboard events.
93
+ */
14
94
  getContainerProps: (ref?: React.Ref<HTMLElement>) => {
95
+ /**
96
+ * Keeps the active item in sync when focus moves onto one of the registered items.
97
+ */
15
98
  onFocus: (event: React.FocusEvent<HTMLElement>) => void;
99
+ /**
100
+ * Handles arrow-key, Home, and End navigation for the roving set.
101
+ */
16
102
  onKeyDown: (event: React.KeyboardEvent<HTMLElement>) => void;
103
+ /**
104
+ * Merges the consumer ref with the internal container ref used by the hook.
105
+ */
17
106
  ref: (element: HTMLElement | null) => void;
18
107
  };
19
- focusNext: (shouldSkipFocusOverride?: (element: HTMLElement | null) => boolean) => number;
20
- };
108
+ /**
109
+ * Returns the current item registry.
110
+ * The map is keyed by item id and stores normalized item records.
111
+ */
112
+ getItemMap: () => Map<Key, Item<Key>>;
113
+ /**
114
+ * Reports whether the supplied item id currently owns `tabIndex=0`.
115
+ */
116
+ isItemActive: (itemId: Key) => boolean;
117
+ /**
118
+ * Registers or updates an item in the roving registry.
119
+ */
120
+ registerItem: (item: Item<Key>) => void;
121
+ /**
122
+ * Updates the current active item id.
123
+ */
124
+ setActiveItemId: (itemId: Key | null) => void;
125
+ /**
126
+ * Removes an item from the roving registry.
127
+ */
128
+ unregisterItem: (itemId: Key) => void;
129
+ }
130
+ export interface UseRovingTabIndexItemParams<Key = unknown> {
131
+ /**
132
+ * The logical id that will be used as the item's registry key.
133
+ */
134
+ id: Key;
135
+ /**
136
+ * The consumer's ref for the item's DOM element.
137
+ * The item hook merges this with its own registration ref callback.
138
+ */
139
+ ref?: React.Ref<HTMLElement> | undefined;
140
+ /**
141
+ * Whether the item ignores user interaction.
142
+ * @default false
143
+ */
144
+ disabled?: boolean | undefined;
145
+ /**
146
+ * Whether the item should still be focusable even when `disabled` is true.
147
+ * @default false
148
+ */
149
+ focusableWhenDisabled?: boolean | undefined;
150
+ /**
151
+ * An optional text string used for typeahead matching.
152
+ */
153
+ textValue?: string | undefined;
154
+ /**
155
+ * Whether the item is selected in the consumer component's state model.
156
+ * @default false
157
+ */
158
+ selected?: boolean | undefined;
159
+ }
160
+ export interface UseRovingTabIndexItemReturnValue {
161
+ /**
162
+ * The merged ref callback that registers the item element with the root registry.
163
+ */
164
+ ref: React.RefCallback<HTMLElement | null>;
165
+ /**
166
+ * The `tabIndex` that should be applied to the item element.
167
+ * The active item receives `0`; every other item receives `-1`.
168
+ */
169
+ tabIndex: number;
170
+ }
21
171
  /**
22
- * Provides roving tab index behavior for a container and its focusable children.
172
+ * Provides roving tab index behavior for a composite container and its focusable children.
23
173
  * This is useful for implementing keyboard navigation in components like menus, tabs, and lists.
24
174
  * The hook manages the focus state of child elements and provides props to be spread on both the container and the items.
25
175
  * The container will handle keyboard events to move focus between items based on the specified orientation and wrapping behavior.
26
- *
27
- * @param options - Configuration options for the roving tab index behavior, including orientation, initial focusable index, RTL support, and custom focus logic.
28
- * @returns An object containing `getItemProps` and `getContainerProps` functions to be spread on the respective elements, and a `focusNext` function to programmatically move focus to the next item.
29
176
  */
30
- export default function useRovingTabIndex(options: UseRovingTabIndexOptions): UseRovingTabIndexReturn;
31
- export {};
177
+ export declare function useRovingTabIndexRoot<Key = unknown>(params: UseRovingTabIndexParams<Key>): UseRovingTabIndexReturnValue<Key>;
178
+ export declare function useRovingTabIndexItem<Key = unknown>(params: UseRovingTabIndexItemParams<Key>): UseRovingTabIndexItemReturnValue;
179
+ export declare function isItemFocusable(item: Item<unknown>): boolean;