@primer/components 0.0.0-202195195330 → 0.0.0-202195195659
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/CHANGELOG.md +1 -1
- package/dist/browser.esm.js +260 -261
- package/dist/browser.esm.js.map +1 -1
- package/dist/browser.umd.js +332 -333
- package/dist/browser.umd.js.map +1 -1
- package/lib/AnchoredOverlay/AnchoredOverlay.d.ts +2 -1
- package/lib/AnchoredOverlay/AnchoredOverlay.js +11 -3
- package/lib/Autocomplete/Autocomplete.d.ts +40 -0
- package/lib/Autocomplete/Autocomplete.js +68 -0
- package/lib/Autocomplete/AutocompleteContext.d.ts +17 -0
- package/lib/Autocomplete/AutocompleteContext.js +11 -0
- package/lib/Autocomplete/AutocompleteInput.d.ts +9 -0
- package/lib/Autocomplete/AutocompleteInput.js +141 -0
- package/lib/Autocomplete/AutocompleteMenu.d.ts +71 -0
- package/lib/Autocomplete/AutocompleteMenu.js +220 -0
- package/lib/Autocomplete/AutocompleteOverlay.d.ts +17 -0
- package/lib/Autocomplete/AutocompleteOverlay.js +69 -0
- package/lib/Autocomplete/index.d.ts +2 -0
- package/lib/Autocomplete/index.js +15 -0
- package/lib/FilteredActionList/FilteredActionList.js +5 -31
- package/lib/Overlay.d.ts +2 -1
- package/lib/Overlay.js +10 -5
- package/lib/hooks/useOverlay.d.ts +2 -1
- package/lib/hooks/useOverlay.js +11 -6
- package/lib/index.d.ts +1 -0
- package/lib/index.js +8 -0
- package/lib/utils/scrollIntoViewingArea.d.ts +1 -0
- package/lib/utils/scrollIntoViewingArea.js +39 -0
- package/lib/utils/types.d.ts +3 -0
- package/lib-esm/AnchoredOverlay/AnchoredOverlay.d.ts +2 -1
- package/lib-esm/AnchoredOverlay/AnchoredOverlay.js +11 -3
- package/lib-esm/Autocomplete/Autocomplete.d.ts +40 -0
- package/lib-esm/Autocomplete/Autocomplete.js +47 -0
- package/lib-esm/Autocomplete/AutocompleteContext.d.ts +17 -0
- package/lib-esm/Autocomplete/AutocompleteContext.js +2 -0
- package/lib-esm/Autocomplete/AutocompleteInput.d.ts +9 -0
- package/lib-esm/Autocomplete/AutocompleteInput.js +122 -0
- package/lib-esm/Autocomplete/AutocompleteMenu.d.ts +71 -0
- package/lib-esm/Autocomplete/AutocompleteMenu.js +201 -0
- package/lib-esm/Autocomplete/AutocompleteOverlay.d.ts +17 -0
- package/lib-esm/Autocomplete/AutocompleteOverlay.js +51 -0
- package/lib-esm/Autocomplete/index.d.ts +2 -0
- package/lib-esm/Autocomplete/index.js +1 -0
- package/lib-esm/FilteredActionList/FilteredActionList.js +3 -31
- package/lib-esm/Overlay.d.ts +2 -1
- package/lib-esm/Overlay.js +8 -5
- package/lib-esm/hooks/useOverlay.d.ts +2 -1
- package/lib-esm/hooks/useOverlay.js +11 -6
- package/lib-esm/index.d.ts +1 -0
- package/lib-esm/index.js +1 -0
- package/lib-esm/utils/scrollIntoViewingArea.d.ts +1 -0
- package/lib-esm/utils/scrollIntoViewingArea.js +30 -0
- package/lib-esm/utils/types.d.ts +3 -0
- package/package.json +1 -1
@@ -0,0 +1,71 @@
|
|
1
|
+
import React from 'react';
|
2
|
+
import { ItemProps } from '../ActionList';
|
3
|
+
import { ComponentProps, MandateProps } from '../utils/types';
|
4
|
+
declare type OnSelectedChange<T> = (item: T | T[]) => void;
|
5
|
+
declare type AutocompleteItemProps<T = Record<string, any>> = MandateProps<ItemProps, 'id'> & {
|
6
|
+
metadata?: T;
|
7
|
+
};
|
8
|
+
declare type AutocompleteMenuInternalProps<T extends AutocompleteItemProps> = {
|
9
|
+
/**
|
10
|
+
* A menu item that is used to allow users make a selection that is not available in the array passed to the `items` prop.
|
11
|
+
* This menu item gets appended to the end of the list of options.
|
12
|
+
*/
|
13
|
+
addNewItem?: Omit<T, 'onAction' | 'leadingVisual' | 'id'> & {
|
14
|
+
handleAddItem: (item: Omit<T, 'onAction' | 'leadingVisual'>) => void;
|
15
|
+
};
|
16
|
+
/**
|
17
|
+
* The text that appears in the menu when there are no options in the array passed to the `items` prop.
|
18
|
+
*/
|
19
|
+
emptyStateText?: React.ReactNode | false;
|
20
|
+
/**
|
21
|
+
* A custom function used to filter the options in the array passed to the `items` prop.
|
22
|
+
* By default, we filter out items that don't match the value of the autocomplete text input. The default filter is not case-sensitive.
|
23
|
+
*/
|
24
|
+
filterFn?: (item: T, i: number) => boolean;
|
25
|
+
/**
|
26
|
+
* The options for field values that are displayed in the dropdown menu.
|
27
|
+
* One or more may be selected depending on the value of the `selectionVariant` prop.
|
28
|
+
*/
|
29
|
+
items: T[];
|
30
|
+
/**
|
31
|
+
* Whether the data is loaded for the menu items
|
32
|
+
*/
|
33
|
+
loading?: boolean;
|
34
|
+
/**
|
35
|
+
* The IDs of the selected items
|
36
|
+
*/
|
37
|
+
selectedItemIds: Array<string | number>;
|
38
|
+
/**
|
39
|
+
* The sort function that is applied to the options in the array passed to the `items` prop after the user closes the menu.
|
40
|
+
* By default, selected items are sorted to the top after the user closes the menu.
|
41
|
+
*/
|
42
|
+
sortOnCloseFn?: (itemIdA: string | number, itemIdB: string | number) => number;
|
43
|
+
/**
|
44
|
+
* Whether there can be one item selected from the menu or multiple items selected from the menu
|
45
|
+
*/
|
46
|
+
selectionVariant?: 'single' | 'multiple';
|
47
|
+
/**
|
48
|
+
* Function that gets called when the menu is opened or closed
|
49
|
+
*/
|
50
|
+
onOpenChange?: (open: boolean) => void;
|
51
|
+
/**
|
52
|
+
* The function that is called when an item in the list is selected or deselected
|
53
|
+
*/
|
54
|
+
onSelectedChange?: OnSelectedChange<T>;
|
55
|
+
/**
|
56
|
+
* If the menu is rendered in a scrolling element other than the `Autocomplete.Overlay` component,
|
57
|
+
* pass the ref of that element to `customScrollContainerRef` to ensure the container automatically
|
58
|
+
* scrolls when the user highlights an item in the menu that is outside the scroll container
|
59
|
+
*/
|
60
|
+
customScrollContainerRef?: React.MutableRefObject<HTMLElement | null>;
|
61
|
+
} & Pick<React.AriaAttributes, 'aria-labelledby'>;
|
62
|
+
declare function AutocompleteMenu<T extends AutocompleteItemProps>(props: AutocompleteMenuInternalProps<T>): JSX.Element;
|
63
|
+
declare namespace AutocompleteMenu {
|
64
|
+
var defaultProps: {
|
65
|
+
emptyStateText: string;
|
66
|
+
selectionVariant: string;
|
67
|
+
};
|
68
|
+
var displayName: string;
|
69
|
+
}
|
70
|
+
export declare type AutocompleteMenuProps = ComponentProps<typeof AutocompleteMenu>;
|
71
|
+
export default AutocompleteMenu;
|
@@ -0,0 +1,201 @@
|
|
1
|
+
import React, { useContext, useEffect, useMemo, useRef, useState } from 'react';
|
2
|
+
import { ActionList } from '../ActionList';
|
3
|
+
import { useFocusZone } from '../hooks/useFocusZone';
|
4
|
+
import { Box, Spinner } from '../';
|
5
|
+
import { AutocompleteContext } from './AutocompleteContext';
|
6
|
+
import { PlusIcon } from '@primer/octicons-react';
|
7
|
+
import { uniqueId } from '../utils/uniqueId';
|
8
|
+
import { scrollIntoViewingArea } from '../utils/scrollIntoViewingArea';
|
9
|
+
|
10
|
+
const getDefaultSortFn = isItemSelectedFn => (itemIdA, itemIdB) => isItemSelectedFn(itemIdA) === isItemSelectedFn(itemIdB) ? 0 : isItemSelectedFn(itemIdA) ? -1 : 1;
|
11
|
+
|
12
|
+
function getDefaultItemFilter(filterValue) {
|
13
|
+
return function (item, _i) {
|
14
|
+
var _item$text;
|
15
|
+
|
16
|
+
return Boolean((_item$text = item.text) === null || _item$text === void 0 ? void 0 : _item$text.toLowerCase().startsWith(filterValue.toLowerCase()));
|
17
|
+
};
|
18
|
+
}
|
19
|
+
|
20
|
+
function getDefaultOnSelectionChange(setInputValueFn) {
|
21
|
+
return function (itemOrItems) {
|
22
|
+
const {
|
23
|
+
text = ''
|
24
|
+
} = Array.isArray(itemOrItems) ? itemOrItems.slice(-1)[0] : itemOrItems;
|
25
|
+
setInputValueFn && setInputValueFn(text);
|
26
|
+
};
|
27
|
+
}
|
28
|
+
|
29
|
+
const isItemSelected = (itemId, selectedItemIds) => selectedItemIds.includes(itemId);
|
30
|
+
|
31
|
+
function getItemById(itemId, items) {
|
32
|
+
return items.find(item => item.id === itemId);
|
33
|
+
}
|
34
|
+
|
35
|
+
// TODO: consider making 'aria-labelledby' required
|
36
|
+
function AutocompleteMenu(props) {
|
37
|
+
const {
|
38
|
+
activeDescendantRef,
|
39
|
+
id,
|
40
|
+
inputRef,
|
41
|
+
inputValue = '',
|
42
|
+
scrollContainerRef,
|
43
|
+
setAutocompleteSuggestion,
|
44
|
+
setShowMenu,
|
45
|
+
setInputValue,
|
46
|
+
setIsMenuDirectlyActivated,
|
47
|
+
setSelectedItemLength,
|
48
|
+
showMenu
|
49
|
+
} = useContext(AutocompleteContext);
|
50
|
+
const {
|
51
|
+
items,
|
52
|
+
selectedItemIds,
|
53
|
+
sortOnCloseFn,
|
54
|
+
emptyStateText,
|
55
|
+
addNewItem,
|
56
|
+
loading,
|
57
|
+
selectionVariant,
|
58
|
+
filterFn = getDefaultItemFilter(inputValue),
|
59
|
+
'aria-labelledby': ariaLabelledBy,
|
60
|
+
onOpenChange,
|
61
|
+
onSelectedChange = getDefaultOnSelectionChange(setInputValue),
|
62
|
+
customScrollContainerRef
|
63
|
+
} = props;
|
64
|
+
const listContainerRef = useRef(null);
|
65
|
+
const [highlightedItem, setHighlightedItem] = useState();
|
66
|
+
const [sortedItemIds, setSortedItemIds] = useState(items.map(({
|
67
|
+
id
|
68
|
+
}) => id));
|
69
|
+
const selectableItems = useMemo(() => items.map(selectableItem => {
|
70
|
+
return { ...selectableItem,
|
71
|
+
role: 'option',
|
72
|
+
id: selectableItem.id,
|
73
|
+
selected: selectionVariant === 'multiple' ? selectedItemIds.includes(selectableItem.id) : undefined,
|
74
|
+
onAction: item => {
|
75
|
+
const otherSelectedItemIds = selectedItemIds.filter(selectedItemId => selectedItemId !== item.id);
|
76
|
+
const newSelectedItemIds = selectedItemIds.includes(item.id) ? otherSelectedItemIds : [...otherSelectedItemIds, item.id];
|
77
|
+
onSelectedChange && onSelectedChange(newSelectedItemIds.map(newSelectedItemId => getItemById(newSelectedItemId, items)));
|
78
|
+
|
79
|
+
if (selectionVariant === 'multiple') {
|
80
|
+
setInputValue && setInputValue('');
|
81
|
+
setAutocompleteSuggestion && setAutocompleteSuggestion('');
|
82
|
+
} else {
|
83
|
+
var _inputRef$current;
|
84
|
+
|
85
|
+
setShowMenu && setShowMenu(false);
|
86
|
+
inputRef === null || inputRef === void 0 ? void 0 : (_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 ? void 0 : _inputRef$current.setSelectionRange(inputRef.current.value.length, inputRef.current.value.length);
|
87
|
+
}
|
88
|
+
}
|
89
|
+
};
|
90
|
+
}), [items, selectedItemIds]);
|
91
|
+
const itemSortOrderData = useMemo(() => sortedItemIds.reduce((acc, curr, i) => {
|
92
|
+
acc[curr] = i;
|
93
|
+
return acc;
|
94
|
+
}, {}), [sortedItemIds]);
|
95
|
+
const sortedAndFilteredItemsToRender = useMemo(() => selectableItems.filter((item, i) => filterFn(item, i)).sort((a, b) => itemSortOrderData[a.id] - itemSortOrderData[b.id]), [selectableItems, itemSortOrderData, filterFn]);
|
96
|
+
const allItemsToRender = useMemo(() => [// sorted and filtered selectable items
|
97
|
+
...sortedAndFilteredItemsToRender, // menu item used for creating a token from whatever is in the text input
|
98
|
+
...(addNewItem ? [{ ...addNewItem,
|
99
|
+
leadingVisual: () => /*#__PURE__*/React.createElement(PlusIcon, null),
|
100
|
+
onAction: (item, e) => {
|
101
|
+
// TODO: make it possible to pass a leadingVisual when using `addNewItem`
|
102
|
+
addNewItem.handleAddItem({ ...item,
|
103
|
+
id: item.id || uniqueId(),
|
104
|
+
leadingVisual: undefined
|
105
|
+
});
|
106
|
+
|
107
|
+
if (selectionVariant === 'multiple') {
|
108
|
+
setInputValue && setInputValue('');
|
109
|
+
setAutocompleteSuggestion && setAutocompleteSuggestion('');
|
110
|
+
}
|
111
|
+
}
|
112
|
+
}] : [])], [sortedAndFilteredItemsToRender, addNewItem]);
|
113
|
+
useFocusZone({
|
114
|
+
containerRef: listContainerRef,
|
115
|
+
focusOutBehavior: 'wrap',
|
116
|
+
focusableElementFilter: element => {
|
117
|
+
return !(element instanceof HTMLInputElement);
|
118
|
+
},
|
119
|
+
activeDescendantFocus: inputRef,
|
120
|
+
onActiveDescendantChanged: (current, _previous, directlyActivated) => {
|
121
|
+
if (activeDescendantRef) {
|
122
|
+
activeDescendantRef.current = current || null;
|
123
|
+
}
|
124
|
+
|
125
|
+
if (current) {
|
126
|
+
const selectedItem = selectableItems.find(item => item.id.toString() === current.dataset.id);
|
127
|
+
setHighlightedItem(selectedItem);
|
128
|
+
setIsMenuDirectlyActivated && setIsMenuDirectlyActivated(directlyActivated);
|
129
|
+
}
|
130
|
+
|
131
|
+
if (current && customScrollContainerRef && customScrollContainerRef.current && directlyActivated) {
|
132
|
+
scrollIntoViewingArea(current, customScrollContainerRef.current);
|
133
|
+
} else if (current && scrollContainerRef && scrollContainerRef.current && directlyActivated) {
|
134
|
+
scrollIntoViewingArea(current, scrollContainerRef.current);
|
135
|
+
}
|
136
|
+
}
|
137
|
+
}, [loading]);
|
138
|
+
useEffect(() => {
|
139
|
+
var _highlightedItem$text;
|
140
|
+
|
141
|
+
if (!setAutocompleteSuggestion) {
|
142
|
+
return;
|
143
|
+
}
|
144
|
+
|
145
|
+
if (highlightedItem !== null && highlightedItem !== void 0 && (_highlightedItem$text = highlightedItem.text) !== null && _highlightedItem$text !== void 0 && _highlightedItem$text.startsWith(inputValue) && !selectedItemIds.includes(highlightedItem.id)) {
|
146
|
+
setAutocompleteSuggestion(highlightedItem.text);
|
147
|
+
} else {
|
148
|
+
setAutocompleteSuggestion('');
|
149
|
+
}
|
150
|
+
}, [highlightedItem, inputValue]);
|
151
|
+
useEffect(() => {
|
152
|
+
if (showMenu === false) {
|
153
|
+
setSortedItemIds([...sortedItemIds].sort(sortOnCloseFn ? sortOnCloseFn : getDefaultSortFn(itemId => isItemSelected(itemId, selectedItemIds))));
|
154
|
+
}
|
155
|
+
|
156
|
+
onOpenChange && onOpenChange(Boolean(showMenu));
|
157
|
+
}, [showMenu]);
|
158
|
+
useEffect(() => {
|
159
|
+
if (selectedItemIds.length) {
|
160
|
+
setSelectedItemLength && setSelectedItemLength(selectedItemIds.length);
|
161
|
+
}
|
162
|
+
}, [selectedItemIds]);
|
163
|
+
return /*#__PURE__*/React.createElement(Box, {
|
164
|
+
sx: !showMenu ? {
|
165
|
+
// visually hides this label for sighted users
|
166
|
+
position: 'absolute',
|
167
|
+
width: '1px',
|
168
|
+
height: '1px',
|
169
|
+
padding: '0',
|
170
|
+
margin: '-1px',
|
171
|
+
overflow: 'hidden',
|
172
|
+
clip: 'rect(0, 0, 0, 0)',
|
173
|
+
whiteSpace: 'nowrap',
|
174
|
+
borderWidth: '0'
|
175
|
+
} : {}
|
176
|
+
}, loading ? /*#__PURE__*/React.createElement(Box, {
|
177
|
+
p: 3,
|
178
|
+
display: "flex",
|
179
|
+
justifyContent: "center"
|
180
|
+
}, /*#__PURE__*/React.createElement(Spinner, null)) : /*#__PURE__*/React.createElement("div", {
|
181
|
+
ref: listContainerRef
|
182
|
+
}, allItemsToRender.length ? /*#__PURE__*/React.createElement(ActionList, {
|
183
|
+
selectionVariant: "multiple" // have to typecast to `ItemProps` because we have an extra property
|
184
|
+
// on `items` for Autocomplete: `metadata`
|
185
|
+
,
|
186
|
+
items: allItemsToRender,
|
187
|
+
role: "listbox",
|
188
|
+
id: `${id}-listbox`,
|
189
|
+
"aria-labelledby": ariaLabelledBy
|
190
|
+
}) : /*#__PURE__*/React.createElement(Box, {
|
191
|
+
p: 3
|
192
|
+
}, emptyStateText)));
|
193
|
+
}
|
194
|
+
|
195
|
+
AutocompleteMenu.displayName = "AutocompleteMenu";
|
196
|
+
AutocompleteMenu.defaultProps = {
|
197
|
+
emptyStateText: 'No selectable options',
|
198
|
+
selectionVariant: 'single'
|
199
|
+
};
|
200
|
+
AutocompleteMenu.displayName = 'AutocompleteMenu';
|
201
|
+
export default AutocompleteMenu;
|
@@ -0,0 +1,17 @@
|
|
1
|
+
import React from 'react';
|
2
|
+
import { OverlayProps } from '../Overlay';
|
3
|
+
import { ComponentProps } from '../utils/types';
|
4
|
+
declare type AutocompleteOverlayInternalProps = {
|
5
|
+
/**
|
6
|
+
* The ref of the element that the position of the menu is based on. By default, the menu is positioned based on the text input
|
7
|
+
*/
|
8
|
+
menuAnchorRef?: React.RefObject<HTMLElement>;
|
9
|
+
/**
|
10
|
+
* Props to be spread on the internal `Overlay` component.
|
11
|
+
*/
|
12
|
+
overlayProps?: Partial<OverlayProps>;
|
13
|
+
children?: React.ReactNode;
|
14
|
+
} & Pick<React.AriaAttributes, 'aria-labelledby'>;
|
15
|
+
declare const AutocompleteOverlay: React.FC<AutocompleteOverlayInternalProps>;
|
16
|
+
export declare type AutocompleteOverlayProps = ComponentProps<typeof AutocompleteOverlay>;
|
17
|
+
export default AutocompleteOverlay;
|
@@ -0,0 +1,51 @@
|
|
1
|
+
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
2
|
+
|
3
|
+
import React, { useCallback, useContext } from 'react';
|
4
|
+
import { useAnchoredPosition } from '../hooks';
|
5
|
+
import Overlay from '../Overlay';
|
6
|
+
import { AutocompleteContext } from './AutocompleteContext';
|
7
|
+
import { useCombinedRefs } from '../hooks/useCombinedRefs';
|
8
|
+
|
9
|
+
// TODO: consider making 'aria-labelledby' required
|
10
|
+
const AutocompleteOverlay = ({
|
11
|
+
menuAnchorRef,
|
12
|
+
overlayProps,
|
13
|
+
children
|
14
|
+
}) => {
|
15
|
+
const {
|
16
|
+
inputRef,
|
17
|
+
scrollContainerRef,
|
18
|
+
selectedItemLength,
|
19
|
+
setShowMenu,
|
20
|
+
showMenu = false
|
21
|
+
} = useContext(AutocompleteContext);
|
22
|
+
const {
|
23
|
+
floatingElementRef,
|
24
|
+
position
|
25
|
+
} = useAnchoredPosition({
|
26
|
+
side: 'outside-bottom',
|
27
|
+
align: 'start',
|
28
|
+
anchorElementRef: menuAnchorRef ? menuAnchorRef : inputRef
|
29
|
+
}, [showMenu, selectedItemLength]);
|
30
|
+
const combinedOverlayRef = useCombinedRefs(scrollContainerRef, floatingElementRef);
|
31
|
+
const closeOptionList = useCallback(() => {
|
32
|
+
setShowMenu && setShowMenu(false);
|
33
|
+
}, [setShowMenu]);
|
34
|
+
return /*#__PURE__*/React.createElement(Overlay, _extends({
|
35
|
+
returnFocusRef: inputRef,
|
36
|
+
preventFocusOnOpen: true,
|
37
|
+
onClickOutside: closeOptionList,
|
38
|
+
onEscape: closeOptionList,
|
39
|
+
ref: combinedOverlayRef,
|
40
|
+
top: position === null || position === void 0 ? void 0 : position.top,
|
41
|
+
left: position === null || position === void 0 ? void 0 : position.left,
|
42
|
+
visibility: showMenu ? 'visible' : 'hidden',
|
43
|
+
sx: {
|
44
|
+
overflow: 'auto'
|
45
|
+
}
|
46
|
+
}, overlayProps), children);
|
47
|
+
};
|
48
|
+
|
49
|
+
AutocompleteOverlay.displayName = "AutocompleteOverlay";
|
50
|
+
AutocompleteOverlay.displayName = 'AutocompleteOverlay';
|
51
|
+
export default AutocompleteOverlay;
|
@@ -0,0 +1 @@
|
|
1
|
+
export { default } from './Autocomplete';
|
@@ -1,6 +1,7 @@
|
|
1
1
|
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
2
2
|
|
3
3
|
import React, { useCallback, useEffect, useRef } from 'react';
|
4
|
+
import { useSSRSafeId } from '@react-aria/ssr';
|
4
5
|
import TextInput from '../TextInput';
|
5
6
|
import Box from '../Box';
|
6
7
|
import { ActionList } from '../ActionList';
|
@@ -11,36 +12,7 @@ import styled from 'styled-components';
|
|
11
12
|
import { get } from '../constants';
|
12
13
|
import { useProvidedRefOrCreate } from '../hooks/useProvidedRefOrCreate';
|
13
14
|
import useScrollFlash from '../hooks/useScrollFlash';
|
14
|
-
import {
|
15
|
-
|
16
|
-
function scrollIntoViewingArea(child, container, margin = 8, behavior = 'smooth') {
|
17
|
-
const {
|
18
|
-
top: childTop,
|
19
|
-
bottom: childBottom
|
20
|
-
} = child.getBoundingClientRect();
|
21
|
-
const {
|
22
|
-
top: containerTop,
|
23
|
-
bottom: containerBottom
|
24
|
-
} = container.getBoundingClientRect();
|
25
|
-
const isChildTopAboveViewingArea = childTop < containerTop + margin;
|
26
|
-
const isChildBottomBelowViewingArea = childBottom > containerBottom - margin;
|
27
|
-
|
28
|
-
if (isChildTopAboveViewingArea) {
|
29
|
-
const scrollHeightToChildTop = childTop - containerTop + container.scrollTop;
|
30
|
-
container.scrollTo({
|
31
|
-
behavior,
|
32
|
-
top: scrollHeightToChildTop - margin
|
33
|
-
});
|
34
|
-
} else if (isChildBottomBelowViewingArea) {
|
35
|
-
const scrollHeightToChildBottom = childBottom - containerBottom + container.scrollTop;
|
36
|
-
container.scrollTo({
|
37
|
-
behavior,
|
38
|
-
top: scrollHeightToChildBottom + margin
|
39
|
-
});
|
40
|
-
} // either completely in view or outside viewing area on both ends, don't scroll
|
41
|
-
|
42
|
-
}
|
43
|
-
|
15
|
+
import { scrollIntoViewingArea } from '../utils/scrollIntoViewingArea';
|
44
16
|
const StyledHeader = styled.div.withConfig({
|
45
17
|
displayName: "FilteredActionList__StyledHeader",
|
46
18
|
componentId: "yg3jkv-0"
|
@@ -94,7 +66,7 @@ export function FilteredActionList({
|
|
94
66
|
useEffect(() => {
|
95
67
|
// if items changed, we want to instantly move active descendant into view
|
96
68
|
if (activeDescendantRef.current && scrollContainerRef.current) {
|
97
|
-
scrollIntoViewingArea(activeDescendantRef.current, scrollContainerRef.current, undefined, 'auto');
|
69
|
+
scrollIntoViewingArea(activeDescendantRef.current, scrollContainerRef.current, 'vertical', undefined, undefined, 'auto');
|
98
70
|
}
|
99
71
|
}, [items]);
|
100
72
|
useScrollFlash(scrollContainerRef);
|
package/lib-esm/Overlay.d.ts
CHANGED
@@ -4,7 +4,7 @@ import { ComponentProps } from './utils/types';
|
|
4
4
|
import { TouchOrMouseEvent } from './hooks';
|
5
5
|
import { SxProp } from './sx';
|
6
6
|
import { AnchorSide } from './behaviors/anchoredPosition';
|
7
|
-
declare type StyledOverlayProps = {
|
7
|
+
export declare type StyledOverlayProps = {
|
8
8
|
width?: keyof typeof widthMap;
|
9
9
|
height?: keyof typeof heightMap;
|
10
10
|
maxHeight?: keyof Omit<typeof heightMap, 'auto' | 'initial'>;
|
@@ -40,6 +40,7 @@ export declare type OverlayProps = {
|
|
40
40
|
top: number;
|
41
41
|
left: number;
|
42
42
|
portalContainerName?: string;
|
43
|
+
preventFocusOnOpen?: boolean;
|
43
44
|
} & Omit<ComponentProps<typeof StyledOverlay>, 'visibility' | keyof SystemPositionProps>;
|
44
45
|
/**
|
45
46
|
* An `Overlay` is a flexible floating surface, used to display transient content such as menus,
|
package/lib-esm/Overlay.js
CHANGED
@@ -92,6 +92,7 @@ const Overlay = /*#__PURE__*/React.forwardRef(({
|
|
92
92
|
left,
|
93
93
|
anchorSide,
|
94
94
|
portalContainerName,
|
95
|
+
preventFocusOnOpen,
|
95
96
|
...rest
|
96
97
|
}, forwardedRef) => {
|
97
98
|
const overlayRef = useRef(null);
|
@@ -107,7 +108,8 @@ const Overlay = /*#__PURE__*/React.forwardRef(({
|
|
107
108
|
onEscape,
|
108
109
|
ignoreClickRefs,
|
109
110
|
onClickOutside,
|
110
|
-
initialFocusRef
|
111
|
+
initialFocusRef,
|
112
|
+
preventFocusOnOpen
|
111
113
|
});
|
112
114
|
useEffect(() => {
|
113
115
|
var _combinedRef$current;
|
@@ -136,9 +138,7 @@ const Overlay = /*#__PURE__*/React.forwardRef(({
|
|
136
138
|
easing: slideAnimationEasing
|
137
139
|
});
|
138
140
|
}, [anchorSide, slideAnimationDistance, slideAnimationEasing, visibility]);
|
139
|
-
|
140
|
-
containerName: portalContainerName
|
141
|
-
}, /*#__PURE__*/React.createElement(StyledOverlay, _extends({
|
141
|
+
const styledOverlay = /*#__PURE__*/React.createElement(StyledOverlay, _extends({
|
142
142
|
height: height,
|
143
143
|
role: role
|
144
144
|
}, rest, {
|
@@ -149,7 +149,10 @@ const Overlay = /*#__PURE__*/React.forwardRef(({
|
|
149
149
|
...rest.style,
|
150
150
|
'--styled-overlay-visibility': visibility
|
151
151
|
}
|
152
|
-
}))
|
152
|
+
}));
|
153
|
+
return /*#__PURE__*/React.createElement(Portal, {
|
154
|
+
containerName: portalContainerName
|
155
|
+
}, styledOverlay);
|
153
156
|
});
|
154
157
|
Overlay.defaultProps = {
|
155
158
|
height: 'auto',
|
@@ -7,8 +7,9 @@ export declare type UseOverlaySettings = {
|
|
7
7
|
onEscape: (e: KeyboardEvent) => void;
|
8
8
|
onClickOutside: (e: TouchOrMouseEvent) => void;
|
9
9
|
overlayRef?: React.RefObject<HTMLDivElement>;
|
10
|
+
preventFocusOnOpen?: boolean;
|
10
11
|
};
|
11
12
|
export declare type OverlayReturnProps = {
|
12
13
|
ref: React.RefObject<HTMLDivElement>;
|
13
14
|
};
|
14
|
-
export declare const useOverlay: ({ overlayRef: _overlayRef, returnFocusRef, initialFocusRef, onEscape, ignoreClickRefs, onClickOutside }: UseOverlaySettings) => OverlayReturnProps;
|
15
|
+
export declare const useOverlay: ({ overlayRef: _overlayRef, returnFocusRef, initialFocusRef, onEscape, ignoreClickRefs, onClickOutside, preventFocusOnOpen }: UseOverlaySettings) => OverlayReturnProps;
|
@@ -8,14 +8,19 @@ export const useOverlay = ({
|
|
8
8
|
initialFocusRef,
|
9
9
|
onEscape,
|
10
10
|
ignoreClickRefs,
|
11
|
-
onClickOutside
|
11
|
+
onClickOutside,
|
12
|
+
preventFocusOnOpen
|
12
13
|
}) => {
|
13
14
|
const overlayRef = useProvidedRefOrCreate(_overlayRef);
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
|
16
|
+
if (!preventFocusOnOpen) {
|
17
|
+
useOpenAndCloseFocus({
|
18
|
+
containerRef: overlayRef,
|
19
|
+
returnFocusRef,
|
20
|
+
initialFocusRef
|
21
|
+
});
|
22
|
+
}
|
23
|
+
|
19
24
|
useOnOutsideClick({
|
20
25
|
containerRef: overlayRef,
|
21
26
|
ignoreClickRefs,
|
package/lib-esm/index.d.ts
CHANGED
@@ -24,6 +24,7 @@ export { useConfirm } from './Dialog/ConfirmationDialog';
|
|
24
24
|
export { ActionList } from './ActionList';
|
25
25
|
export { ActionMenu } from './ActionMenu';
|
26
26
|
export type { ActionMenuProps } from './ActionMenu';
|
27
|
+
export { default as Autocomplete } from './Autocomplete';
|
27
28
|
export { default as Avatar } from './Avatar';
|
28
29
|
export type { AvatarProps } from './Avatar';
|
29
30
|
export { default as AvatarPair } from './AvatarPair';
|
package/lib-esm/index.js
CHANGED
@@ -19,6 +19,7 @@ export { useConfirm } from './Dialog/ConfirmationDialog'; // Components
|
|
19
19
|
|
20
20
|
export { ActionList } from './ActionList';
|
21
21
|
export { ActionMenu } from './ActionMenu';
|
22
|
+
export { default as Autocomplete } from './Autocomplete';
|
22
23
|
export { default as Avatar } from './Avatar';
|
23
24
|
export { default as AvatarPair } from './AvatarPair';
|
24
25
|
export { default as AvatarStack } from './AvatarStack';
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare const scrollIntoViewingArea: (child: HTMLElement, container: HTMLElement, direction?: 'horizontal' | 'vertical', startMargin?: number, endMargin?: number, behavior?: ScrollBehavior) => void;
|
@@ -0,0 +1,30 @@
|
|
1
|
+
export const scrollIntoViewingArea = (child, container, direction = 'vertical', startMargin = 8, endMargin = 0, behavior = 'smooth') => {
|
2
|
+
const startSide = direction === 'vertical' ? 'top' : 'left';
|
3
|
+
const endSide = direction === 'vertical' ? 'bottom' : 'right';
|
4
|
+
const scrollSide = direction === 'vertical' ? 'scrollTop' : 'scrollLeft';
|
5
|
+
const {
|
6
|
+
[startSide]: childStart,
|
7
|
+
[endSide]: childEnd
|
8
|
+
} = child.getBoundingClientRect();
|
9
|
+
const {
|
10
|
+
[startSide]: containerStart,
|
11
|
+
[endSide]: containerEnd
|
12
|
+
} = container.getBoundingClientRect();
|
13
|
+
const isChildStartAboveViewingArea = childStart < containerStart + endMargin;
|
14
|
+
const isChildBottomBelowViewingArea = childEnd > containerEnd - startMargin;
|
15
|
+
|
16
|
+
if (isChildStartAboveViewingArea) {
|
17
|
+
const scrollHeightToChildStart = childStart - containerStart + container[scrollSide];
|
18
|
+
container.scrollTo({
|
19
|
+
behavior,
|
20
|
+
[startSide]: scrollHeightToChildStart - endMargin
|
21
|
+
});
|
22
|
+
} else if (isChildBottomBelowViewingArea) {
|
23
|
+
const scrollHeightToChildBottom = childEnd - containerEnd + container[scrollSide];
|
24
|
+
container.scrollTo({
|
25
|
+
behavior,
|
26
|
+
[startSide]: scrollHeightToChildBottom + startMargin
|
27
|
+
});
|
28
|
+
} // either completely in view or outside viewing area on both ends, don't scroll
|
29
|
+
|
30
|
+
};
|
package/lib-esm/utils/types.d.ts
CHANGED
@@ -12,3 +12,6 @@ export declare type ComponentProps<T> = T extends React.ComponentType<infer Prop
|
|
12
12
|
*/
|
13
13
|
export declare type Flatten<T extends unknown> = T extends (infer U)[] ? U : never;
|
14
14
|
export declare type AriaRole = 'alert' | 'alertdialog' | 'application' | 'article' | 'banner' | 'button' | 'cell' | 'checkbox' | 'columnheader' | 'combobox' | 'complementary' | 'contentinfo' | 'definition' | 'dialog' | 'directory' | 'document' | 'feed' | 'figure' | 'form' | 'grid' | 'gridcell' | 'group' | 'heading' | 'img' | 'link' | 'list' | 'listbox' | 'listitem' | 'log' | 'main' | 'marquee' | 'math' | 'menu' | 'menubar' | 'menuitem' | 'menuitemcheckbox ' | 'menuitemradio' | 'navigation' | 'none' | 'note' | 'option' | 'presentation' | 'progressbar' | 'radio' | 'radiogroup' | 'region' | 'row' | 'rowgroup' | 'rowheader' | 'scrollbar' | 'search' | 'searchbox' | 'separator' | 'slider' | 'spinbutton' | 'status' | 'switch' | 'tab' | 'table' | 'tablist' | 'tabpanel' | 'term' | 'textbox' | 'timer' | 'toolbar' | 'tooltip' | 'tree' | 'treegrid' | 'treeitem';
|
15
|
+
export declare type MandateProps<T extends {}, K extends keyof T> = Omit<T, K> & {
|
16
|
+
[MK in K]-?: NonNullable<T[MK]>;
|
17
|
+
};
|