@govtechsg/sgds-web-component 0.0.7 → 0.0.10

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 (263) hide show
  1. package/.github/workflows/publish-latest.yml +22 -0
  2. package/.github/workflows/publish-pr.yml +28 -0
  3. package/.husky/commit-msg +4 -0
  4. package/.husky/prepare-commit-msg +8 -0
  5. package/.storybook/main.js +16 -0
  6. package/.storybook/preview-head.html +11 -0
  7. package/.storybook/preview.js +9 -0
  8. package/.vscode/settings.json +7 -0
  9. package/CONTRIBUTING.md +56 -0
  10. package/LICENSE +20 -0
  11. package/amplify.yml +22 -0
  12. package/commitlint.config.js +1 -0
  13. package/coverage/lcov-report/base.css +224 -0
  14. package/coverage/lcov-report/block-navigation.js +87 -0
  15. package/coverage/lcov-report/button-element.scss.html +112 -0
  16. package/coverage/lcov-report/button-element.ts.html +145 -0
  17. package/coverage/lcov-report/favicon.png +0 -0
  18. package/coverage/lcov-report/index.html +116 -0
  19. package/coverage/lcov-report/prettify.css +1 -0
  20. package/coverage/lcov-report/prettify.js +2 -0
  21. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  22. package/coverage/lcov-report/sorter.js +196 -0
  23. package/coverage/lcov.info +32 -0
  24. package/index.html +430 -0
  25. package/{Button → lib/Button}/index.d.ts +0 -0
  26. package/{Button → lib/Button}/index.js +304 -39
  27. package/lib/Button/index.js.map +1 -0
  28. package/{Button → lib/Button}/package.json +0 -0
  29. package/lib/Button/sgds-button.d.ts +48 -0
  30. package/lib/Card/index.d.ts +1 -0
  31. package/lib/Card/index.js +6150 -0
  32. package/lib/Card/index.js.map +1 -0
  33. package/lib/Card/package.json +7 -0
  34. package/lib/Card/sgds-action-card.d.ts +20 -0
  35. package/lib/Checkbox/index.d.ts +1 -0
  36. package/lib/Checkbox/index.js +6366 -0
  37. package/lib/Checkbox/index.js.map +1 -0
  38. package/lib/Checkbox/package.json +7 -0
  39. package/lib/Checkbox/sgds-checkbox.d.ts +36 -0
  40. package/lib/Dropdown/index.d.ts +3 -0
  41. package/{Mainnav → lib/Dropdown}/index.js +2786 -9258
  42. package/lib/Dropdown/index.js.map +1 -0
  43. package/lib/Dropdown/package.json +7 -0
  44. package/lib/Dropdown/sgds-dropdown-item.d.ts +7 -0
  45. package/lib/Dropdown/sgds-dropdown.d.ts +7 -0
  46. package/{Footer → lib/Footer}/index.d.ts +0 -0
  47. package/{Footer → lib/Footer}/index.js +111 -95
  48. package/lib/Footer/index.js.map +1 -0
  49. package/{Footer → lib/Footer}/package.json +0 -0
  50. package/{Footer → lib/Footer}/sgds-footer.d.ts +2 -2
  51. package/lib/Input/index.d.ts +1 -0
  52. package/lib/Input/index.js +6656 -0
  53. package/lib/Input/index.js.map +1 -0
  54. package/lib/Input/package.json +7 -0
  55. package/lib/Input/sgds-input.d.ts +42 -0
  56. package/{Mainnav → lib/Mainnav}/index.d.ts +1 -0
  57. package/{index.js → lib/Mainnav/index.js} +3876 -23415
  58. package/lib/Mainnav/index.js.map +1 -0
  59. package/{Mainnav → lib/Mainnav}/package.json +0 -0
  60. package/lib/Mainnav/sgds-mainnav-dropdown.d.ts +5 -0
  61. package/lib/Mainnav/sgds-mainnav-item.d.ts +4 -0
  62. package/{Mainnav → lib/Mainnav}/sgds-mainnav.d.ts +3 -2
  63. package/{Masthead → lib/Masthead}/index.d.ts +0 -0
  64. package/{Masthead → lib/Masthead}/index.js +140 -114
  65. package/lib/Masthead/index.js.map +1 -0
  66. package/{Masthead → lib/Masthead}/package.json +0 -0
  67. package/{Masthead → lib/Masthead}/sgds-masthead.d.ts +1 -1
  68. package/lib/Modal/index.d.ts +1 -0
  69. package/lib/Modal/index.js +6432 -0
  70. package/lib/Modal/index.js.map +1 -0
  71. package/lib/Modal/package.json +7 -0
  72. package/lib/Modal/sgds-modal.d.ts +28 -0
  73. package/lib/QuantityToggle/index.d.ts +1 -0
  74. package/lib/QuantityToggle/index.js +7049 -0
  75. package/lib/QuantityToggle/index.js.map +1 -0
  76. package/lib/QuantityToggle/package.json +7 -0
  77. package/lib/QuantityToggle/sgds-quantitytoggle.d.ts +30 -0
  78. package/lib/Radio/index.d.ts +2 -0
  79. package/lib/Radio/index.js +12607 -0
  80. package/lib/Radio/index.js.map +1 -0
  81. package/lib/Radio/package.json +7 -0
  82. package/lib/Radio/sgds-radio.d.ts +31 -0
  83. package/lib/Radio/sgds-radiogroup.d.ts +41 -0
  84. package/{Sidenav → lib/Sidenav}/index.d.ts +0 -0
  85. package/{Sidenav → lib/Sidenav}/index.js +2266 -2171
  86. package/lib/Sidenav/index.js.map +1 -0
  87. package/{Sidenav → lib/Sidenav}/package.json +0 -0
  88. package/{Sidenav → lib/Sidenav}/sgds-sidenav-item.d.ts +2 -1
  89. package/lib/Sidenav/sgds-sidenav-link.d.ts +4 -0
  90. package/{Sidenav → lib/Sidenav}/sgds-sidenav.d.ts +1 -1
  91. package/lib/Tab/index.d.ts +3 -0
  92. package/lib/Tab/index.js +13557 -0
  93. package/lib/Tab/index.js.map +1 -0
  94. package/lib/Tab/package.json +7 -0
  95. package/lib/Tab/sgds-tab.d.ts +26 -0
  96. package/lib/Tab/sgds-tabgroup.d.ts +47 -0
  97. package/lib/Tab/sgds-tabpanel.d.ts +25 -0
  98. package/lib/Textarea/index.d.ts +1 -0
  99. package/lib/Textarea/index.js +6696 -0
  100. package/lib/Textarea/index.js.map +1 -0
  101. package/lib/Textarea/package.json +7 -0
  102. package/lib/Textarea/sgds-textarea.d.ts +53 -0
  103. package/lib/index.d.ts +16 -0
  104. package/lib/index.js +134580 -0
  105. package/lib/index.js.map +1 -0
  106. package/lib/umd/index.js +134587 -0
  107. package/lib/umd/index.js.map +1 -0
  108. package/lib/utils/animate.d.ts +10 -0
  109. package/lib/utils/animation-registry.d.ts +18 -0
  110. package/{utils → lib/utils}/breakpoints.d.ts +0 -0
  111. package/lib/utils/card-element.d.ts +11 -0
  112. package/lib/utils/defaultvalue.d.ts +2 -0
  113. package/lib/utils/dropdown-element.d.ts +37 -0
  114. package/lib/utils/event.d.ts +2 -0
  115. package/lib/utils/form.d.ts +38 -0
  116. package/{utils → lib/utils}/generateId.d.ts +0 -0
  117. package/lib/utils/link-element.d.ts +7 -0
  118. package/lib/utils/mergeDeep.d.ts +2 -0
  119. package/lib/utils/modal.d.ts +12 -0
  120. package/lib/utils/object.d.ts +2 -0
  121. package/lib/utils/offset.d.ts +4 -0
  122. package/lib/utils/scroll.d.ts +13 -0
  123. package/{utils → lib/utils}/sgds-element.d.ts +0 -0
  124. package/lib/utils/slot.d.ts +22 -0
  125. package/lib/utils/tabbable.d.ts +8 -0
  126. package/lib/utils/watch.d.ts +14 -0
  127. package/mocks/dropdown.d.ts +4 -0
  128. package/mocks/dropdown.ts +27 -0
  129. package/mocks/link.d.ts +3 -0
  130. package/mocks/link.ts +6 -0
  131. package/package.json +65 -10
  132. package/rollup.config.js +73 -0
  133. package/rollup.test.config.js +42 -0
  134. package/scripts/buildUtils.js +30 -0
  135. package/scripts/frankBuild.js +49 -0
  136. package/src/Button/index.ts +1 -0
  137. package/src/Button/sgds-button.scss +28 -0
  138. package/src/Button/sgds-button.ts +153 -0
  139. package/src/Card/index.ts +1 -0
  140. package/src/Card/sgds-action-card.scss +27 -0
  141. package/src/Card/sgds-action-card.ts +115 -0
  142. package/src/Checkbox/index.ts +1 -0
  143. package/src/Checkbox/sgds-checkbox.scss +4 -0
  144. package/src/Checkbox/sgds-checkbox.ts +149 -0
  145. package/src/Dropdown/index.ts +3 -0
  146. package/src/Dropdown/sgds-dropdown-item.ts +39 -0
  147. package/src/Dropdown/sgds-dropdown.scss +5 -0
  148. package/src/Dropdown/sgds-dropdown.ts +54 -0
  149. package/src/Footer/index.ts +3 -0
  150. package/src/Footer/sgds-footer.scss +5 -0
  151. package/src/Footer/sgds-footer.ts +121 -0
  152. package/src/Input/index.ts +1 -0
  153. package/src/Input/sgds-input.scss +20 -0
  154. package/src/Input/sgds-input.ts +178 -0
  155. package/src/Mainnav/index.ts +4 -0
  156. package/src/Mainnav/sgds-mainnav-dropdown.scss +13 -0
  157. package/src/Mainnav/sgds-mainnav-dropdown.ts +45 -0
  158. package/src/Mainnav/sgds-mainnav-item.scss +24 -0
  159. package/src/Mainnav/sgds-mainnav-item.ts +8 -0
  160. package/src/Mainnav/sgds-mainnav.scss +39 -0
  161. package/src/Mainnav/sgds-mainnav.ts +183 -0
  162. package/src/Masthead/index.ts +1 -0
  163. package/src/Masthead/sgds-masthead.scss +217 -0
  164. package/src/Masthead/sgds-masthead.ts +189 -0
  165. package/src/Modal/index.ts +1 -0
  166. package/src/Modal/sgds-modal.scss +128 -0
  167. package/src/Modal/sgds-modal.ts +309 -0
  168. package/src/QuantityToggle/index.ts +1 -0
  169. package/src/QuantityToggle/sgds-quantitytoggle.scss +10 -0
  170. package/src/QuantityToggle/sgds-quantitytoggle.ts +130 -0
  171. package/src/Radio/index.ts +2 -0
  172. package/src/Radio/sgds-radio.scss +5 -0
  173. package/src/Radio/sgds-radio.ts +120 -0
  174. package/src/Radio/sgds-radiogroup.scss +22 -0
  175. package/src/Radio/sgds-radiogroup.ts +221 -0
  176. package/src/Sidenav/index.ts +4 -0
  177. package/src/Sidenav/sgds-sidenav-item.scss +73 -0
  178. package/src/Sidenav/sgds-sidenav-item.ts +145 -0
  179. package/src/Sidenav/sgds-sidenav-link.scss +25 -0
  180. package/src/Sidenav/sgds-sidenav-link.ts +8 -0
  181. package/src/Sidenav/sgds-sidenav.scss +6 -0
  182. package/src/Sidenav/sgds-sidenav.ts +33 -0
  183. package/src/Tab/index.ts +3 -0
  184. package/src/Tab/sgds-tab.scss +84 -0
  185. package/src/Tab/sgds-tab.ts +87 -0
  186. package/src/Tab/sgds-tabgroup.scss +198 -0
  187. package/src/Tab/sgds-tabgroup.ts +295 -0
  188. package/src/Tab/sgds-tabpanel.scss +12 -0
  189. package/src/Tab/sgds-tabpanel.ts +55 -0
  190. package/src/Textarea/index.ts +1 -0
  191. package/src/Textarea/sgds-textarea.scss +23 -0
  192. package/src/Textarea/sgds-textarea.ts +201 -0
  193. package/src/index.ts +16 -0
  194. package/src/utils/animate.ts +69 -0
  195. package/src/utils/animation-registry.ts +71 -0
  196. package/src/utils/base.scss +14 -0
  197. package/src/utils/breakpoints.ts +5 -0
  198. package/src/utils/card-element.ts +42 -0
  199. package/src/utils/components.style.scss +531 -0
  200. package/src/utils/defaultvalue.ts +51 -0
  201. package/src/utils/dropdown-element.ts +244 -0
  202. package/src/utils/event.ts +13 -0
  203. package/src/utils/form.ts +183 -0
  204. package/src/utils/generateId.ts +4 -0
  205. package/src/utils/link-element.ts +34 -0
  206. package/src/utils/mergeDeep.ts +22 -0
  207. package/src/utils/modal.ts +64 -0
  208. package/src/utils/object.ts +2 -0
  209. package/src/utils/offset.ts +6 -0
  210. package/src/utils/scroll.ts +57 -0
  211. package/src/utils/sgds-element.ts +18 -0
  212. package/src/utils/slot.ts +102 -0
  213. package/src/utils/tabbable.ts +81 -0
  214. package/src/utils/watch.ts +62 -0
  215. package/stories/ActionCard.stories.mdx +199 -0
  216. package/stories/Button.stories.mdx +194 -0
  217. package/stories/Checkbox.stories.mdx +196 -0
  218. package/stories/Dropdown.stories.mdx +152 -0
  219. package/stories/Footer.stories.mdx +261 -0
  220. package/stories/Input.stories.mdx +236 -0
  221. package/stories/MainNav.stories.mdx +169 -0
  222. package/stories/Masthead.stories.mdx +112 -0
  223. package/stories/Modal.stories.mdx +103 -0
  224. package/stories/QuantityToggle.stories.mdx +97 -0
  225. package/stories/Radio.stories.mdx +262 -0
  226. package/stories/Sample.stories.js +29 -0
  227. package/stories/Sample.stories.mdx +33 -0
  228. package/stories/SideNav.stories.mdx +245 -0
  229. package/stories/common.js +185 -0
  230. package/stories/textarea.stories.mdx +253 -0
  231. package/test/button.element.test.ts +185 -0
  232. package/test/checkbox.test.ts +240 -0
  233. package/test/dropdown.test.ts +637 -0
  234. package/test/footer.test.ts +181 -0
  235. package/test/generateId.test.ts +18 -0
  236. package/test/input.element.test.ts +316 -0
  237. package/test/link-element.test.ts +38 -0
  238. package/test/mainnav.test.ts +313 -0
  239. package/test/masthead.test.ts +116 -0
  240. package/test/modal.test.ts +149 -0
  241. package/test/quantitytoggle.test.ts +76 -0
  242. package/test/radio.test.ts +310 -0
  243. package/test/selectable-card.test.ts +159 -0
  244. package/test/sidenav.test.ts +390 -0
  245. package/test/tab.test.ts +76 -0
  246. package/test/textarea.test.ts +126 -0
  247. package/tsconfig.json +26 -0
  248. package/tsconfig.test.json +24 -0
  249. package/typings/scss.d.ts +5 -0
  250. package/web-dev-server.config.mjs +7 -0
  251. package/web-test-runner.config.mjs +47 -0
  252. package/Button/index.js.map +0 -1
  253. package/Button/sgds-button.d.ts +0 -23
  254. package/Footer/index.js.map +0 -1
  255. package/Mainnav/index.js.map +0 -1
  256. package/Mainnav/sgds-mainnav-item.d.ts +0 -7
  257. package/Masthead/index.js.map +0 -1
  258. package/Sidenav/index.js.map +0 -1
  259. package/Sidenav/sgds-sidenav-link.d.ts +0 -7
  260. package/index.d.ts +0 -5
  261. package/index.js.map +0 -1
  262. package/umd/index.js +0 -52092
  263. package/umd/index.js.map +0 -1
@@ -0,0 +1,244 @@
1
+ import { property, state } from "lit/decorators.js";
2
+ import { Dropdown } from "bootstrap";
3
+ import * as Popper from "@popperjs/core";
4
+ import type { StrictModifiers } from "@popperjs/core";
5
+ import { createRef, Ref } from "lit/directives/ref.js";
6
+ import mergeDeep from "./mergeDeep";
7
+ import genId from "./generateId";
8
+ import { SgdsDropdownItem } from "../Dropdown/sgds-dropdown-item";
9
+ import SgdsElement from "./sgds-element";
10
+
11
+ const ARROW_DOWN = "ArrowDown";
12
+ const ARROW_UP = "ArrowUp";
13
+ const ESC = "Escape";
14
+ const ENTER = "Enter";
15
+ const TAB = "Tab";
16
+
17
+ export type DropdownButtonVariant =
18
+ | "primary"
19
+ | "secondary"
20
+ | "success"
21
+ | "danger"
22
+ | "warning"
23
+ | "info"
24
+ | "light"
25
+ | "dark";
26
+ export type DropDirection = "left" | "right" | "up" | "down";
27
+
28
+ export class DropdownElement extends SgdsElement {
29
+ myDropdown: Ref<HTMLElement> = createRef();
30
+ bsDropdown: Dropdown = null;
31
+
32
+ @property({ type: Boolean, reflect: true })
33
+ noFlip = false;
34
+ @property({ type: Boolean, reflect: true })
35
+ menuAlignRight = false;
36
+ @property({ type: String, reflect: true })
37
+ drop: DropDirection = "down";
38
+ @property({ type: Object })
39
+ popperOpts = {};
40
+ @property({ type: String })
41
+ togglerId = genId("dropdown", "button");
42
+
43
+ @property({ type: String })
44
+ togglerText = "";
45
+ @property({ type: String })
46
+ variant: DropdownButtonVariant = "secondary";
47
+
48
+ @property({ type: Boolean })
49
+ menuIsOpen = false;
50
+
51
+ @property({ type: String })
52
+ close: "outside" | "default" | "inside" = "default";
53
+
54
+ @property({ type: Boolean, reflect: true })
55
+ disabled = false;
56
+
57
+ @state()
58
+ nextDropdownItemNo: number = 0;
59
+ @state()
60
+ prevDropdownItemNo: number = -1;
61
+ @state()
62
+ dropdownConfig: Partial<Popper.Options>;
63
+ @state()
64
+ modifierOpt: StrictModifiers[] = [];
65
+
66
+ firstUpdated() {
67
+ this.bsDropdown = new Dropdown(this.myDropdown.value, {
68
+ // autoClose not working as bootstrap is using attribute data-bs-toggle="dropdown" to configure autoclose. But it doesnt look into this attribute in the shadow dom
69
+ reference: "toggle", // working
70
+ popperConfig: (defaultConfig?: Partial<Popper.Options>) => {
71
+ //working
72
+ this.dropdownConfig = {
73
+ placement: "bottom-start",
74
+ modifiers: !this.noFlip
75
+ ? this.modifierOpt
76
+ : [
77
+ ...this.modifierOpt,
78
+ {
79
+ name: "flip",
80
+ options: { fallbackPlacements: [] },
81
+ },
82
+ ],
83
+ };
84
+
85
+ switch (this.drop) {
86
+ case "up":
87
+ this.dropdownConfig.placement = this.menuAlignRight
88
+ ? "top-end"
89
+ : "top-start";
90
+ break;
91
+ case "right":
92
+ this.dropdownConfig.placement = "right-start";
93
+ break;
94
+ case "left":
95
+ this.dropdownConfig.placement = "left-start";
96
+ break;
97
+ case "down":
98
+ this.dropdownConfig.placement = this.menuAlignRight
99
+ ? "bottom-end"
100
+ : "bottom-start";
101
+ break;
102
+ default:
103
+ this.dropdownConfig.placement = undefined;
104
+ break;
105
+ }
106
+ return mergeDeep(
107
+ defaultConfig,
108
+ mergeDeep(this.dropdownConfig, this.popperOpts)
109
+ );
110
+ },
111
+ });
112
+ this.myDropdown.value.addEventListener("show.bs.dropdown", () => {
113
+ this.menuIsOpen = true;
114
+ this.emit("sgds-show");
115
+ });
116
+ this.myDropdown.value.addEventListener("shown.bs.dropdown", () => {
117
+ this.menuIsOpen = true;
118
+ this.emit("sgds-shown");
119
+ });
120
+ this.myDropdown.value.addEventListener("hide.bs.dropdown", () => {
121
+ this.menuIsOpen = false;
122
+ this._resetMenu();
123
+ this.emit("sgds-hide");
124
+ });
125
+ this.myDropdown.value.addEventListener("hidden.bs.dropdown", () => {
126
+ this.menuIsOpen = false;
127
+ this.emit("sgds-hidden");
128
+ });
129
+
130
+ this.addEventListener("keydown", this._handleKeyboardEvent);
131
+ if (this.close !== "inside") {
132
+ this.addEventListener("blur", (e) => {
133
+ return e.relatedTarget == null
134
+ ? this.bsDropdown.hide()
135
+ : e.stopPropagation();
136
+ });
137
+ addEventListener("click", (e) => this._handleClickOutOfElement(e, this));
138
+ }
139
+ if (this.menuIsOpen) this.bsDropdown.show();
140
+ }
141
+
142
+ public showMenu() {
143
+ this.bsDropdown.show();
144
+ }
145
+ public hideMenu() {
146
+ this.bsDropdown.hide();
147
+ }
148
+
149
+ _onClickButton() {
150
+ this.bsDropdown.toggle();
151
+ }
152
+
153
+ _resetMenu() {
154
+ this.nextDropdownItemNo = 0;
155
+ this.prevDropdownItemNo = -1;
156
+ // reset the tabindex
157
+ const items = this._getMenuItems();
158
+ items.forEach((i) => {
159
+ i.removeAttribute("tabindex");
160
+ });
161
+ }
162
+
163
+ _getMenuItems(): SgdsDropdownItem[] {
164
+ return this.shadowRoot
165
+ .querySelector("slot")
166
+ .assignedElements({ flatten: true }) as SgdsDropdownItem[];
167
+ }
168
+
169
+ _getActiveMenuItems(): SgdsDropdownItem[] {
170
+ return this._getMenuItems().filter((item) => !item.disabled);
171
+ }
172
+
173
+ _setMenuItem(currentItemIdx: number, isArrowDown: boolean = true) {
174
+ const items = this._getActiveMenuItems();
175
+ if (items.length === 0) return;
176
+ let item = items[currentItemIdx];
177
+ this.nextDropdownItemNo = currentItemIdx + 1;
178
+ this.prevDropdownItemNo =
179
+ currentItemIdx - 1 < 0 ? items.length - 1 : currentItemIdx - 1;
180
+ let activeItem: SgdsDropdownItem;
181
+ if (item.disabled) {
182
+ return this._setMenuItem(
183
+ isArrowDown ? this.nextDropdownItemNo : this.prevDropdownItemNo
184
+ );
185
+ } else activeItem = item;
186
+
187
+ // focus or blur items depending on active or not
188
+ items.forEach((i) => {
189
+ i.setAttribute("tabindex", i === activeItem ? "0" : "-1");
190
+ i === activeItem ? i.focus() : i.blur();
191
+ });
192
+ }
193
+
194
+ _handleSelectSlot(e: KeyboardEvent | MouseEvent) {
195
+ const items = this._getActiveMenuItems();
196
+ const currentItemNo = items.indexOf(e.target as SgdsDropdownItem);
197
+ this.nextDropdownItemNo = currentItemNo + 1;
198
+ this.prevDropdownItemNo =
199
+ currentItemNo <= 0 ? items.length - 1 : currentItemNo - 1;
200
+
201
+ // assign selected dropdown-item value to sgds-dropdown value
202
+ const selectedItem = e.target as SgdsDropdownItem;
203
+ if (!selectedItem.disabled) {
204
+ this.emit("sgds-select");
205
+ this.close !== "outside" && this.bsDropdown.hide();
206
+ } else return;
207
+ }
208
+ _handleKeyboardEvent(e: KeyboardEvent) {
209
+ const menuItems = this._getActiveMenuItems();
210
+ switch (e.key) {
211
+ case ARROW_DOWN:
212
+ if (!this.menuIsOpen) return this.bsDropdown.show();
213
+ if (this.nextDropdownItemNo === menuItems.length) {
214
+ return this._setMenuItem(0);
215
+ } else {
216
+ return this._setMenuItem(
217
+ this.nextDropdownItemNo > 0 ? this.nextDropdownItemNo : 0
218
+ );
219
+ }
220
+ case ARROW_UP:
221
+ if (!this.menuIsOpen) return this.bsDropdown.show();
222
+ if (this.prevDropdownItemNo < 0) {
223
+ return this._setMenuItem(menuItems.length - 1, false);
224
+ } else {
225
+ return this._setMenuItem(this.prevDropdownItemNo, false);
226
+ }
227
+ case ESC:
228
+ return this.bsDropdown.hide();
229
+ case ENTER:
230
+ if (menuItems.includes(e.target as SgdsDropdownItem)) {
231
+ return this._handleSelectSlot(e);
232
+ }
233
+ break;
234
+ default:
235
+ break;
236
+ }
237
+ }
238
+
239
+ _handleClickOutOfElement(e: MouseEvent, self: DropdownElement) {
240
+ if (!e.composedPath().includes(self)) {
241
+ this.bsDropdown.hide();
242
+ }
243
+ }
244
+ }
@@ -0,0 +1,13 @@
1
+ /** Waits for a specific event to be emitted from an element. Ignores events that bubble up from child elements. */
2
+ export function waitForEvent(el: HTMLElement, eventName: string) {
3
+ return new Promise<void>(resolve => {
4
+ function done(event: Event) {
5
+ if (event.target === el) {
6
+ el.removeEventListener(eventName, done);
7
+ resolve();
8
+ }
9
+ }
10
+
11
+ el.addEventListener(eventName, done);
12
+ });
13
+ }
@@ -0,0 +1,183 @@
1
+ import type SgdsButton from '../Button/sgds-button';
2
+ import type { ReactiveController, ReactiveControllerHost } from 'lit';
3
+
4
+ const reportValidityOverloads: WeakMap<HTMLFormElement, () => boolean> = new WeakMap();
5
+
6
+ export interface FormSubmitControllerOptions {
7
+ /** A function that returns the form containing the form control. */
8
+ form: (input: unknown) => HTMLFormElement | null;
9
+ /** A function that returns the form control's name, which will be submitted with the form data. */
10
+ name: (input: unknown) => string;
11
+ /** A function that returns the form control's current value. */
12
+ value: (input: unknown) => unknown | unknown[];
13
+ /** A function that returns the form control's default value. */
14
+ defaultValue: (input: unknown) => unknown | unknown[];
15
+ /** A function that returns the form control's current disabled state. If disabled, the value won't be submitted. */
16
+ disabled: (input: unknown) => boolean;
17
+ /**
18
+ * A function that maps to the form control's reportValidity() function. When the control is invalid, this will
19
+ * prevent submission and trigger the browser's constraint violation warning.
20
+ */
21
+ reportValidity: (input: unknown) => boolean;
22
+
23
+ /** A function that sets the form control's value */
24
+ setValue: (input: unknown, value: unknown) => void;
25
+ }
26
+
27
+ export class FormSubmitController implements ReactiveController {
28
+ host?: ReactiveControllerHost & Element;
29
+ form?: HTMLFormElement | null;
30
+ options: FormSubmitControllerOptions;
31
+
32
+ constructor(host: ReactiveControllerHost & Element, options?: Partial<FormSubmitControllerOptions>) {
33
+ (this.host = host).addController(this);
34
+ this.options = {
35
+ form: (input: HTMLInputElement) => input.closest('form'),
36
+ name: (input: HTMLInputElement) => input.name,
37
+ value: (input: HTMLInputElement) => input.value,
38
+ defaultValue: (input: HTMLInputElement) => input.defaultValue,
39
+ disabled: (input: HTMLInputElement) => input.disabled,
40
+ reportValidity: (input: HTMLInputElement) => {
41
+ return typeof input.reportValidity === 'function' ? input.reportValidity() : true;
42
+ },
43
+ setValue: (input: HTMLInputElement, value: string) => {
44
+ input.value = value;
45
+ },
46
+ ...options
47
+ };
48
+ this.handleFormData = this.handleFormData.bind(this);
49
+ this.handleFormSubmit = this.handleFormSubmit.bind(this);
50
+ this.handleFormReset = this.handleFormReset.bind(this);
51
+ this.reportFormValidity = this.reportFormValidity.bind(this);
52
+ }
53
+
54
+ hostConnected() {
55
+ this.form = this.options.form(this.host);
56
+
57
+ if (this.form) {
58
+ this.form.addEventListener('formdata', this.handleFormData);
59
+ this.form.addEventListener('submit', this.handleFormSubmit);
60
+ this.form.addEventListener('reset', this.handleFormReset);
61
+
62
+ // Overload the form's reportValidity() method so it looks at Shoelace form controls
63
+ if (!reportValidityOverloads.has(this.form)) {
64
+ reportValidityOverloads.set(this.form, this.form.reportValidity);
65
+ this.form.reportValidity = () => this.reportFormValidity();
66
+ }
67
+ }
68
+ }
69
+
70
+ hostDisconnected() {
71
+ if (this.form) {
72
+ this.form.removeEventListener('formdata', this.handleFormData);
73
+ this.form.removeEventListener('submit', this.handleFormSubmit);
74
+ this.form.removeEventListener('reset', this.handleFormReset);
75
+
76
+ // Remove the overload and restore the original method
77
+ if (reportValidityOverloads.has(this.form)) {
78
+ this.form.reportValidity = reportValidityOverloads.get(this.form)!;
79
+ reportValidityOverloads.delete(this.form);
80
+ }
81
+
82
+ this.form = undefined;
83
+ }
84
+ }
85
+
86
+ handleFormData(event: FormDataEvent) {
87
+ const disabled = this.options.disabled(this.host);
88
+ const name = this.options.name(this.host);
89
+ const value = this.options.value(this.host);
90
+
91
+ if (!disabled && typeof name === 'string' && typeof value !== 'undefined') {
92
+ if (Array.isArray(value)) {
93
+ (value as unknown[]).forEach(val => {
94
+ event.formData.append(name, (val as string | number | boolean).toString());
95
+ });
96
+ } else {
97
+ event.formData.append(name, (value as string | number | boolean).toString());
98
+ }
99
+ }
100
+ }
101
+
102
+ handleFormSubmit(event: Event) {
103
+ const disabled = this.options.disabled(this.host);
104
+ const reportValidity = this.options.reportValidity;
105
+
106
+ if (this.form && !this.form.noValidate && !disabled && !reportValidity(this.host)) {
107
+ event.preventDefault();
108
+ event.stopImmediatePropagation();
109
+ }
110
+ }
111
+
112
+ handleFormReset() {
113
+ this.options.setValue(this.host, this.options.defaultValue(this.host));
114
+ }
115
+
116
+ reportFormValidity() {
117
+ //
118
+ // Shoelace form controls work hard to act like regular form controls. They support the Constraint Validation API
119
+ // and its associated methods such as setCustomValidity() and reportValidity(). However, the HTMLFormElement also
120
+ // has a reportValidity() method that will trigger validation on all child controls. Since we're not yet using
121
+ // ElementInternals, we need to overload this method so it looks for any element with the reportValidity() method.
122
+ //
123
+ // We preserve the original method in a WeakMap, but we don't call it from the overload because that would trigger
124
+ // validations in an unexpected order. When the element disconnects, we revert to the original behavior. This won't
125
+ // be necessary once we can use ElementInternals.
126
+ //
127
+ // Note that we're also honoring the form's novalidate attribute.
128
+ //
129
+ if (this.form && !this.form.noValidate) {
130
+ // This seems sloppy, but checking all elements will cover native inputs, Shoelace inputs, and other custom
131
+ // elements that support the constraint validation API.
132
+ const elements = this.form.querySelectorAll<HTMLInputElement>('*');
133
+
134
+ for (const element of elements) {
135
+ if (typeof element.reportValidity === 'function') {
136
+ if (!element.reportValidity()) {
137
+ return false;
138
+ }
139
+ }
140
+ }
141
+ }
142
+
143
+ return true;
144
+ }
145
+
146
+ doAction(type: 'submit' | 'reset', invoker?: HTMLInputElement | SgdsButton) {
147
+ if (this.form) {
148
+ const button = document.createElement('button');
149
+ button.type = type;
150
+ button.style.position = 'absolute';
151
+ button.style.width = '0';
152
+ button.style.height = '0';
153
+ button.style.clipPath = 'inset(50%)';
154
+ button.style.overflow = 'hidden';
155
+ button.style.whiteSpace = 'nowrap';
156
+
157
+ // Pass form attributes through to the temporary button
158
+ if (invoker) {
159
+ ['formaction', 'formmethod', 'formnovalidate', 'formtarget'].forEach(attr => {
160
+ if (invoker.hasAttribute(attr)) {
161
+ button.setAttribute(attr, invoker.getAttribute(attr)!);
162
+ }
163
+ });
164
+ }
165
+
166
+ this.form.append(button);
167
+ button.click();
168
+ button.remove();
169
+ }
170
+ }
171
+
172
+ /** Resets the form, restoring all the control to their default value */
173
+ reset(invoker?: HTMLInputElement | SgdsButton) {
174
+ this.doAction('reset', invoker);
175
+ }
176
+
177
+ /** Submits the form, triggering validation and form data injection. */
178
+ submit(invoker?: HTMLInputElement | SgdsButton) {
179
+ // Calling form.submit() bypasses the submit event and constraint validation. To prevent this, we can inject a
180
+ // native submit button into the form, "click" it, then remove it to simulate a standard form submission.
181
+ this.doAction('submit', invoker);
182
+ }
183
+ }
@@ -0,0 +1,4 @@
1
+ export default function (componentName = '', elementName=''): string {
2
+ return `id-${Math.random().toString().substring(2, 6)}-sgds-${componentName}-${elementName}`
3
+ }
4
+
@@ -0,0 +1,34 @@
1
+ import { html } from "lit";
2
+ import SgdsElement from "../utils/sgds-element";
3
+ import { property } from "lit/decorators.js";
4
+ import { classMap } from "lit/directives/class-map.js";
5
+
6
+ export default class LinkElement extends SgdsElement {
7
+
8
+ @property({ type: Boolean })
9
+ active = false;
10
+
11
+ @property({ type: String })
12
+ href = "";
13
+
14
+ @property({ type: Boolean, reflect: true })
15
+ disabled = false;
16
+
17
+ render() {
18
+ return html`
19
+ <li>
20
+ <a
21
+ href="${this.href}"
22
+ class="nav-link ${classMap({
23
+ disabled: this.disabled,
24
+ active: this.active,
25
+ })} "
26
+ ?disabled=${this.disabled}
27
+ aria-disabled=${this.disabled ? "true" : "false"}
28
+ ><slot></slot
29
+ ></a>
30
+ </li>
31
+ `;
32
+ }
33
+ }
34
+
@@ -0,0 +1,22 @@
1
+ import {assign, keys} from './object'
2
+ function _isObject(item: any) {
3
+ return item && typeof item === 'object' && item.constructor === Object
4
+ }
5
+
6
+ const mergeDeep = (target:any, source:any) => {
7
+ if (_isObject(target) && _isObject(source)) {
8
+ keys(source).forEach(key => {
9
+ if (_isObject(source[key])) {
10
+ if (!target[key] || !_isObject(target[key])) {
11
+ target[key] = source[key]
12
+ }
13
+ mergeDeep(target[key], source[key])
14
+ } else {
15
+ assign(target, { [key]: source[key] })
16
+ }
17
+ })
18
+ }
19
+ return target
20
+ }
21
+
22
+ export default mergeDeep
@@ -0,0 +1,64 @@
1
+ import { getTabbableBoundary } from './tabbable';
2
+
3
+ let activeModals: HTMLElement[] = [];
4
+
5
+ export default class Modal {
6
+ element: HTMLElement;
7
+ tabDirection: 'forward' | 'backward' = 'forward';
8
+
9
+ constructor(element: HTMLElement) {
10
+ this.element = element;
11
+ this.handleFocusIn = this.handleFocusIn.bind(this);
12
+ this.handleKeyDown = this.handleKeyDown.bind(this);
13
+ this.handleKeyUp = this.handleKeyUp.bind(this);
14
+ }
15
+
16
+ activate() {
17
+ activeModals.push(this.element);
18
+ document.addEventListener('focusin', this.handleFocusIn);
19
+ document.addEventListener('keydown', this.handleKeyDown);
20
+ document.addEventListener('keyup', this.handleKeyUp);
21
+ }
22
+
23
+ deactivate() {
24
+ activeModals = activeModals.filter(modal => modal !== this.element);
25
+ document.removeEventListener('focusin', this.handleFocusIn);
26
+ document.removeEventListener('keydown', this.handleKeyDown);
27
+ document.removeEventListener('keyup', this.handleKeyUp);
28
+ }
29
+
30
+ isActive() {
31
+ // The "active" modal is always the most recent one shown
32
+ return activeModals[activeModals.length - 1] === this.element;
33
+ }
34
+
35
+ checkFocus() {
36
+ if (this.isActive()) {
37
+ if (!this.element.matches(':focus-within')) {
38
+ const { start, end } = getTabbableBoundary(this.element);
39
+ const target = this.tabDirection === 'forward' ? start : end;
40
+
41
+ if (typeof target?.focus === 'function') {
42
+ target.focus({ preventScroll: true });
43
+ }
44
+ }
45
+ }
46
+ }
47
+
48
+ handleFocusIn() {
49
+ this.checkFocus();
50
+ }
51
+
52
+ handleKeyDown(event: KeyboardEvent) {
53
+ if (event.key === 'Tab' && event.shiftKey) {
54
+ this.tabDirection = 'backward';
55
+
56
+ // Ensure focus remains trapped after the key is pressed
57
+ requestAnimationFrame(() => this.checkFocus());
58
+ }
59
+ }
60
+
61
+ handleKeyUp() {
62
+ this.tabDirection = 'forward';
63
+ }
64
+ }
@@ -0,0 +1,2 @@
1
+ export const assign = (target: any, ...args: any[]) => Object.assign(target, ...args)
2
+ export const keys = (obj:any) => Object.keys(obj)
@@ -0,0 +1,6 @@
1
+ export function getOffset(element: HTMLElement, parent: HTMLElement) {
2
+ return {
3
+ top: Math.round(element.getBoundingClientRect().top - parent.getBoundingClientRect().top),
4
+ left: Math.round(element.getBoundingClientRect().left - parent.getBoundingClientRect().left)
5
+ };
6
+ }
@@ -0,0 +1,57 @@
1
+ import { getOffset } from './offset';
2
+
3
+ const locks = new Set();
4
+
5
+ /**
6
+ * Prevents body scrolling. Keeps track of which elements requested a lock so multiple levels of locking are possible
7
+ * without premature unlocking.
8
+ */
9
+ export function lockBodyScrolling(lockingEl: HTMLElement) {
10
+ locks.add(lockingEl);
11
+ document.body.classList.add('sl-scroll-lock');
12
+ }
13
+
14
+ /**
15
+ * Unlocks body scrolling. Scrolling will only be unlocked once all elements that requested a lock call this method.
16
+ */
17
+ export function unlockBodyScrolling(lockingEl: HTMLElement) {
18
+ locks.delete(lockingEl);
19
+
20
+ if (locks.size === 0) {
21
+ document.body.classList.remove('sl-scroll-lock');
22
+ }
23
+ }
24
+
25
+ /**
26
+ * Scrolls an element into view of its container. If the element is already in view, nothing will happen.
27
+ */
28
+ export function scrollIntoView(
29
+ element: HTMLElement,
30
+ container: HTMLElement,
31
+ direction: 'horizontal' | 'vertical' | 'both' = 'vertical',
32
+ behavior: 'smooth' | 'auto' = 'smooth'
33
+ ) {
34
+ const offset = getOffset(element, container);
35
+ const offsetTop = offset.top + container.scrollTop;
36
+ const offsetLeft = offset.left + container.scrollLeft;
37
+ const minX = container.scrollLeft;
38
+ const maxX = container.scrollLeft + container.offsetWidth;
39
+ const minY = container.scrollTop;
40
+ const maxY = container.scrollTop + container.offsetHeight;
41
+
42
+ if (direction === 'horizontal' || direction === 'both') {
43
+ if (offsetLeft < minX) {
44
+ container.scrollTo({ left: offsetLeft, behavior });
45
+ } else if (offsetLeft + element.clientWidth > maxX) {
46
+ container.scrollTo({ left: offsetLeft - container.offsetWidth + element.clientWidth, behavior });
47
+ }
48
+ }
49
+
50
+ if (direction === 'vertical' || direction === 'both') {
51
+ if (offsetTop < minY) {
52
+ container.scrollTo({ top: offsetTop, behavior });
53
+ } else if (offsetTop + element.clientHeight > maxY) {
54
+ container.scrollTo({ top: offsetTop - container.offsetHeight + element.clientHeight, behavior });
55
+ }
56
+ }
57
+ }
@@ -0,0 +1,18 @@
1
+ import { LitElement } from 'lit';
2
+
3
+ export default class SgdsElement extends LitElement {
4
+ /** Emits a custom event with more convenient defaults. */
5
+ emit(name: string, options?: CustomEventInit) {
6
+ const event = new CustomEvent(name, {
7
+ bubbles: true,
8
+ cancelable: false,
9
+ composed: true,
10
+ detail: {},
11
+ ...options
12
+ });
13
+
14
+ this.dispatchEvent(event);
15
+
16
+ return event;
17
+ }
18
+ }