@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,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"}
|
package/lib/index.d.ts
CHANGED
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,QAAQ,MAAM,YAAY,CAAC;AAElC,OAAO,EAAE,QAAQ,EAAE,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACpB,wBAAmC"}
|
package/lib/index.js
CHANGED
|
@@ -5,6 +5,12 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", {
|
|
6
6
|
value: true
|
|
7
7
|
});
|
|
8
|
+
Object.defineProperty(exports, "Dropdown", {
|
|
9
|
+
enumerable: true,
|
|
10
|
+
get: function () {
|
|
11
|
+
return _dropdown.default;
|
|
12
|
+
}
|
|
13
|
+
});
|
|
8
14
|
exports.default = void 0;
|
|
9
15
|
|
|
10
16
|
var _blue = _interopRequireDefault(require("./colors/blue"));
|
|
@@ -13,6 +19,8 @@ var _red = _interopRequireDefault(require("./colors/red"));
|
|
|
13
19
|
|
|
14
20
|
var _grey = _interopRequireDefault(require("./colors/grey"));
|
|
15
21
|
|
|
22
|
+
var _dropdown = _interopRequireDefault(require("./dropdown"));
|
|
23
|
+
|
|
16
24
|
var _default = {
|
|
17
25
|
blue: _blue.default,
|
|
18
26
|
red: _red.default,
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"names":["blue","red","grey"],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":["blue","red","grey"],"mappings":";;;;;;;;;;;;;;;AAAA;;AACA;;AACA;;AACA;;eAGe;AAAEA,EAAAA,IAAI,EAAJA,aAAF;AAAQC,EAAAA,GAAG,EAAHA,YAAR;AAAaC,EAAAA,IAAI,EAAJA;AAAb,C","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"}
|
|
@@ -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.0
|
|
3
|
+
"version": "1.2.0",
|
|
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.
|
|
13
|
-
"@opentripplanner/core-utils": "^11.
|
|
12
|
+
"@opentripplanner/types": "^6.5.0",
|
|
13
|
+
"@opentripplanner/core-utils": "^11.4.1"
|
|
14
14
|
},
|
|
15
15
|
"peerDependencies": {
|
|
16
|
-
"react": "^
|
|
16
|
+
"react": "^18.2.0",
|
|
17
17
|
"styled-components": "^5.3.0"
|
|
18
18
|
},
|
|
19
19
|
"repository": {
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import React, {
|
|
2
|
+
HTMLAttributes,
|
|
3
|
+
KeyboardEvent,
|
|
4
|
+
useCallback,
|
|
5
|
+
useEffect,
|
|
6
|
+
useRef,
|
|
7
|
+
useState
|
|
8
|
+
} from "react";
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
getNextSibling,
|
|
12
|
+
getPreviousSibling
|
|
13
|
+
} from "../focus-trap-wrapper/dom-query";
|
|
14
|
+
import { DropdownButton, DropdownMenu, DropdownWrapper } from "./styled";
|
|
15
|
+
|
|
16
|
+
export interface Props extends HTMLAttributes<HTMLElement> {
|
|
17
|
+
alignMenuLeft?: boolean;
|
|
18
|
+
buttonStyle?: React.CSSProperties;
|
|
19
|
+
label?: string;
|
|
20
|
+
listLabel?: string;
|
|
21
|
+
text?: JSX.Element | string;
|
|
22
|
+
nav?: boolean;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Renders a dropdown menu. By default, only a passed "text" is rendered. If clicked,
|
|
27
|
+
* a floating div is rendered below the "text" with list contents inside. Clicking anywhere
|
|
28
|
+
* outside the floating div will close the dropdown.
|
|
29
|
+
*/
|
|
30
|
+
const Dropdown = ({
|
|
31
|
+
alignMenuLeft,
|
|
32
|
+
children,
|
|
33
|
+
className,
|
|
34
|
+
id,
|
|
35
|
+
label,
|
|
36
|
+
listLabel,
|
|
37
|
+
text,
|
|
38
|
+
buttonStyle
|
|
39
|
+
}: Props): JSX.Element => {
|
|
40
|
+
const [open, setOpen] = useState(false);
|
|
41
|
+
|
|
42
|
+
const containerRef = useRef<HTMLSpanElement>(null);
|
|
43
|
+
|
|
44
|
+
const toggleOpen = useCallback(() => setOpen(!open), [open, setOpen]);
|
|
45
|
+
|
|
46
|
+
// Argument for document.querySelectorAll to target focusable elements.
|
|
47
|
+
const queryId = `#${id} button, #${id}-label`;
|
|
48
|
+
|
|
49
|
+
const isList = Array.isArray(children)
|
|
50
|
+
? children.every(
|
|
51
|
+
child => React.isValidElement(child) && child.type === "li"
|
|
52
|
+
)
|
|
53
|
+
: React.isValidElement(children) && children.type === "li";
|
|
54
|
+
|
|
55
|
+
// Adding document event listeners allows us to close the dropdown
|
|
56
|
+
// when the user interacts with any part of the page that isn't the dropdown
|
|
57
|
+
useEffect(() => {
|
|
58
|
+
const handleExternalAction = (e: Event): void => {
|
|
59
|
+
if (!containerRef?.current?.contains(e.target as HTMLElement)) {
|
|
60
|
+
setOpen(false);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
document.addEventListener("mousedown", handleExternalAction);
|
|
64
|
+
document.addEventListener("focusin", handleExternalAction);
|
|
65
|
+
document.addEventListener("keydown", handleExternalAction);
|
|
66
|
+
return () => {
|
|
67
|
+
document.removeEventListener("mousedown", handleExternalAction);
|
|
68
|
+
document.removeEventListener("focusin", handleExternalAction);
|
|
69
|
+
document.removeEventListener("keydown", handleExternalAction);
|
|
70
|
+
};
|
|
71
|
+
}, [containerRef]);
|
|
72
|
+
|
|
73
|
+
const handleKeyDown = useCallback(
|
|
74
|
+
(e: KeyboardEvent): void => {
|
|
75
|
+
const element = e.target as HTMLElement;
|
|
76
|
+
switch (e.key) {
|
|
77
|
+
case "ArrowUp":
|
|
78
|
+
e.preventDefault();
|
|
79
|
+
getPreviousSibling(queryId, element)?.focus();
|
|
80
|
+
break;
|
|
81
|
+
case "ArrowDown":
|
|
82
|
+
e.preventDefault();
|
|
83
|
+
getNextSibling(queryId, element)?.focus();
|
|
84
|
+
break;
|
|
85
|
+
case "Escape":
|
|
86
|
+
setOpen(false);
|
|
87
|
+
break;
|
|
88
|
+
case " ":
|
|
89
|
+
case "Enter":
|
|
90
|
+
e.preventDefault();
|
|
91
|
+
element.click();
|
|
92
|
+
if (element.id === `${id}-label` || element.id === `${id}-wrapper`) {
|
|
93
|
+
toggleOpen();
|
|
94
|
+
}
|
|
95
|
+
break;
|
|
96
|
+
default:
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
[id, toggleOpen]
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
return (
|
|
103
|
+
<DropdownWrapper
|
|
104
|
+
className={className}
|
|
105
|
+
id={`${id}-wrapper`}
|
|
106
|
+
onKeyDown={handleKeyDown}
|
|
107
|
+
ref={containerRef}
|
|
108
|
+
>
|
|
109
|
+
<DropdownButton
|
|
110
|
+
// Only set aria-controls when the dropdown is open
|
|
111
|
+
// (otherwise, assistive technologies may not announce the dropdown correctly).
|
|
112
|
+
aria-controls={open ? id : undefined}
|
|
113
|
+
aria-expanded={open}
|
|
114
|
+
aria-haspopup="listbox"
|
|
115
|
+
aria-label={label}
|
|
116
|
+
id={`${id}-label`}
|
|
117
|
+
onClick={toggleOpen}
|
|
118
|
+
style={buttonStyle}
|
|
119
|
+
title={label}
|
|
120
|
+
>
|
|
121
|
+
<span>{text}</span>
|
|
122
|
+
<span className="caret" role="presentation" />
|
|
123
|
+
</DropdownButton>
|
|
124
|
+
{open && (
|
|
125
|
+
<DropdownMenu
|
|
126
|
+
as={isList && "ul"}
|
|
127
|
+
aria-label={listLabel}
|
|
128
|
+
aria-labelledby={listLabel ? undefined : `${id}-label`}
|
|
129
|
+
id={id}
|
|
130
|
+
onClick={toggleOpen}
|
|
131
|
+
alignLeft={alignMenuLeft}
|
|
132
|
+
role={isList && "list"}
|
|
133
|
+
tabIndex={-1}
|
|
134
|
+
>
|
|
135
|
+
{children}
|
|
136
|
+
</DropdownMenu>
|
|
137
|
+
)}
|
|
138
|
+
</DropdownWrapper>
|
|
139
|
+
);
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
export default Dropdown;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import styled from "styled-components";
|
|
2
|
+
import grey from "../colors/grey";
|
|
3
|
+
|
|
4
|
+
export const DropdownButton = styled.button`
|
|
5
|
+
background: #fff;
|
|
6
|
+
border: 1px solid black;
|
|
7
|
+
border-radius: 5px;
|
|
8
|
+
color: inherit;
|
|
9
|
+
padding: 5px 7px;
|
|
10
|
+
transition: all 0.1s ease-in-out;
|
|
11
|
+
|
|
12
|
+
span.caret {
|
|
13
|
+
border-left: 4px solid transparent;
|
|
14
|
+
border-right: 4px solid transparent;
|
|
15
|
+
border-top: 4px solid;
|
|
16
|
+
color: inherit;
|
|
17
|
+
display: inline-block;
|
|
18
|
+
height: 0;
|
|
19
|
+
margin-left: 5px;
|
|
20
|
+
vertical-align: middle;
|
|
21
|
+
width: 0;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
&:hover,
|
|
25
|
+
&[aria-expanded="true"] {
|
|
26
|
+
background: ${grey[50]};
|
|
27
|
+
color: black;
|
|
28
|
+
cursor: pointer;
|
|
29
|
+
}
|
|
30
|
+
`;
|
|
31
|
+
|
|
32
|
+
export const DropdownMenu = styled.div<{ alignLeft?: boolean }>`
|
|
33
|
+
background-clip: padding-box;
|
|
34
|
+
background-color: #fff;
|
|
35
|
+
border-radius: 4px;
|
|
36
|
+
border: 1px solid rgba(0, 0, 0, 0.15);
|
|
37
|
+
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.175);
|
|
38
|
+
color: #333;
|
|
39
|
+
list-style: none;
|
|
40
|
+
margin: 2px 0 0;
|
|
41
|
+
min-width: 160px;
|
|
42
|
+
padding: 5px 0;
|
|
43
|
+
position: absolute;
|
|
44
|
+
${props => (props.alignLeft ? "left: 0;" : "right: 0;")}
|
|
45
|
+
top: 100%;
|
|
46
|
+
width: 100%;
|
|
47
|
+
z-index: 1000;
|
|
48
|
+
|
|
49
|
+
hr {
|
|
50
|
+
margin: 0;
|
|
51
|
+
padding: 0;
|
|
52
|
+
}
|
|
53
|
+
a,
|
|
54
|
+
button {
|
|
55
|
+
background: transparent;
|
|
56
|
+
border: none;
|
|
57
|
+
cursor: pointer;
|
|
58
|
+
padding: 5px 15px;
|
|
59
|
+
text-align: start;
|
|
60
|
+
width: 100%;
|
|
61
|
+
|
|
62
|
+
&:hover {
|
|
63
|
+
background: ${grey[50]};
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
`;
|
|
67
|
+
|
|
68
|
+
export const DropdownWrapper = styled.span<{ pullRight?: boolean }>`
|
|
69
|
+
float: ${props => (props.pullRight ? "right" : "left")};
|
|
70
|
+
position: relative;
|
|
71
|
+
`;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
function getEntries(query: string) {
|
|
2
|
+
const entries = Array.from(document.querySelectorAll(query));
|
|
3
|
+
const firstElement = entries[0];
|
|
4
|
+
const lastElement = entries[entries.length - 1];
|
|
5
|
+
|
|
6
|
+
return { entries, firstElement, lastElement };
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Helper method to find the next focusable sibling element relative to the
|
|
11
|
+
* specified element.
|
|
12
|
+
*
|
|
13
|
+
* @param {string} query - Argument that gets passed to document.querySelectorAll
|
|
14
|
+
* @param {HTMLElement} element - Specified element (e.target)
|
|
15
|
+
* @returns {HTMLElement} - element to be focused
|
|
16
|
+
*/
|
|
17
|
+
export function getNextSibling(
|
|
18
|
+
query: string,
|
|
19
|
+
element: EventTarget
|
|
20
|
+
): HTMLElement {
|
|
21
|
+
const { entries, firstElement, lastElement } = getEntries(query);
|
|
22
|
+
|
|
23
|
+
if (element === lastElement) {
|
|
24
|
+
return firstElement as HTMLElement;
|
|
25
|
+
}
|
|
26
|
+
const elementIndex = entries.indexOf(element as HTMLElement);
|
|
27
|
+
return entries[elementIndex + 1] as HTMLElement;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Helper method to find the previous focusable sibling element relative to the
|
|
32
|
+
* specified element.
|
|
33
|
+
*
|
|
34
|
+
* @param {string} query - Argument that gets passed to document.querySelectorAll
|
|
35
|
+
* @param {HTMLElement} element - Specified element (e.target)
|
|
36
|
+
* @returns {HTMLElement} - element to be focused
|
|
37
|
+
*/
|
|
38
|
+
export function getPreviousSibling(
|
|
39
|
+
query: string,
|
|
40
|
+
element: EventTarget
|
|
41
|
+
): HTMLElement {
|
|
42
|
+
const { entries, firstElement, lastElement } = getEntries(query);
|
|
43
|
+
|
|
44
|
+
if (element === firstElement) {
|
|
45
|
+
return lastElement as HTMLElement;
|
|
46
|
+
}
|
|
47
|
+
const elementIndex = entries.indexOf(element as HTMLButtonElement);
|
|
48
|
+
return entries[elementIndex - 1] as HTMLElement;
|
|
49
|
+
}
|
|
@@ -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;
|
package/src/index.ts
CHANGED
|
@@ -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
|
+
`;
|