@allxsmith/bestax-bulma 5.22.0 → 5.23.0

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/dist/index.cjs CHANGED
@@ -2256,10 +2256,12 @@ function getActiveElementInTree(node) {
2256
2256
  * opener sits in. A closed shadow root cannot be entered, so focus inside one
2257
2257
  * reads as its host.
2258
2258
  *
2259
+ * @param doc - The document to start from: the component's own, when it may
2260
+ * render into one other than the page's (an iframe's). Defaults to `document`.
2259
2261
  * @returns The focused element, or `null` when the document has none.
2260
2262
  */
2261
- function getDeepestActiveElement() {
2262
- let active = document.activeElement;
2263
+ function getDeepestActiveElement(doc = document) {
2264
+ let active = doc.activeElement;
2263
2265
  while (active?.shadowRoot?.activeElement) {
2264
2266
  active = active.shadowRoot.activeElement;
2265
2267
  }
@@ -2796,6 +2798,32 @@ function useScrollLock(active) {
2796
2798
  }, [active]);
2797
2799
  }
2798
2800
 
2801
+ // "Are we past hydration?" as a store that never changes: React reads the
2802
+ // server snapshot both while server-rendering and while hydrating, and the
2803
+ // client snapshot from the first post-hydration render onward.
2804
+ const subscribeToNothing = () => () => { };
2805
+ const getClientSnapshot = () => true;
2806
+ const getServerSnapshot = () => false;
2807
+ /**
2808
+ * Reports whether the component has passed hydration.
2809
+ *
2810
+ * Returns `false` during server rendering and during the hydrating client
2811
+ * render, then `true` from the commit that follows. Use it to defer
2812
+ * DOM-only behaviour (such as portalling) past hydration so the first client
2813
+ * render matches the server markup instead of tripping hydration recovery.
2814
+ *
2815
+ * Only a component that hydrates ever sees `false` on the client. One that
2816
+ * mounts later (after a route change, or in an app with no server rendering)
2817
+ * gets `true` on its first render, so it pays for no extra render. It
2818
+ * subscribes to nothing, so no later render flips it back.
2819
+ *
2820
+ * @function useIsHydrated
2821
+ * @returns `true` once the hydrating render has completed, otherwise `false`.
2822
+ */
2823
+ function useIsHydrated() {
2824
+ return React.useSyncExternalStore(subscribeToNothing, getClientSnapshot, getServerSnapshot);
2825
+ }
2826
+
2799
2827
  /**
2800
2828
  * Resolves a portal target: an `HTMLElement` is used directly, a non-empty
2801
2829
  * `string` is treated as a `document.querySelector` selector (falling back to
@@ -2819,31 +2847,49 @@ function resolvePortalContainer(container) {
2819
2847
  }
2820
2848
  return container;
2821
2849
  }
2822
-
2823
- // "Are we past hydration?" as a store that never changes: React reads the
2824
- // server snapshot both while server-rendering and while hydrating, and the
2825
- // client snapshot from the first post-hydration render onward.
2826
- const subscribeToNothing = () => () => { };
2827
- const getClientSnapshot = () => true;
2828
- const getServerSnapshot = () => false;
2829
2850
  /**
2830
- * Reports whether the component has passed hydration.
2851
+ * Renders its children into another part of the page, `document.body` unless `container` says otherwise, so floating content escapes an ancestor's `overflow`, `transform` or stacking context.
2831
2852
  *
2832
- * Returns `false` during server rendering and during the hydrating client
2833
- * render, then `true` from the commit that follows. Use it to defer
2834
- * DOM-only behaviour (such as portalling) past hydration so the first client
2835
- * render matches the server markup instead of tripping hydration recovery.
2853
+ * React's server renderer can't render a portal, so `Portal` renders nothing
2854
+ * on the server and during hydration, and mounts its children in the commit
2855
+ * that follows. The first client render then matches the server markup. In an app
2856
+ * without server rendering it portals on the first render.
2836
2857
  *
2837
- * Only a component that hydrates ever sees `false` on the client. One that
2838
- * mounts later (after a route change, or in an app with no server rendering)
2839
- * gets `true` on its first render, so it pays for no extra render. It
2840
- * subscribes to nothing, so no later render flips it back.
2858
+ * React context, such as `ConfigProvider`'s class prefix, reaches the
2859
+ * portaled children, and their React events bubble through the React tree.
2860
+ * CSS inheritance follows the DOM instead: a scoped (non-`isRoot`) `Theme`
2861
+ * sets its variables on a wrapper element the portaled content has left, so
2862
+ * that content sees the page-level theme. Point `container` at an element
2863
+ * inside the themed area to keep it.
2841
2864
  *
2842
- * @function useIsHydrated
2843
- * @returns `true` once the hydrating render has completed, otherwise `false`.
2865
+ * Tab order and focus follow the DOM too. Content portaled from inside a
2866
+ * focus trap is no longer inside the trapped element, so the trap neither
2867
+ * reaches it nor holds focus there: render nested overlays inside the
2868
+ * trapped element, or give them a trap of their own.
2869
+ *
2870
+ * @function
2871
+ * @param {PortalProps} props - Props for the Portal component.
2872
+ * @returns The portal, the children in place when `disabled`, or nothing
2873
+ * before hydration.
2874
+ *
2875
+ * @example
2876
+ * <Portal>
2877
+ * <div className="box">Rendered at the end of document.body</div>
2878
+ * </Portal>
2879
+ *
2880
+ * @example
2881
+ * // Into a dedicated overlay root
2882
+ * <Portal container="#overlays">
2883
+ * <CommandPalette />
2884
+ * </Portal>
2844
2885
  */
2845
- function useIsHydrated() {
2846
- return React.useSyncExternalStore(subscribeToNothing, getClientSnapshot, getServerSnapshot);
2886
+ function Portal({ children, container, disabled = false }) {
2887
+ const hydrated = useIsHydrated();
2888
+ if (disabled)
2889
+ return jsxRuntime.jsx(jsxRuntime.Fragment, { children: children });
2890
+ if (!hydrated)
2891
+ return null;
2892
+ return reactDom.createPortal(children, resolvePortalContainer(container));
2847
2893
  }
2848
2894
 
2849
2895
  /**
@@ -2857,7 +2903,7 @@ const activeModalStack = [];
2857
2903
  * excluded because `focus()` on them is a no-op, which would leave focus
2858
2904
  * outside the modal.
2859
2905
  */
2860
- const FOCUSABLE_SELECTOR$1 = 'button:not(:disabled), [href], input:not(:disabled):not([type="hidden"]), select:not(:disabled), textarea:not(:disabled), [tabindex]';
2906
+ const FOCUSABLE_SELECTOR = 'button:not(:disabled), [href], input:not(:disabled):not([type="hidden"]), select:not(:disabled), textarea:not(:disabled), [tabindex]';
2861
2907
  /**
2862
2908
  * The modal's tab stops, in document order. The selector alone is not the
2863
2909
  * tabbable set — `[href]` and `button` match regardless of `tabindex`, so an
@@ -2866,7 +2912,7 @@ const FOCUSABLE_SELECTOR$1 = 'button:not(:disabled), [href], input:not(:disabled
2866
2912
  * resolved value, so filtering on it drops every negative index (not just
2867
2913
  * `-1`) without a second selector to keep in sync.
2868
2914
  */
2869
- const getTabbable = (node) => Array.from(node.querySelectorAll(FOCUSABLE_SELECTOR$1)).filter(el => el.tabIndex >= 0);
2915
+ const getTabbable = (node) => Array.from(node.querySelectorAll(FOCUSABLE_SELECTOR)).filter(el => el.tabIndex >= 0);
2870
2916
  /**
2871
2917
  * Modal.Background - Renders the modal background overlay.
2872
2918
  *
@@ -10926,63 +10972,395 @@ const Calendar = ({ value, focusedDate, onSelect, onFocusedDateChange, min, max,
10926
10972
  };
10927
10973
  Calendar.displayName = 'Calendar';
10928
10974
 
10929
- const FOCUSABLE_SELECTOR = [
10930
- 'a[href]',
10931
- 'button:not([disabled])',
10932
- 'input:not([disabled]):not([type="hidden"])',
10933
- 'select:not([disabled])',
10934
- 'textarea:not([disabled])',
10935
- '[tabindex]:not([tabindex="-1"])',
10936
- ].join(',');
10937
- function useFocusTrap(containerRef, active, options = {}) {
10938
- const { initialFocusRef, restoreFocus = true } = options;
10975
+ /**
10976
+ * Whether `el` is editable while its parent is not, which makes it an editing
10977
+ * host: the one tab stop an editable region gets. `contenteditable="false"`
10978
+ * turns editing off, and a missing or unknown value inherits the parent's.
10979
+ */
10980
+ function isEditingHost(el) {
10981
+ const editable = (node) => {
10982
+ const value = node.getAttribute('contenteditable')?.toLowerCase();
10983
+ if (value === undefined)
10984
+ return null;
10985
+ if (value === '' || value === 'true' || value === 'plaintext-only') {
10986
+ return true;
10987
+ }
10988
+ return value === 'false' ? false : null;
10989
+ };
10990
+ if (editable(el) !== true)
10991
+ return false;
10992
+ for (let node = el.parentElement; node; node = node.parentElement) {
10993
+ const state = editable(node);
10994
+ if (state !== null)
10995
+ return !state;
10996
+ }
10997
+ return true;
10998
+ }
10999
+ /** Whether `el` is the summary of its `<details>`: the first one, not a stray. */
11000
+ const isDetailsSummary = (el) => el.parentElement?.localName === 'details' &&
11001
+ Array.from(el.parentElement.children).find(child => child.localName === 'summary') === el;
11002
+ /**
11003
+ * Whether the browser puts `el` in the tab order, before anything that hides
11004
+ * or disables it. The `tabIndex` property can't answer this on its own: it
11005
+ * can read `-1` for an editable region or an `<embed>` that Tab still visits,
11006
+ * and `0` for a `<video>` without controls that it doesn't. So a valid
11007
+ * `tabindex` attribute decides, and otherwise the element's kind does.
11008
+ */
11009
+ function isTabbableKind(el) {
11010
+ const tabindex = el.getAttribute('tabindex');
11011
+ if (tabindex !== null && /^\s*[+-]?\d/.test(tabindex)) {
11012
+ return el.tabIndex >= 0;
11013
+ }
11014
+ switch (el.localName) {
11015
+ case 'a':
11016
+ case 'area':
11017
+ return el.hasAttribute('href');
11018
+ case 'input':
11019
+ return el.type !== 'hidden';
11020
+ case 'button':
11021
+ case 'select':
11022
+ case 'textarea':
11023
+ case 'iframe':
11024
+ case 'embed':
11025
+ case 'object':
11026
+ return true;
11027
+ case 'audio':
11028
+ case 'video':
11029
+ return el.hasAttribute('controls');
11030
+ case 'summary':
11031
+ return isDetailsSummary(el);
11032
+ default:
11033
+ return isEditingHost(el);
11034
+ }
11035
+ }
11036
+ /**
11037
+ * The node's parent in the flattened tree, the one the browser renders and
11038
+ * tabs through: the slot it is assigned to, else its parent, else the host of
11039
+ * the shadow root it tops, and nothing above the document. A closed shadow
11040
+ * root hides its slots (`assignedSlot` reads `null` under one), so a child of
11041
+ * its host goes straight to the host, whether a slot takes it or not.
11042
+ */
11043
+ const flatParent = (node) => node.assignedSlot ??
11044
+ node.parentElement ??
11045
+ node.parentNode?.host ??
11046
+ null;
11047
+ /**
11048
+ * The element's children in the flattened tree, in the order they render: a
11049
+ * host's shadow root content, a slot's assigned elements (or its fallback
11050
+ * content when nothing is assigned), anything else's own children. A host
11051
+ * whose shadow root is closed has no root to read, so its own children stand
11052
+ * in, including any that no slot takes and that aren't rendered.
11053
+ */
11054
+ function flatChildren(el) {
11055
+ if (el.shadowRoot)
11056
+ return Array.from(el.shadowRoot.children);
11057
+ if (el.localName === 'slot') {
11058
+ const assigned = el.assignedNodes();
11059
+ if (assigned.length > 0) {
11060
+ return assigned.filter((node) => node.nodeType === Node.ELEMENT_NODE);
11061
+ }
11062
+ }
11063
+ return Array.from(el.children);
11064
+ }
11065
+ const isShown = (el) => !el.hasAttribute('inert') && getComputedStyle(el).display !== 'none';
11066
+ const isVisible = (el) => {
11067
+ const { visibility } = getComputedStyle(el);
11068
+ return visibility !== 'hidden' && visibility !== 'collapse';
11069
+ };
11070
+ /**
11071
+ * Whether nothing from `el` up to `top` hides it: `inert`, `display: none`
11072
+ * (not inherited, so each element is checked) or a closed `<details>` it
11073
+ * isn't the summary of. `top` itself isn't checked, and `null` walks to the
11074
+ * top of the page.
11075
+ */
11076
+ function isShownUpTo(el, top) {
11077
+ let below = null;
11078
+ for (let node = el; node && node !== top; node = flatParent(node)) {
11079
+ if (!isShown(node))
11080
+ return false;
11081
+ if (below &&
11082
+ node.localName === 'details' &&
11083
+ !node.hasAttribute('open') &&
11084
+ !isDetailsSummary(below)) {
11085
+ return false;
11086
+ }
11087
+ below = node;
11088
+ }
11089
+ return true;
11090
+ }
11091
+ /**
11092
+ * The images that use the image map `area` belongs to. An image names its
11093
+ * map with `usemap="#…"`, matching the map's `name` or `id`.
11094
+ */
11095
+ function imagesUsingMapOf(area) {
11096
+ const map = area.closest('map');
11097
+ if (!map)
11098
+ return [];
11099
+ const names = [map.getAttribute('name'), map.id].filter(name => name);
11100
+ return Array.from(area.getRootNode().querySelectorAll('img[usemap]')).filter(img => {
11101
+ const ref = img.getAttribute('usemap');
11102
+ return ref.startsWith('#') && names.includes(ref.slice(1));
11103
+ });
11104
+ }
11105
+ /**
11106
+ * Whether an image map area is shown. An area has no box of its own: the
11107
+ * HTML rendering rules give it `display: none`, though a browser may not
11108
+ * report that. So it counts where an image using its map is shown, as long
11109
+ * as nothing makes it inert.
11110
+ */
11111
+ function isAreaShown(area, container) {
11112
+ for (let node = area; node && node !== container; node = flatParent(node)) {
11113
+ if (node.hasAttribute('inert'))
11114
+ return false;
11115
+ }
11116
+ return imagesUsingMapOf(area).some(img => isShownUpTo(img, null) && isVisible(img));
11117
+ }
11118
+ /**
11119
+ * Whether Tab can land on `el`, one of the kinds it visits. A disabled control
11120
+ * (including one inside a disabled `<fieldset>`), an inert subtree and
11121
+ * anything not rendered are skipped. `visibility` is inherited, so the
11122
+ * element's own computed value covers its ancestors.
11123
+ */
11124
+ function isRenderedAndEnabled(el, container) {
11125
+ if (el.matches(':disabled'))
11126
+ return false;
11127
+ if (el.localName === 'area')
11128
+ return isAreaShown(el, container);
11129
+ return isShownUpTo(el, container) && isVisible(el);
11130
+ }
11131
+ /** A radio button with a name, which shares one tab stop with its group. */
11132
+ const isGroupedRadio = (el) => el.localName === 'input' &&
11133
+ el.type === 'radio' &&
11134
+ el.name !== '';
11135
+ /** The flattened-tree ancestors of `node` below the container, outermost first, then `node`. */
11136
+ function flatChain(node, container) {
11137
+ const chain = [];
11138
+ for (let n = node; n && n !== container; n = flatParent(n)) {
11139
+ chain.unshift(n);
11140
+ }
11141
+ return chain;
11142
+ }
11143
+ /**
11144
+ * Whether `node` is inside the container in the flattened tree: in it, in a
11145
+ * shadow root it holds, or slotted into it. A child of a closed-root host
11146
+ * counts as inside wherever the host is, as its slot can't be seen.
11147
+ */
11148
+ function inside(container, node) {
11149
+ for (let n = node; n; n = flatParent(n)) {
11150
+ if (n === container)
11151
+ return true;
11152
+ }
11153
+ return false;
11154
+ }
11155
+ /**
11156
+ * `true` when Tab reaches `b` after `a`, which is their order in the
11157
+ * flattened tree: a shadow host comes before its shadow root's content, and
11158
+ * slotted content comes where its slot is. A closed root's slots can't be
11159
+ * seen, so its host's children keep their place in the light DOM. Two
11160
+ * flattened-tree siblings are always in one DOM tree, so their document
11161
+ * position decides between them.
11162
+ */
11163
+ function follows(container, a, b) {
11164
+ const chainA = flatChain(a, container);
11165
+ const chainB = flatChain(b, container);
11166
+ for (let i = 0; i < Math.max(chainA.length, chainB.length); i++) {
11167
+ const [x, y] = [chainA[i], chainB[i]];
11168
+ if (x === y)
11169
+ continue;
11170
+ if (!x)
11171
+ return true;
11172
+ if (!y)
11173
+ return false;
11174
+ return Boolean(x.compareDocumentPosition(y) & Node.DOCUMENT_POSITION_FOLLOWING);
11175
+ }
11176
+ return false;
11177
+ }
11178
+ /**
11179
+ * Every element of a tabbable kind under `el` in the flattened tree, depth
11180
+ * first, which is the order Tab visits them. Under a host whose shadow root
11181
+ * is closed, that takes in the host's own children instead, rendered or not.
11182
+ */
11183
+ function collectCandidates(el, out = []) {
11184
+ for (const child of flatChildren(el)) {
11185
+ if (isTabbableKind(child))
11186
+ out.push(child);
11187
+ collectCandidates(child, out);
11188
+ }
11189
+ return out;
11190
+ }
11191
+ /**
11192
+ * The first and last tab stops inside `container`, or `null` when there are
11193
+ * none. Only the ends matter for wrapping, so the style checks run from each
11194
+ * end until they find a stop rather than over every candidate.
11195
+ *
11196
+ * A radio group is one stop, the way the browser treats it: its checked
11197
+ * button, or its first when none is checked. Counting every button would make
11198
+ * the group's last one an end, and Tab from the checked one would leave.
11199
+ */
11200
+ function findTabStops(container) {
11201
+ const candidates = collectCandidates(container);
11202
+ const checked = new Map();
11203
+ const usable = (el) => {
11204
+ let ok = checked.get(el);
11205
+ if (ok === undefined) {
11206
+ ok = isRenderedAndEnabled(el, container);
11207
+ checked.set(el, ok);
11208
+ }
11209
+ return ok;
11210
+ };
11211
+ const groupStop = (radio) => {
11212
+ const group = candidates.filter((el) => isGroupedRadio(el) &&
11213
+ el.name === radio.name &&
11214
+ el.form === radio.form &&
11215
+ el.getRootNode() === radio.getRootNode());
11216
+ return group.find(el => el.checked && usable(el)) ?? group.find(usable);
11217
+ };
11218
+ const isStop = (el) => usable(el) && (!isGroupedRadio(el) || groupStop(el) === el);
11219
+ const first = candidates.find(isStop);
11220
+ if (!first)
11221
+ return null;
11222
+ let last = first;
11223
+ for (let i = candidates.length - 1; candidates[i] !== first; i--) {
11224
+ if (isStop(candidates[i])) {
11225
+ last = candidates[i];
11226
+ break;
11227
+ }
11228
+ }
11229
+ return {
11230
+ first,
11231
+ last,
11232
+ stopFor: node => (isGroupedRadio(node) && groupStop(node)) || node,
11233
+ };
11234
+ }
11235
+ /**
11236
+ * The element that has focus in the container's own document (an iframe's,
11237
+ * when it renders into one), inside open shadow roots too. Focus inside a
11238
+ * closed one reads as its host, which the comparisons above place like any
11239
+ * other element. Browsers fall back to <body> when nothing has
11240
+ * focus. jsdom, where consumers run their tests, reports nothing once focus
11241
+ * was inside a shadow root that was removed, so this falls back for it.
11242
+ */
11243
+ const focusedElement = (doc) => getDeepestActiveElement(doc) ?? doc.body;
11244
+ /**
11245
+ * Keeps keyboard focus inside a container while it is active: focus moves in
11246
+ * when the trap turns on, Tab and Shift+Tab wrap between the first and last
11247
+ * tab stops, and focus goes back where it came from when the trap turns off.
11248
+ *
11249
+ * Tab stops are what the browser's Tab visits, in document order: links,
11250
+ * form controls, frames, media with controls, a `<details>` summary, editable
11251
+ * regions, anything with `tabIndex={0}`, and the content of open shadow
11252
+ * roots. Hidden, disabled, inert and `tabIndex={-1}` elements are skipped,
11253
+ * and a radio group counts once. Give the container `tabIndex={-1}` so it can
11254
+ * hold focus itself when nothing inside can. The trap listens on the
11255
+ * container, so a trap nested inside another handles Tab first and the outer
11256
+ * one stands aside.
11257
+ *
11258
+ * The trap is about Tab. If a Tab it let through still takes focus out (a
11259
+ * browser can leave out a stop the page has, such as links when its settings
11260
+ * say to), it sends focus back to the other end. A pointer or a script moving
11261
+ * focus out is left alone, because pulling focus back from a click is modal
11262
+ * behaviour, and a modal also has to block the page behind it, which `Modal`
11263
+ * does. Content the container renders through a portal lives elsewhere in
11264
+ * the DOM, so it is outside the trap: render nested overlays inside the
11265
+ * container, or give them a trap of their own.
11266
+ *
11267
+ * The container has to be in the DOM when the trap turns on. The trap waits
11268
+ * for hydration, so a container that only appears once the page has
11269
+ * hydrated, the way portaled content does, still has it attach in the commit
11270
+ * where the container appears.
11271
+ *
11272
+ * @function useFocusTrap
11273
+ * @param containerRef - Ref to the element focus stays inside.
11274
+ * @param options - When the trap is on, and where focus starts and ends.
11275
+ *
11276
+ * @example
11277
+ * const panelRef = useRef<HTMLDivElement>(null);
11278
+ * useFocusTrap(panelRef, { active: isOpen });
11279
+ * return isOpen ? (
11280
+ * <div ref={panelRef} role="dialog" aria-label="Filters" tabIndex={-1}>
11281
+ * …
11282
+ * </div>
11283
+ * ) : null;
11284
+ */
11285
+ function useFocusTrap(containerRef, options = {}) {
11286
+ const { active = true, initialFocusRef, restoreFocus = true } = options;
11287
+ // Portaled content has no server-rendered counterpart, so a portaled
11288
+ // container only exists from the commit after hydration.
11289
+ const hydrated = useIsHydrated();
10939
11290
  React.useEffect(() => {
10940
- if (!active)
11291
+ if (!active || !hydrated)
10941
11292
  return undefined;
10942
11293
  const container = containerRef.current;
10943
11294
  if (!container)
10944
11295
  return undefined;
10945
- const previouslyFocused = typeof document !== 'undefined'
10946
- ? document.activeElement
10947
- : null;
10948
- const focusInitial = () => {
10949
- if (initialFocusRef?.current) {
10950
- initialFocusRef.current.focus();
10951
- return;
10952
- }
10953
- const focusables = Array.from(container.querySelectorAll(FOCUSABLE_SELECTOR)).filter(el => !el.hasAttribute('disabled'));
10954
- const target = focusables[0] ?? container;
10955
- target.focus();
10956
- };
10957
- focusInitial();
11296
+ const doc = container.ownerDocument;
11297
+ const opener = focusedElement(doc);
11298
+ (initialFocusRef?.current ??
11299
+ findTabStops(container)?.first ??
11300
+ container).focus();
11301
+ // Where focus goes if a Tab the trap let the browser handle takes it out,
11302
+ // held until that Tab has finished moving focus.
11303
+ let wrapTo = null;
11304
+ let wrapTimer;
10958
11305
  const handleKeyDown = (e) => {
10959
- if (e.key !== 'Tab')
11306
+ // A trap nested inside this one has already wrapped focus.
11307
+ if (e.key !== 'Tab' || e.defaultPrevented)
10960
11308
  return;
10961
- const focusables = Array.from(container.querySelectorAll(FOCUSABLE_SELECTOR)).filter(el => !el.hasAttribute('disabled'));
10962
- if (focusables.length === 0) {
11309
+ const stops = findTabStops(container);
11310
+ if (!stops) {
10963
11311
  e.preventDefault();
11312
+ container.focus();
10964
11313
  return;
10965
11314
  }
10966
- const first = focusables[0];
10967
- const last = focusables[focusables.length - 1];
10968
- const activeEl = document.activeElement;
10969
- if (e.shiftKey && activeEl === first) {
11315
+ const current = stops.stopFor(focusedElement(doc));
11316
+ // Wrap whenever the browser has no stop left in that direction inside
11317
+ // the container. Comparing positions rather than testing `=== first`
11318
+ // also covers focus resting on the container or on an element outside
11319
+ // the tab order, which the browser would otherwise walk straight out of.
11320
+ if (e.shiftKey
11321
+ ? !follows(container, stops.first, current)
11322
+ : !follows(container, current, stops.last)) {
10970
11323
  e.preventDefault();
10971
- last.focus();
10972
- }
10973
- else if (!e.shiftKey && activeEl === last) {
10974
- e.preventDefault();
10975
- first.focus();
11324
+ (e.shiftKey ? stops.last : stops.first).focus();
11325
+ return;
10976
11326
  }
11327
+ wrapTo = e.shiftKey ? stops.last : stops.first;
11328
+ clearTimeout(wrapTimer);
11329
+ wrapTimer = setTimeout(() => {
11330
+ wrapTo = null;
11331
+ });
11332
+ };
11333
+ // The browser moves focus as soon as the keydown is over, so focus that
11334
+ // lands outside while `wrapTo` is set got there by that Tab.
11335
+ const handleFocusIn = (e) => {
11336
+ if (!wrapTo || inside(container, e.composedPath()[0]))
11337
+ return;
11338
+ const target = wrapTo;
11339
+ wrapTo = null;
11340
+ target.focus();
10977
11341
  };
10978
11342
  container.addEventListener('keydown', handleKeyDown);
11343
+ doc.addEventListener('focusin', handleFocusIn, true);
10979
11344
  return () => {
10980
11345
  container.removeEventListener('keydown', handleKeyDown);
10981
- if (restoreFocus && previouslyFocused?.focus) {
10982
- previouslyFocused.focus();
11346
+ doc.removeEventListener('focusin', handleFocusIn, true);
11347
+ clearTimeout(wrapTimer);
11348
+ let target = restoreFocus ? opener : null;
11349
+ // `null` is an object too, and a caller without types can pass it.
11350
+ if (restoreFocus && typeof restoreFocus === 'object') {
11351
+ // Read now rather than when the trap turned on: a trigger that
11352
+ // re-mounted in between has a new node.
11353
+ // eslint-disable-next-line react-hooks/exhaustive-deps
11354
+ target = restoreFocus.current;
11355
+ }
11356
+ // Hand focus back only while the trap still holds it: inside the
11357
+ // container, or dropped to <body> because the container left the page.
11358
+ const now = focusedElement(doc);
11359
+ if (target && (now === doc.body || inside(container, now))) {
11360
+ target.focus();
10983
11361
  }
10984
11362
  };
10985
- }, [active, containerRef, initialFocusRef, restoreFocus]);
11363
+ }, [active, hydrated, containerRef, initialFocusRef, restoreFocus]);
10986
11364
  }
10987
11365
 
10988
11366
  const isBrowser = typeof window !== 'undefined';
@@ -11089,7 +11467,7 @@ const PickerPopover = ({ isOpen, onClose, anchorRef, children, position = 'botto
11089
11467
  document.addEventListener('keydown', handler);
11090
11468
  return () => document.removeEventListener('keydown', handler);
11091
11469
  }, [isOpen, closeOnEscape, onClose]);
11092
- useFocusTrap(panelRef, isOpen && trapFocus);
11470
+ useFocusTrap(panelRef, { active: isOpen && trapFocus });
11093
11471
  const panelClass = usePrefixedClassNames('picker-popover', {
11094
11472
  'is-active': isOpen,
11095
11473
  [`is-${resolved.position}`]: true,
@@ -14768,6 +15146,7 @@ exports.PanelIcon = PanelIcon;
14768
15146
  exports.PanelInputBlock = PanelInputBlock;
14769
15147
  exports.PanelTabs = PanelTabs;
14770
15148
  exports.Paragraph = Paragraph;
15149
+ exports.Portal = Portal;
14771
15150
  exports.Pre = Pre;
14772
15151
  exports.Progress = Progress;
14773
15152
  exports.Radio = Radio;
@@ -14833,6 +15212,7 @@ exports.useColorClasses = useColorClasses;
14833
15212
  exports.useColorStyles = useColorStyles;
14834
15213
  exports.useConfig = useConfig;
14835
15214
  exports.useFlexboxClasses = useFlexboxClasses;
15215
+ exports.useFocusTrap = useFocusTrap;
14836
15216
  exports.useIconLibrary = useIconLibrary;
14837
15217
  exports.useInsideControl = useInsideControl;
14838
15218
  exports.useInsideField = useInsideField;