@primer/components 0.0.0-20211030175556 → 0.0.0-20211030182910
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 +34 -2
- package/dist/browser.esm.js +186 -183
- package/dist/browser.esm.js.map +1 -1
- package/dist/browser.umd.js +190 -187
- package/dist/browser.umd.js.map +1 -1
- package/lib/ActionList/Item.js +3 -3
- package/lib/ActionList/index.js +23 -12
- package/lib/ActionList2/Item.js +3 -1
- package/lib/ActionList2/List.js +1 -2
- package/lib/ActionList2/Selection.js +3 -1
- package/lib/ActionList2/index.js +41 -23
- package/lib/AnchoredOverlay/index.js +12 -4
- package/lib/Autocomplete/Autocomplete.d.ts +1 -0
- package/lib/Autocomplete/AutocompleteInput.d.ts +1 -0
- package/lib/Autocomplete/index.js +14 -7
- package/lib/Avatar.d.ts +1 -2
- package/lib/Avatar.js +1 -1
- package/lib/BranchName.d.ts +1 -2
- package/lib/BranchName.js +1 -1
- package/lib/Button/Button.d.ts +1 -0
- package/lib/Button/ButtonClose.d.ts +2 -1
- package/lib/Button/ButtonDanger.d.ts +1 -0
- package/lib/Button/ButtonInvisible.d.ts +1 -0
- package/lib/Button/ButtonOutline.d.ts +1 -0
- package/lib/Button/ButtonPrimary.d.ts +1 -0
- package/lib/Button/index.js +70 -21
- package/lib/Checkbox.d.ts +29 -0
- package/lib/Checkbox.js +64 -0
- package/lib/CircleOcticon.d.ts +1 -0
- package/lib/Details.d.ts +1 -2
- package/lib/Details.js +1 -3
- package/lib/Dialog.d.ts +3 -2
- package/lib/Dropdown.d.ts +6 -66
- package/lib/DropdownMenu/DropdownButton.d.ts +2 -1
- package/lib/DropdownMenu/index.js +20 -6
- package/lib/DropdownStyles.js +26 -18
- package/lib/FilterList.d.ts +1 -0
- package/lib/FilteredActionList/index.js +12 -4
- package/lib/Heading.d.ts +1 -2
- package/lib/Heading.js +1 -6
- package/lib/NewButton/index.js +12 -5
- package/lib/NewButton/types.js +1 -2
- package/lib/Overlay.d.ts +5 -3
- package/lib/Pagination/index.js +12 -6
- package/lib/Portal/index.js +16 -5
- package/lib/Position.d.ts +4 -4
- package/lib/ProgressBar.d.ts +16 -11
- package/lib/ProgressBar.js +6 -10
- package/lib/SelectMenu/SelectMenu.d.ts +10 -4
- package/lib/SelectMenu/SelectMenuItem.d.ts +1 -1
- package/lib/SelectMenu/SelectMenuModal.d.ts +1 -1
- package/lib/SelectMenu/hooks/useKeyboardNav.js +96 -80
- package/lib/SelectMenu/index.js +14 -7
- package/lib/SelectPanel/index.js +12 -4
- package/lib/Spinner.d.ts +1 -2
- package/lib/Spinner.js +1 -3
- package/lib/TextInputWithTokens.d.ts +1 -0
- package/lib/Token/AvatarToken.d.ts +1 -1
- package/lib/Token/IssueLabelToken.d.ts +1 -1
- package/lib/Token/Token.d.ts +1 -1
- package/lib/Token/index.js +30 -11
- package/lib/behaviors/anchoredPosition.js +234 -205
- package/lib/behaviors/focusTrap.js +157 -121
- package/lib/behaviors/focusZone.js +509 -434
- package/lib/behaviors/scrollIntoViewingArea.js +35 -18
- package/lib/constants.js +43 -39
- package/lib/drafts.js +30 -20
- package/lib/hooks/index.js +60 -16
- package/lib/hooks/useAnchoredPosition.js +40 -32
- package/lib/hooks/useCombinedRefs.js +36 -32
- package/lib/hooks/useDialog.js +96 -72
- package/lib/hooks/useFocusTrap.js +60 -43
- package/lib/hooks/useFocusZone.js +50 -54
- package/lib/hooks/useOnEscapePress.js +36 -25
- package/lib/hooks/useOpenAndCloseFocus.js +34 -22
- package/lib/hooks/useProvidedRefOrCreate.js +14 -10
- package/lib/hooks/useProvidedStateOrCreate.js +16 -13
- package/lib/hooks/useRenderForcingRef.js +17 -13
- package/lib/hooks/useResizeObserver.js +18 -15
- package/lib/hooks/useSafeTimeout.js +30 -22
- package/lib/hooks/useScrollFlash.js +23 -16
- package/lib/index.d.ts +2 -0
- package/lib/index.js +652 -163
- package/lib/polyfills/eventListenerSignal.js +45 -37
- package/lib/sx.js +22 -10
- package/lib/theme-preval.js +3169 -64
- package/lib/theme.js +12 -3
- package/lib/utils/iterateFocusableElements.js +85 -63
- package/lib/utils/testing.d.ts +2 -1
- package/lib/utils/testing.js +29 -0
- package/lib/utils/theme.js +47 -33
- package/lib/utils/types/AriaRole.js +1 -2
- package/lib/utils/types/ComponentProps.js +1 -2
- package/lib/utils/types/Flatten.js +1 -2
- package/lib/utils/types/KeyPaths.js +1 -2
- package/lib/utils/types/MandateProps.js +1 -16
- package/lib/utils/types/Merge.js +1 -2
- package/lib/utils/types/index.js +69 -16
- package/lib/utils/uniqueId.js +8 -5
- package/lib/utils/use-force-update.js +14 -8
- package/lib/utils/useIsomorphicLayoutEffect.js +11 -8
- package/lib/utils/userAgent.js +12 -8
- package/lib-esm/ActionList/Item.js +3 -3
- package/lib-esm/ActionList2/Item.js +3 -1
- package/lib-esm/ActionList2/List.js +1 -2
- package/lib-esm/ActionList2/Selection.js +3 -1
- package/lib-esm/Autocomplete/Autocomplete.d.ts +1 -0
- package/lib-esm/Autocomplete/AutocompleteInput.d.ts +1 -0
- package/lib-esm/Avatar.d.ts +1 -2
- package/lib-esm/Avatar.js +2 -2
- package/lib-esm/BranchName.d.ts +1 -2
- package/lib-esm/BranchName.js +2 -2
- package/lib-esm/Button/Button.d.ts +1 -0
- package/lib-esm/Button/ButtonClose.d.ts +2 -1
- package/lib-esm/Button/ButtonDanger.d.ts +1 -0
- package/lib-esm/Button/ButtonInvisible.d.ts +1 -0
- package/lib-esm/Button/ButtonOutline.d.ts +1 -0
- package/lib-esm/Button/ButtonPrimary.d.ts +1 -0
- package/lib-esm/Checkbox.d.ts +29 -0
- package/lib-esm/Checkbox.js +44 -0
- package/lib-esm/CircleOcticon.d.ts +1 -0
- package/lib-esm/Details.d.ts +1 -2
- package/lib-esm/Details.js +1 -2
- package/lib-esm/Dialog.d.ts +3 -2
- package/lib-esm/Dropdown.d.ts +6 -66
- package/lib-esm/DropdownMenu/DropdownButton.d.ts +2 -1
- package/lib-esm/FilterList.d.ts +1 -0
- package/lib-esm/Heading.d.ts +1 -2
- package/lib-esm/Heading.js +2 -6
- package/lib-esm/Overlay.d.ts +5 -3
- package/lib-esm/Position.d.ts +4 -4
- package/lib-esm/ProgressBar.d.ts +16 -11
- package/lib-esm/ProgressBar.js +7 -11
- package/lib-esm/SelectMenu/SelectMenu.d.ts +10 -4
- package/lib-esm/SelectMenu/SelectMenuItem.d.ts +1 -1
- package/lib-esm/SelectMenu/SelectMenuModal.d.ts +1 -1
- package/lib-esm/Spinner.d.ts +1 -2
- package/lib-esm/Spinner.js +1 -2
- package/lib-esm/TextInputWithTokens.d.ts +1 -0
- package/lib-esm/Token/AvatarToken.d.ts +1 -1
- package/lib-esm/Token/IssueLabelToken.d.ts +1 -1
- package/lib-esm/Token/Token.d.ts +1 -1
- package/lib-esm/index.d.ts +2 -0
- package/lib-esm/index.js +1 -0
- package/lib-esm/theme-preval.js +446 -0
- package/lib-esm/utils/testing.d.ts +2 -1
- package/lib-esm/utils/testing.js +24 -0
- package/package.json +4 -5
- package/lib/ActionList/Divider.jsx +0 -29
- package/lib/ActionList/Group.jsx +0 -23
- package/lib/ActionList/Header.jsx +0 -66
- package/lib/ActionList/Item.jsx +0 -288
- package/lib/ActionList/List.jsx +0 -138
- package/lib/ActionList2/Description.jsx +0 -29
- package/lib/ActionList2/Divider.jsx +0 -22
- package/lib/ActionList2/Group.jsx +0 -54
- package/lib/ActionList2/Header.d.ts +0 -26
- package/lib/ActionList2/Header.js +0 -55
- package/lib/ActionList2/Header.jsx +0 -36
- package/lib/ActionList2/Item.jsx +0 -174
- package/lib/ActionList2/LinkItem.jsx +0 -28
- package/lib/ActionList2/List.jsx +0 -41
- package/lib/ActionList2/Selection.jsx +0 -50
- package/lib/ActionList2/Visuals.jsx +0 -48
- package/lib/ActionMenu.jsx +0 -73
- package/lib/AnchoredOverlay/AnchoredOverlay.jsx +0 -100
- package/lib/Autocomplete/Autocomplete.jsx +0 -100
- package/lib/Autocomplete/AutocompleteContext.jsx +0 -5
- package/lib/Autocomplete/AutocompleteInput.jsx +0 -113
- package/lib/Autocomplete/AutocompleteMenu.jsx +0 -190
- package/lib/Autocomplete/AutocompleteOverlay.jsx +0 -55
- package/lib/Avatar.jsx +0 -34
- package/lib/AvatarPair.jsx +0 -29
- package/lib/AvatarStack.jsx +0 -151
- package/lib/BaseStyles.jsx +0 -65
- package/lib/BorderBox.jsx +0 -18
- package/lib/Box.jsx +0 -10
- package/lib/BranchName.jsx +0 -20
- package/lib/Breadcrumbs.jsx +0 -71
- package/lib/Button/Button.jsx +0 -40
- package/lib/Button/ButtonBase.jsx +0 -33
- package/lib/Button/ButtonClose.jsx +0 -53
- package/lib/Button/ButtonDanger.jsx +0 -43
- package/lib/Button/ButtonGroup.jsx +0 -55
- package/lib/Button/ButtonInvisible.jsx +0 -32
- package/lib/Button/ButtonOutline.jsx +0 -43
- package/lib/Button/ButtonPrimary.jsx +0 -42
- package/lib/Button/ButtonStyles.jsx +0 -37
- package/lib/Button/ButtonTableList.jsx +0 -46
- package/lib/Caret.jsx +0 -93
- package/lib/CircleBadge.jsx +0 -42
- package/lib/CircleOcticon.jsx +0 -21
- package/lib/CounterLabel.jsx +0 -43
- package/lib/Details.jsx +0 -21
- package/lib/Dialog/ConfirmationDialog.jsx +0 -146
- package/lib/Dialog/Dialog.jsx +0 -279
- package/lib/Dialog.jsx +0 -129
- package/lib/Dropdown.jsx +0 -131
- package/lib/DropdownMenu/DropdownButton.jsx +0 -14
- package/lib/DropdownMenu/DropdownMenu.jsx +0 -70
- package/lib/FilterList.jsx +0 -59
- package/lib/FilteredActionList/FilteredActionList.jsx +0 -100
- package/lib/FilteredSearch.jsx +0 -28
- package/lib/Flash.jsx +0 -69
- package/lib/Flex.jsx +0 -15
- package/lib/FormGroup.jsx +0 -22
- package/lib/Grid.jsx +0 -15
- package/lib/Header.jsx +0 -83
- package/lib/Heading.jsx +0 -21
- package/lib/Label.jsx +0 -82
- package/lib/LabelGroup.jsx +0 -18
- package/lib/Link.jsx +0 -36
- package/lib/NewButton/button-counter.jsx +0 -14
- package/lib/NewButton/button.jsx +0 -279
- package/lib/Overlay.jsx +0 -154
- package/lib/Pagehead.jsx +0 -17
- package/lib/Pagination/Pagination.jsx +0 -161
- package/lib/Pagination/model.jsx +0 -174
- package/lib/PointerBox.jsx +0 -25
- package/lib/Popover.jsx +0 -202
- package/lib/Portal/Portal.jsx +0 -79
- package/lib/Position.jsx +0 -46
- package/lib/ProgressBar.jsx +0 -39
- package/lib/SelectMenu/SelectMenu.jsx +0 -112
- package/lib/SelectMenu/SelectMenuContext.jsx +0 -5
- package/lib/SelectMenu/SelectMenuDivider.jsx +0 -42
- package/lib/SelectMenu/SelectMenuFilter.jsx +0 -58
- package/lib/SelectMenu/SelectMenuFooter.jsx +0 -45
- package/lib/SelectMenu/SelectMenuHeader.jsx +0 -42
- package/lib/SelectMenu/SelectMenuItem.jsx +0 -142
- package/lib/SelectMenu/SelectMenuList.jsx +0 -59
- package/lib/SelectMenu/SelectMenuLoadingAnimation.jsx +0 -22
- package/lib/SelectMenu/SelectMenuModal.jsx +0 -118
- package/lib/SelectMenu/SelectMenuTab.jsx +0 -92
- package/lib/SelectMenu/SelectMenuTabPanel.jsx +0 -42
- package/lib/SelectMenu/SelectMenuTabs.jsx +0 -57
- package/lib/SelectPanel/SelectPanel.jsx +0 -105
- package/lib/SideNav.jsx +0 -173
- package/lib/Spinner.jsx +0 -35
- package/lib/StateLabel.jsx +0 -93
- package/lib/StyledOcticon.jsx +0 -18
- package/lib/SubNav.jsx +0 -101
- package/lib/TabNav.jsx +0 -58
- package/lib/Text.jsx +0 -14
- package/lib/TextInput.jsx +0 -23
- package/lib/TextInputWithTokens.jsx +0 -218
- package/lib/ThemeProvider.jsx +0 -130
- package/lib/Timeline.jsx +0 -123
- package/lib/Token/AvatarToken.jsx +0 -54
- package/lib/Token/IssueLabelToken.jsx +0 -125
- package/lib/Token/Token.jsx +0 -103
- package/lib/Token/TokenBase.jsx +0 -88
- package/lib/Token/_RemoveTokenButton.jsx +0 -108
- package/lib/Token/_TokenTextContainer.jsx +0 -49
- package/lib/Tooltip.jsx +0 -246
- package/lib/Truncate.jsx +0 -24
- package/lib/UnderlineNav.jsx +0 -88
- package/lib/_TextInputWrapper.jsx +0 -120
- package/lib/_UnstyledTextInput.jsx +0 -22
- package/lib/hooks/useDetails.jsx +0 -39
- package/lib/hooks/useOnOutsideClick.jsx +0 -61
- package/lib/hooks/useOverlay.jsx +0 -15
- package/lib/utils/create-slots.jsx +0 -65
- package/lib/utils/deprecate.jsx +0 -59
- package/lib/utils/isNumeric.jsx +0 -7
- package/lib/utils/ssr.jsx +0 -6
- package/lib/utils/test-deprecations.jsx +0 -20
- package/lib/utils/test-helpers.jsx +0 -8
- package/lib/utils/test-matchers.jsx +0 -100
- package/lib/utils/testing.jsx +0 -206
- package/lib-esm/ActionList2/Header.d.ts +0 -26
- package/lib-esm/ActionList2/Header.js +0 -44
@@ -1,15 +1,26 @@
|
|
1
1
|
"use strict";
|
2
|
-
|
3
|
-
exports
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.getAnchoredPosition = getAnchoredPosition;
|
7
|
+
// When prettier supports template literal types...
|
8
|
+
// export type AnchorSide = `${'inside' | 'outside'}-${'top' | 'bottom' | 'right' | 'left'}` | 'inside-center'
|
9
|
+
|
10
|
+
/**
|
11
|
+
* Settings that customize how a floating element is positioned
|
12
|
+
* with respect to an anchor element.
|
13
|
+
*/
|
4
14
|
// For each outside anchor position, list the order of alternate positions to try in
|
5
15
|
// the event that the original position overflows. See comment on `allowOutOfBounds`
|
6
16
|
// for a more detailed description.
|
7
17
|
const alternateOrders = {
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
18
|
+
'outside-top': ['outside-bottom', 'outside-right', 'outside-left', 'outside-bottom'],
|
19
|
+
'outside-bottom': ['outside-top', 'outside-right', 'outside-left', 'outside-bottom'],
|
20
|
+
'outside-left': ['outside-right', 'outside-bottom', 'outside-top', 'outside-bottom'],
|
21
|
+
'outside-right': ['outside-left', 'outside-bottom', 'outside-top', 'outside-bottom']
|
12
22
|
};
|
23
|
+
|
13
24
|
/**
|
14
25
|
* Given a floating element and an anchor element, return coordinates for the top-left
|
15
26
|
* of the floating element in order to absolutely position it such that it appears
|
@@ -21,31 +32,35 @@ const alternateOrders = {
|
|
21
32
|
* @returns {top: number, left: number} coordinates for the floating element
|
22
33
|
*/
|
23
34
|
function getAnchoredPosition(floatingElement, anchorElement, settings = {}) {
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
35
|
+
const parentElement = getPositionedParent(floatingElement);
|
36
|
+
const clippingRect = getClippingRect(parentElement);
|
37
|
+
const parentElementStyle = getComputedStyle(parentElement);
|
38
|
+
const parentElementRect = parentElement.getBoundingClientRect();
|
39
|
+
const [borderTop, borderLeft] = [parentElementStyle.borderTopWidth, parentElementStyle.borderLeftWidth].map(v => parseInt(v, 10) || 0);
|
40
|
+
const relativeRect = {
|
41
|
+
top: parentElementRect.top + borderTop,
|
42
|
+
left: parentElementRect.left + borderLeft
|
43
|
+
};
|
44
|
+
return pureCalculateAnchoredPosition(clippingRect, relativeRect, floatingElement.getBoundingClientRect(), anchorElement instanceof Element ? anchorElement.getBoundingClientRect() : anchorElement, getDefaultSettings(settings));
|
34
45
|
}
|
35
|
-
exports.getAnchoredPosition = getAnchoredPosition;
|
36
46
|
/**
|
37
47
|
* Returns the nearest proper HTMLElement parent of `element` whose
|
38
48
|
* position is not "static", or document.body, whichever is closer
|
39
49
|
*/
|
50
|
+
|
51
|
+
|
40
52
|
function getPositionedParent(element) {
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
parentNode = parentNode.parentNode;
|
53
|
+
let parentNode = element.parentNode;
|
54
|
+
|
55
|
+
while (parentNode !== null) {
|
56
|
+
if (parentNode instanceof HTMLElement && getComputedStyle(parentNode).position !== 'static') {
|
57
|
+
return parentNode;
|
47
58
|
}
|
48
|
-
|
59
|
+
|
60
|
+
parentNode = parentNode.parentNode;
|
61
|
+
}
|
62
|
+
|
63
|
+
return document.body;
|
49
64
|
}
|
50
65
|
/**
|
51
66
|
* Returns the rectangle (relative to the window) that will clip the given element
|
@@ -53,63 +68,68 @@ function getPositionedParent(element) {
|
|
53
68
|
* @param element
|
54
69
|
* @returns
|
55
70
|
*/
|
71
|
+
|
72
|
+
|
56
73
|
function getClippingRect(element) {
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
const parentNodeStyle = getComputedStyle(parentNode);
|
63
|
-
if (parentNodeStyle.overflow !== 'visible') {
|
64
|
-
break;
|
65
|
-
}
|
66
|
-
parentNode = parentNode.parentNode;
|
74
|
+
let parentNode = element;
|
75
|
+
|
76
|
+
while (parentNode !== null) {
|
77
|
+
if (parentNode === document.body) {
|
78
|
+
break;
|
67
79
|
}
|
68
|
-
|
69
|
-
const
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
80
|
+
|
81
|
+
const parentNodeStyle = getComputedStyle(parentNode);
|
82
|
+
|
83
|
+
if (parentNodeStyle.overflow !== 'visible') {
|
84
|
+
break;
|
85
|
+
}
|
86
|
+
|
87
|
+
parentNode = parentNode.parentNode;
|
88
|
+
}
|
89
|
+
|
90
|
+
const clippingNode = parentNode === document.body || !(parentNode instanceof HTMLElement) ? document.body : parentNode;
|
91
|
+
const elemRect = clippingNode.getBoundingClientRect();
|
92
|
+
const elemStyle = getComputedStyle(clippingNode);
|
93
|
+
const [borderTop, borderLeft, borderRight, borderBottom] = [elemStyle.borderTopWidth, elemStyle.borderLeftWidth, elemStyle.borderRightWidth, elemStyle.borderBottomWidth].map(v => parseInt(v, 10) || 0);
|
94
|
+
return {
|
95
|
+
top: elemRect.top + borderTop,
|
96
|
+
left: elemRect.left + borderLeft,
|
97
|
+
width: elemRect.width - borderRight - borderLeft,
|
98
|
+
// If the clipping node is document.body, it can expand to the full height of the window
|
99
|
+
height: Math.max(elemRect.height - borderTop - borderBottom, clippingNode === document.body ? window.innerHeight : -Infinity)
|
100
|
+
};
|
101
|
+
} // Default settings to position a floating element
|
102
|
+
|
103
|
+
|
86
104
|
const positionDefaults = {
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
105
|
+
side: 'outside-bottom',
|
106
|
+
align: 'start',
|
107
|
+
// note: the following default is not applied if side === "inside-center"
|
108
|
+
anchorOffset: 4,
|
109
|
+
// note: the following default is only applied if side starts with "inside"
|
110
|
+
// and align is not center
|
111
|
+
alignmentOffset: 4,
|
112
|
+
allowOutOfBounds: false
|
95
113
|
};
|
96
114
|
/**
|
97
115
|
* Compute a full PositionSettings object from the given partial PositionSettings object
|
98
116
|
* by filling in with defaults where applicable.
|
99
117
|
* @param settings Partial settings - any omissions will be defaulted
|
100
118
|
*/
|
119
|
+
|
101
120
|
function getDefaultSettings(settings = {}) {
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
121
|
+
var _settings$side, _settings$align, _settings$anchorOffse, _settings$alignmentOf, _settings$allowOutOfB;
|
122
|
+
|
123
|
+
const side = (_settings$side = settings.side) !== null && _settings$side !== void 0 ? _settings$side : positionDefaults.side;
|
124
|
+
const align = (_settings$align = settings.align) !== null && _settings$align !== void 0 ? _settings$align : positionDefaults.align;
|
125
|
+
return {
|
126
|
+
side,
|
127
|
+
align,
|
128
|
+
// offsets always default to 0 if their respective side/alignment is centered
|
129
|
+
anchorOffset: (_settings$anchorOffse = settings.anchorOffset) !== null && _settings$anchorOffse !== void 0 ? _settings$anchorOffse : side === 'inside-center' ? 0 : positionDefaults.anchorOffset,
|
130
|
+
alignmentOffset: (_settings$alignmentOf = settings.alignmentOffset) !== null && _settings$alignmentOf !== void 0 ? _settings$alignmentOf : align !== 'center' && side.startsWith('inside') ? positionDefaults.alignmentOffset : 0,
|
131
|
+
allowOutOfBounds: (_settings$allowOutOfB = settings.allowOutOfBounds) !== null && _settings$allowOutOfB !== void 0 ? _settings$allowOutOfB : positionDefaults.allowOutOfBounds
|
132
|
+
};
|
113
133
|
}
|
114
134
|
/**
|
115
135
|
* Note: This is a pure function with no dependency on DOM APIs.
|
@@ -122,56 +142,71 @@ function getDefaultSettings(settings = {}) {
|
|
122
142
|
* @param anchorRect BoxPosition for the anchor element
|
123
143
|
* @param PositionSettings to customize the calculated position for the floating element.
|
124
144
|
*/
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
145
|
+
|
146
|
+
|
147
|
+
function pureCalculateAnchoredPosition(viewportRect, relativePosition, floatingRect, anchorRect, {
|
148
|
+
side,
|
149
|
+
align,
|
150
|
+
allowOutOfBounds,
|
151
|
+
anchorOffset,
|
152
|
+
alignmentOffset
|
153
|
+
}) {
|
154
|
+
// Compute the relative viewport rect, to bring it into the same coordinate space as `pos`
|
155
|
+
const relativeViewportRect = {
|
156
|
+
top: viewportRect.top - relativePosition.top,
|
157
|
+
left: viewportRect.left - relativePosition.left,
|
158
|
+
width: viewportRect.width,
|
159
|
+
height: viewportRect.height
|
160
|
+
};
|
161
|
+
let pos = calculatePosition(floatingRect, anchorRect, side, align, anchorOffset, alignmentOffset);
|
162
|
+
let anchorSide = side;
|
163
|
+
pos.top -= relativePosition.top;
|
164
|
+
pos.left -= relativePosition.left; // Handle screen overflow
|
165
|
+
|
166
|
+
if (!allowOutOfBounds) {
|
167
|
+
const alternateOrder = alternateOrders[side];
|
168
|
+
let positionAttempt = 0;
|
169
|
+
|
170
|
+
if (alternateOrder) {
|
171
|
+
let prevSide = side; // Try all the alternate sides until one does not overflow
|
172
|
+
|
173
|
+
while (positionAttempt < alternateOrder.length && shouldRecalculatePosition(prevSide, pos, relativeViewportRect, floatingRect)) {
|
174
|
+
const nextSide = alternateOrder[positionAttempt++];
|
175
|
+
prevSide = nextSide; // If we have cut off in the same dimension as the "side" option, try flipping to the opposite side.
|
176
|
+
|
177
|
+
pos = calculatePosition(floatingRect, anchorRect, nextSide, align, anchorOffset, alignmentOffset);
|
178
|
+
pos.top -= relativePosition.top;
|
179
|
+
pos.left -= relativePosition.left;
|
180
|
+
anchorSide = nextSide;
|
181
|
+
}
|
182
|
+
} // At this point we've flipped the position if applicable. Now just nudge until it's on-screen.
|
183
|
+
|
184
|
+
|
185
|
+
if (pos.top < relativeViewportRect.top) {
|
186
|
+
pos.top = relativeViewportRect.top;
|
187
|
+
}
|
188
|
+
|
189
|
+
if (pos.left < relativeViewportRect.left) {
|
190
|
+
pos.left = relativeViewportRect.left;
|
191
|
+
}
|
192
|
+
|
193
|
+
if (pos.left + floatingRect.width > viewportRect.width + relativeViewportRect.left) {
|
194
|
+
pos.left = viewportRect.width + relativeViewportRect.left - floatingRect.width;
|
195
|
+
} // If we have exhausted all possible positions and none of them worked, we
|
196
|
+
// say that overflowing the bottom of the screen is acceptable since it is
|
197
|
+
// likely to be able to scroll.
|
198
|
+
|
199
|
+
|
200
|
+
if (alternateOrder && positionAttempt < alternateOrder.length) {
|
201
|
+
if (pos.top + floatingRect.height > viewportRect.height + relativeViewportRect.top) {
|
202
|
+
pos.top = viewportRect.height + relativeViewportRect.top - floatingRect.height;
|
203
|
+
}
|
173
204
|
}
|
174
|
-
|
205
|
+
}
|
206
|
+
|
207
|
+
return { ...pos,
|
208
|
+
anchorSide
|
209
|
+
};
|
175
210
|
}
|
176
211
|
/**
|
177
212
|
* Given a floating element and an anchor element, return coordinates for the
|
@@ -186,87 +221,82 @@ function pureCalculateAnchoredPosition(viewportRect, relativePosition, floatingR
|
|
186
221
|
* @param alignmentOffset Absolute pixel offset for alignment
|
187
222
|
* @returns {top: number, left: number} coordinates for the floating element
|
188
223
|
*/
|
224
|
+
|
225
|
+
|
189
226
|
function calculatePosition(elementDimensions, anchorPosition, side, align, anchorOffset, alignmentOffset) {
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
227
|
+
const anchorRight = anchorPosition.left + anchorPosition.width;
|
228
|
+
const anchorBottom = anchorPosition.top + anchorPosition.height;
|
229
|
+
let top = -1;
|
230
|
+
let left = -1;
|
231
|
+
|
232
|
+
if (side === 'outside-top') {
|
233
|
+
top = anchorPosition.top - anchorOffset - elementDimensions.height;
|
234
|
+
} else if (side === 'outside-bottom') {
|
235
|
+
top = anchorBottom + anchorOffset;
|
236
|
+
} else if (side === 'outside-left') {
|
237
|
+
left = anchorPosition.left - anchorOffset - elementDimensions.width;
|
238
|
+
} else if (side === 'outside-right') {
|
239
|
+
left = anchorRight + anchorOffset;
|
240
|
+
}
|
241
|
+
|
242
|
+
if (side === 'outside-top' || side === 'outside-bottom') {
|
243
|
+
if (align === 'start') {
|
244
|
+
left = anchorPosition.left + alignmentOffset;
|
245
|
+
} else if (align === 'center') {
|
246
|
+
left = anchorPosition.left - (elementDimensions.width - anchorPosition.width) / 2 + alignmentOffset;
|
247
|
+
} else {
|
248
|
+
// end
|
249
|
+
left = anchorRight - elementDimensions.width - alignmentOffset;
|
202
250
|
}
|
203
|
-
|
204
|
-
|
251
|
+
}
|
252
|
+
|
253
|
+
if (side === 'outside-left' || side === 'outside-right') {
|
254
|
+
if (align === 'start') {
|
255
|
+
top = anchorPosition.top + alignmentOffset;
|
256
|
+
} else if (align === 'center') {
|
257
|
+
top = anchorPosition.top - (elementDimensions.height - anchorPosition.height) / 2 + alignmentOffset;
|
258
|
+
} else {
|
259
|
+
// end
|
260
|
+
top = anchorBottom - elementDimensions.height - alignmentOffset;
|
205
261
|
}
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
262
|
+
}
|
263
|
+
|
264
|
+
if (side === 'inside-top') {
|
265
|
+
top = anchorPosition.top + anchorOffset;
|
266
|
+
} else if (side === 'inside-bottom') {
|
267
|
+
top = anchorBottom - anchorOffset - elementDimensions.height;
|
268
|
+
} else if (side === 'inside-left') {
|
269
|
+
left = anchorPosition.left + anchorOffset;
|
270
|
+
} else if (side === 'inside-right') {
|
271
|
+
left = anchorRight - anchorOffset - elementDimensions.width;
|
272
|
+
} else if (side === 'inside-center') {
|
273
|
+
left = (anchorRight + anchorPosition.left) / 2 - elementDimensions.width / 2 + anchorOffset;
|
274
|
+
}
|
275
|
+
|
276
|
+
if (side === 'inside-top' || side === 'inside-bottom') {
|
277
|
+
if (align === 'start') {
|
278
|
+
left = anchorPosition.left + alignmentOffset;
|
279
|
+
} else if (align === 'center') {
|
280
|
+
left = anchorPosition.left - (elementDimensions.width - anchorPosition.width) / 2 + alignmentOffset;
|
281
|
+
} else {
|
282
|
+
// end
|
283
|
+
left = anchorRight - elementDimensions.width - alignmentOffset;
|
217
284
|
}
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
// end
|
227
|
-
top = anchorBottom - elementDimensions.height - alignmentOffset;
|
228
|
-
}
|
285
|
+
} else if (side === 'inside-left' || side === 'inside-right' || side === 'inside-center') {
|
286
|
+
if (align === 'start') {
|
287
|
+
top = anchorPosition.top + alignmentOffset;
|
288
|
+
} else if (align === 'center') {
|
289
|
+
top = anchorPosition.top - (elementDimensions.height - anchorPosition.height) / 2 + alignmentOffset;
|
290
|
+
} else {
|
291
|
+
// end
|
292
|
+
top = anchorBottom - elementDimensions.height - alignmentOffset;
|
229
293
|
}
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
else if (side === 'inside-left') {
|
237
|
-
left = anchorPosition.left + anchorOffset;
|
238
|
-
}
|
239
|
-
else if (side === 'inside-right') {
|
240
|
-
left = anchorRight - anchorOffset - elementDimensions.width;
|
241
|
-
}
|
242
|
-
else if (side === 'inside-center') {
|
243
|
-
left = (anchorRight + anchorPosition.left) / 2 - elementDimensions.width / 2 + anchorOffset;
|
244
|
-
}
|
245
|
-
if (side === 'inside-top' || side === 'inside-bottom') {
|
246
|
-
if (align === 'start') {
|
247
|
-
left = anchorPosition.left + alignmentOffset;
|
248
|
-
}
|
249
|
-
else if (align === 'center') {
|
250
|
-
left = anchorPosition.left - (elementDimensions.width - anchorPosition.width) / 2 + alignmentOffset;
|
251
|
-
}
|
252
|
-
else {
|
253
|
-
// end
|
254
|
-
left = anchorRight - elementDimensions.width - alignmentOffset;
|
255
|
-
}
|
256
|
-
}
|
257
|
-
else if (side === 'inside-left' || side === 'inside-right' || side === 'inside-center') {
|
258
|
-
if (align === 'start') {
|
259
|
-
top = anchorPosition.top + alignmentOffset;
|
260
|
-
}
|
261
|
-
else if (align === 'center') {
|
262
|
-
top = anchorPosition.top - (elementDimensions.height - anchorPosition.height) / 2 + alignmentOffset;
|
263
|
-
}
|
264
|
-
else {
|
265
|
-
// end
|
266
|
-
top = anchorBottom - elementDimensions.height - alignmentOffset;
|
267
|
-
}
|
268
|
-
}
|
269
|
-
return { top, left };
|
294
|
+
}
|
295
|
+
|
296
|
+
return {
|
297
|
+
top,
|
298
|
+
left
|
299
|
+
};
|
270
300
|
}
|
271
301
|
/**
|
272
302
|
* Determines if there is an overflow
|
@@ -275,13 +305,12 @@ function calculatePosition(elementDimensions, anchorPosition, side, align, ancho
|
|
275
305
|
* @param containerDimensions
|
276
306
|
* @param elementDimensions
|
277
307
|
*/
|
308
|
+
|
309
|
+
|
278
310
|
function shouldRecalculatePosition(side, currentPos, containerDimensions, elementDimensions) {
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
currentPos.left + elementDimensions.width > containerDimensions.width + containerDimensions.left);
|
286
|
-
}
|
287
|
-
}
|
311
|
+
if (side === 'outside-top' || side === 'outside-bottom') {
|
312
|
+
return currentPos.top < containerDimensions.top || currentPos.top + elementDimensions.height > containerDimensions.height + containerDimensions.top;
|
313
|
+
} else {
|
314
|
+
return currentPos.left < containerDimensions.left || currentPos.left + elementDimensions.width > containerDimensions.width + containerDimensions.left;
|
315
|
+
}
|
316
|
+
}
|