@flytedan/flytebot-design-system 0.17.0 → 0.17.1

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.js CHANGED
@@ -11666,6 +11666,42 @@ var FOCUSABLE_SELECTOR = 'a[href], button:not([disabled]), textarea:not([disable
11666
11666
  function getFocusable(container) {
11667
11667
  return Array.from(container.querySelectorAll(FOCUSABLE_SELECTOR));
11668
11668
  }
11669
+ function useFocusTrap(navRef, active) {
11670
+ React55.useEffect(() => {
11671
+ if (!active) return;
11672
+ const container = navRef.current;
11673
+ if (!container) return;
11674
+ const previouslyFocused = document.activeElement;
11675
+ const focusable = getFocusable(container);
11676
+ (focusable[0] ?? container).focus();
11677
+ const handler = (e) => {
11678
+ if (e.key !== "Tab") return;
11679
+ const items = getFocusable(container);
11680
+ if (items.length === 0) {
11681
+ e.preventDefault();
11682
+ return;
11683
+ }
11684
+ const first = items[0];
11685
+ const last = items[items.length - 1];
11686
+ const activeElement = document.activeElement;
11687
+ const outside = !(activeElement instanceof Node) || !container.contains(activeElement);
11688
+ if (e.shiftKey) {
11689
+ if (outside || activeElement === first) {
11690
+ e.preventDefault();
11691
+ last.focus();
11692
+ }
11693
+ } else if (outside || activeElement === last) {
11694
+ e.preventDefault();
11695
+ first.focus();
11696
+ }
11697
+ };
11698
+ document.addEventListener("keydown", handler);
11699
+ return () => {
11700
+ document.removeEventListener("keydown", handler);
11701
+ previouslyFocused?.focus?.();
11702
+ };
11703
+ }, [active, navRef]);
11704
+ }
11669
11705
  function useResponsiveRail(options = {}) {
11670
11706
  const { breakpoint = 1024, initialOpen = true, onPersist } = options;
11671
11707
  const navRef = React55.useRef(null);
@@ -11706,40 +11742,7 @@ function useResponsiveRail(options = {}) {
11706
11742
  document.body.style.overflow = previousOverflow;
11707
11743
  };
11708
11744
  }, [overlay, open]);
11709
- React55.useEffect(() => {
11710
- if (!overlay || !open) return;
11711
- const container = navRef.current;
11712
- if (!container) return;
11713
- const previouslyFocused = document.activeElement;
11714
- const focusable = getFocusable(container);
11715
- (focusable[0] ?? container).focus();
11716
- const handler = (e) => {
11717
- if (e.key !== "Tab") return;
11718
- const items = getFocusable(container);
11719
- if (items.length === 0) {
11720
- e.preventDefault();
11721
- return;
11722
- }
11723
- const first = items[0];
11724
- const last = items[items.length - 1];
11725
- const active = document.activeElement;
11726
- const outside = !(active instanceof Node) || !container.contains(active);
11727
- if (e.shiftKey) {
11728
- if (outside || active === first) {
11729
- e.preventDefault();
11730
- last.focus();
11731
- }
11732
- } else if (outside || active === last) {
11733
- e.preventDefault();
11734
- first.focus();
11735
- }
11736
- };
11737
- document.addEventListener("keydown", handler);
11738
- return () => {
11739
- document.removeEventListener("keydown", handler);
11740
- previouslyFocused?.focus?.();
11741
- };
11742
- }, [overlay, open]);
11745
+ useFocusTrap(navRef, overlay && open);
11743
11746
  return { open, overlay, navRef, toggle, close, setOpen };
11744
11747
  }
11745
11748
  export {