@opentripplanner/building-blocks 1.0.3 → 1.2.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/esm/dropdown/index.js +121 -0
- package/esm/dropdown/index.js.map +1 -0
- package/esm/dropdown/styled.js +19 -0
- package/esm/dropdown/styled.js.map +1 -0
- package/esm/focus-trap-wrapper/dom-query.js +56 -0
- package/esm/focus-trap-wrapper/dom-query.js.map +1 -0
- package/esm/focus-trap-wrapper/index.js +48 -0
- package/esm/focus-trap-wrapper/index.js.map +1 -0
- package/esm/index.js +2 -0
- package/esm/index.js.map +1 -1
- package/lib/dropdown/index.d.ts +17 -0
- package/lib/dropdown/index.d.ts.map +1 -0
- package/lib/dropdown/index.js +126 -0
- package/lib/dropdown/index.js.map +1 -0
- package/lib/dropdown/styled.d.ts +8 -0
- package/lib/dropdown/styled.d.ts.map +1 -0
- package/lib/dropdown/styled.js +34 -0
- package/lib/dropdown/styled.js.map +1 -0
- package/lib/focus-trap-wrapper/dom-query.d.ts +19 -0
- package/lib/focus-trap-wrapper/dom-query.d.ts.map +1 -0
- package/lib/focus-trap-wrapper/dom-query.js +67 -0
- package/lib/focus-trap-wrapper/dom-query.js.map +1 -0
- package/lib/focus-trap-wrapper/index.d.ts +9 -0
- package/lib/focus-trap-wrapper/index.d.ts.map +1 -0
- package/lib/focus-trap-wrapper/index.js +58 -0
- package/lib/focus-trap-wrapper/index.js.map +1 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +8 -0
- package/lib/index.js.map +1 -1
- package/lib/stories/ColorPalette.d.ts +1 -0
- package/lib/stories/ColorPalette.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/dropdown/index.tsx +142 -0
- package/src/dropdown/styled.tsx +71 -0
- package/src/focus-trap-wrapper/dom-query.ts +49 -0
- package/src/focus-trap-wrapper/index.tsx +46 -0
- package/src/index.ts +2 -0
- package/src/stories/__snapshots__/dropdown.story.tsx.snap +91 -0
- package/src/stories/__snapshots__/focusTrapStory.story.tsx.snap +64 -0
- package/src/stories/__snapshots__/index.story.tsx.snap +250 -0
- package/src/stories/dropdown.story.tsx +105 -0
- package/src/stories/focusTrapStory.story.tsx +46 -0
- package/tsconfig.json +1 -2
- package/tsconfig.tsbuildinfo +1 -1
- package/lib/stories/index.story.d.ts +0 -9
- package/lib/stories/index.story.d.ts.map +0 -1
- package/lib/stories/index.story.js +0 -29
- package/lib/stories/index.story.js.map +0 -1
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
2
|
+
import React, { useCallback, useEffect, useRef, useState } from "react";
|
|
3
|
+
import { getNextSibling, getPreviousSibling } from "../focus-trap-wrapper/dom-query";
|
|
4
|
+
import { DropdownButton, DropdownMenu, DropdownWrapper } from "./styled";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Renders a dropdown menu. By default, only a passed "text" is rendered. If clicked,
|
|
8
|
+
* a floating div is rendered below the "text" with list contents inside. Clicking anywhere
|
|
9
|
+
* outside the floating div will close the dropdown.
|
|
10
|
+
*/
|
|
11
|
+
var Dropdown = function Dropdown(_ref) {
|
|
12
|
+
var alignMenuLeft = _ref.alignMenuLeft,
|
|
13
|
+
children = _ref.children,
|
|
14
|
+
className = _ref.className,
|
|
15
|
+
id = _ref.id,
|
|
16
|
+
label = _ref.label,
|
|
17
|
+
listLabel = _ref.listLabel,
|
|
18
|
+
text = _ref.text,
|
|
19
|
+
buttonStyle = _ref.buttonStyle;
|
|
20
|
+
|
|
21
|
+
var _useState = useState(false),
|
|
22
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
23
|
+
open = _useState2[0],
|
|
24
|
+
setOpen = _useState2[1];
|
|
25
|
+
|
|
26
|
+
var containerRef = useRef(null);
|
|
27
|
+
var toggleOpen = useCallback(function () {
|
|
28
|
+
return setOpen(!open);
|
|
29
|
+
}, [open, setOpen]); // Argument for document.querySelectorAll to target focusable elements.
|
|
30
|
+
|
|
31
|
+
var queryId = "#".concat(id, " button, #").concat(id, "-label");
|
|
32
|
+
var isList = Array.isArray(children) ? children.every(function (child) {
|
|
33
|
+
return /*#__PURE__*/React.isValidElement(child) && child.type === "li";
|
|
34
|
+
}) : /*#__PURE__*/React.isValidElement(children) && children.type === "li"; // Adding document event listeners allows us to close the dropdown
|
|
35
|
+
// when the user interacts with any part of the page that isn't the dropdown
|
|
36
|
+
|
|
37
|
+
useEffect(function () {
|
|
38
|
+
var handleExternalAction = function handleExternalAction(e) {
|
|
39
|
+
var _containerRef$current;
|
|
40
|
+
|
|
41
|
+
if (!(containerRef !== null && containerRef !== void 0 && (_containerRef$current = containerRef.current) !== null && _containerRef$current !== void 0 && _containerRef$current.contains(e.target))) {
|
|
42
|
+
setOpen(false);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
document.addEventListener("mousedown", handleExternalAction);
|
|
47
|
+
document.addEventListener("focusin", handleExternalAction);
|
|
48
|
+
document.addEventListener("keydown", handleExternalAction);
|
|
49
|
+
return function () {
|
|
50
|
+
document.removeEventListener("mousedown", handleExternalAction);
|
|
51
|
+
document.removeEventListener("focusin", handleExternalAction);
|
|
52
|
+
document.removeEventListener("keydown", handleExternalAction);
|
|
53
|
+
};
|
|
54
|
+
}, [containerRef]);
|
|
55
|
+
var handleKeyDown = useCallback(function (e) {
|
|
56
|
+
var _getPreviousSibling, _getNextSibling;
|
|
57
|
+
|
|
58
|
+
var element = e.target;
|
|
59
|
+
|
|
60
|
+
switch (e.key) {
|
|
61
|
+
case "ArrowUp":
|
|
62
|
+
e.preventDefault();
|
|
63
|
+
(_getPreviousSibling = getPreviousSibling(queryId, element)) === null || _getPreviousSibling === void 0 ? void 0 : _getPreviousSibling.focus();
|
|
64
|
+
break;
|
|
65
|
+
|
|
66
|
+
case "ArrowDown":
|
|
67
|
+
e.preventDefault();
|
|
68
|
+
(_getNextSibling = getNextSibling(queryId, element)) === null || _getNextSibling === void 0 ? void 0 : _getNextSibling.focus();
|
|
69
|
+
break;
|
|
70
|
+
|
|
71
|
+
case "Escape":
|
|
72
|
+
setOpen(false);
|
|
73
|
+
break;
|
|
74
|
+
|
|
75
|
+
case " ":
|
|
76
|
+
case "Enter":
|
|
77
|
+
e.preventDefault();
|
|
78
|
+
element.click();
|
|
79
|
+
|
|
80
|
+
if (element.id === "".concat(id, "-label") || element.id === "".concat(id, "-wrapper")) {
|
|
81
|
+
toggleOpen();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
break;
|
|
85
|
+
|
|
86
|
+
default:
|
|
87
|
+
}
|
|
88
|
+
}, [id, toggleOpen]);
|
|
89
|
+
return /*#__PURE__*/React.createElement(DropdownWrapper, {
|
|
90
|
+
className: className,
|
|
91
|
+
id: "".concat(id, "-wrapper"),
|
|
92
|
+
onKeyDown: handleKeyDown,
|
|
93
|
+
ref: containerRef
|
|
94
|
+
}, /*#__PURE__*/React.createElement(DropdownButton // Only set aria-controls when the dropdown is open
|
|
95
|
+
// (otherwise, assistive technologies may not announce the dropdown correctly).
|
|
96
|
+
, {
|
|
97
|
+
"aria-controls": open ? id : undefined,
|
|
98
|
+
"aria-expanded": open,
|
|
99
|
+
"aria-haspopup": "listbox",
|
|
100
|
+
"aria-label": label,
|
|
101
|
+
id: "".concat(id, "-label"),
|
|
102
|
+
onClick: toggleOpen,
|
|
103
|
+
style: buttonStyle,
|
|
104
|
+
title: label
|
|
105
|
+
}, /*#__PURE__*/React.createElement("span", null, text), /*#__PURE__*/React.createElement("span", {
|
|
106
|
+
className: "caret",
|
|
107
|
+
role: "presentation"
|
|
108
|
+
})), open && /*#__PURE__*/React.createElement(DropdownMenu, {
|
|
109
|
+
as: isList && "ul",
|
|
110
|
+
"aria-label": listLabel,
|
|
111
|
+
"aria-labelledby": listLabel ? undefined : "".concat(id, "-label"),
|
|
112
|
+
id: id,
|
|
113
|
+
onClick: toggleOpen,
|
|
114
|
+
alignLeft: alignMenuLeft,
|
|
115
|
+
role: isList && "list",
|
|
116
|
+
tabIndex: -1
|
|
117
|
+
}, children));
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
export default Dropdown;
|
|
121
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +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,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,QAAQ,CAAC,KAAD,CAAhC;AAAA;AAAA,MAAOe,IAAP;AAAA,MAAaC,OAAb;;AAEA,MAAMC,YAAY,GAAGlB,MAAM,CAAkB,IAAlB,CAA3B;AAEA,MAAMmB,UAAU,GAAGrB,WAAW,CAAC;AAAA,WAAMmB,OAAO,CAAC,CAACD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F,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,19 @@
|
|
|
1
|
+
import styled from "styled-components";
|
|
2
|
+
import grey from "../colors/grey";
|
|
3
|
+
export var DropdownButton = styled.button.withConfig({
|
|
4
|
+
displayName: "styled__DropdownButton",
|
|
5
|
+
componentId: "sc-kes9ha-0"
|
|
6
|
+
})(["background:#fff;border:1px solid black;border-radius:5px;color:inherit;padding:5px 7px;transition:all 0.1s ease-in-out;span.caret{border-left:4px solid transparent;border-right:4px solid transparent;border-top:4px solid;color:inherit;display:inline-block;height:0;margin-left:5px;vertical-align:middle;width:0;}&:hover,&[aria-expanded=\"true\"]{background:", ";color:black;cursor:pointer;}"], grey[50]);
|
|
7
|
+
export var DropdownMenu = styled.div.withConfig({
|
|
8
|
+
displayName: "styled__DropdownMenu",
|
|
9
|
+
componentId: "sc-kes9ha-1"
|
|
10
|
+
})(["background-clip:padding-box;background-color:#fff;border-radius:4px;border:1px solid rgba(0,0,0,0.15);box-shadow:0 6px 12px rgba(0,0,0,0.175);color:#333;list-style:none;margin:2px 0 0;min-width:160px;padding:5px 0;position:absolute;", " top:100%;width:100%;z-index:1000;hr{margin:0;padding:0;}a,button{background:transparent;border:none;cursor:pointer;padding:5px 15px;text-align:start;width:100%;&:hover{background:", ";}}"], function (props) {
|
|
11
|
+
return props.alignLeft ? "left: 0;" : "right: 0;";
|
|
12
|
+
}, grey[50]);
|
|
13
|
+
export var DropdownWrapper = styled.span.withConfig({
|
|
14
|
+
displayName: "styled__DropdownWrapper",
|
|
15
|
+
componentId: "sc-kes9ha-2"
|
|
16
|
+
})(["float:", ";position:relative;"], function (props) {
|
|
17
|
+
return props.pullRight ? "right" : "left";
|
|
18
|
+
});
|
|
19
|
+
//# sourceMappingURL=styled.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/dropdown/styled.tsx"],"names":["styled","grey","DropdownButton","button","DropdownMenu","div","props","alignLeft","DropdownWrapper","span","pullRight"],"mappings":"AAAA,OAAOA,MAAP,MAAmB,mBAAnB;AACA,OAAOC,IAAP,MAAiB,gBAAjB;AAEA,OAAO,IAAMC,cAAc,GAAGF,MAAM,CAACG,MAAV;AAAA;AAAA;AAAA,8YAsBTF,IAAI,CAAC,EAAD,CAtBK,CAApB;AA4BP,OAAO,IAAMG,YAAY,GAAGJ,MAAM,CAACK,GAAV;AAAA;AAAA;AAAA,gbAYrB,UAAAC,KAAK;AAAA,SAAKA,KAAK,CAACC,SAAN,GAAkB,UAAlB,GAA+B,WAApC;AAAA,CAZgB,EA+BLN,IAAI,CAAC,EAAD,CA/BC,CAAlB;AAoCP,OAAO,IAAMO,eAAe,GAAGR,MAAM,CAACS,IAAV;AAAA;AAAA;AAAA,sCACjB,UAAAH,KAAK;AAAA,SAAKA,KAAK,CAACI,SAAN,GAAkB,OAAlB,GAA4B,MAAjC;AAAA,CADY,CAArB","sourcesContent":["import styled from \"styled-components\";\nimport grey from \"../colors/grey\";\n\nexport const DropdownButton = styled.button`\n background: #fff;\n border: 1px solid black;\n border-radius: 5px;\n color: inherit;\n padding: 5px 7px;\n transition: all 0.1s ease-in-out;\n\n span.caret {\n border-left: 4px solid transparent;\n border-right: 4px solid transparent;\n border-top: 4px solid;\n color: inherit;\n display: inline-block;\n height: 0;\n margin-left: 5px;\n vertical-align: middle;\n width: 0;\n }\n\n &:hover,\n &[aria-expanded=\"true\"] {\n background: ${grey[50]};\n color: black;\n cursor: pointer;\n }\n`;\n\nexport const DropdownMenu = styled.div<{ alignLeft?: boolean }>`\n background-clip: padding-box;\n background-color: #fff;\n border-radius: 4px;\n border: 1px solid rgba(0, 0, 0, 0.15);\n box-shadow: 0 6px 12px rgba(0, 0, 0, 0.175);\n color: #333;\n list-style: none;\n margin: 2px 0 0;\n min-width: 160px;\n padding: 5px 0;\n position: absolute;\n ${props => (props.alignLeft ? \"left: 0;\" : \"right: 0;\")}\n top: 100%;\n width: 100%;\n z-index: 1000;\n\n hr {\n margin: 0;\n padding: 0;\n }\n a,\n button {\n background: transparent;\n border: none;\n cursor: pointer;\n padding: 5px 15px;\n text-align: start;\n width: 100%;\n\n &:hover {\n background: ${grey[50]};\n }\n }\n`;\n\nexport const DropdownWrapper = styled.span<{ pullRight?: boolean }>`\n float: ${props => (props.pullRight ? \"right\" : \"left\")};\n position: relative;\n`;\n"],"file":"styled.js"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
function getEntries(query) {
|
|
2
|
+
var entries = Array.from(document.querySelectorAll(query));
|
|
3
|
+
var firstElement = entries[0];
|
|
4
|
+
var lastElement = entries[entries.length - 1];
|
|
5
|
+
return {
|
|
6
|
+
entries: entries,
|
|
7
|
+
firstElement: firstElement,
|
|
8
|
+
lastElement: lastElement
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Helper method to find the next focusable sibling element relative to the
|
|
13
|
+
* specified element.
|
|
14
|
+
*
|
|
15
|
+
* @param {string} query - Argument that gets passed to document.querySelectorAll
|
|
16
|
+
* @param {HTMLElement} element - Specified element (e.target)
|
|
17
|
+
* @returns {HTMLElement} - element to be focused
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
export function getNextSibling(query, element) {
|
|
22
|
+
var _getEntries = getEntries(query),
|
|
23
|
+
entries = _getEntries.entries,
|
|
24
|
+
firstElement = _getEntries.firstElement,
|
|
25
|
+
lastElement = _getEntries.lastElement;
|
|
26
|
+
|
|
27
|
+
if (element === lastElement) {
|
|
28
|
+
return firstElement;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
var elementIndex = entries.indexOf(element);
|
|
32
|
+
return entries[elementIndex + 1];
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Helper method to find the previous focusable sibling element relative to the
|
|
36
|
+
* specified element.
|
|
37
|
+
*
|
|
38
|
+
* @param {string} query - Argument that gets passed to document.querySelectorAll
|
|
39
|
+
* @param {HTMLElement} element - Specified element (e.target)
|
|
40
|
+
* @returns {HTMLElement} - element to be focused
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
export function getPreviousSibling(query, element) {
|
|
44
|
+
var _getEntries2 = getEntries(query),
|
|
45
|
+
entries = _getEntries2.entries,
|
|
46
|
+
firstElement = _getEntries2.firstElement,
|
|
47
|
+
lastElement = _getEntries2.lastElement;
|
|
48
|
+
|
|
49
|
+
if (element === firstElement) {
|
|
50
|
+
return lastElement;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
var elementIndex = entries.indexOf(element);
|
|
54
|
+
return entries[elementIndex - 1];
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=dom-query.js.map
|
|
@@ -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"}
|
package/esm/index.js
CHANGED
package/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"names":["blue","red","grey"],"mappings":"AAAA,OAAOA,IAAP,MAAiB,eAAjB;AACA,OAAOC,GAAP,MAAgB,cAAhB;AACA,OAAOC,IAAP,MAAiB,eAAjB;AAEA,eAAe;
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":["blue","red","grey","Dropdown"],"mappings":"AAAA,OAAOA,IAAP,MAAiB,eAAjB;AACA,OAAOC,GAAP,MAAgB,cAAhB;AACA,OAAOC,IAAP,MAAiB,eAAjB;AACA,OAAOC,QAAP,MAAqB,YAArB;AAEA,SAASA,QAAT;AACA,eAAe;AAAEH,EAAAA,IAAI,EAAJA,IAAF;AAAQC,EAAAA,GAAG,EAAHA,GAAR;AAAaC,EAAAA,IAAI,EAAJA;AAAb,CAAf","sourcesContent":["import blue from \"./colors/blue\";\nimport red from \"./colors/red\";\nimport grey from \"./colors/grey\";\nimport Dropdown from \"./dropdown\";\n\nexport { Dropdown };\nexport default { blue, red, grey };\n"],"file":"index.js"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import React, { HTMLAttributes } from "react";
|
|
2
|
+
export interface Props extends HTMLAttributes<HTMLElement> {
|
|
3
|
+
alignMenuLeft?: boolean;
|
|
4
|
+
buttonStyle?: React.CSSProperties;
|
|
5
|
+
label?: string;
|
|
6
|
+
listLabel?: string;
|
|
7
|
+
text?: JSX.Element | string;
|
|
8
|
+
nav?: boolean;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Renders a dropdown menu. By default, only a passed "text" is rendered. If clicked,
|
|
12
|
+
* a floating div is rendered below the "text" with list contents inside. Clicking anywhere
|
|
13
|
+
* outside the floating div will close the dropdown.
|
|
14
|
+
*/
|
|
15
|
+
declare const Dropdown: ({ alignMenuLeft, children, className, id, label, listLabel, text, buttonStyle }: Props) => JSX.Element;
|
|
16
|
+
export default Dropdown;
|
|
17
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +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;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"}
|
|
@@ -0,0 +1,126 @@
|
|
|
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("../focus-trap-wrapper/dom-query");
|
|
11
|
+
|
|
12
|
+
var _styled = require("./styled");
|
|
13
|
+
|
|
14
|
+
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); }
|
|
15
|
+
|
|
16
|
+
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; }
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Renders a dropdown menu. By default, only a passed "text" is rendered. If clicked,
|
|
20
|
+
* a floating div is rendered below the "text" with list contents inside. Clicking anywhere
|
|
21
|
+
* outside the floating div will close the dropdown.
|
|
22
|
+
*/
|
|
23
|
+
const Dropdown = ({
|
|
24
|
+
alignMenuLeft,
|
|
25
|
+
children,
|
|
26
|
+
className,
|
|
27
|
+
id,
|
|
28
|
+
label,
|
|
29
|
+
listLabel,
|
|
30
|
+
text,
|
|
31
|
+
buttonStyle
|
|
32
|
+
}) => {
|
|
33
|
+
const [open, setOpen] = (0, _react.useState)(false);
|
|
34
|
+
const containerRef = (0, _react.useRef)(null);
|
|
35
|
+
const toggleOpen = (0, _react.useCallback)(() => setOpen(!open), [open, setOpen]); // Argument for document.querySelectorAll to target focusable elements.
|
|
36
|
+
|
|
37
|
+
const queryId = `#${id} button, #${id}-label`;
|
|
38
|
+
const isList = Array.isArray(children) ? children.every(child => /*#__PURE__*/_react.default.isValidElement(child) && child.type === "li") : /*#__PURE__*/_react.default.isValidElement(children) && children.type === "li"; // Adding document event listeners allows us to close the dropdown
|
|
39
|
+
// when the user interacts with any part of the page that isn't the dropdown
|
|
40
|
+
|
|
41
|
+
(0, _react.useEffect)(() => {
|
|
42
|
+
const handleExternalAction = e => {
|
|
43
|
+
var _containerRef$current;
|
|
44
|
+
|
|
45
|
+
if (!(containerRef !== null && containerRef !== void 0 && (_containerRef$current = containerRef.current) !== null && _containerRef$current !== void 0 && _containerRef$current.contains(e.target))) {
|
|
46
|
+
setOpen(false);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
document.addEventListener("mousedown", handleExternalAction);
|
|
51
|
+
document.addEventListener("focusin", handleExternalAction);
|
|
52
|
+
document.addEventListener("keydown", handleExternalAction);
|
|
53
|
+
return () => {
|
|
54
|
+
document.removeEventListener("mousedown", handleExternalAction);
|
|
55
|
+
document.removeEventListener("focusin", handleExternalAction);
|
|
56
|
+
document.removeEventListener("keydown", handleExternalAction);
|
|
57
|
+
};
|
|
58
|
+
}, [containerRef]);
|
|
59
|
+
const handleKeyDown = (0, _react.useCallback)(e => {
|
|
60
|
+
var _getPreviousSibling, _getNextSibling;
|
|
61
|
+
|
|
62
|
+
const element = e.target;
|
|
63
|
+
|
|
64
|
+
switch (e.key) {
|
|
65
|
+
case "ArrowUp":
|
|
66
|
+
e.preventDefault();
|
|
67
|
+
(_getPreviousSibling = (0, _domQuery.getPreviousSibling)(queryId, element)) === null || _getPreviousSibling === void 0 ? void 0 : _getPreviousSibling.focus();
|
|
68
|
+
break;
|
|
69
|
+
|
|
70
|
+
case "ArrowDown":
|
|
71
|
+
e.preventDefault();
|
|
72
|
+
(_getNextSibling = (0, _domQuery.getNextSibling)(queryId, element)) === null || _getNextSibling === void 0 ? void 0 : _getNextSibling.focus();
|
|
73
|
+
break;
|
|
74
|
+
|
|
75
|
+
case "Escape":
|
|
76
|
+
setOpen(false);
|
|
77
|
+
break;
|
|
78
|
+
|
|
79
|
+
case " ":
|
|
80
|
+
case "Enter":
|
|
81
|
+
e.preventDefault();
|
|
82
|
+
element.click();
|
|
83
|
+
|
|
84
|
+
if (element.id === `${id}-label` || element.id === `${id}-wrapper`) {
|
|
85
|
+
toggleOpen();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
break;
|
|
89
|
+
|
|
90
|
+
default:
|
|
91
|
+
}
|
|
92
|
+
}, [id, toggleOpen]);
|
|
93
|
+
return /*#__PURE__*/_react.default.createElement(_styled.DropdownWrapper, {
|
|
94
|
+
className: className,
|
|
95
|
+
id: `${id}-wrapper`,
|
|
96
|
+
onKeyDown: handleKeyDown,
|
|
97
|
+
ref: containerRef
|
|
98
|
+
}, /*#__PURE__*/_react.default.createElement(_styled.DropdownButton // Only set aria-controls when the dropdown is open
|
|
99
|
+
// (otherwise, assistive technologies may not announce the dropdown correctly).
|
|
100
|
+
, {
|
|
101
|
+
"aria-controls": open ? id : undefined,
|
|
102
|
+
"aria-expanded": open,
|
|
103
|
+
"aria-haspopup": "listbox",
|
|
104
|
+
"aria-label": label,
|
|
105
|
+
id: `${id}-label`,
|
|
106
|
+
onClick: toggleOpen,
|
|
107
|
+
style: buttonStyle,
|
|
108
|
+
title: label
|
|
109
|
+
}, /*#__PURE__*/_react.default.createElement("span", null, text), /*#__PURE__*/_react.default.createElement("span", {
|
|
110
|
+
className: "caret",
|
|
111
|
+
role: "presentation"
|
|
112
|
+
})), open && /*#__PURE__*/_react.default.createElement(_styled.DropdownMenu, {
|
|
113
|
+
as: isList && "ul",
|
|
114
|
+
"aria-label": listLabel,
|
|
115
|
+
"aria-labelledby": listLabel ? undefined : `${id}-label`,
|
|
116
|
+
id: id,
|
|
117
|
+
onClick: toggleOpen,
|
|
118
|
+
alignLeft: alignMenuLeft,
|
|
119
|
+
role: isList && "list",
|
|
120
|
+
tabIndex: -1
|
|
121
|
+
}, children));
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
var _default = Dropdown;
|
|
125
|
+
exports.default = _default;
|
|
126
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +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;;AAIA;;;;;;AAWA;AACA;AACA;AACA;AACA;AACA,MAAMA,QAAQ,GAAG,CAAC;AAChBC,EAAAA,aADgB;AAEhBC,EAAAA,QAFgB;AAGhBC,EAAAA,SAHgB;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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,8 @@
|
|
|
1
|
+
export declare const DropdownButton: import("styled-components").StyledComponent<"button", any, {}, never>;
|
|
2
|
+
export declare const DropdownMenu: import("styled-components").StyledComponent<"div", any, {
|
|
3
|
+
alignLeft?: boolean;
|
|
4
|
+
}, never>;
|
|
5
|
+
export declare const DropdownWrapper: import("styled-components").StyledComponent<"span", any, {
|
|
6
|
+
pullRight?: boolean;
|
|
7
|
+
}, never>;
|
|
8
|
+
//# sourceMappingURL=styled.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"styled.d.ts","sourceRoot":"","sources":["../../src/dropdown/styled.tsx"],"names":[],"mappings":"AAGA,eAAO,MAAM,cAAc,uEA0B1B,CAAC;AAEF,eAAO,MAAM,YAAY;gBAA4B,OAAO;SAkC3D,CAAC;AAEF,eAAO,MAAM,eAAe;gBAA6B,OAAO;SAG/D,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.DropdownWrapper = exports.DropdownMenu = exports.DropdownButton = void 0;
|
|
9
|
+
|
|
10
|
+
var _styledComponents = _interopRequireDefault(require("styled-components"));
|
|
11
|
+
|
|
12
|
+
var _grey = _interopRequireDefault(require("../colors/grey"));
|
|
13
|
+
|
|
14
|
+
const DropdownButton = _styledComponents.default.button.withConfig({
|
|
15
|
+
displayName: "styled__DropdownButton",
|
|
16
|
+
componentId: "sc-kes9ha-0"
|
|
17
|
+
})(["background:#fff;border:1px solid black;border-radius:5px;color:inherit;padding:5px 7px;transition:all 0.1s ease-in-out;span.caret{border-left:4px solid transparent;border-right:4px solid transparent;border-top:4px solid;color:inherit;display:inline-block;height:0;margin-left:5px;vertical-align:middle;width:0;}&:hover,&[aria-expanded=\"true\"]{background:", ";color:black;cursor:pointer;}"], _grey.default[50]);
|
|
18
|
+
|
|
19
|
+
exports.DropdownButton = DropdownButton;
|
|
20
|
+
|
|
21
|
+
const DropdownMenu = _styledComponents.default.div.withConfig({
|
|
22
|
+
displayName: "styled__DropdownMenu",
|
|
23
|
+
componentId: "sc-kes9ha-1"
|
|
24
|
+
})(["background-clip:padding-box;background-color:#fff;border-radius:4px;border:1px solid rgba(0,0,0,0.15);box-shadow:0 6px 12px rgba(0,0,0,0.175);color:#333;list-style:none;margin:2px 0 0;min-width:160px;padding:5px 0;position:absolute;", " top:100%;width:100%;z-index:1000;hr{margin:0;padding:0;}a,button{background:transparent;border:none;cursor:pointer;padding:5px 15px;text-align:start;width:100%;&:hover{background:", ";}}"], props => props.alignLeft ? "left: 0;" : "right: 0;", _grey.default[50]);
|
|
25
|
+
|
|
26
|
+
exports.DropdownMenu = DropdownMenu;
|
|
27
|
+
|
|
28
|
+
const DropdownWrapper = _styledComponents.default.span.withConfig({
|
|
29
|
+
displayName: "styled__DropdownWrapper",
|
|
30
|
+
componentId: "sc-kes9ha-2"
|
|
31
|
+
})(["float:", ";position:relative;"], props => props.pullRight ? "right" : "left");
|
|
32
|
+
|
|
33
|
+
exports.DropdownWrapper = DropdownWrapper;
|
|
34
|
+
//# sourceMappingURL=styled.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/dropdown/styled.tsx"],"names":["DropdownButton","styled","button","grey","DropdownMenu","div","props","alignLeft","DropdownWrapper","span","pullRight"],"mappings":";;;;;;;;;AAAA;;AACA;;AAEO,MAAMA,cAAc,GAAGC,0BAAOC,MAAV;AAAA;AAAA;AAAA,8YAsBTC,cAAK,EAAL,CAtBS,CAApB;;;;AA4BA,MAAMC,YAAY,GAAGH,0BAAOI,GAAV;AAAA;AAAA;AAAA,gbAYrBC,KAAK,IAAKA,KAAK,CAACC,SAAN,GAAkB,UAAlB,GAA+B,WAZpB,EA+BLJ,cAAK,EAAL,CA/BK,CAAlB;;;;AAoCA,MAAMK,eAAe,GAAGP,0BAAOQ,IAAV;AAAA;AAAA;AAAA,sCACjBH,KAAK,IAAKA,KAAK,CAACI,SAAN,GAAkB,OAAlB,GAA4B,MADrB,CAArB","sourcesContent":["import styled from \"styled-components\";\nimport grey from \"../colors/grey\";\n\nexport const DropdownButton = styled.button`\n background: #fff;\n border: 1px solid black;\n border-radius: 5px;\n color: inherit;\n padding: 5px 7px;\n transition: all 0.1s ease-in-out;\n\n span.caret {\n border-left: 4px solid transparent;\n border-right: 4px solid transparent;\n border-top: 4px solid;\n color: inherit;\n display: inline-block;\n height: 0;\n margin-left: 5px;\n vertical-align: middle;\n width: 0;\n }\n\n &:hover,\n &[aria-expanded=\"true\"] {\n background: ${grey[50]};\n color: black;\n cursor: pointer;\n }\n`;\n\nexport const DropdownMenu = styled.div<{ alignLeft?: boolean }>`\n background-clip: padding-box;\n background-color: #fff;\n border-radius: 4px;\n border: 1px solid rgba(0, 0, 0, 0.15);\n box-shadow: 0 6px 12px rgba(0, 0, 0, 0.175);\n color: #333;\n list-style: none;\n margin: 2px 0 0;\n min-width: 160px;\n padding: 5px 0;\n position: absolute;\n ${props => (props.alignLeft ? \"left: 0;\" : \"right: 0;\")}\n top: 100%;\n width: 100%;\n z-index: 1000;\n\n hr {\n margin: 0;\n padding: 0;\n }\n a,\n button {\n background: transparent;\n border: none;\n cursor: pointer;\n padding: 5px 15px;\n text-align: start;\n width: 100%;\n\n &:hover {\n background: ${grey[50]};\n }\n }\n`;\n\nexport const DropdownWrapper = styled.span<{ pullRight?: boolean }>`\n float: ${props => (props.pullRight ? \"right\" : \"left\")};\n position: relative;\n`;\n"],"file":"styled.js"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helper method to find the next focusable sibling element relative to the
|
|
3
|
+
* specified element.
|
|
4
|
+
*
|
|
5
|
+
* @param {string} query - Argument that gets passed to document.querySelectorAll
|
|
6
|
+
* @param {HTMLElement} element - Specified element (e.target)
|
|
7
|
+
* @returns {HTMLElement} - element to be focused
|
|
8
|
+
*/
|
|
9
|
+
export declare function getNextSibling(query: string, element: EventTarget): HTMLElement;
|
|
10
|
+
/**
|
|
11
|
+
* Helper method to find the previous focusable sibling element relative to the
|
|
12
|
+
* specified element.
|
|
13
|
+
*
|
|
14
|
+
* @param {string} query - Argument that gets passed to document.querySelectorAll
|
|
15
|
+
* @param {HTMLElement} element - Specified element (e.target)
|
|
16
|
+
* @returns {HTMLElement} - element to be focused
|
|
17
|
+
*/
|
|
18
|
+
export declare function getPreviousSibling(query: string, element: EventTarget): HTMLElement;
|
|
19
|
+
//# sourceMappingURL=dom-query.d.ts.map
|
|
@@ -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,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.getNextSibling = getNextSibling;
|
|
7
|
+
exports.getPreviousSibling = getPreviousSibling;
|
|
8
|
+
|
|
9
|
+
function getEntries(query) {
|
|
10
|
+
const entries = Array.from(document.querySelectorAll(query));
|
|
11
|
+
const firstElement = entries[0];
|
|
12
|
+
const lastElement = entries[entries.length - 1];
|
|
13
|
+
return {
|
|
14
|
+
entries,
|
|
15
|
+
firstElement,
|
|
16
|
+
lastElement
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Helper method to find the next focusable sibling element relative to the
|
|
21
|
+
* specified element.
|
|
22
|
+
*
|
|
23
|
+
* @param {string} query - Argument that gets passed to document.querySelectorAll
|
|
24
|
+
* @param {HTMLElement} element - Specified element (e.target)
|
|
25
|
+
* @returns {HTMLElement} - element to be focused
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
function getNextSibling(query, element) {
|
|
30
|
+
const {
|
|
31
|
+
entries,
|
|
32
|
+
firstElement,
|
|
33
|
+
lastElement
|
|
34
|
+
} = getEntries(query);
|
|
35
|
+
|
|
36
|
+
if (element === lastElement) {
|
|
37
|
+
return firstElement;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const elementIndex = entries.indexOf(element);
|
|
41
|
+
return entries[elementIndex + 1];
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Helper method to find the previous focusable sibling element relative to the
|
|
45
|
+
* specified element.
|
|
46
|
+
*
|
|
47
|
+
* @param {string} query - Argument that gets passed to document.querySelectorAll
|
|
48
|
+
* @param {HTMLElement} element - Specified element (e.target)
|
|
49
|
+
* @returns {HTMLElement} - element to be focused
|
|
50
|
+
*/
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
function getPreviousSibling(query, element) {
|
|
54
|
+
const {
|
|
55
|
+
entries,
|
|
56
|
+
firstElement,
|
|
57
|
+
lastElement
|
|
58
|
+
} = getEntries(query);
|
|
59
|
+
|
|
60
|
+
if (element === firstElement) {
|
|
61
|
+
return lastElement;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const elementIndex = entries.indexOf(element);
|
|
65
|
+
return entries[elementIndex - 1];
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=dom-query.js.map
|
|
@@ -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"}
|