1 line
2.1 KiB
Plaintext
1 line
2.1 KiB
Plaintext
{"version":3,"sources":["../src/use-focus-on-hide.ts"],"sourcesContent":["import {\n contains,\n focus,\n FocusableElement,\n getActiveElement,\n isTabbable,\n} from \"@chakra-ui/utils\"\nimport { RefObject } from \"react\"\nimport { useUpdateEffect } from \"./use-update-effect\"\n\nexport interface UseFocusOnHideOptions {\n focusRef: RefObject<FocusableElement>\n shouldFocus?: boolean\n visible?: boolean\n}\n\nfunction preventReturnFocus(containerRef: React.RefObject<HTMLElement>) {\n const el = containerRef.current\n if (!el) return false\n\n const activeElement = getActiveElement(el)\n\n if (!activeElement) return false\n if (contains(el, activeElement)) return false\n if (isTabbable(activeElement)) return true\n\n return false\n}\n\n/**\n * Popover hook to manage the focus when the popover closes or hides.\n *\n * We either want to return focus back to the popover trigger or\n * let focus proceed normally if user moved to another interactive\n * element in the viewport.\n */\nexport function useFocusOnHide(\n containerRef: RefObject<HTMLElement>,\n options: UseFocusOnHideOptions,\n) {\n const { shouldFocus: shouldFocusProp, visible, focusRef } = options\n\n const shouldFocus = shouldFocusProp && !visible\n\n useUpdateEffect(() => {\n if (!shouldFocus) return\n\n if (preventReturnFocus(containerRef)) {\n return\n }\n\n const el = focusRef?.current || containerRef.current\n\n if (el) {\n focus(el, { nextTick: true })\n }\n }, [shouldFocus, containerRef, focusRef])\n}\n"],"mappings":";;;;;;AAAA;AAAA,EACE;AAAA,EACA;AAAA,EAEA;AAAA,EACA;AAAA,OACK;AAUP,SAAS,mBAAmB,cAA4C;AACtE,QAAM,KAAK,aAAa;AACxB,MAAI,CAAC;AAAI,WAAO;AAEhB,QAAM,gBAAgB,iBAAiB,EAAE;AAEzC,MAAI,CAAC;AAAe,WAAO;AAC3B,MAAI,SAAS,IAAI,aAAa;AAAG,WAAO;AACxC,MAAI,WAAW,aAAa;AAAG,WAAO;AAEtC,SAAO;AACT;AASO,SAAS,eACd,cACA,SACA;AACA,QAAM,EAAE,aAAa,iBAAiB,SAAS,SAAS,IAAI;AAE5D,QAAM,cAAc,mBAAmB,CAAC;AAExC,kBAAgB,MAAM;AACpB,QAAI,CAAC;AAAa;AAElB,QAAI,mBAAmB,YAAY,GAAG;AACpC;AAAA,IACF;AAEA,UAAM,MAAK,qCAAU,YAAW,aAAa;AAE7C,QAAI,IAAI;AACN,YAAM,IAAI,EAAE,UAAU,KAAK,CAAC;AAAA,IAC9B;AAAA,EACF,GAAG,CAAC,aAAa,cAAc,QAAQ,CAAC;AAC1C;","names":[]} |