@primer/components 0.0.0-20219819465 → 0.0.0-202198201647

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.
Files changed (66) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/dist/browser.esm.js +254 -253
  3. package/dist/browser.esm.js.map +1 -1
  4. package/dist/browser.umd.js +332 -331
  5. package/dist/browser.umd.js.map +1 -1
  6. package/lib/AnchoredOverlay/AnchoredOverlay.d.ts +1 -2
  7. package/lib/AnchoredOverlay/AnchoredOverlay.js +3 -11
  8. package/lib/FilteredActionList/FilteredActionList.js +31 -5
  9. package/lib/Overlay.d.ts +1 -2
  10. package/lib/Overlay.js +5 -10
  11. package/lib/Token/TokenBase.d.ts +15 -1
  12. package/lib/hooks/useOpenAndCloseFocus.d.ts +1 -2
  13. package/lib/hooks/useOpenAndCloseFocus.js +2 -7
  14. package/lib/hooks/useOverlay.d.ts +1 -2
  15. package/lib/hooks/useOverlay.js +2 -4
  16. package/lib/index.d.ts +0 -1
  17. package/lib/index.js +0 -8
  18. package/lib/utils/types/index.d.ts +0 -1
  19. package/lib/utils/types/index.js +0 -13
  20. package/lib-esm/AnchoredOverlay/AnchoredOverlay.d.ts +1 -2
  21. package/lib-esm/AnchoredOverlay/AnchoredOverlay.js +3 -11
  22. package/lib-esm/FilteredActionList/FilteredActionList.js +31 -3
  23. package/lib-esm/Overlay.d.ts +1 -2
  24. package/lib-esm/Overlay.js +5 -8
  25. package/lib-esm/Token/TokenBase.d.ts +15 -1
  26. package/lib-esm/hooks/useOpenAndCloseFocus.d.ts +1 -2
  27. package/lib-esm/hooks/useOpenAndCloseFocus.js +2 -7
  28. package/lib-esm/hooks/useOverlay.d.ts +1 -2
  29. package/lib-esm/hooks/useOverlay.js +2 -4
  30. package/lib-esm/index.d.ts +0 -1
  31. package/lib-esm/index.js +0 -1
  32. package/lib-esm/utils/types/index.d.ts +0 -1
  33. package/lib-esm/utils/types/index.js +1 -2
  34. package/package.json +1 -1
  35. package/lib/Autocomplete/Autocomplete.d.ts +0 -40
  36. package/lib/Autocomplete/Autocomplete.js +0 -68
  37. package/lib/Autocomplete/AutocompleteContext.d.ts +0 -17
  38. package/lib/Autocomplete/AutocompleteContext.js +0 -11
  39. package/lib/Autocomplete/AutocompleteInput.d.ts +0 -9
  40. package/lib/Autocomplete/AutocompleteInput.js +0 -150
  41. package/lib/Autocomplete/AutocompleteMenu.d.ts +0 -71
  42. package/lib/Autocomplete/AutocompleteMenu.js +0 -223
  43. package/lib/Autocomplete/AutocompleteOverlay.d.ts +0 -17
  44. package/lib/Autocomplete/AutocompleteOverlay.js +0 -74
  45. package/lib/Autocomplete/index.d.ts +0 -2
  46. package/lib/Autocomplete/index.js +0 -15
  47. package/lib/utils/scrollIntoViewingArea.d.ts +0 -1
  48. package/lib/utils/scrollIntoViewingArea.js +0 -39
  49. package/lib/utils/types/MandateProps.d.ts +0 -3
  50. package/lib/utils/types/MandateProps.js +0 -1
  51. package/lib-esm/Autocomplete/Autocomplete.d.ts +0 -40
  52. package/lib-esm/Autocomplete/Autocomplete.js +0 -47
  53. package/lib-esm/Autocomplete/AutocompleteContext.d.ts +0 -17
  54. package/lib-esm/Autocomplete/AutocompleteContext.js +0 -2
  55. package/lib-esm/Autocomplete/AutocompleteInput.d.ts +0 -9
  56. package/lib-esm/Autocomplete/AutocompleteInput.js +0 -131
  57. package/lib-esm/Autocomplete/AutocompleteMenu.d.ts +0 -71
  58. package/lib-esm/Autocomplete/AutocompleteMenu.js +0 -204
  59. package/lib-esm/Autocomplete/AutocompleteOverlay.d.ts +0 -17
  60. package/lib-esm/Autocomplete/AutocompleteOverlay.js +0 -56
  61. package/lib-esm/Autocomplete/index.d.ts +0 -2
  62. package/lib-esm/Autocomplete/index.js +0 -1
  63. package/lib-esm/utils/scrollIntoViewingArea.d.ts +0 -1
  64. package/lib-esm/utils/scrollIntoViewingArea.js +0 -30
  65. package/lib-esm/utils/types/MandateProps.d.ts +0 -3
  66. package/lib-esm/utils/types/MandateProps.js +0 -1
@@ -1,204 +0,0 @@
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
- } // eslint-disable-next-line @typescript-eslint/no-explicit-any
34
-
35
-
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: itemId
68
- }) => itemId));
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, inputRef, onSelectedChange, selectionVariant, setAutocompleteSuggestion, setInputValue, setShowMenu]);
91
- const itemSortOrderData = useMemo(() => sortedItemIds.reduce((acc, curr, i) => {
92
- acc[curr] = i;
93
- return acc;
94
- }, {}), [sortedItemIds]);
95
- const sortedAndFilteredItemsToRender = useMemo(() => selectableItems.filter(filterFn).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 => {
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, setAutocompleteSuggestion, selectionVariant, setInputValue]);
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.getAttribute('data-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, selectedItemIds, setAutocompleteSuggestion]);
151
- useEffect(() => {
152
- const itemIdSortResult = [...sortedItemIds].sort(sortOnCloseFn ? sortOnCloseFn : getDefaultSortFn(itemId => isItemSelected(itemId, selectedItemIds)));
153
- const sortResultMatchesState = itemIdSortResult.length === sortedItemIds.length && itemIdSortResult.every((element, index) => element === sortedItemIds[index]);
154
-
155
- if (showMenu === false && !sortResultMatchesState) {
156
- setSortedItemIds(itemIdSortResult);
157
- }
158
-
159
- onOpenChange && onOpenChange(Boolean(showMenu));
160
- }, [showMenu, onOpenChange, selectedItemIds, sortOnCloseFn, sortedItemIds]);
161
- useEffect(() => {
162
- if (selectedItemIds.length) {
163
- setSelectedItemLength && setSelectedItemLength(selectedItemIds.length);
164
- }
165
- }, [selectedItemIds, setSelectedItemLength]);
166
- return /*#__PURE__*/React.createElement(Box, {
167
- sx: !showMenu ? {
168
- // visually hides this label for sighted users
169
- position: 'absolute',
170
- width: '1px',
171
- height: '1px',
172
- padding: '0',
173
- margin: '-1px',
174
- overflow: 'hidden',
175
- clip: 'rect(0, 0, 0, 0)',
176
- whiteSpace: 'nowrap',
177
- borderWidth: '0'
178
- } : {}
179
- }, loading ? /*#__PURE__*/React.createElement(Box, {
180
- p: 3,
181
- display: "flex",
182
- justifyContent: "center"
183
- }, /*#__PURE__*/React.createElement(Spinner, null)) : /*#__PURE__*/React.createElement("div", {
184
- ref: listContainerRef
185
- }, allItemsToRender.length ? /*#__PURE__*/React.createElement(ActionList, {
186
- selectionVariant: "multiple" // have to typecast to `ItemProps` because we have an extra property
187
- // on `items` for Autocomplete: `metadata`
188
- ,
189
- items: allItemsToRender,
190
- role: "listbox",
191
- id: `${id}-listbox`,
192
- "aria-labelledby": ariaLabelledBy
193
- }) : /*#__PURE__*/React.createElement(Box, {
194
- p: 3
195
- }, emptyStateText)));
196
- }
197
-
198
- AutocompleteMenu.displayName = "AutocompleteMenu";
199
- AutocompleteMenu.defaultProps = {
200
- emptyStateText: 'No selectable options',
201
- selectionVariant: 'single'
202
- };
203
- AutocompleteMenu.displayName = 'AutocompleteMenu';
204
- export default AutocompleteMenu;
@@ -1,17 +0,0 @@
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;
@@ -1,56 +0,0 @@
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
-
35
- if (typeof window === 'undefined') {
36
- return null;
37
- }
38
-
39
- return /*#__PURE__*/React.createElement(Overlay, _extends({
40
- returnFocusRef: inputRef,
41
- preventFocusOnOpen: true,
42
- onClickOutside: closeOptionList,
43
- onEscape: closeOptionList,
44
- ref: combinedOverlayRef,
45
- top: position === null || position === void 0 ? void 0 : position.top,
46
- left: position === null || position === void 0 ? void 0 : position.left,
47
- visibility: showMenu ? 'visible' : 'hidden',
48
- sx: {
49
- overflow: 'auto'
50
- }
51
- }, overlayProps), children);
52
- };
53
-
54
- AutocompleteOverlay.displayName = "AutocompleteOverlay";
55
- AutocompleteOverlay.displayName = 'AutocompleteOverlay';
56
- export default AutocompleteOverlay;
@@ -1,2 +0,0 @@
1
- export { default } from './Autocomplete';
2
- export type { AutocompleteMenuProps, AutocompleteInputProps, AutocompleteOverlayProps } from './Autocomplete';
@@ -1 +0,0 @@
1
- export { default } from './Autocomplete';
@@ -1 +0,0 @@
1
- export declare const scrollIntoViewingArea: (child: HTMLElement, container: HTMLElement, direction?: 'horizontal' | 'vertical', startMargin?: number, endMargin?: number, behavior?: ScrollBehavior) => void;
@@ -1,30 +0,0 @@
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
- };
@@ -1,3 +0,0 @@
1
- export declare type MandateProps<T extends unknown, K extends keyof T> = Omit<T, K> & {
2
- [MK in K]-?: NonNullable<T[MK]>;
3
- };
@@ -1 +0,0 @@
1
- export {};