@opentripplanner/building-blocks 1.0.2 → 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.
Files changed (42) hide show
  1. package/esm/dropdown/index.js +121 -0
  2. package/esm/dropdown/index.js.map +1 -0
  3. package/esm/dropdown/styled.js +19 -0
  4. package/esm/dropdown/styled.js.map +1 -0
  5. package/esm/index.js +7 -1
  6. package/esm/index.js.map +1 -1
  7. package/esm/utils/dom-query.js +56 -0
  8. package/esm/utils/dom-query.js.map +1 -0
  9. package/lib/dropdown/index.d.ts +17 -0
  10. package/lib/dropdown/index.d.ts.map +1 -0
  11. package/lib/dropdown/index.js +126 -0
  12. package/lib/dropdown/index.js.map +1 -0
  13. package/lib/dropdown/styled.d.ts +8 -0
  14. package/lib/dropdown/styled.d.ts.map +1 -0
  15. package/lib/dropdown/styled.js +34 -0
  16. package/lib/dropdown/styled.js.map +1 -0
  17. package/lib/index.d.ts +41 -4
  18. package/lib/index.d.ts.map +1 -1
  19. package/lib/index.js +12 -14
  20. package/lib/index.js.map +1 -1
  21. package/lib/stories/ColorPalette.d.ts +0 -1
  22. package/lib/stories/ColorPalette.d.ts.map +1 -1
  23. package/lib/stories/dropdown.story.d.ts +9 -0
  24. package/lib/stories/dropdown.story.d.ts.map +1 -0
  25. package/lib/stories/dropdown.story.js +59 -0
  26. package/lib/stories/dropdown.story.js.map +1 -0
  27. package/lib/stories/index.story.d.ts +4 -4
  28. package/lib/stories/index.story.d.ts.map +1 -1
  29. package/lib/stories/index.story.js +4 -2
  30. package/lib/stories/index.story.js.map +1 -1
  31. package/lib/utils/dom-query.d.ts +19 -0
  32. package/lib/utils/dom-query.d.ts.map +1 -0
  33. package/lib/utils/dom-query.js +67 -0
  34. package/lib/utils/dom-query.js.map +1 -0
  35. package/package.json +3 -3
  36. package/src/dropdown/index.tsx +139 -0
  37. package/src/dropdown/styled.tsx +71 -0
  38. package/src/index.ts +3 -1
  39. package/src/stories/dropdown.story.tsx +105 -0
  40. package/src/stories/index.story.tsx +6 -2
  41. package/src/utils/dom-query.ts +49 -0
  42. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.DropdownAsOtprrNavItem = exports.DropdownWithListAlignMenuLeft = exports.DropdownWithList = exports.DropdownWithLabel = void 0;
7
+ const react_1 = __importDefault(require("react"));
8
+ const styled_components_1 = __importDefault(require("styled-components"));
9
+ const dropdown_1 = __importDefault(require("../dropdown"));
10
+ const blue_1 = __importDefault(require("../colors/blue"));
11
+ const options = [
12
+ { value: "1", label: "One" },
13
+ { value: "2", label: "Two" },
14
+ { value: "3", label: "Three" }
15
+ ];
16
+ const NavItemWrapper = styled_components_1.default.div `
17
+ align-items: center;
18
+ background: ${blue_1.default[900]};
19
+ display: flex;
20
+ min-height: 50px;
21
+ padding: 0 10px;
22
+ position: relative;
23
+ width: 100%;
24
+
25
+ .navBarItem {
26
+ position: static;
27
+ & > button {
28
+ background: transparent;
29
+ border: none;
30
+ color: white;
31
+ padding: 15px;
32
+
33
+ @media (max-width: 768px) {
34
+ padding: 10px;
35
+ }
36
+
37
+ &:hover {
38
+ background: rgba(0, 0, 0, 0.05);
39
+ color: #ececec;
40
+ }
41
+ }
42
+ }
43
+ `;
44
+ exports.default = {
45
+ title: "Building-Blocks/Dropdown",
46
+ component: dropdown_1.default
47
+ };
48
+ const DropdownWithLabel = () => (react_1.default.createElement(dropdown_1.default, { id: "dropdown-with-label", label: "Dropdown with label", listLabel: "Dropdown menu", text: "Dropdown with label" }, "More content here"));
49
+ exports.DropdownWithLabel = DropdownWithLabel;
50
+ const DropdownWithList = () => (react_1.default.createElement(dropdown_1.default, { id: "dropdown-with-ul", label: "Dropdown with ul", listLabel: "Dropdown menu", text: "Dropdown with ul" }, options.map(option => (react_1.default.createElement("li", { key: option.value, value: option.value },
51
+ react_1.default.createElement("button", { type: "button" }, option.label))))));
52
+ exports.DropdownWithList = DropdownWithList;
53
+ const DropdownWithListAlignMenuLeft = () => (react_1.default.createElement(dropdown_1.default, { alignMenuLeft: true, id: "dropdown-with-ul", label: "Dropdown with ul", listLabel: "Dropdown menu", text: "Dropdown with ul" }, options.map(option => (react_1.default.createElement("li", { key: option.value, value: option.value },
54
+ react_1.default.createElement("button", { type: "button" }, option.label))))));
55
+ exports.DropdownWithListAlignMenuLeft = DropdownWithListAlignMenuLeft;
56
+ const DropdownAsOtprrNavItem = () => (react_1.default.createElement(NavItemWrapper, null,
57
+ react_1.default.createElement(dropdown_1.default, { className: "navBarItem", id: "dropdown-navbar", label: "Dropdown navbar", listLabel: "Dropdown within navbar", text: "Icon" }, options.map(option => (react_1.default.createElement("button", { key: option.value, type: "button" }, option.label))))));
58
+ exports.DropdownAsOtprrNavItem = DropdownAsOtprrNavItem;
59
+ //# sourceMappingURL=dropdown.story.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dropdown.story.js","sourceRoot":"","sources":["../../src/stories/dropdown.story.tsx"],"names":[],"mappings":";;;;;;AAAA,kDAA0B;AAE1B,0EAAuC;AACvC,2DAAmC;AACnC,0DAAkC;AAElC,MAAM,OAAO,GAAG;IACd,EAAE,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,KAAK,EAAE;IAC5B,EAAE,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,KAAK,EAAE;IAC5B,EAAE,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,OAAO,EAAE;CAC/B,CAAC;AAEF,MAAM,cAAc,GAAG,2BAAM,CAAC,GAAG,CAAA;;gBAEjB,cAAI,CAAC,GAAG,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;CAyBxB,CAAC;AAEF,kBAAe;IACb,KAAK,EAAE,0BAA0B;IACjC,SAAS,EAAE,kBAAQ;CACc,CAAC;AAE7B,MAAM,iBAAiB,GAAG,GAAuB,EAAE,CAAC,CACzD,8BAAC,kBAAQ,IACP,EAAE,EAAC,qBAAqB,EACxB,KAAK,EAAC,qBAAqB,EAC3B,SAAS,EAAC,eAAe,EACzB,IAAI,EAAC,qBAAqB,wBAGjB,CACZ,CAAC;AATW,QAAA,iBAAiB,qBAS5B;AAEK,MAAM,gBAAgB,GAAG,GAAuB,EAAE,CAAC,CACxD,8BAAC,kBAAQ,IACP,EAAE,EAAC,kBAAkB,EACrB,KAAK,EAAC,kBAAkB,EACxB,SAAS,EAAC,eAAe,EACzB,IAAI,EAAC,kBAAkB,IAEtB,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,CACrB,sCAAI,GAAG,EAAE,MAAM,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK;IACxC,0CAAQ,IAAI,EAAC,QAAQ,IAAE,MAAM,CAAC,KAAK,CAAU,CAC1C,CACN,CAAC,CACO,CACZ,CAAC;AAbW,QAAA,gBAAgB,oBAa3B;AAEK,MAAM,6BAA6B,GAAG,GAAuB,EAAE,CAAC,CACrE,8BAAC,kBAAQ,IACP,aAAa,QACb,EAAE,EAAC,kBAAkB,EACrB,KAAK,EAAC,kBAAkB,EACxB,SAAS,EAAC,eAAe,EACzB,IAAI,EAAC,kBAAkB,IAEtB,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,CACrB,sCAAI,GAAG,EAAE,MAAM,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK;IACxC,0CAAQ,IAAI,EAAC,QAAQ,IAAE,MAAM,CAAC,KAAK,CAAU,CAC1C,CACN,CAAC,CACO,CACZ,CAAC;AAdW,QAAA,6BAA6B,iCAcxC;AAEK,MAAM,sBAAsB,GAAG,GAAuB,EAAE,CAAC,CAC9D,8BAAC,cAAc;IACb,8BAAC,kBAAQ,IACP,SAAS,EAAC,YAAY,EACtB,EAAE,EAAC,iBAAiB,EACpB,KAAK,EAAC,iBAAiB,EACvB,SAAS,EAAC,wBAAwB,EAClC,IAAI,EAAC,MAAM,IAEV,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,CACrB,0CAAQ,GAAG,EAAE,MAAM,CAAC,KAAK,EAAE,IAAI,EAAC,QAAQ,IACrC,MAAM,CAAC,KAAK,CACN,CACV,CAAC,CACO,CACI,CAClB,CAAC;AAhBW,QAAA,sBAAsB,0BAgBjC"}
@@ -1,8 +1,8 @@
1
1
  import { ReactElement } from "react";
2
- declare const _default: {
3
- title: string;
4
- };
5
- export default _default;
2
+ import { Meta } from "@storybook/react";
3
+ import ColorPalette from "./ColorPalette";
4
+ declare const meta: Meta<typeof ColorPalette>;
5
+ export default meta;
6
6
  export declare const Grey: () => ReactElement;
7
7
  export declare const Blue: () => ReactElement;
8
8
  export declare const Red: () => ReactElement;
@@ -1 +1 @@
1
- {"version":3,"file":"index.story.d.ts","sourceRoot":"","sources":["../../src/stories/index.story.tsx"],"names":[],"mappings":"AAAA,OAAc,EAAE,YAAY,EAAE,MAAM,OAAO,CAAC;;;;AAO5C,wBAEE;AAEF,eAAO,MAAM,IAAI,QAAO,YAEvB,CAAC;AAEF,eAAO,MAAM,IAAI,QAAO,YAEvB,CAAC;AAEF,eAAO,MAAM,GAAG,QAAO,YAEtB,CAAC"}
1
+ {"version":3,"file":"index.story.d.ts","sourceRoot":"","sources":["../../src/stories/index.story.tsx"],"names":[],"mappings":"AAAA,OAAc,EAAE,YAAY,EAAE,MAAM,OAAO,CAAC;AAC5C,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACxC,OAAO,YAAY,MAAM,gBAAgB,CAAC;AAM1C,QAAA,MAAM,IAAI,EAAE,IAAI,CAAC,OAAO,YAAY,CAGnC,CAAC;AAEF,eAAe,IAAI,CAAC;AAEpB,eAAO,MAAM,IAAI,QAAO,YAEvB,CAAC;AAEF,eAAO,MAAM,IAAI,QAAO,YAEvB,CAAC;AAEF,eAAO,MAAM,GAAG,QAAO,YAEtB,CAAC"}
@@ -9,9 +9,11 @@ const ColorPalette_1 = __importDefault(require("./ColorPalette"));
9
9
  const grey_1 = __importDefault(require("../colors/grey"));
10
10
  const blue_1 = __importDefault(require("../colors/blue"));
11
11
  const red_1 = __importDefault(require("../colors/red"));
12
- exports.default = {
13
- title: "Building-Blocks/Colors"
12
+ const meta = {
13
+ title: "Building-Blocks/Colors",
14
+ component: ColorPalette_1.default
14
15
  };
16
+ exports.default = meta;
15
17
  const Grey = () => {
16
18
  return react_1.default.createElement(ColorPalette_1.default, { color: grey_1.default });
17
19
  };
@@ -1 +1 @@
1
- {"version":3,"file":"index.story.js","sourceRoot":"","sources":["../../src/stories/index.story.tsx"],"names":[],"mappings":";;;;;;AAAA,kDAA4C;AAC5C,kEAA0C;AAE1C,0DAAkC;AAClC,0DAAkC;AAClC,wDAAgC;AAEhC,kBAAe;IACb,KAAK,EAAE,wBAAwB;CAChC,CAAC;AAEK,MAAM,IAAI,GAAG,GAAiB,EAAE;IACrC,OAAO,8BAAC,sBAAY,IAAC,KAAK,EAAE,cAAI,GAAI,CAAC;AACvC,CAAC,CAAC;AAFW,QAAA,IAAI,QAEf;AAEK,MAAM,IAAI,GAAG,GAAiB,EAAE;IACrC,OAAO,8BAAC,sBAAY,IAAC,KAAK,EAAE,cAAI,GAAI,CAAC;AACvC,CAAC,CAAC;AAFW,QAAA,IAAI,QAEf;AAEK,MAAM,GAAG,GAAG,GAAiB,EAAE;IACpC,OAAO,8BAAC,sBAAY,IAAC,KAAK,EAAE,aAAG,GAAI,CAAC;AACtC,CAAC,CAAC;AAFW,QAAA,GAAG,OAEd"}
1
+ {"version":3,"file":"index.story.js","sourceRoot":"","sources":["../../src/stories/index.story.tsx"],"names":[],"mappings":";;;;;;AAAA,kDAA4C;AAE5C,kEAA0C;AAE1C,0DAAkC;AAClC,0DAAkC;AAClC,wDAAgC;AAEhC,MAAM,IAAI,GAA8B;IACtC,KAAK,EAAE,wBAAwB;IAC/B,SAAS,EAAE,sBAAY;CACxB,CAAC;AAEF,kBAAe,IAAI,CAAC;AAEb,MAAM,IAAI,GAAG,GAAiB,EAAE;IACrC,OAAO,8BAAC,sBAAY,IAAC,KAAK,EAAE,cAAI,GAAI,CAAC;AACvC,CAAC,CAAC;AAFW,QAAA,IAAI,QAEf;AAEK,MAAM,IAAI,GAAG,GAAiB,EAAE;IACrC,OAAO,8BAAC,sBAAY,IAAC,KAAK,EAAE,cAAI,GAAI,CAAC;AACvC,CAAC,CAAC;AAFW,QAAA,IAAI,QAEf;AAEK,MAAM,GAAG,GAAG,GAAiB,EAAE;IACpC,OAAO,8BAAC,sBAAY,IAAC,KAAK,EAAE,aAAG,GAAI,CAAC;AACtC,CAAC,CAAC;AAFW,QAAA,GAAG,OAEd"}
@@ -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/utils/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/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.2",
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.4.0",
13
- "@opentripplanner/core-utils": "^11.2.3"
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
@@ -1,5 +1,7 @@
1
1
  import blue from "./colors/blue";
2
2
  import red from "./colors/red";
3
3
  import grey from "./colors/grey";
4
+ import Dropdown from "./dropdown";
4
5
 
5
- export { blue, red, grey };
6
+ export { Dropdown };
7
+ export default { blue, red, grey };
@@ -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
+ );
@@ -1,14 +1,18 @@
1
1
  import React, { ReactElement } from "react";
2
+ import { Meta } from "@storybook/react";
2
3
  import ColorPalette from "./ColorPalette";
3
4
 
4
5
  import grey from "../colors/grey";
5
6
  import blue from "../colors/blue";
6
7
  import red from "../colors/red";
7
8
 
8
- export default {
9
- title: "Building-Blocks/Colors"
9
+ const meta: Meta<typeof ColorPalette> = {
10
+ title: "Building-Blocks/Colors",
11
+ component: ColorPalette
10
12
  };
11
13
 
14
+ export default meta;
15
+
12
16
  export const Grey = (): ReactElement => {
13
17
  return <ColorPalette color={grey} />;
14
18
  };
@@ -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
+ }