-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathuseActionGroup.ts
More file actions
132 lines (122 loc) · 4.06 KB
/
Copy pathuseActionGroup.ts
File metadata and controls
132 lines (122 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import {
AriaLabelingProps,
DOMAttributes,
DOMProps,
FocusableElement,
ItemElement,
ItemRenderer,
Key,
MultipleSelection,
Orientation,
RefObject
} from '@react-types/shared';
import {createFocusManager} from '../focus/FocusScope';
import {filterDOMProps} from '../utils/filterDOMProps';
import {ListState} from 'react-stately/useListState';
import {useKeyboard} from '../interactions/useKeyboard';
import {useLayoutEffect} from '../utils/useLayoutEffect';
import {useLocale} from '../i18n/I18nProvider';
import {useState} from 'react';
const BUTTON_GROUP_ROLES = {
none: 'toolbar',
single: 'radiogroup',
multiple: 'toolbar'
};
// Not extending CollectionBase to avoid async loading props
export interface ActionGroupProps<T> extends MultipleSelection {
/**
* The axis the ActionGroup should align with.
*
* @default 'horizontal'
*/
orientation?: Orientation;
/**
* An list of `Item` elements or a function. If the latter, a list of items must be provided using
* the `items` prop.
*/
children: ItemElement<T> | ItemElement<T>[] | ItemRenderer<T>;
/** A list of items to display as children. Must be used with a function as the sole child. */
items?: Iterable<T>;
/** A list of keys to disable. */
disabledKeys?: Iterable<Key>;
/**
* Whether the ActionGroup is disabled.
* Shows that a selection exists, but is not available in that circumstance.
*/
isDisabled?: boolean;
/**
* Invoked when an action is taken on a child. Especially useful when `selectionMode` is none.
* The sole argument `key` is the key for the item.
*/
onAction?: (key: Key) => void;
}
export interface AriaActionGroupProps<T> extends ActionGroupProps<T>, DOMProps, AriaLabelingProps {}
export interface ActionGroupAria {
actionGroupProps: DOMAttributes;
}
export function useActionGroup<T>(
props: AriaActionGroupProps<T>,
state: ListState<T>,
ref: RefObject<FocusableElement | null>
): ActionGroupAria {
let {isDisabled, orientation = 'horizontal' as Orientation} = props;
let [isInToolbar, setInToolbar] = useState(false);
useLayoutEffect(() => {
setInToolbar(!!(ref.current && ref.current.parentElement?.closest('[role="toolbar"]')));
}, [ref]);
let allKeys = [...state.collection.getKeys()];
if (!allKeys.some(key => !state.disabledKeys.has(key))) {
isDisabled = true;
}
let {direction} = useLocale();
let focusManager = createFocusManager(ref);
let flipDirection = direction === 'rtl' && orientation === 'horizontal';
let {keyboardProps} = useKeyboard({
shortcuts: {
ArrowRight: () => {
if (flipDirection) {
focusManager.focusPrevious({wrap: true});
} else {
focusManager.focusNext({wrap: true});
}
},
ArrowDown: () => {
focusManager.focusNext({wrap: true});
},
ArrowLeft: () => {
if (flipDirection) {
focusManager.focusNext({wrap: true});
} else {
focusManager.focusPrevious({wrap: true});
}
},
ArrowUp: () => {
focusManager.focusPrevious({wrap: true});
}
}
});
let role: string | undefined = BUTTON_GROUP_ROLES[state.selectionManager.selectionMode];
if (isInToolbar && role === 'toolbar') {
role = 'group';
}
return {
actionGroupProps: {
...filterDOMProps(props, {labelable: true}),
role,
'aria-orientation': role === 'toolbar' ? orientation : undefined,
'aria-disabled': isDisabled,
...keyboardProps
}
};
}