@opentripplanner/building-blocks 1.1.0 → 1.2.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.
Files changed (40) hide show
  1. package/esm/dropdown/index.js +1 -1
  2. package/esm/dropdown/index.js.map +1 -1
  3. package/esm/focus-trap-wrapper/dom-query.js.map +1 -0
  4. package/esm/focus-trap-wrapper/index.js +48 -0
  5. package/esm/focus-trap-wrapper/index.js.map +1 -0
  6. package/lib/dropdown/index.d.ts.map +1 -1
  7. package/lib/dropdown/index.js +1 -1
  8. package/lib/dropdown/index.js.map +1 -1
  9. package/lib/focus-trap-wrapper/dom-query.d.ts.map +1 -0
  10. package/lib/focus-trap-wrapper/dom-query.js.map +1 -0
  11. package/lib/focus-trap-wrapper/index.d.ts +9 -0
  12. package/lib/focus-trap-wrapper/index.d.ts.map +1 -0
  13. package/lib/focus-trap-wrapper/index.js +58 -0
  14. package/lib/focus-trap-wrapper/index.js.map +1 -0
  15. package/lib/stories/ColorPalette.d.ts +1 -0
  16. package/lib/stories/ColorPalette.d.ts.map +1 -1
  17. package/package.json +4 -4
  18. package/src/dropdown/index.tsx +4 -1
  19. package/src/focus-trap-wrapper/index.tsx +46 -0
  20. package/src/stories/__snapshots__/dropdown.story.tsx.snap +91 -0
  21. package/src/stories/__snapshots__/focusTrapStory.story.tsx.snap +64 -0
  22. package/src/stories/__snapshots__/index.story.tsx.snap +250 -0
  23. package/src/stories/focusTrapStory.story.tsx +46 -0
  24. package/tsconfig.json +1 -2
  25. package/tsconfig.tsbuildinfo +1 -1
  26. package/esm/utils/dom-query.js.map +0 -1
  27. package/lib/stories/dropdown.story.d.ts +0 -9
  28. package/lib/stories/dropdown.story.d.ts.map +0 -1
  29. package/lib/stories/dropdown.story.js +0 -59
  30. package/lib/stories/dropdown.story.js.map +0 -1
  31. package/lib/stories/index.story.d.ts +0 -9
  32. package/lib/stories/index.story.d.ts.map +0 -1
  33. package/lib/stories/index.story.js +0 -29
  34. package/lib/stories/index.story.js.map +0 -1
  35. package/lib/utils/dom-query.d.ts.map +0 -1
  36. package/lib/utils/dom-query.js.map +0 -1
  37. /package/esm/{utils → focus-trap-wrapper}/dom-query.js +0 -0
  38. /package/lib/{utils → focus-trap-wrapper}/dom-query.d.ts +0 -0
  39. /package/lib/{utils → focus-trap-wrapper}/dom-query.js +0 -0
  40. /package/src/{utils → focus-trap-wrapper}/dom-query.ts +0 -0
@@ -1,6 +1,6 @@
1
1
  import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
2
2
  import React, { useCallback, useEffect, useRef, useState } from "react";
3
- import { getNextSibling, getPreviousSibling } from "../utils/dom-query";
3
+ import { getNextSibling, getPreviousSibling } from "../focus-trap-wrapper/dom-query";
4
4
  import { DropdownButton, DropdownMenu, DropdownWrapper } from "./styled";
5
5
 
6
6
  /**
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/dropdown/index.tsx"],"names":["React","useCallback","useEffect","useRef","useState","getNextSibling","getPreviousSibling","DropdownButton","DropdownMenu","DropdownWrapper","Dropdown","alignMenuLeft","children","className","id","label","listLabel","text","buttonStyle","open","setOpen","containerRef","toggleOpen","queryId","isList","Array","isArray","every","child","isValidElement","type","handleExternalAction","e","current","contains","target","document","addEventListener","removeEventListener","handleKeyDown","element","key","preventDefault","focus","click","undefined"],"mappings":";AAAA,OAAOA,KAAP,IAGEC,WAHF,EAIEC,SAJF,EAKEC,MALF,EAMEC,QANF,QAOO,OAPP;AASA,SAASC,cAAT,EAAyBC,kBAAzB,QAAmD,oBAAnD;AACA,SAASC,cAAT,EAAyBC,YAAzB,EAAuCC,eAAvC,QAA8D,UAA9D;;AAWA;AACA;AACA;AACA;AACA;AACA,IAAMC,QAAQ,GAAG,SAAXA,QAAW,OASS;AAAA,MARxBC,aAQwB,QARxBA,aAQwB;AAAA,MAPxBC,QAOwB,QAPxBA,QAOwB;AAAA,MANxBC,SAMwB,QANxBA,SAMwB;AAAA,MALxBC,EAKwB,QALxBA,EAKwB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,IAAA,SAAS,EAAEyB,aAHb;AAIE,IAAA,GAAG,EAAElB;AAJP,kBAME,oBAAC,cAAD,CACE;AACA;AAFF;AAGE,qBAAeF,IAAI,GAAGL,EAAH,GAAQ+B,SAH7B;AAIE,qBAAe1B,IAJjB;AAKE,qBAAc,SALhB;AAME,kBAAYJ,KANd;AAOE,IAAA,EAAE,YAAKD,EAAL,WAPJ;AAQE,IAAA,OAAO,EAAEQ,UARX;AASE,IAAA,KAAK,EAAEJ,WATT;AAUE,IAAA,KAAK,EAAEH;AAVT,kBAYE,kCAAOE,IAAP,CAZF,eAaE;AAAM,IAAA,SAAS,EAAC,OAAhB;AAAwB,IAAA,IAAI,EAAC;AAA7B,IAbF,CANF,EAqBGE,IAAI,iBACH,oBAAC,YAAD;AACE,IAAA,EAAE,EAAEK,MAAM,IAAI,IADhB;AAEE,kBAAYR,SAFd;AAGE,uBAAiBA,SAAS,GAAG6B,SAAH,aAAkB/B,EAAlB,WAH5B;AAIE,IAAA,EAAE,EAAEA,EAJN;AAKE,IAAA,OAAO,EAAEQ,UALX;AAME,IAAA,SAAS,EAAEX,aANb;AAOE,IAAA,IAAI,EAAEa,MAAM,IAAI,MAPlB;AAQE,IAAA,QAAQ,EAAE,CAAC;AARb,KAUGZ,QAVH,CAtBJ,CADF;AAsCD,CA9GD;;AAgHA,eAAeF,QAAf","sourcesContent":["import React, {\n HTMLAttributes,\n KeyboardEvent,\n useCallback,\n useEffect,\n useRef,\n useState\n} from \"react\";\n\nimport { getNextSibling, getPreviousSibling } from \"../utils/dom-query\";\nimport { DropdownButton, DropdownMenu, DropdownWrapper } from \"./styled\";\n\nexport interface Props extends HTMLAttributes<HTMLElement> {\n alignMenuLeft?: boolean;\n buttonStyle?: React.CSSProperties;\n label?: string;\n listLabel?: string;\n text?: JSX.Element | string;\n nav?: boolean;\n}\n\n/**\n * Renders a dropdown menu. By default, only a passed \"text\" is rendered. If clicked,\n * a floating div is rendered below the \"text\" with list contents inside. Clicking anywhere\n * outside the floating div will close the dropdown.\n */\nconst Dropdown = ({\n alignMenuLeft,\n children,\n className,\n id,\n label,\n listLabel,\n text,\n buttonStyle\n}: Props): JSX.Element => {\n const [open, setOpen] = useState(false);\n\n const containerRef = useRef<HTMLSpanElement>(null);\n\n const toggleOpen = useCallback(() => setOpen(!open), [open, setOpen]);\n\n // Argument for document.querySelectorAll to target focusable elements.\n const queryId = `#${id} button, #${id}-label`;\n\n const isList = Array.isArray(children)\n ? children.every(\n child => React.isValidElement(child) && child.type === \"li\"\n )\n : React.isValidElement(children) && children.type === \"li\";\n\n // Adding document event listeners allows us to close the dropdown\n // when the user interacts with any part of the page that isn't the dropdown\n useEffect(() => {\n const handleExternalAction = (e: Event): void => {\n if (!containerRef?.current?.contains(e.target as HTMLElement)) {\n setOpen(false);\n }\n };\n document.addEventListener(\"mousedown\", handleExternalAction);\n document.addEventListener(\"focusin\", handleExternalAction);\n document.addEventListener(\"keydown\", handleExternalAction);\n return () => {\n document.removeEventListener(\"mousedown\", handleExternalAction);\n document.removeEventListener(\"focusin\", handleExternalAction);\n document.removeEventListener(\"keydown\", handleExternalAction);\n };\n }, [containerRef]);\n\n const handleKeyDown = useCallback(\n (e: KeyboardEvent): void => {\n const element = e.target as HTMLElement;\n switch (e.key) {\n case \"ArrowUp\":\n e.preventDefault();\n getPreviousSibling(queryId, element)?.focus();\n break;\n case \"ArrowDown\":\n e.preventDefault();\n getNextSibling(queryId, element)?.focus();\n break;\n case \"Escape\":\n setOpen(false);\n break;\n case \" \":\n case \"Enter\":\n e.preventDefault();\n element.click();\n if (element.id === `${id}-label` || element.id === `${id}-wrapper`) {\n toggleOpen();\n }\n break;\n default:\n }\n },\n [id, toggleOpen]\n );\n\n return (\n <DropdownWrapper\n className={className}\n id={`${id}-wrapper`}\n onKeyDown={handleKeyDown}\n ref={containerRef}\n >\n <DropdownButton\n // Only set aria-controls when the dropdown is open\n // (otherwise, assistive technologies may not announce the dropdown correctly).\n aria-controls={open ? id : undefined}\n aria-expanded={open}\n aria-haspopup=\"listbox\"\n aria-label={label}\n id={`${id}-label`}\n onClick={toggleOpen}\n style={buttonStyle}\n title={label}\n >\n <span>{text}</span>\n <span className=\"caret\" role=\"presentation\" />\n </DropdownButton>\n {open && (\n <DropdownMenu\n as={isList && \"ul\"}\n aria-label={listLabel}\n aria-labelledby={listLabel ? undefined : `${id}-label`}\n id={id}\n onClick={toggleOpen}\n alignLeft={alignMenuLeft}\n role={isList && \"list\"}\n tabIndex={-1}\n >\n {children}\n </DropdownMenu>\n )}\n </DropdownWrapper>\n );\n};\n\nexport default Dropdown;\n"],"file":"index.js"}
1
+ {"version":3,"sources":["../../src/dropdown/index.tsx"],"names":["React","useCallback","useEffect","useRef","useState","getNextSibling","getPreviousSibling","DropdownButton","DropdownMenu","DropdownWrapper","Dropdown","alignMenuLeft","children","className","id","label","listLabel","text","buttonStyle","open","setOpen","containerRef","toggleOpen","queryId","isList","Array","isArray","every","child","isValidElement","type","handleExternalAction","e","current","contains","target","document","addEventListener","removeEventListener","handleKeyDown","element","key","preventDefault","focus","click","undefined"],"mappings":";AAAA,OAAOA,KAAP,IAGEC,WAHF,EAIEC,SAJF,EAKEC,MALF,EAMEC,QANF,QAOO,OAPP;AASA,SACEC,cADF,EAEEC,kBAFF,QAGO,iCAHP;AAIA,SAASC,cAAT,EAAyBC,YAAzB,EAAuCC,eAAvC,QAA8D,UAA9D;;AAWA;AACA;AACA;AACA;AACA;AACA,IAAMC,QAAQ,GAAG,SAAXA,QAAW,OASS;AAAA,MARxBC,aAQwB,QARxBA,aAQwB;AAAA,MAPxBC,QAOwB,QAPxBA,QAOwB;AAAA,MANxBC,SAMwB,QANxBA,SAMwB;AAAA,MALxBC,EAKwB,QALxBA,EAKwB;AAAA,MAJxBC,KAIwB,QAJxBA,KAIwB;AAAA,MAHxBC,SAGwB,QAHxBA,SAGwB;AAAA,MAFxBC,IAEwB,QAFxBA,IAEwB;AAAA,MADxBC,WACwB,QADxBA,WACwB;;AACxB,kBAAwBd,Q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jB;AAKE,qBAAc,SALhB;AAME,kBAAYJ,KANd;AAOE,IAAA,EAAE,YAAKD,EAAL,WAPJ;AAQE,IAAA,OAAO,EAAEQ,UARX;AASE,IAAA,KAAK,EAAEJ,WATT;AAUE,IAAA,KAAK,EAAEH;AAVT,kBAYE,kCAAOE,IAAP,CAZF,eAaE;AAAM,IAAA,SAAS,EAAC,OAAhB;AAAwB,IAAA,IAAI,EAAC;AAA7B,IAbF,CANF,EAqBGE,IAAI,iBACH,oBAAC,YAAD;AACE,IAAA,EAAE,EAAEK,MAAM,IAAI,IADhB;AAEE,kBAAYR,SAFd;AAGE,uBAAiBA,SAAS,GAAG6B,SAAH,aAAkB/B,EAAlB,WAH5B;AAIE,IAAA,EAAE,EAAEA,EAJN;AAKE,IAAA,OAAO,EAAEQ,UALX;AAME,IAAA,SAAS,EAAEX,aANb;AAOE,IAAA,IAAI,EAAEa,MAAM,IAAI,MAPlB;AAQE,IAAA,QAAQ,EAAE,CAAC;AARb,KAUGZ,QAVH,CAtBJ,CADF;AAsCD,CA9GD;;AAgHA,eAAeF,QAAf","sourcesContent":["import React, {\n HTMLAttributes,\n KeyboardEvent,\n useCallback,\n useEffect,\n useRef,\n useState\n} from \"react\";\n\nimport {\n getNextSibling,\n getPreviousSibling\n} from \"../focus-trap-wrapper/dom-query\";\nimport { DropdownButton, DropdownMenu, DropdownWrapper } from \"./styled\";\n\nexport interface Props extends HTMLAttributes<HTMLElement> {\n alignMenuLeft?: boolean;\n buttonStyle?: React.CSSProperties;\n label?: string;\n listLabel?: string;\n text?: JSX.Element | string;\n nav?: boolean;\n}\n\n/**\n * Renders a dropdown menu. By default, only a passed \"text\" is rendered. If clicked,\n * a floating div is rendered below the \"text\" with list contents inside. Clicking anywhere\n * outside the floating div will close the dropdown.\n */\nconst Dropdown = ({\n alignMenuLeft,\n children,\n className,\n id,\n label,\n listLabel,\n text,\n buttonStyle\n}: Props): JSX.Element => {\n const [open, setOpen] = useState(false);\n\n const containerRef = useRef<HTMLSpanElement>(null);\n\n const toggleOpen = useCallback(() => setOpen(!open), [open, setOpen]);\n\n // Argument for document.querySelectorAll to target focusable elements.\n const queryId = `#${id} button, #${id}-label`;\n\n const isList = Array.isArray(children)\n ? children.every(\n child => React.isValidElement(child) && child.type === \"li\"\n )\n : React.isValidElement(children) && children.type === \"li\";\n\n // Adding document event listeners allows us to close the dropdown\n // when the user interacts with any part of the page that isn't the dropdown\n useEffect(() => {\n const handleExternalAction = (e: Event): void => {\n if (!containerRef?.current?.contains(e.target as HTMLElement)) {\n setOpen(false);\n }\n };\n document.addEventListener(\"mousedown\", handleExternalAction);\n document.addEventListener(\"focusin\", handleExternalAction);\n document.addEventListener(\"keydown\", handleExternalAction);\n return () => {\n document.removeEventListener(\"mousedown\", handleExternalAction);\n document.removeEventListener(\"focusin\", handleExternalAction);\n document.removeEventListener(\"keydown\", handleExternalAction);\n };\n }, [containerRef]);\n\n const handleKeyDown = useCallback(\n (e: KeyboardEvent): void => {\n const element = e.target as HTMLElement;\n switch (e.key) {\n case \"ArrowUp\":\n e.preventDefault();\n getPreviousSibling(queryId, element)?.focus();\n break;\n case \"ArrowDown\":\n e.preventDefault();\n getNextSibling(queryId, element)?.focus();\n break;\n case \"Escape\":\n setOpen(false);\n break;\n case \" \":\n case \"Enter\":\n e.preventDefault();\n element.click();\n if (element.id === `${id}-label` || element.id === `${id}-wrapper`) {\n toggleOpen();\n }\n break;\n default:\n }\n },\n [id, toggleOpen]\n );\n\n return (\n <DropdownWrapper\n className={className}\n id={`${id}-wrapper`}\n onKeyDown={handleKeyDown}\n ref={containerRef}\n >\n <DropdownButton\n // Only set aria-controls when the dropdown is open\n // (otherwise, assistive technologies may not announce the dropdown correctly).\n aria-controls={open ? id : undefined}\n aria-expanded={open}\n aria-haspopup=\"listbox\"\n aria-label={label}\n id={`${id}-label`}\n onClick={toggleOpen}\n style={buttonStyle}\n title={label}\n >\n <span>{text}</span>\n <span className=\"caret\" role=\"presentation\" />\n </DropdownButton>\n {open && (\n <DropdownMenu\n as={isList && \"ul\"}\n aria-label={listLabel}\n aria-labelledby={listLabel ? undefined : `${id}-label`}\n id={id}\n onClick={toggleOpen}\n alignLeft={alignMenuLeft}\n role={isList && \"list\"}\n tabIndex={-1}\n >\n {children}\n </DropdownMenu>\n )}\n </DropdownWrapper>\n );\n};\n\nexport default Dropdown;\n"],"file":"index.js"}
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/focus-trap-wrapper/dom-query.ts"],"names":["getEntries","query","entries","Array","from","document","querySelectorAll","firstElement","lastElement","length","getNextSibling","element","elementIndex","indexOf","getPreviousSibling"],"mappings":"AAAA,SAASA,UAAT,CAAoBC,KAApB,EAAmC;AACjC,MAAMC,OAAO,GAAGC,KAAK,CAACC,IAAN,CAAWC,QAAQ,CAACC,gBAAT,CAA0BL,KAA1B,CAAX,CAAhB;AACA,MAAMM,YAAY,GAAGL,OAAO,CAAC,CAAD,CAA5B;AACA,MAAMM,WAAW,GAAGN,OAAO,CAACA,OAAO,CAACO,MAAR,GAAiB,CAAlB,CAA3B;AAEA,SAAO;AAAEP,IAAAA,OAAO,EAAPA,OAAF;AAAWK,IAAAA,YAAY,EAAZA,YAAX;AAAyBC,IAAAA,WAAW,EAAXA;AAAzB,GAAP;AACD;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACA,OAAO,SAASE,cAAT,CACLT,KADK,EAELU,OAFK,EAGQ;AACb,oBAA+CX,UAAU,CAACC,KAAD,CAAzD;AAAA,MAAQC,OAAR,eAAQA,OAAR;AAAA,MAAiBK,YAAjB,eAAiBA,YAAjB;AAAA,MAA+BC,WAA/B,eAA+BA,WAA/B;;AAEA,MAAIG,OAAO,KAAKH,WAAhB,EAA6B;AAC3B,WAAOD,YAAP;AACD;;AACD,MAAMK,YAAY,GAAGV,OAAO,CAACW,OAAR,CAAgBF,OAAhB,CAArB;AACA,SAAOT,OAAO,CAACU,YAAY,GAAG,CAAhB,CAAd;AACD;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASE,kBAAT,CACLb,KADK,EAELU,OAFK,EAGQ;AACb,qBAA+CX,UAAU,CAACC,KAAD,CAAzD;AAAA,MAAQC,OAAR,gBAAQA,OAAR;AAAA,MAAiBK,YAAjB,gBAAiBA,YAAjB;AAAA,MAA+BC,WAA/B,gBAA+BA,WAA/B;;AAEA,MAAIG,OAAO,KAAKJ,YAAhB,EAA8B;AAC5B,WAAOC,WAAP;AACD;;AACD,MAAMI,YAAY,GAAGV,OAAO,CAACW,OAAR,CAAgBF,OAAhB,CAArB;AACA,SAAOT,OAAO,CAACU,YAAY,GAAG,CAAhB,CAAd;AACD","sourcesContent":["function getEntries(query: string) {\n const entries = Array.from(document.querySelectorAll(query));\n const firstElement = entries[0];\n const lastElement = entries[entries.length - 1];\n\n return { entries, firstElement, lastElement };\n}\n\n/**\n * Helper method to find the next focusable sibling element relative to the\n * specified element.\n *\n * @param {string} query - Argument that gets passed to document.querySelectorAll\n * @param {HTMLElement} element - Specified element (e.target)\n * @returns {HTMLElement} - element to be focused\n */\nexport function getNextSibling(\n query: string,\n element: EventTarget\n): HTMLElement {\n const { entries, firstElement, lastElement } = getEntries(query);\n\n if (element === lastElement) {\n return firstElement as HTMLElement;\n }\n const elementIndex = entries.indexOf(element as HTMLElement);\n return entries[elementIndex + 1] as HTMLElement;\n}\n\n/**\n * Helper method to find the previous focusable sibling element relative to the\n * specified element.\n *\n * @param {string} query - Argument that gets passed to document.querySelectorAll\n * @param {HTMLElement} element - Specified element (e.target)\n * @returns {HTMLElement} - element to be focused\n */\nexport function getPreviousSibling(\n query: string,\n element: EventTarget\n): HTMLElement {\n const { entries, firstElement, lastElement } = getEntries(query);\n\n if (element === firstElement) {\n return lastElement as HTMLElement;\n }\n const elementIndex = entries.indexOf(element as HTMLButtonElement);\n return entries[elementIndex - 1] as HTMLElement;\n}\n"],"file":"dom-query.js"}
@@ -0,0 +1,48 @@
1
+ import React, { useCallback } from "react";
2
+ import { getNextSibling, getPreviousSibling } from "./dom-query";
3
+
4
+ var FocusTrapWrapper = function FocusTrapWrapper(_ref) {
5
+ var children = _ref.children,
6
+ _ref$closePopup = _ref.closePopup,
7
+ closePopup = _ref$closePopup === void 0 ? null : _ref$closePopup,
8
+ _ref$focusElements = _ref.focusElements,
9
+ focusElements = _ref$focusElements === void 0 ? ["button", "a", "input", "select"] : _ref$focusElements,
10
+ id = _ref.id;
11
+ var queryId = focusElements.map(function (el) {
12
+ return "#".concat(id, "-focus-trap ").concat(el, ":not([disabled])");
13
+ }).join(", ");
14
+ var handleKeyDown = useCallback(function (e) {
15
+ var element = e.target;
16
+
17
+ switch (e.key) {
18
+ case "Escape":
19
+ closePopup();
20
+ break;
21
+
22
+ case "Tab":
23
+ e.preventDefault();
24
+
25
+ if (e.shiftKey) {
26
+ var _getPreviousSibling;
27
+
28
+ (_getPreviousSibling = getPreviousSibling(queryId, element)) === null || _getPreviousSibling === void 0 ? void 0 : _getPreviousSibling.focus();
29
+ } else {
30
+ var _getNextSibling;
31
+
32
+ (_getNextSibling = getNextSibling(queryId, element)) === null || _getNextSibling === void 0 ? void 0 : _getNextSibling.focus();
33
+ }
34
+
35
+ break;
36
+
37
+ default:
38
+ }
39
+ }, [closePopup]);
40
+ return /*#__PURE__*/React.createElement("div", {
41
+ id: "".concat(id, "-focus-trap"),
42
+ onKeyDown: handleKeyDown,
43
+ role: "presentation"
44
+ }, children);
45
+ };
46
+
47
+ export default FocusTrapWrapper;
48
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/focus-trap-wrapper/index.tsx"],"names":["React","useCallback","getNextSibling","getPreviousSibling","FocusTrapWrapper","children","closePopup","focusElements","id","queryId","map","el","join","handleKeyDown","e","element","target","key","preventDefault","shiftKey","focus"],"mappings":"AAAA,OAAOA,KAAP,IAA2BC,WAA3B,QAA8C,OAA9C;AACA,SAASC,cAAT,EAAyBC,kBAAzB,QAAmD,aAAnD;;AAEA,IAAMC,gBAAgB,GAAG,SAAnBA,gBAAmB,OAUN;AAAA,MATjBC,QASiB,QATjBA,QASiB;AAAA,6BARjBC,UAQiB;AAAA,MARjBA,UAQiB,gCARJ,IAQI;AAAA,gCAPjBC,aAOiB;AAAA,MAPjBA,aAOiB,mCAPD,CAAC,QAAD,EAAW,GAAX,EAAgB,OAAhB,EAAyB,QAAzB,CAOC;AAAA,MANjBC,EAMiB,QANjBA,EAMiB;AACjB,MAAMC,OAAO,GAAGF,aAAa,CAC1BG,GADa,CACT,UAAAC,EAAE;AAAA,sBAAQH,EAAR,yBAAyBG,EAAzB;AAAA,GADO,EAEbC,IAFa,CAER,IAFQ,CAAhB;AAGA,MAAMC,aAAa,GAAGZ,WAAW,CAC/B,UAAAa,CAAC,EAAI;AACH,QAAMC,OAAO,GAAGD,CAAC,CAACE,MAAlB;;AACA,YAAQF,CAAC,CAACG,GAAV;AACE,WAAK,QAAL;AACEX,QAAAA,UAAU;AACV;;AACF,WAAK,KAAL;AACEQ,QAAAA,CAAC,CAACI,cAAF;;AACA,YAAIJ,CAAC,CAACK,QAAN,EAAgB;AAAA;;AACd,iCAAAhB,kBAAkB,CAACM,OAAD,EAAUM,OAAV,CAAlB,4EAAsCK,KAAtC;AACD,SAFD,MAEO;AAAA;;AACL,6BAAAlB,cAAc,CAACO,OAAD,EAAUM,OAAV,CAAd,oEAAkCK,KAAlC;AACD;;AACD;;AACF;AAZF;AAcD,GAjB8B,EAkB/B,CAACd,UAAD,CAlB+B,CAAjC;AAqBA,sBACE;AAAK,IAAA,EAAE,YAAKE,EAAL,gBAAP;AAA6B,IAAA,SAAS,EAAEK,aAAxC;AAAuD,IAAA,IAAI,EAAC;AAA5D,KACGR,QADH,CADF;AAKD,CAxCD;;AA0CA,eAAeD,gBAAf","sourcesContent":["import React, { ReactNode, useCallback } from \"react\";\nimport { getNextSibling, getPreviousSibling } from \"./dom-query\";\n\nconst FocusTrapWrapper = ({\n children,\n closePopup = null,\n focusElements = [\"button\", \"a\", \"input\", \"select\"],\n id\n}: {\n children: ReactNode | ReactNode[];\n closePopup?: (arg?: boolean) => void;\n id: string;\n focusElements?: string[];\n}): JSX.Element => {\n const queryId = focusElements\n .map(el => `#${id}-focus-trap ${el}:not([disabled])`)\n .join(\", \");\n const handleKeyDown = useCallback(\n e => {\n const element = e.target as HTMLElement;\n switch (e.key) {\n case \"Escape\":\n closePopup();\n break;\n case \"Tab\":\n e.preventDefault();\n if (e.shiftKey) {\n getPreviousSibling(queryId, element)?.focus();\n } else {\n getNextSibling(queryId, element)?.focus();\n }\n break;\n default:\n }\n },\n [closePopup]\n );\n\n return (\n <div id={`${id}-focus-trap`} onKeyDown={handleKeyDown} role=\"presentation\">\n {children}\n </div>\n );\n};\n\nexport default FocusTrapWrapper;\n"],"file":"index.js"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/dropdown/index.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EACZ,cAAc,EAMf,MAAM,OAAO,CAAC;AAKf,MAAM,WAAW,KAAM,SAAQ,cAAc,CAAC,WAAW,CAAC;IACxD,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,WAAW,CAAC,EAAE,KAAK,CAAC,aAAa,CAAC;IAClC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,IAAI,CAAC,EAAE,GAAG,CAAC,OAAO,GAAG,MAAM,CAAC;IAC5B,GAAG,CAAC,EAAE,OAAO,CAAC;CACf;AAED;;;;GAIG;AACH,QAAA,MAAM,QAAQ,oFASX,KAAK,KAAG,WAqGV,CAAC;AAEF,eAAe,QAAQ,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/dropdown/index.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EACZ,cAAc,EAMf,MAAM,OAAO,CAAC;AAQf,MAAM,WAAW,KAAM,SAAQ,cAAc,CAAC,WAAW,CAAC;IACxD,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,WAAW,CAAC,EAAE,KAAK,CAAC,aAAa,CAAC;IAClC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,IAAI,CAAC,EAAE,GAAG,CAAC,OAAO,GAAG,MAAM,CAAC;IAC5B,GAAG,CAAC,EAAE,OAAO,CAAC;CACf;AAED;;;;GAIG;AACH,QAAA,MAAM,QAAQ,oFASX,KAAK,KAAG,WAqGV,CAAC;AAEF,eAAe,QAAQ,CAAC"}
@@ -7,7 +7,7 @@ exports.default = void 0;
7
7
 
8
8
  var _react = _interopRequireWildcard(require("react"));
9
9
 
10
- var _domQuery = require("../utils/dom-query");
10
+ var _domQuery = require("../focus-trap-wrapper/dom-query");
11
11
 
12
12
  var _styled = require("./styled");
13
13
 
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/dropdown/index.tsx"],"names":["Dropdown","alignMenuLeft","children","className","id","label","listLabel","text","buttonStyle","open","setOpen","containerRef","toggleOpen","queryId","isList","Array","isArray","every","child","isValidElement","type","handleExternalAction","e","current","contains","target","document","addEventListener","removeEventListener","handleKeyDown","element","key","preventDefault","focus","click","undefined"],"mappings":";;;;;;;AAAA;;AASA;;AACA;;;;;;AAWA;AACA;AACA;AACA;AACA;AACA,MAAMA,QAAQ,GAAG,CAAC;AAChBC,EAAAA,aADgB;AAEhBC,EAAAA,QAFgB;AAGhBC,EAAAA,SAHgB;AAIhBC,EAAAA,EAJgB;AAKhBC,EAAAA,KALgB;AAMhBC,EAAAA,SANgB;AAOhBC,EAAAA,IAPgB;AAQhBC,EAAAA;AARgB,CAAD,KASS;AACxB,QAAM,CAACC,IAAD,EAAOC,OAAP,IAAkB,qBAAS,KAAT,CAAxB;AAEA,QAAMC,YAAY,GAAG,mBAAwB,IAAxB,CAArB;AAEA,QAAMC,UAAU,GAAG,wBAAY,MAAMF,OAAO,CAAC,CAACD,IAAF,CAAzB,EAAkC,CA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IAAI,IADhB;AAEE,kBAAYR,SAFd;AAGE,uBAAiBA,SAAS,GAAG6B,SAAH,GAAgB,GAAE/B,EAAG,QAHjD;AAIE,IAAA,EAAE,EAAEA,EAJN;AAKE,IAAA,OAAO,EAAEQ,UALX;AAME,IAAA,SAAS,EAAEX,aANb;AAOE,IAAA,IAAI,EAAEa,MAAM,IAAI,MAPlB;AAQE,IAAA,QAAQ,EAAE,CAAC;AARb,KAUGZ,QAVH,CAtBJ,CADF;AAsCD,CA9GD;;eAgHeF,Q","sourcesContent":["import React, {\n HTMLAttributes,\n KeyboardEvent,\n useCallback,\n useEffect,\n useRef,\n useState\n} from \"react\";\n\nimport { getNextSibling, getPreviousSibling } from \"../utils/dom-query\";\nimport { DropdownButton, DropdownMenu, DropdownWrapper } from \"./styled\";\n\nexport interface Props extends HTMLAttributes<HTMLElement> {\n alignMenuLeft?: boolean;\n buttonStyle?: React.CSSProperties;\n label?: string;\n listLabel?: string;\n text?: JSX.Element | string;\n nav?: boolean;\n}\n\n/**\n * Renders a dropdown menu. By default, only a passed \"text\" is rendered. If clicked,\n * a floating div is rendered below the \"text\" with list contents inside. Clicking anywhere\n * outside the floating div will close the dropdown.\n */\nconst Dropdown = ({\n alignMenuLeft,\n children,\n className,\n id,\n label,\n listLabel,\n text,\n buttonStyle\n}: Props): JSX.Element => {\n const [open, setOpen] = useState(false);\n\n const containerRef = useRef<HTMLSpanElement>(null);\n\n const toggleOpen = useCallback(() => setOpen(!open), [open, setOpen]);\n\n // Argument for document.querySelectorAll to target focusable elements.\n const queryId = `#${id} button, #${id}-label`;\n\n const isList = Array.isArray(children)\n ? children.every(\n child => React.isValidElement(child) && child.type === \"li\"\n )\n : React.isValidElement(children) && children.type === \"li\";\n\n // Adding document event listeners allows us to close the dropdown\n // when the user interacts with any part of the page that isn't the dropdown\n useEffect(() => {\n const handleExternalAction = (e: Event): void => {\n if (!containerRef?.current?.contains(e.target as HTMLElement)) {\n setOpen(false);\n }\n };\n document.addEventListener(\"mousedown\", handleExternalAction);\n document.addEventListener(\"focusin\", handleExternalAction);\n document.addEventListener(\"keydown\", handleExternalAction);\n return () => {\n document.removeEventListener(\"mousedown\", handleExternalAction);\n document.removeEventListener(\"focusin\", handleExternalAction);\n document.removeEventListener(\"keydown\", handleExternalAction);\n };\n }, [containerRef]);\n\n const handleKeyDown = useCallback(\n (e: KeyboardEvent): void => {\n const element = e.target as HTMLElement;\n switch (e.key) {\n case \"ArrowUp\":\n e.preventDefault();\n getPreviousSibling(queryId, element)?.focus();\n break;\n case \"ArrowDown\":\n e.preventDefault();\n getNextSibling(queryId, element)?.focus();\n break;\n case \"Escape\":\n setOpen(false);\n break;\n case \" \":\n case \"Enter\":\n e.preventDefault();\n element.click();\n if (element.id === `${id}-label` || element.id === `${id}-wrapper`) {\n toggleOpen();\n }\n break;\n default:\n }\n },\n [id, toggleOpen]\n );\n\n return (\n <DropdownWrapper\n className={className}\n id={`${id}-wrapper`}\n onKeyDown={handleKeyDown}\n ref={containerRef}\n >\n <DropdownButton\n // Only set aria-controls when the dropdown is open\n // (otherwise, assistive technologies may not announce the dropdown correctly).\n aria-controls={open ? id : undefined}\n aria-expanded={open}\n aria-haspopup=\"listbox\"\n aria-label={label}\n id={`${id}-label`}\n onClick={toggleOpen}\n style={buttonStyle}\n title={label}\n >\n <span>{text}</span>\n <span className=\"caret\" role=\"presentation\" />\n </DropdownButton>\n {open && (\n <DropdownMenu\n as={isList && \"ul\"}\n aria-label={listLabel}\n aria-labelledby={listLabel ? undefined : `${id}-label`}\n id={id}\n onClick={toggleOpen}\n alignLeft={alignMenuLeft}\n role={isList && \"list\"}\n tabIndex={-1}\n >\n {children}\n </DropdownMenu>\n )}\n </DropdownWrapper>\n );\n};\n\nexport default Dropdown;\n"],"file":"index.js"}
1
+ {"version":3,"sources":["../../src/dropdown/index.tsx"],"names":["Dropdown","alignMenuLeft","children","className","id","label","listLabel","text","buttonStyle","open","setOpen","containerRef","toggleOpen","queryId","isList","Array","isArray","every","child","isValidElement","type","handleExternalAction","e","current","contains","target","document","addEventListener","removeEventListener","handleKeyDown","element","key","preventDefault","focus","click","undefined"],"mappings":";;;;;;;AAAA;;AASA;;AAIA;;;;;;AAWA;AACA;AACA;AACA;AACA;AACA,MAAMA,QAAQ,GAAG,CAAC;AAChBC,EAAAA,aADgB;AAEhBC,EAAAA,QAFgB;AAGhBC,EAAAA,SAHgB;AAIhBC,EAAAA,EAJgB;AAKhBC,EAAAA,KALgB;AAMhBC,EAAAA,SANgB;AAOhBC,EAAAA,IAPgB;AAQhBC,EAAAA;AARgB,CAAD,KASS;AACxB,QAAM,CAACC,IAAD,EAAOC,OAAP,IAAkB,qBAAS,KAAT,CAAxB;AAEA,QAAMC,YAAY,GAAG,mBAAwB,IAAxB,CAArB;AAEA,QAAMC,UAAU,GAAG,wBAAY,MAAMF,OAAO,CAAC,CAACD,IAAF,CAAzB,EAAkC,CAACA,IAAD,EAAOC,OAAP,CAAlC,CAAnB,CALwB,CAOxB;;AACA,QAAMG,OAAO,GAAI,IAAGT,EAAG,aAAYA,EAAG,QAAtC;AAEA,QAAMU,MAAM,GAAGC,KAAK,CAACC,OAAN,CAAcd,QAAd,IACXA,QAAQ,CAACe,KAAT,CACEC,KAAK,IAAI,4BAAMC,cAAN,CAAqBD,KAArB,KAA+BA,KAAK,CAACE,IAAN,KAAe,IADzD,CADW,GAIX,4BAAMD,cAAN,CAAqBjB,QAArB,KAAkCA,QAAQ,CAACkB,IAAT,KAAkB,IAJxD,CAVwB,CAgBxB;AACA;;AACA,wBAAU,MAAM;AACd,UAAMC,oBAAoB,GAAIC,CAAD,IAAoB;AAAA;;AAC/C,UAAI,EAACX,YAAD,aAACA,YAAD,wCAACA,YAAY,CAAEY,OAAf,kDAAC,sBAAuBC,QAAvB,CAAgCF,CAAC,CAACG,MAAlC,CAAD,CAAJ,EAA+D;AAC7Df,QAAAA,OAAO,CAAC,KAAD,CAAP;AACD;AACF,KAJD;;AAKAgB,IAAAA,QAAQ,CAACC,gBAAT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,EAAE,EAAG,GAAEC,EAAG,UAFZ;AAGE,IAAA,SAAS,EAAEyB,aAHb;AAIE,IAAA,GAAG,EAAElB;AAJP,kBAME,6BAAC,sBAAD,CACE;AACA;AAFF;AAGE,qBAAeF,IAAI,GAAGL,EAAH,GAAQ+B,SAH7B;AAIE,qBAAe1B,IAJjB;AAKE,qBAAc,SALhB;AAME,kBAAYJ,KANd;AAOE,IAAA,EAAE,EAAG,GAAED,EAAG,QAPZ;AAQE,IAAA,OAAO,EAAEQ,UARX;AASE,IAAA,KAAK,EAAEJ,WATT;AAUE,IAAA,KAAK,EAAEH;AAVT,kBAYE,2CAAOE,IAAP,CAZF,eAaE;AAAM,IAAA,SAAS,EAAC,OAAhB;AAAwB,IAAA,IAAI,EAAC;AAA7B,IAbF,CANF,EAqBGE,IAAI,iBACH,6BAAC,oBAAD;AACE,IAAA,EAAE,EAAEK,MAAM,IAAI,IADhB;AAEE,kBAAYR,SAFd;AAGE,uBAAiBA,SAAS,GAAG6B,SAAH,GAAgB,GAAE/B,EAAG,QAHjD;AAIE,IAAA,EAAE,EAAEA,EAJN;AAKE,IAAA,OAAO,EAAEQ,UALX;AAME,IAAA,SAAS,EAAEX,aANb;AAOE,IAAA,IAAI,EAAEa,MAAM,IAAI,MAPlB;AAQE,IAAA,QAAQ,EAAE,CAAC;AARb,KAUGZ,QAVH,CAtBJ,CADF;AAsCD,CA9GD;;eAgHeF,Q","sourcesContent":["import React, {\n HTMLAttributes,\n KeyboardEvent,\n useCallback,\n useEffect,\n useRef,\n useState\n} from \"react\";\n\nimport {\n getNextSibling,\n getPreviousSibling\n} from \"../focus-trap-wrapper/dom-query\";\nimport { DropdownButton, DropdownMenu, DropdownWrapper } from \"./styled\";\n\nexport interface Props extends HTMLAttributes<HTMLElement> {\n alignMenuLeft?: boolean;\n buttonStyle?: React.CSSProperties;\n label?: string;\n listLabel?: string;\n text?: JSX.Element | string;\n nav?: boolean;\n}\n\n/**\n * Renders a dropdown menu. By default, only a passed \"text\" is rendered. If clicked,\n * a floating div is rendered below the \"text\" with list contents inside. Clicking anywhere\n * outside the floating div will close the dropdown.\n */\nconst Dropdown = ({\n alignMenuLeft,\n children,\n className,\n id,\n label,\n listLabel,\n text,\n buttonStyle\n}: Props): JSX.Element => {\n const [open, setOpen] = useState(false);\n\n const containerRef = useRef<HTMLSpanElement>(null);\n\n const toggleOpen = useCallback(() => setOpen(!open), [open, setOpen]);\n\n // Argument for document.querySelectorAll to target focusable elements.\n const queryId = `#${id} button, #${id}-label`;\n\n const isList = Array.isArray(children)\n ? children.every(\n child => React.isValidElement(child) && child.type === \"li\"\n )\n : React.isValidElement(children) && children.type === \"li\";\n\n // Adding document event listeners allows us to close the dropdown\n // when the user interacts with any part of the page that isn't the dropdown\n useEffect(() => {\n const handleExternalAction = (e: Event): void => {\n if (!containerRef?.current?.contains(e.target as HTMLElement)) {\n setOpen(false);\n }\n };\n document.addEventListener(\"mousedown\", handleExternalAction);\n document.addEventListener(\"focusin\", handleExternalAction);\n document.addEventListener(\"keydown\", handleExternalAction);\n return () => {\n document.removeEventListener(\"mousedown\", handleExternalAction);\n document.removeEventListener(\"focusin\", handleExternalAction);\n document.removeEventListener(\"keydown\", handleExternalAction);\n };\n }, [containerRef]);\n\n const handleKeyDown = useCallback(\n (e: KeyboardEvent): void => {\n const element = e.target as HTMLElement;\n switch (e.key) {\n case \"ArrowUp\":\n e.preventDefault();\n getPreviousSibling(queryId, element)?.focus();\n break;\n case \"ArrowDown\":\n e.preventDefault();\n getNextSibling(queryId, element)?.focus();\n break;\n case \"Escape\":\n setOpen(false);\n break;\n case \" \":\n case \"Enter\":\n e.preventDefault();\n element.click();\n if (element.id === `${id}-label` || element.id === `${id}-wrapper`) {\n toggleOpen();\n }\n break;\n default:\n }\n },\n [id, toggleOpen]\n );\n\n return (\n <DropdownWrapper\n className={className}\n id={`${id}-wrapper`}\n onKeyDown={handleKeyDown}\n ref={containerRef}\n >\n <DropdownButton\n // Only set aria-controls when the dropdown is open\n // (otherwise, assistive technologies may not announce the dropdown correctly).\n aria-controls={open ? id : undefined}\n aria-expanded={open}\n aria-haspopup=\"listbox\"\n aria-label={label}\n id={`${id}-label`}\n onClick={toggleOpen}\n style={buttonStyle}\n title={label}\n >\n <span>{text}</span>\n <span className=\"caret\" role=\"presentation\" />\n </DropdownButton>\n {open && (\n <DropdownMenu\n as={isList && \"ul\"}\n aria-label={listLabel}\n aria-labelledby={listLabel ? undefined : `${id}-label`}\n id={id}\n onClick={toggleOpen}\n alignLeft={alignMenuLeft}\n role={isList && \"list\"}\n tabIndex={-1}\n >\n {children}\n </DropdownMenu>\n )}\n </DropdownWrapper>\n );\n};\n\nexport default Dropdown;\n"],"file":"index.js"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dom-query.d.ts","sourceRoot":"","sources":["../../src/focus-trap-wrapper/dom-query.ts"],"names":[],"mappings":"AAQA;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAC5B,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,WAAW,GACnB,WAAW,CAQb;AAED;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,WAAW,GACnB,WAAW,CAQb"}
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/focus-trap-wrapper/dom-query.ts"],"names":["getEntries","query","entries","Array","from","document","querySelectorAll","firstElement","lastElement","length","getNextSibling","element","elementIndex","indexOf","getPreviousSibling"],"mappings":";;;;;;;;AAAA,SAASA,UAAT,CAAoBC,KAApB,EAAmC;AACjC,QAAMC,OAAO,GAAGC,KAAK,CAACC,IAAN,CAAWC,QAAQ,CAACC,gBAAT,CAA0BL,KAA1B,CAAX,CAAhB;AACA,QAAMM,YAAY,GAAGL,OAAO,CAAC,CAAD,CAA5B;AACA,QAAMM,WAAW,GAAGN,OAAO,CAACA,OAAO,CAACO,MAAR,GAAiB,CAAlB,CAA3B;AAEA,SAAO;AAAEP,IAAAA,OAAF;AAAWK,IAAAA,YAAX;AAAyBC,IAAAA;AAAzB,GAAP;AACD;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,SAASE,cAAT,CACLT,KADK,EAELU,OAFK,EAGQ;AACb,QAAM;AAAET,IAAAA,OAAF;AAAWK,IAAAA,YAAX;AAAyBC,IAAAA;AAAzB,MAAyCR,UAAU,CAACC,KAAD,CAAzD;;AAEA,MAAIU,OAAO,KAAKH,WAAhB,EAA6B;AAC3B,WAAOD,YAAP;AACD;;AACD,QAAMK,YAAY,GAAGV,OAAO,CAACW,OAAR,CAAgBF,OAAhB,CAArB;AACA,SAAOT,OAAO,CAACU,YAAY,GAAG,CAAhB,CAAd;AACD;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,SAASE,kBAAT,CACLb,KADK,EAELU,OAFK,EAGQ;AACb,QAAM;AAAET,IAAAA,OAAF;AAAWK,IAAAA,YAAX;AAAyBC,IAAAA;AAAzB,MAAyCR,UAAU,CAACC,KAAD,CAAzD;;AAEA,MAAIU,OAAO,KAAKJ,YAAhB,EAA8B;AAC5B,WAAOC,WAAP;AACD;;AACD,QAAMI,YAAY,GAAGV,OAAO,CAACW,OAAR,CAAgBF,OAAhB,CAArB;AACA,SAAOT,OAAO,CAACU,YAAY,GAAG,CAAhB,CAAd;AACD","sourcesContent":["function getEntries(query: string) {\n const entries = Array.from(document.querySelectorAll(query));\n const firstElement = entries[0];\n const lastElement = entries[entries.length - 1];\n\n return { entries, firstElement, lastElement };\n}\n\n/**\n * Helper method to find the next focusable sibling element relative to the\n * specified element.\n *\n * @param {string} query - Argument that gets passed to document.querySelectorAll\n * @param {HTMLElement} element - Specified element (e.target)\n * @returns {HTMLElement} - element to be focused\n */\nexport function getNextSibling(\n query: string,\n element: EventTarget\n): HTMLElement {\n const { entries, firstElement, lastElement } = getEntries(query);\n\n if (element === lastElement) {\n return firstElement as HTMLElement;\n }\n const elementIndex = entries.indexOf(element as HTMLElement);\n return entries[elementIndex + 1] as HTMLElement;\n}\n\n/**\n * Helper method to find the previous focusable sibling element relative to the\n * specified element.\n *\n * @param {string} query - Argument that gets passed to document.querySelectorAll\n * @param {HTMLElement} element - Specified element (e.target)\n * @returns {HTMLElement} - element to be focused\n */\nexport function getPreviousSibling(\n query: string,\n element: EventTarget\n): HTMLElement {\n const { entries, firstElement, lastElement } = getEntries(query);\n\n if (element === firstElement) {\n return lastElement as HTMLElement;\n }\n const elementIndex = entries.indexOf(element as HTMLButtonElement);\n return entries[elementIndex - 1] as HTMLElement;\n}\n"],"file":"dom-query.js"}
@@ -0,0 +1,9 @@
1
+ import { ReactNode } from "react";
2
+ declare const FocusTrapWrapper: ({ children, closePopup, focusElements, id }: {
3
+ children: ReactNode | ReactNode[];
4
+ closePopup?: (arg?: boolean) => void;
5
+ id: string;
6
+ focusElements?: string[];
7
+ }) => JSX.Element;
8
+ export default FocusTrapWrapper;
9
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/focus-trap-wrapper/index.tsx"],"names":[],"mappings":"AAAA,OAAc,EAAE,SAAS,EAAe,MAAM,OAAO,CAAC;AAGtD,QAAA,MAAM,gBAAgB;cAMV,SAAS,GAAG,SAAS,EAAE;wBACb,OAAO,KAAK,IAAI;QAChC,MAAM;oBACM,MAAM,EAAE;MACtB,WA8BH,CAAC;AAEF,eAAe,gBAAgB,CAAC"}
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _react = _interopRequireWildcard(require("react"));
9
+
10
+ var _domQuery = require("./dom-query");
11
+
12
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
13
+
14
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
15
+
16
+ const FocusTrapWrapper = ({
17
+ children,
18
+ closePopup = null,
19
+ focusElements = ["button", "a", "input", "select"],
20
+ id
21
+ }) => {
22
+ const queryId = focusElements.map(el => `#${id}-focus-trap ${el}:not([disabled])`).join(", ");
23
+ const handleKeyDown = (0, _react.useCallback)(e => {
24
+ const element = e.target;
25
+
26
+ switch (e.key) {
27
+ case "Escape":
28
+ closePopup();
29
+ break;
30
+
31
+ case "Tab":
32
+ e.preventDefault();
33
+
34
+ if (e.shiftKey) {
35
+ var _getPreviousSibling;
36
+
37
+ (_getPreviousSibling = (0, _domQuery.getPreviousSibling)(queryId, element)) === null || _getPreviousSibling === void 0 ? void 0 : _getPreviousSibling.focus();
38
+ } else {
39
+ var _getNextSibling;
40
+
41
+ (_getNextSibling = (0, _domQuery.getNextSibling)(queryId, element)) === null || _getNextSibling === void 0 ? void 0 : _getNextSibling.focus();
42
+ }
43
+
44
+ break;
45
+
46
+ default:
47
+ }
48
+ }, [closePopup]);
49
+ return /*#__PURE__*/_react.default.createElement("div", {
50
+ id: `${id}-focus-trap`,
51
+ onKeyDown: handleKeyDown,
52
+ role: "presentation"
53
+ }, children);
54
+ };
55
+
56
+ var _default = FocusTrapWrapper;
57
+ exports.default = _default;
58
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/focus-trap-wrapper/index.tsx"],"names":["FocusTrapWrapper","children","closePopup","focusElements","id","queryId","map","el","join","handleKeyDown","e","element","target","key","preventDefault","shiftKey","focus"],"mappings":";;;;;;;AAAA;;AACA;;;;;;AAEA,MAAMA,gBAAgB,GAAG,CAAC;AACxBC,EAAAA,QADwB;AAExBC,EAAAA,UAAU,GAAG,IAFW;AAGxBC,EAAAA,aAAa,GAAG,CAAC,QAAD,EAAW,GAAX,EAAgB,OAAhB,EAAyB,QAAzB,CAHQ;AAIxBC,EAAAA;AAJwB,CAAD,KAUN;AACjB,QAAMC,OAAO,GAAGF,aAAa,CAC1BG,GADa,CACTC,EAAE,IAAK,IAAGH,EAAG,eAAcG,EAAG,kBADrB,EAEbC,IAFa,CAER,IAFQ,CAAhB;AAGA,QAAMC,aAAa,GAAG,wBACpBC,CAAC,IAAI;AACH,UAAMC,OAAO,GAAGD,CAAC,CAACE,MAAlB;;AACA,YAAQF,CAAC,CAACG,GAAV;AACE,WAAK,QAAL;AACEX,QAAAA,UAAU;AACV;;AACF,WAAK,KAAL;AACEQ,QAAAA,CAAC,CAACI,cAAF;;AACA,YAAIJ,CAAC,CAACK,QAAN,EAAgB;AAAA;;AACd,mEAAmBV,OAAnB,EAA4BM,OAA5B,6EAAsCK,KAAtC;AACD,SAFD,MAEO;AAAA;;AACL,2DAAeX,OAAf,EAAwBM,OAAxB,qEAAkCK,KAAlC;AACD;;AACD;;AACF;AAZF;AAcD,GAjBmB,EAkBpB,CAACd,UAAD,CAlBoB,CAAtB;AAqBA,sBACE;AAAK,IAAA,EAAE,EAAG,GAAEE,EAAG,aAAf;AAA6B,IAAA,SAAS,EAAEK,aAAxC;AAAuD,IAAA,IAAI,EAAC;AAA5D,KACGR,QADH,CADF;AAKD,CAxCD;;eA0CeD,gB","sourcesContent":["import React, { ReactNode, useCallback } from \"react\";\nimport { getNextSibling, getPreviousSibling } from \"./dom-query\";\n\nconst FocusTrapWrapper = ({\n children,\n closePopup = null,\n focusElements = [\"button\", \"a\", \"input\", \"select\"],\n id\n}: {\n children: ReactNode | ReactNode[];\n closePopup?: (arg?: boolean) => void;\n id: string;\n focusElements?: string[];\n}): JSX.Element => {\n const queryId = focusElements\n .map(el => `#${id}-focus-trap ${el}:not([disabled])`)\n .join(\", \");\n const handleKeyDown = useCallback(\n e => {\n const element = e.target as HTMLElement;\n switch (e.key) {\n case \"Escape\":\n closePopup();\n break;\n case \"Tab\":\n e.preventDefault();\n if (e.shiftKey) {\n getPreviousSibling(queryId, element)?.focus();\n } else {\n getNextSibling(queryId, element)?.focus();\n }\n break;\n default:\n }\n },\n [closePopup]\n );\n\n return (\n <div id={`${id}-focus-trap`} onKeyDown={handleKeyDown} role=\"presentation\">\n {children}\n </div>\n );\n};\n\nexport default FocusTrapWrapper;\n"],"file":"index.js"}
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  export interface Hue {
2
3
  [key: number]: string;
3
4
  }
@@ -1 +1 @@
1
- {"version":3,"file":"ColorPalette.d.ts","sourceRoot":"","sources":["../../src/stories/ColorPalette.tsx"],"names":[],"mappings":"AAKA,MAAM,WAAW,GAAG;IAClB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC;CACvB;AAED,UAAU,KAAK;IACb,KAAK,EAAE,GAAG,CAAC;CACZ;AA0BD,QAAA,MAAM,YAAY,cAAe,KAAK,KAAG,WAiBxC,CAAC;AACF,eAAe,YAAY,CAAC"}
1
+ {"version":3,"file":"ColorPalette.d.ts","sourceRoot":"","sources":["../../src/stories/ColorPalette.tsx"],"names":[],"mappings":";AAKA,MAAM,WAAW,GAAG;IAClB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC;CACvB;AAED,UAAU,KAAK;IACb,KAAK,EAAE,GAAG,CAAC;CACZ;AA0BD,QAAA,MAAM,YAAY,cAAe,KAAK,KAAG,WAiBxC,CAAC;AACF,eAAe,YAAY,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opentripplanner/building-blocks",
3
- "version": "1.1.0",
3
+ "version": "1.2.1",
4
4
  "description": "Styling and UI Component Library",
5
5
  "author": "@binh-dam-ibigroup",
6
6
  "homepage": "https://github.com/opentripplanner/otp-ui/#readme",
@@ -9,11 +9,11 @@
9
9
  "module": "esm/index.js",
10
10
  "private": false,
11
11
  "devDependencies": {
12
- "@opentripplanner/types": "^6.5.0",
13
- "@opentripplanner/core-utils": "^11.4.0"
12
+ "@opentripplanner/types": "^6.5.2",
13
+ "@opentripplanner/core-utils": "^11.4.3"
14
14
  },
15
15
  "peerDependencies": {
16
- "react": "^16.14.0",
16
+ "react": "^18.2.0",
17
17
  "styled-components": "^5.3.0"
18
18
  },
19
19
  "repository": {
@@ -7,7 +7,10 @@ import React, {
7
7
  useState
8
8
  } from "react";
9
9
 
10
- import { getNextSibling, getPreviousSibling } from "../utils/dom-query";
10
+ import {
11
+ getNextSibling,
12
+ getPreviousSibling
13
+ } from "../focus-trap-wrapper/dom-query";
11
14
  import { DropdownButton, DropdownMenu, DropdownWrapper } from "./styled";
12
15
 
13
16
  export interface Props extends HTMLAttributes<HTMLElement> {
@@ -0,0 +1,46 @@
1
+ import React, { ReactNode, useCallback } from "react";
2
+ import { getNextSibling, getPreviousSibling } from "./dom-query";
3
+
4
+ const FocusTrapWrapper = ({
5
+ children,
6
+ closePopup = null,
7
+ focusElements = ["button", "a", "input", "select"],
8
+ id
9
+ }: {
10
+ children: ReactNode | ReactNode[];
11
+ closePopup?: (arg?: boolean) => void;
12
+ id: string;
13
+ focusElements?: string[];
14
+ }): JSX.Element => {
15
+ const queryId = focusElements
16
+ .map(el => `#${id}-focus-trap ${el}:not([disabled])`)
17
+ .join(", ");
18
+ const handleKeyDown = useCallback(
19
+ e => {
20
+ const element = e.target as HTMLElement;
21
+ switch (e.key) {
22
+ case "Escape":
23
+ closePopup();
24
+ break;
25
+ case "Tab":
26
+ e.preventDefault();
27
+ if (e.shiftKey) {
28
+ getPreviousSibling(queryId, element)?.focus();
29
+ } else {
30
+ getNextSibling(queryId, element)?.focus();
31
+ }
32
+ break;
33
+ default:
34
+ }
35
+ },
36
+ [closePopup]
37
+ );
38
+
39
+ return (
40
+ <div id={`${id}-focus-trap`} onKeyDown={handleKeyDown} role="presentation">
41
+ {children}
42
+ </div>
43
+ );
44
+ };
45
+
46
+ export default FocusTrapWrapper;
@@ -0,0 +1,91 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`Building-Blocks/Dropdown DropdownAsOtprrNavItem smoke-test 1`] = `
4
+ <div class="dropdownstory__NavItemWrapper-sc-giygrt-0 fXLCFd">
5
+ <span class="styled__DropdownWrapper-sc-kes9ha-2 dgncpu navBarItem"
6
+ id="dropdown-navbar-wrapper"
7
+ >
8
+ <button aria-expanded="false"
9
+ aria-haspopup="listbox"
10
+ aria-label="Dropdown navbar"
11
+ id="dropdown-navbar-label"
12
+ title="Dropdown navbar"
13
+ class="styled__DropdownButton-sc-kes9ha-0 eBZyNH"
14
+ >
15
+ <span>
16
+ Icon
17
+ </span>
18
+ <span class="caret"
19
+ role="presentation"
20
+ >
21
+ </span>
22
+ </button>
23
+ </span>
24
+ </div>
25
+ `;
26
+
27
+ exports[`Building-Blocks/Dropdown DropdownWithLabel smoke-test 1`] = `
28
+ <span class="styled__DropdownWrapper-sc-kes9ha-2 dgncpu"
29
+ id="dropdown-with-label-wrapper"
30
+ >
31
+ <button aria-expanded="false"
32
+ aria-haspopup="listbox"
33
+ aria-label="Dropdown with label"
34
+ id="dropdown-with-label-label"
35
+ title="Dropdown with label"
36
+ class="styled__DropdownButton-sc-kes9ha-0 eBZyNH"
37
+ >
38
+ <span>
39
+ Dropdown with label
40
+ </span>
41
+ <span class="caret"
42
+ role="presentation"
43
+ >
44
+ </span>
45
+ </button>
46
+ </span>
47
+ `;
48
+
49
+ exports[`Building-Blocks/Dropdown DropdownWithList smoke-test 1`] = `
50
+ <span class="styled__DropdownWrapper-sc-kes9ha-2 dgncpu"
51
+ id="dropdown-with-ul-wrapper"
52
+ >
53
+ <button aria-expanded="false"
54
+ aria-haspopup="listbox"
55
+ aria-label="Dropdown with ul"
56
+ id="dropdown-with-ul-label"
57
+ title="Dropdown with ul"
58
+ class="styled__DropdownButton-sc-kes9ha-0 eBZyNH"
59
+ >
60
+ <span>
61
+ Dropdown with ul
62
+ </span>
63
+ <span class="caret"
64
+ role="presentation"
65
+ >
66
+ </span>
67
+ </button>
68
+ </span>
69
+ `;
70
+
71
+ exports[`Building-Blocks/Dropdown DropdownWithListAlignMenuLeft smoke-test 1`] = `
72
+ <span class="styled__DropdownWrapper-sc-kes9ha-2 dgncpu"
73
+ id="dropdown-with-ul-wrapper"
74
+ >
75
+ <button aria-expanded="false"
76
+ aria-haspopup="listbox"
77
+ aria-label="Dropdown with ul"
78
+ id="dropdown-with-ul-label"
79
+ title="Dropdown with ul"
80
+ class="styled__DropdownButton-sc-kes9ha-0 eBZyNH"
81
+ >
82
+ <span>
83
+ Dropdown with ul
84
+ </span>
85
+ <span class="caret"
86
+ role="presentation"
87
+ >
88
+ </span>
89
+ </button>
90
+ </span>
91
+ `;
@@ -0,0 +1,64 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`Building-Blocks/FocusTrapWrapper FocusTrapAroundButtonSet smoke-test 1`] = `
4
+ <div id="button-set-story-focus-trap"
5
+ role="presentation"
6
+ >
7
+ <button type="button">
8
+ Button 1
9
+ </button>
10
+ <button type="button">
11
+ Button 2
12
+ </button>
13
+ <button type="button">
14
+ Button 3
15
+ </button>
16
+ <button type="button">
17
+ Button 4
18
+ </button>
19
+ </div>
20
+ `;
21
+
22
+ exports[`Building-Blocks/FocusTrapWrapper FocusTrapWithVariousEls smoke-test 1`] = `
23
+ <div id="various-els-story-focus-trap"
24
+ role="presentation"
25
+ >
26
+ <button type="button">
27
+ Button 1
28
+ </button>
29
+ <br>
30
+ <a href="/">
31
+ link
32
+ </a>
33
+ <br>
34
+ <div tabindex="-1">
35
+ focusable div
36
+ </div>
37
+ <label for="disabled-input">
38
+ Input (disabled)
39
+ <input id="disabled-input"
40
+ disabled
41
+ type="text"
42
+ >
43
+ </label>
44
+ <br>
45
+ <label for="input">
46
+ Input
47
+ <input id="input"
48
+ type="text"
49
+ >
50
+ </label>
51
+ <br>
52
+ <label for="select">
53
+ Select
54
+ <select id="select">
55
+ <option>
56
+ Option 1
57
+ </option>
58
+ <option>
59
+ Option 2
60
+ </option>
61
+ </select>
62
+ </label>
63
+ </div>
64
+ `;