@primer/behaviors 0.0.0-202201892424 → 0.0.0-2022027213841

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 (78) hide show
  1. package/dist/cjs/anchored-position.d.ts +15 -0
  2. package/dist/cjs/anchored-position.js +210 -0
  3. package/dist/cjs/focus-trap.d.ts +1 -0
  4. package/dist/cjs/focus-trap.js +86 -0
  5. package/dist/cjs/focus-zone.d.ts +32 -0
  6. package/dist/cjs/focus-zone.js +410 -0
  7. package/dist/{index.d.ts → cjs/index.d.ts} +0 -0
  8. package/dist/cjs/index.js +16 -0
  9. package/dist/{polyfills → cjs/polyfills}/event-listener-signal.d.ts +0 -0
  10. package/dist/cjs/polyfills/event-listener-signal.js +44 -0
  11. package/dist/{scroll-into-view.d.ts → cjs/scroll-into-view.d.ts} +0 -0
  12. package/dist/cjs/scroll-into-view.js +21 -0
  13. package/dist/{utils → cjs/utils}/index.d.ts +0 -0
  14. package/dist/cjs/utils/index.js +15 -0
  15. package/dist/cjs/utils/iterate-focusable-elements.d.ts +9 -0
  16. package/dist/cjs/utils/iterate-focusable-elements.js +68 -0
  17. package/dist/{utils → cjs/utils}/unique-id.d.ts +0 -0
  18. package/dist/cjs/utils/unique-id.js +8 -0
  19. package/dist/{utils → cjs/utils}/user-agent.d.ts +0 -0
  20. package/dist/cjs/utils/user-agent.js +11 -0
  21. package/dist/esm/anchored-position.d.ts +15 -0
  22. package/dist/esm/anchored-position.js +206 -0
  23. package/dist/esm/focus-trap.d.ts +1 -0
  24. package/dist/esm/focus-trap.js +82 -0
  25. package/dist/esm/focus-zone.d.ts +32 -0
  26. package/dist/esm/focus-zone.js +406 -0
  27. package/{lib-esm → dist/esm}/index.d.ts +0 -0
  28. package/{lib/index.d.ts → dist/esm/index.js} +0 -0
  29. package/{lib-esm → dist/esm}/polyfills/event-listener-signal.d.ts +0 -0
  30. package/dist/esm/polyfills/event-listener-signal.js +40 -0
  31. package/{lib-esm → dist/esm}/scroll-into-view.d.ts +0 -0
  32. package/dist/esm/scroll-into-view.js +17 -0
  33. package/{lib-esm → dist/esm}/utils/index.d.ts +0 -0
  34. package/{lib/utils/index.d.ts → dist/esm/utils/index.js} +0 -0
  35. package/dist/esm/utils/iterate-focusable-elements.d.ts +9 -0
  36. package/dist/esm/utils/iterate-focusable-elements.js +61 -0
  37. package/{lib-esm → dist/esm}/utils/unique-id.d.ts +0 -0
  38. package/dist/esm/utils/unique-id.js +4 -0
  39. package/{lib-esm → dist/esm}/utils/user-agent.d.ts +0 -0
  40. package/dist/esm/utils/user-agent.js +7 -0
  41. package/package.json +18 -30
  42. package/utils/package.json +6 -6
  43. package/dist/anchored-position.d.ts +0 -89
  44. package/dist/focus-trap.d.ts +0 -12
  45. package/dist/focus-zone.d.ts +0 -137
  46. package/dist/utils/iterate-focusable-elements.d.ts +0 -42
  47. package/lib/anchored-position.d.ts +0 -89
  48. package/lib/anchored-position.js +0 -316
  49. package/lib/focus-trap.d.ts +0 -12
  50. package/lib/focus-trap.js +0 -179
  51. package/lib/focus-zone.d.ts +0 -137
  52. package/lib/focus-zone.js +0 -578
  53. package/lib/index.js +0 -57
  54. package/lib/polyfills/event-listener-signal.d.ts +0 -6
  55. package/lib/polyfills/event-listener-signal.js +0 -64
  56. package/lib/scroll-into-view.d.ts +0 -7
  57. package/lib/scroll-into-view.js +0 -42
  58. package/lib/utils/index.js +0 -44
  59. package/lib/utils/iterate-focusable-elements.d.ts +0 -42
  60. package/lib/utils/iterate-focusable-elements.js +0 -113
  61. package/lib/utils/unique-id.d.ts +0 -1
  62. package/lib/utils/unique-id.js +0 -12
  63. package/lib/utils/user-agent.d.ts +0 -1
  64. package/lib/utils/user-agent.js +0 -15
  65. package/lib-esm/anchored-position.d.ts +0 -89
  66. package/lib-esm/anchored-position.js +0 -309
  67. package/lib-esm/focus-trap.d.ts +0 -12
  68. package/lib-esm/focus-trap.js +0 -170
  69. package/lib-esm/focus-zone.d.ts +0 -137
  70. package/lib-esm/focus-zone.js +0 -559
  71. package/lib-esm/index.js +0 -4
  72. package/lib-esm/polyfills/event-listener-signal.js +0 -57
  73. package/lib-esm/scroll-into-view.js +0 -35
  74. package/lib-esm/utils/index.js +0 -3
  75. package/lib-esm/utils/iterate-focusable-elements.d.ts +0 -42
  76. package/lib-esm/utils/iterate-focusable-elements.js +0 -102
  77. package/lib-esm/utils/unique-id.js +0 -5
  78. package/lib-esm/utils/user-agent.js +0 -8
@@ -1,7 +1,7 @@
1
1
  {
2
- "name": "@primer/behaviors/utils",
3
- "types": "../lib/utils/index.d.ts",
4
- "main": "../lib/utils/index.js",
5
- "module": "../lib-esm/utils/index.js",
6
- "sideEffects": false
7
- }
2
+ "name": "@primer/behaviors/utils",
3
+ "types": "../dist/esm/utils/index.d.ts",
4
+ "main": "../dist/esm/utils/index.js",
5
+ "type": "module",
6
+ "sideEffects": false
7
+ }
@@ -1,89 +0,0 @@
1
- export declare type AnchorAlignment = 'start' | 'center' | 'end';
2
- export declare type AnchorSide = 'inside-top' | 'inside-bottom' | 'inside-left' | 'inside-right' | 'inside-center' | 'outside-top' | 'outside-bottom' | 'outside-left' | 'outside-right';
3
- /**
4
- * Settings that customize how a floating element is positioned
5
- * with respect to an anchor element.
6
- */
7
- export interface PositionSettings {
8
- /**
9
- * Sets the side of the anchor element that the floating element should be
10
- * pinned to. This side is given by a string starting with either "inside" or
11
- * "outside", followed by a hyphen, followed by either "top", "right", "bottom",
12
- * or "left". Additionally, "inside-center" is an allowed value.
13
- *
14
- * The first part of this string, "inside" or "outside", determines whether the
15
- * floating element should be attempted to be placed "inside" the anchor element
16
- * or "outside" of it. Using "inside" is useful for making it appear that the
17
- * anchor _contains_ the floating element, and it can be used for implementing a
18
- * dialog that is centered on the screen. The "outside" value is more common and
19
- * can be used for tooltips, popovers, menus, etc.
20
- *
21
- * The second part of this string determines the _edge_ on the anchor element that
22
- * the floating element will be anchored to. If side is "inside-center", then
23
- * the floating element will be centered in the X-direction (while align is used
24
- * to position it in the Y-direction).
25
- * Note: "outside-center" is _not_ a valid value for this property.
26
- */
27
- side: AnchorSide;
28
- /**
29
- * Determines how the floating element should align with the anchor element. If
30
- * set to "start", the floating element's first edge (top or left) will align
31
- * with the anchor element's first edge. If set to "center", the floating
32
- * element will be centered along the axis of the anchor edge. If set to "end",
33
- * the floating element's last edge will align with the anchor element's last edge.
34
- */
35
- align: AnchorAlignment;
36
- /**
37
- * The number of pixels between the anchor edge and the floating element.
38
- *
39
- * Positive values move the floating element farther from the anchor element
40
- * (for outside positioning) or further inside the anchor element (for inside
41
- * positioning). Negative values have the opposite effect.
42
- */
43
- anchorOffset: number;
44
- /**
45
- * An additional offset, in pixels, to move the floating element from
46
- * the aligning edge.
47
- *
48
- * Positive values move the floating element in the direction of center-
49
- * alignment. Negative values move the floating element away from center-
50
- * alignment. When align is "center", positive offsets move the floating
51
- * element right (top or bottom anchor side) or down (left or right
52
- * anchor side).
53
- */
54
- alignmentOffset: number;
55
- /**
56
- * If false, when the above settings result in rendering the floating element
57
- * wholly or partially outside of the bounds of the containing element, attempt
58
- * to adjust the settings to prevent this. Only applies to "outside" positioning.
59
- *
60
- * First, attempt to flip to the opposite edge of the anchor if the floating
61
- * element is getting clipped in that direction. If flipping results in a
62
- * similar clipping, try moving to the adjacent sides.
63
- *
64
- * Once we find a side that does not clip the overlay in its own dimension,
65
- * check the rest of the sides to see if we need to adjust the alignment offset
66
- * to fit in other dimensions.
67
- *
68
- * If we try all four sides and get clipped each time, settle for overflowing
69
- * and use the "bottom" side, since the ability to scroll is most likely in
70
- * this direction.
71
- */
72
- allowOutOfBounds: boolean;
73
- }
74
- export interface AnchorPosition {
75
- top: number;
76
- left: number;
77
- anchorSide: AnchorSide;
78
- }
79
- /**
80
- * Given a floating element and an anchor element, return coordinates for the top-left
81
- * of the floating element in order to absolutely position it such that it appears
82
- * near the anchor element.
83
- *
84
- * @param floatingElement Element intended to be positioned near or within an anchor
85
- * @param anchorElement The element to serve as the position anchor
86
- * @param settings Settings to determine the rules for positioning the floating element
87
- * @returns {top: number, left: number} coordinates for the floating element
88
- */
89
- export declare function getAnchoredPosition(floatingElement: Element, anchorElement: Element | DOMRect, settings?: Partial<PositionSettings>): AnchorPosition;
@@ -1,12 +0,0 @@
1
- /**
2
- * Traps focus within the given container.
3
- * @param container The container in which to trap focus
4
- * @returns AbortController - call `.abort()` to disable the focus trap
5
- */
6
- export declare function focusTrap(container: HTMLElement, initialFocus?: HTMLElement): AbortController;
7
- /**
8
- * Traps focus within the given container.
9
- * @param container The container in which to trap focus
10
- * @param abortSignal An AbortSignal to control the focus trap.
11
- */
12
- export declare function focusTrap(container: HTMLElement, initialFocus: HTMLElement | undefined, abortSignal: AbortSignal): void;
@@ -1,137 +0,0 @@
1
- export declare type Direction = 'previous' | 'next' | 'start' | 'end';
2
- export declare type FocusMovementKeys = 'ArrowLeft' | 'ArrowDown' | 'ArrowUp' | 'ArrowRight' | 'h' | 'j' | 'k' | 'l' | 'a' | 's' | 'w' | 'd' | 'Tab' | 'Home' | 'End' | 'PageUp' | 'PageDown';
3
- export declare enum FocusKeys {
4
- ArrowHorizontal = 1,
5
- ArrowVertical = 2,
6
- JK = 4,
7
- HL = 8,
8
- HomeAndEnd = 16,
9
- PageUpDown = 256,
10
- WS = 32,
11
- AD = 64,
12
- Tab = 128,
13
- ArrowAll = 3,
14
- HJKL = 12,
15
- WASD = 96,
16
- All = 511
17
- }
18
- /**
19
- * Options that control the behavior of the arrow focus behavior.
20
- */
21
- export interface FocusZoneSettings {
22
- /**
23
- * Choose the behavior applied in cases where focus is currently at either the first or
24
- * last element of the container.
25
- *
26
- * "stop" - do nothing and keep focus where it was
27
- * "wrap" - wrap focus around to the first element from the last, or the last element from the first
28
- *
29
- * Default: "stop"
30
- */
31
- focusOutBehavior?: 'stop' | 'wrap';
32
- /**
33
- * If set, this will be called to get the next focusable element. If this function
34
- * returns null, we will try to determine the next direction ourselves. Use the
35
- * `bindKeys` option to customize which keys are listened to.
36
- *
37
- * The function can accept a Direction, indicating the direction focus should move,
38
- * the HTMLElement that was previously focused, and lastly the `KeyboardEvent` object
39
- * created by the original `"keydown"` event.
40
- */
41
- getNextFocusable?: (direction: Direction, from: Element | undefined, event: KeyboardEvent) => HTMLElement | undefined;
42
- /**
43
- * Called to decide if a focusable element is allowed to participate in the arrow
44
- * key focus behavior.
45
- *
46
- * By default, all focusable elements within the given container will participate
47
- * in the arrow key focus behavior. If you need to withhold some elements from
48
- * participation, implement this callback to return false for those elements.
49
- */
50
- focusableElementFilter?: (element: HTMLElement) => boolean;
51
- /**
52
- * Bit flags that identify keys that will be bound to. Each available key either
53
- * moves focus to the "next" element or the "previous" element, so it is best
54
- * to only bind the keys that make sense to move focus in your UI. Use the `FocusKeys`
55
- * object to discover supported keys.
56
- *
57
- * Use the bitwise "OR" operator (`|`) to combine key types. For example,
58
- * `FocusKeys.WASD | FocusKeys.HJKL` represents all of W, A, S, D, H, J, K, and L.
59
- *
60
- * A note on FocusKeys.PageUpDown: This behavior does not support paging, so by default
61
- * using these keys will result in the same behavior as Home and End. To override this
62
- * behavior, implement `getNextFocusable`.
63
- *
64
- * The default for this setting is `FocusKeys.ArrowVertical | FocusKeys.HomeAndEnd`, unless
65
- * `getNextFocusable` is provided, in which case `FocusKeys.ArrowAll | FocusKeys.HomeAndEnd`
66
- * is used as the default.
67
- */
68
- bindKeys?: FocusKeys;
69
- /**
70
- * If provided, this signal can be used to disable the behavior and remove any
71
- * event listeners.
72
- */
73
- abortSignal?: AbortSignal;
74
- /**
75
- * If `activeDescendantControl` is supplied, do not move focus or alter `tabindex` on
76
- * any element. Instead, manage `aria-activedescendant` according to the ARIA best
77
- * practices guidelines.
78
- * @see https://www.w3.org/TR/wai-aria-practices-1.1/#kbd_focus_activedescendant
79
- *
80
- * The given `activeDescendantControl` will be given an `aria-controls` attribute that
81
- * references the ID of the `container`. Additionally, it will be given an
82
- * `aria-activedescendant` attribute that references the ID of the currently-active
83
- * descendant.
84
- *
85
- * This element will retain DOM focus as arrow keys are pressed.
86
- */
87
- activeDescendantControl?: HTMLElement;
88
- /**
89
- * Called each time the active descendant changes. Note that either of the parameters
90
- * may be undefined, e.g. when an element in the container first becomes active, or
91
- * when the controlling element becomes unfocused.
92
- */
93
- onActiveDescendantChanged?: (newActiveDescendant: HTMLElement | undefined, previousActiveDescendant: HTMLElement | undefined, directlyActivated: boolean) => void;
94
- /**
95
- * This option allows customization of the behavior that determines which of the
96
- * focusable elements should be focused when focus enters the container via the Tab key.
97
- *
98
- * When set to "first", whenever focus enters the container via Tab, we will focus the
99
- * first focusable element. When set to "previous", the most recently focused element
100
- * will be focused (fallback to first if there was no previous).
101
- *
102
- * The "closest" strategy works like "first", except either the first or the last element
103
- * of the container will be focused, depending on the direction from which focus comes.
104
- *
105
- * If a function is provided, this function should return the HTMLElement intended
106
- * to receive focus. This is useful if you want to focus the currently "selected"
107
- * item or element.
108
- *
109
- * Default: "previous"
110
- *
111
- * For more information, @see https://www.w3.org/TR/wai-aria-practices-1.1/#kbd_general_within
112
- */
113
- focusInStrategy?: 'first' | 'closest' | 'previous' | ((previousFocusedElement: Element) => HTMLElement | undefined);
114
- }
115
- export declare const isActiveDescendantAttribute = "data-is-active-descendant";
116
- /**
117
- * A value of activated-directly for data-is-active-descendant indicates the descendant was activated
118
- * by a manual user interaction with intent to move active descendant. This usually translates to the
119
- * user pressing one of the bound keys (up/down arrow, etc) to move through the focus zone. This is
120
- * intended to be roughly equivalent to the :focus-visible pseudo-class
121
- **/
122
- export declare const activeDescendantActivatedDirectly = "activated-directly";
123
- /**
124
- * A value of activated-indirectly for data-is-active-descendant indicates the descendant was activated
125
- * implicitly, and not by a direct key press. This includes focus zone being created from scratch, focusable
126
- * elements being added/removed, and mouseover events. This is intended to be roughly equivalent
127
- * to :focus:not(:focus-visible)
128
- **/
129
- export declare const activeDescendantActivatedIndirectly = "activated-indirectly";
130
- export declare const hasActiveDescendantAttribute = "data-has-active-descendant";
131
- /**
132
- * Sets up the arrow key focus behavior for all focusable elements in the given `container`.
133
- * @param container
134
- * @param settings
135
- * @returns
136
- */
137
- export declare function focusZone(container: HTMLElement, settings?: FocusZoneSettings): AbortController;
@@ -1,42 +0,0 @@
1
- /**
2
- * Options to the focusable elements iterator
3
- */
4
- export interface IterateFocusableElements {
5
- /**
6
- * (Default: false) Iterate through focusable elements in reverse-order
7
- */
8
- reverse?: boolean;
9
- /**
10
- * (Default: false) Perform additional checks to determine tabbability
11
- * which may adversely affect app performance.
12
- */
13
- strict?: boolean;
14
- /**
15
- * (Default: false) Only iterate tabbable elements, which is the subset
16
- * of focusable elements that are part of the page's tab sequence.
17
- */
18
- onlyTabbable?: boolean;
19
- }
20
- /**
21
- * Returns an iterator over all of the focusable elements within `container`.
22
- * Note: If `container` is itself focusable it will be included in the results.
23
- * @param container The container over which to find focusable elements.
24
- * @param reverse If true, iterate backwards through focusable elements.
25
- */
26
- export declare function iterateFocusableElements(container: HTMLElement, options?: IterateFocusableElements): Generator<HTMLElement, undefined, undefined>;
27
- /**
28
- * Determines whether the given element is focusable. If `strict` is true, we may
29
- * perform additional checks that require a reflow (less performant).
30
- * @param elem
31
- * @param strict
32
- */
33
- export declare function isFocusable(elem: HTMLElement, strict?: boolean): boolean;
34
- /**
35
- * Determines whether the given element is tabbable. If `strict` is true, we may
36
- * perform additional checks that require a reflow (less performant). This check
37
- * ensures that the element is focusable and that its tabindex is not explicitly
38
- * set to "-1" (which makes it focusable, but removes it from the tab order).
39
- * @param elem
40
- * @param strict
41
- */
42
- export declare function isTabbable(elem: HTMLElement, strict?: boolean): boolean;
@@ -1,89 +0,0 @@
1
- export declare type AnchorAlignment = 'start' | 'center' | 'end';
2
- export declare type AnchorSide = 'inside-top' | 'inside-bottom' | 'inside-left' | 'inside-right' | 'inside-center' | 'outside-top' | 'outside-bottom' | 'outside-left' | 'outside-right';
3
- /**
4
- * Settings that customize how a floating element is positioned
5
- * with respect to an anchor element.
6
- */
7
- export interface PositionSettings {
8
- /**
9
- * Sets the side of the anchor element that the floating element should be
10
- * pinned to. This side is given by a string starting with either "inside" or
11
- * "outside", followed by a hyphen, followed by either "top", "right", "bottom",
12
- * or "left". Additionally, "inside-center" is an allowed value.
13
- *
14
- * The first part of this string, "inside" or "outside", determines whether the
15
- * floating element should be attempted to be placed "inside" the anchor element
16
- * or "outside" of it. Using "inside" is useful for making it appear that the
17
- * anchor _contains_ the floating element, and it can be used for implementing a
18
- * dialog that is centered on the screen. The "outside" value is more common and
19
- * can be used for tooltips, popovers, menus, etc.
20
- *
21
- * The second part of this string determines the _edge_ on the anchor element that
22
- * the floating element will be anchored to. If side is "inside-center", then
23
- * the floating element will be centered in the X-direction (while align is used
24
- * to position it in the Y-direction).
25
- * Note: "outside-center" is _not_ a valid value for this property.
26
- */
27
- side: AnchorSide;
28
- /**
29
- * Determines how the floating element should align with the anchor element. If
30
- * set to "start", the floating element's first edge (top or left) will align
31
- * with the anchor element's first edge. If set to "center", the floating
32
- * element will be centered along the axis of the anchor edge. If set to "end",
33
- * the floating element's last edge will align with the anchor element's last edge.
34
- */
35
- align: AnchorAlignment;
36
- /**
37
- * The number of pixels between the anchor edge and the floating element.
38
- *
39
- * Positive values move the floating element farther from the anchor element
40
- * (for outside positioning) or further inside the anchor element (for inside
41
- * positioning). Negative values have the opposite effect.
42
- */
43
- anchorOffset: number;
44
- /**
45
- * An additional offset, in pixels, to move the floating element from
46
- * the aligning edge.
47
- *
48
- * Positive values move the floating element in the direction of center-
49
- * alignment. Negative values move the floating element away from center-
50
- * alignment. When align is "center", positive offsets move the floating
51
- * element right (top or bottom anchor side) or down (left or right
52
- * anchor side).
53
- */
54
- alignmentOffset: number;
55
- /**
56
- * If false, when the above settings result in rendering the floating element
57
- * wholly or partially outside of the bounds of the containing element, attempt
58
- * to adjust the settings to prevent this. Only applies to "outside" positioning.
59
- *
60
- * First, attempt to flip to the opposite edge of the anchor if the floating
61
- * element is getting clipped in that direction. If flipping results in a
62
- * similar clipping, try moving to the adjacent sides.
63
- *
64
- * Once we find a side that does not clip the overlay in its own dimension,
65
- * check the rest of the sides to see if we need to adjust the alignment offset
66
- * to fit in other dimensions.
67
- *
68
- * If we try all four sides and get clipped each time, settle for overflowing
69
- * and use the "bottom" side, since the ability to scroll is most likely in
70
- * this direction.
71
- */
72
- allowOutOfBounds: boolean;
73
- }
74
- export interface AnchorPosition {
75
- top: number;
76
- left: number;
77
- anchorSide: AnchorSide;
78
- }
79
- /**
80
- * Given a floating element and an anchor element, return coordinates for the top-left
81
- * of the floating element in order to absolutely position it such that it appears
82
- * near the anchor element.
83
- *
84
- * @param floatingElement Element intended to be positioned near or within an anchor
85
- * @param anchorElement The element to serve as the position anchor
86
- * @param settings Settings to determine the rules for positioning the floating element
87
- * @returns {top: number, left: number} coordinates for the floating element
88
- */
89
- export declare function getAnchoredPosition(floatingElement: Element, anchorElement: Element | DOMRect, settings?: Partial<PositionSettings>): AnchorPosition;