@opentripplanner/building-blocks 1.0.3 → 1.1.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/index.js +2 -0
- package/esm/index.js.map +1 -1
- package/esm/utils/dom-query.js +56 -0
- package/esm/utils/dom-query.js.map +1 -0
- 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/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/dropdown.story.d.ts +9 -0
- package/lib/stories/dropdown.story.d.ts.map +1 -0
- package/lib/stories/dropdown.story.js +59 -0
- package/lib/stories/dropdown.story.js.map +1 -0
- package/lib/utils/dom-query.d.ts +19 -0
- package/lib/utils/dom-query.d.ts.map +1 -0
- package/lib/utils/dom-query.js +67 -0
- package/lib/utils/dom-query.js.map +1 -0
- package/package.json +3 -3
- package/src/dropdown/index.tsx +139 -0
- package/src/dropdown/styled.tsx +71 -0
- package/src/index.ts +2 -0
- package/src/stories/dropdown.story.tsx +105 -0
- package/src/utils/dom-query.ts +49 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -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/utils/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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opentripplanner/building-blocks",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.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,8 +9,8 @@
|
|
|
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.0"
|
|
14
14
|
},
|
|
15
15
|
"peerDependencies": {
|
|
16
16
|
"react": "^16.14.0",
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import React, {
|
|
2
|
+
HTMLAttributes,
|
|
3
|
+
KeyboardEvent,
|
|
4
|
+
useCallback,
|
|
5
|
+
useEffect,
|
|
6
|
+
useRef,
|
|
7
|
+
useState
|
|
8
|
+
} from "react";
|
|
9
|
+
|
|
10
|
+
import { getNextSibling, getPreviousSibling } from "../utils/dom-query";
|
|
11
|
+
import { DropdownButton, DropdownMenu, DropdownWrapper } from "./styled";
|
|
12
|
+
|
|
13
|
+
export interface Props extends HTMLAttributes<HTMLElement> {
|
|
14
|
+
alignMenuLeft?: boolean;
|
|
15
|
+
buttonStyle?: React.CSSProperties;
|
|
16
|
+
label?: string;
|
|
17
|
+
listLabel?: string;
|
|
18
|
+
text?: JSX.Element | string;
|
|
19
|
+
nav?: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Renders a dropdown menu. By default, only a passed "text" is rendered. If clicked,
|
|
24
|
+
* a floating div is rendered below the "text" with list contents inside. Clicking anywhere
|
|
25
|
+
* outside the floating div will close the dropdown.
|
|
26
|
+
*/
|
|
27
|
+
const Dropdown = ({
|
|
28
|
+
alignMenuLeft,
|
|
29
|
+
children,
|
|
30
|
+
className,
|
|
31
|
+
id,
|
|
32
|
+
label,
|
|
33
|
+
listLabel,
|
|
34
|
+
text,
|
|
35
|
+
buttonStyle
|
|
36
|
+
}: Props): JSX.Element => {
|
|
37
|
+
const [open, setOpen] = useState(false);
|
|
38
|
+
|
|
39
|
+
const containerRef = useRef<HTMLSpanElement>(null);
|
|
40
|
+
|
|
41
|
+
const toggleOpen = useCallback(() => setOpen(!open), [open, setOpen]);
|
|
42
|
+
|
|
43
|
+
// Argument for document.querySelectorAll to target focusable elements.
|
|
44
|
+
const queryId = `#${id} button, #${id}-label`;
|
|
45
|
+
|
|
46
|
+
const isList = Array.isArray(children)
|
|
47
|
+
? children.every(
|
|
48
|
+
child => React.isValidElement(child) && child.type === "li"
|
|
49
|
+
)
|
|
50
|
+
: React.isValidElement(children) && children.type === "li";
|
|
51
|
+
|
|
52
|
+
// Adding document event listeners allows us to close the dropdown
|
|
53
|
+
// when the user interacts with any part of the page that isn't the dropdown
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
const handleExternalAction = (e: Event): void => {
|
|
56
|
+
if (!containerRef?.current?.contains(e.target as HTMLElement)) {
|
|
57
|
+
setOpen(false);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
document.addEventListener("mousedown", handleExternalAction);
|
|
61
|
+
document.addEventListener("focusin", handleExternalAction);
|
|
62
|
+
document.addEventListener("keydown", handleExternalAction);
|
|
63
|
+
return () => {
|
|
64
|
+
document.removeEventListener("mousedown", handleExternalAction);
|
|
65
|
+
document.removeEventListener("focusin", handleExternalAction);
|
|
66
|
+
document.removeEventListener("keydown", handleExternalAction);
|
|
67
|
+
};
|
|
68
|
+
}, [containerRef]);
|
|
69
|
+
|
|
70
|
+
const handleKeyDown = useCallback(
|
|
71
|
+
(e: KeyboardEvent): void => {
|
|
72
|
+
const element = e.target as HTMLElement;
|
|
73
|
+
switch (e.key) {
|
|
74
|
+
case "ArrowUp":
|
|
75
|
+
e.preventDefault();
|
|
76
|
+
getPreviousSibling(queryId, element)?.focus();
|
|
77
|
+
break;
|
|
78
|
+
case "ArrowDown":
|
|
79
|
+
e.preventDefault();
|
|
80
|
+
getNextSibling(queryId, element)?.focus();
|
|
81
|
+
break;
|
|
82
|
+
case "Escape":
|
|
83
|
+
setOpen(false);
|
|
84
|
+
break;
|
|
85
|
+
case " ":
|
|
86
|
+
case "Enter":
|
|
87
|
+
e.preventDefault();
|
|
88
|
+
element.click();
|
|
89
|
+
if (element.id === `${id}-label` || element.id === `${id}-wrapper`) {
|
|
90
|
+
toggleOpen();
|
|
91
|
+
}
|
|
92
|
+
break;
|
|
93
|
+
default:
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
[id, toggleOpen]
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
return (
|
|
100
|
+
<DropdownWrapper
|
|
101
|
+
className={className}
|
|
102
|
+
id={`${id}-wrapper`}
|
|
103
|
+
onKeyDown={handleKeyDown}
|
|
104
|
+
ref={containerRef}
|
|
105
|
+
>
|
|
106
|
+
<DropdownButton
|
|
107
|
+
// Only set aria-controls when the dropdown is open
|
|
108
|
+
// (otherwise, assistive technologies may not announce the dropdown correctly).
|
|
109
|
+
aria-controls={open ? id : undefined}
|
|
110
|
+
aria-expanded={open}
|
|
111
|
+
aria-haspopup="listbox"
|
|
112
|
+
aria-label={label}
|
|
113
|
+
id={`${id}-label`}
|
|
114
|
+
onClick={toggleOpen}
|
|
115
|
+
style={buttonStyle}
|
|
116
|
+
title={label}
|
|
117
|
+
>
|
|
118
|
+
<span>{text}</span>
|
|
119
|
+
<span className="caret" role="presentation" />
|
|
120
|
+
</DropdownButton>
|
|
121
|
+
{open && (
|
|
122
|
+
<DropdownMenu
|
|
123
|
+
as={isList && "ul"}
|
|
124
|
+
aria-label={listLabel}
|
|
125
|
+
aria-labelledby={listLabel ? undefined : `${id}-label`}
|
|
126
|
+
id={id}
|
|
127
|
+
onClick={toggleOpen}
|
|
128
|
+
alignLeft={alignMenuLeft}
|
|
129
|
+
role={isList && "list"}
|
|
130
|
+
tabIndex={-1}
|
|
131
|
+
>
|
|
132
|
+
{children}
|
|
133
|
+
</DropdownMenu>
|
|
134
|
+
)}
|
|
135
|
+
</DropdownWrapper>
|
|
136
|
+
);
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
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
|
+
`;
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { ComponentMeta } from "@storybook/react";
|
|
3
|
+
import styled from "styled-components";
|
|
4
|
+
import Dropdown from "../dropdown";
|
|
5
|
+
import blue from "../colors/blue";
|
|
6
|
+
|
|
7
|
+
const options = [
|
|
8
|
+
{ value: "1", label: "One" },
|
|
9
|
+
{ value: "2", label: "Two" },
|
|
10
|
+
{ value: "3", label: "Three" }
|
|
11
|
+
];
|
|
12
|
+
|
|
13
|
+
const NavItemWrapper = styled.div`
|
|
14
|
+
align-items: center;
|
|
15
|
+
background: ${blue[900]};
|
|
16
|
+
display: flex;
|
|
17
|
+
min-height: 50px;
|
|
18
|
+
padding: 0 10px;
|
|
19
|
+
position: relative;
|
|
20
|
+
width: 100%;
|
|
21
|
+
|
|
22
|
+
.navBarItem {
|
|
23
|
+
position: static;
|
|
24
|
+
& > button {
|
|
25
|
+
background: transparent;
|
|
26
|
+
border: none;
|
|
27
|
+
color: white;
|
|
28
|
+
padding: 15px;
|
|
29
|
+
|
|
30
|
+
@media (max-width: 768px) {
|
|
31
|
+
padding: 10px;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
&:hover {
|
|
35
|
+
background: rgba(0, 0, 0, 0.05);
|
|
36
|
+
color: #ececec;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
`;
|
|
41
|
+
|
|
42
|
+
export default {
|
|
43
|
+
title: "Building-Blocks/Dropdown",
|
|
44
|
+
component: Dropdown
|
|
45
|
+
} as ComponentMeta<typeof Dropdown>;
|
|
46
|
+
|
|
47
|
+
export const DropdownWithLabel = (): React.ReactElement => (
|
|
48
|
+
<Dropdown
|
|
49
|
+
id="dropdown-with-label"
|
|
50
|
+
label="Dropdown with label"
|
|
51
|
+
listLabel="Dropdown menu"
|
|
52
|
+
text="Dropdown with label"
|
|
53
|
+
>
|
|
54
|
+
More content here
|
|
55
|
+
</Dropdown>
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
export const DropdownWithList = (): React.ReactElement => (
|
|
59
|
+
<Dropdown
|
|
60
|
+
id="dropdown-with-ul"
|
|
61
|
+
label="Dropdown with ul"
|
|
62
|
+
listLabel="Dropdown menu"
|
|
63
|
+
text="Dropdown with ul"
|
|
64
|
+
>
|
|
65
|
+
{options.map(option => (
|
|
66
|
+
<li key={option.value} value={option.value}>
|
|
67
|
+
<button type="button">{option.label}</button>
|
|
68
|
+
</li>
|
|
69
|
+
))}
|
|
70
|
+
</Dropdown>
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
export const DropdownWithListAlignMenuLeft = (): React.ReactElement => (
|
|
74
|
+
<Dropdown
|
|
75
|
+
alignMenuLeft
|
|
76
|
+
id="dropdown-with-ul"
|
|
77
|
+
label="Dropdown with ul"
|
|
78
|
+
listLabel="Dropdown menu"
|
|
79
|
+
text="Dropdown with ul"
|
|
80
|
+
>
|
|
81
|
+
{options.map(option => (
|
|
82
|
+
<li key={option.value} value={option.value}>
|
|
83
|
+
<button type="button">{option.label}</button>
|
|
84
|
+
</li>
|
|
85
|
+
))}
|
|
86
|
+
</Dropdown>
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
export const DropdownAsOtprrNavItem = (): React.ReactElement => (
|
|
90
|
+
<NavItemWrapper>
|
|
91
|
+
<Dropdown
|
|
92
|
+
className="navBarItem"
|
|
93
|
+
id="dropdown-navbar"
|
|
94
|
+
label="Dropdown navbar"
|
|
95
|
+
listLabel="Dropdown within navbar"
|
|
96
|
+
text="Icon"
|
|
97
|
+
>
|
|
98
|
+
{options.map(option => (
|
|
99
|
+
<button key={option.value} type="button">
|
|
100
|
+
{option.label}
|
|
101
|
+
</button>
|
|
102
|
+
))}
|
|
103
|
+
</Dropdown>
|
|
104
|
+
</NavItemWrapper>
|
|
105
|
+
);
|
|
@@ -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
|
+
}
|