@noctuatech/uswds 0.0.1

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 (187) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +25 -0
  3. package/package.json +89 -0
  4. package/src/lib/alert/alert-types.ts +33 -0
  5. package/src/lib/alert/alert.element.ts +105 -0
  6. package/src/lib/alert/alert.stories.ts +63 -0
  7. package/src/lib/alert/alert.test.ts +23 -0
  8. package/src/lib/button/button.element.ts +224 -0
  9. package/src/lib/button/button.stories.ts +34 -0
  10. package/src/lib/button/button.test.ts +17 -0
  11. package/src/lib/checkbox/checkbox.element.ts +166 -0
  12. package/src/lib/checkbox/checkbox.stories.ts +57 -0
  13. package/src/lib/checkbox/checkbox.test.ts +47 -0
  14. package/src/lib/config/config.element.ts +31 -0
  15. package/src/lib/config/config.test.ts +15 -0
  16. package/src/lib/define.ts +14 -0
  17. package/src/lib/description/description.element.ts +22 -0
  18. package/src/lib/description/description.test.ts +15 -0
  19. package/src/lib/file-input/file-input-preview.element.ts +121 -0
  20. package/src/lib/file-input/file-input-preview.test.ts +95 -0
  21. package/src/lib/file-input/file-input.element.ts +140 -0
  22. package/src/lib/file-input/file-input.stories.ts +46 -0
  23. package/src/lib/file-input/file-input.test.ts +47 -0
  24. package/src/lib/icon/icon-types.ts +263 -0
  25. package/src/lib/icon/icon.element.ts +65 -0
  26. package/src/lib/icon/icon.stories.ts +50 -0
  27. package/src/lib/input/input.element.ts +138 -0
  28. package/src/lib/input/input.stories.ts +30 -0
  29. package/src/lib/input/input.test.ts +48 -0
  30. package/src/lib/input-mask/format.ts +56 -0
  31. package/src/lib/input-mask/input-mask.element.ts +93 -0
  32. package/src/lib/input-mask/input-mask.stories.ts +38 -0
  33. package/src/lib/input-mask/input-mask.test.ts +106 -0
  34. package/src/lib/input-mask/maskable.element.ts +5 -0
  35. package/src/lib/link/link.element.ts +62 -0
  36. package/src/lib/link/link.stories.ts +30 -0
  37. package/src/lib/radio/radio-option.element.ts +46 -0
  38. package/src/lib/radio/radio-option.test.ts +20 -0
  39. package/src/lib/radio/radio.element.ts +152 -0
  40. package/src/lib/radio/radio.stories.ts +47 -0
  41. package/src/lib/radio/radio.test.ts +174 -0
  42. package/src/lib/select/select-option.element.ts +40 -0
  43. package/src/lib/select/select.element.ts +121 -0
  44. package/src/lib/select/select.stories.ts +33 -0
  45. package/src/lib/select/select.test.ts +113 -0
  46. package/src/lib/tag/tag.element.ts +46 -0
  47. package/src/lib/tag/tag.stories.ts +31 -0
  48. package/src/lib/tag/tag.test.ts +15 -0
  49. package/src/lib.ts +13 -0
  50. package/target/lib/alert/alert-types.d.ts +7 -0
  51. package/target/lib/alert/alert-types.js +25 -0
  52. package/target/lib/alert/alert-types.js.map +1 -0
  53. package/target/lib/alert/alert.element.d.ts +11 -0
  54. package/target/lib/alert/alert.element.js +124 -0
  55. package/target/lib/alert/alert.element.js.map +1 -0
  56. package/target/lib/alert/alert.stories.d.ts +11 -0
  57. package/target/lib/alert/alert.stories.js +56 -0
  58. package/target/lib/alert/alert.stories.js.map +1 -0
  59. package/target/lib/alert/alert.test.d.ts +1 -0
  60. package/target/lib/alert/alert.test.js +20 -0
  61. package/target/lib/alert/alert.test.js.map +1 -0
  62. package/target/lib/button/button.element.d.ts +17 -0
  63. package/target/lib/button/button.element.js +259 -0
  64. package/target/lib/button/button.element.js.map +1 -0
  65. package/target/lib/button/button.stories.d.ts +12 -0
  66. package/target/lib/button/button.stories.js +25 -0
  67. package/target/lib/button/button.stories.js.map +1 -0
  68. package/target/lib/button/button.test.d.ts +1 -0
  69. package/target/lib/button/button.test.js +14 -0
  70. package/target/lib/button/button.test.js.map +1 -0
  71. package/target/lib/checkbox/checkbox.element.d.ts +16 -0
  72. package/target/lib/checkbox/checkbox.element.js +205 -0
  73. package/target/lib/checkbox/checkbox.element.js.map +1 -0
  74. package/target/lib/checkbox/checkbox.stories.d.ts +31 -0
  75. package/target/lib/checkbox/checkbox.stories.js +46 -0
  76. package/target/lib/checkbox/checkbox.stories.js.map +1 -0
  77. package/target/lib/checkbox/checkbox.test.d.ts +1 -0
  78. package/target/lib/checkbox/checkbox.test.js +38 -0
  79. package/target/lib/checkbox/checkbox.test.js.map +1 -0
  80. package/target/lib/config/config.element.d.ts +8 -0
  81. package/target/lib/config/config.element.js +57 -0
  82. package/target/lib/config/config.element.js.map +1 -0
  83. package/target/lib/config/config.test.d.ts +1 -0
  84. package/target/lib/config/config.test.js +11 -0
  85. package/target/lib/config/config.test.js.map +1 -0
  86. package/target/lib/define.d.ts +14 -0
  87. package/target/lib/define.js +15 -0
  88. package/target/lib/define.js.map +1 -0
  89. package/target/lib/description/description.element.d.ts +7 -0
  90. package/target/lib/description/description.element.js +34 -0
  91. package/target/lib/description/description.element.js.map +1 -0
  92. package/target/lib/description/description.test.d.ts +1 -0
  93. package/target/lib/description/description.test.js +11 -0
  94. package/target/lib/description/description.test.js.map +1 -0
  95. package/target/lib/file-input/file-input-preview.element.d.ts +11 -0
  96. package/target/lib/file-input/file-input-preview.element.js +136 -0
  97. package/target/lib/file-input/file-input-preview.element.js.map +1 -0
  98. package/target/lib/file-input/file-input-preview.test.d.ts +2 -0
  99. package/target/lib/file-input/file-input-preview.test.js +67 -0
  100. package/target/lib/file-input/file-input-preview.test.js.map +1 -0
  101. package/target/lib/file-input/file-input.element.d.ts +18 -0
  102. package/target/lib/file-input/file-input.element.js +180 -0
  103. package/target/lib/file-input/file-input.element.js.map +1 -0
  104. package/target/lib/file-input/file-input.stories.d.ts +12 -0
  105. package/target/lib/file-input/file-input.stories.js +36 -0
  106. package/target/lib/file-input/file-input.stories.js.map +1 -0
  107. package/target/lib/file-input/file-input.test.d.ts +1 -0
  108. package/target/lib/file-input/file-input.test.js +37 -0
  109. package/target/lib/file-input/file-input.test.js.map +1 -0
  110. package/target/lib/icon/icon-types.d.ts +2 -0
  111. package/target/lib/icon/icon-types.js +262 -0
  112. package/target/lib/icon/icon-types.js.map +1 -0
  113. package/target/lib/icon/icon.element.d.ts +12 -0
  114. package/target/lib/icon/icon.element.js +84 -0
  115. package/target/lib/icon/icon.element.js.map +1 -0
  116. package/target/lib/icon/icon.stories.d.ts +12 -0
  117. package/target/lib/icon/icon.stories.js +39 -0
  118. package/target/lib/icon/icon.stories.js.map +1 -0
  119. package/target/lib/input/input.element.d.ts +19 -0
  120. package/target/lib/input/input.element.js +166 -0
  121. package/target/lib/input/input.element.js.map +1 -0
  122. package/target/lib/input/input.stories.d.ts +12 -0
  123. package/target/lib/input/input.stories.js +23 -0
  124. package/target/lib/input/input.stories.js.map +1 -0
  125. package/target/lib/input/input.test.d.ts +1 -0
  126. package/target/lib/input/input.test.js +38 -0
  127. package/target/lib/input/input.test.js.map +1 -0
  128. package/target/lib/input-mask/format.d.ts +15 -0
  129. package/target/lib/input-mask/format.js +47 -0
  130. package/target/lib/input-mask/format.js.map +1 -0
  131. package/target/lib/input-mask/input-mask.element.d.ts +12 -0
  132. package/target/lib/input-mask/input-mask.element.js +111 -0
  133. package/target/lib/input-mask/input-mask.element.js.map +1 -0
  134. package/target/lib/input-mask/input-mask.stories.d.ts +14 -0
  135. package/target/lib/input-mask/input-mask.stories.js +31 -0
  136. package/target/lib/input-mask/input-mask.stories.js.map +1 -0
  137. package/target/lib/input-mask/input-mask.test.d.ts +2 -0
  138. package/target/lib/input-mask/input-mask.test.js +85 -0
  139. package/target/lib/input-mask/input-mask.test.js.map +1 -0
  140. package/target/lib/input-mask/maskable.element.d.ts +5 -0
  141. package/target/lib/input-mask/maskable.element.js +2 -0
  142. package/target/lib/input-mask/maskable.element.js.map +1 -0
  143. package/target/lib/link/link.element.d.ts +13 -0
  144. package/target/lib/link/link.element.js +98 -0
  145. package/target/lib/link/link.element.js.map +1 -0
  146. package/target/lib/link/link.stories.d.ts +16 -0
  147. package/target/lib/link/link.stories.js +23 -0
  148. package/target/lib/link/link.stories.js.map +1 -0
  149. package/target/lib/radio/radio-option.element.d.ts +13 -0
  150. package/target/lib/radio/radio-option.element.js +63 -0
  151. package/target/lib/radio/radio-option.element.js.map +1 -0
  152. package/target/lib/radio/radio-option.test.d.ts +2 -0
  153. package/target/lib/radio/radio-option.test.js +15 -0
  154. package/target/lib/radio/radio-option.test.js.map +1 -0
  155. package/target/lib/radio/radio.element.d.ts +18 -0
  156. package/target/lib/radio/radio.element.js +177 -0
  157. package/target/lib/radio/radio.element.js.map +1 -0
  158. package/target/lib/radio/radio.stories.d.ts +12 -0
  159. package/target/lib/radio/radio.stories.js +40 -0
  160. package/target/lib/radio/radio.stories.js.map +1 -0
  161. package/target/lib/radio/radio.test.d.ts +2 -0
  162. package/target/lib/radio/radio.test.js +147 -0
  163. package/target/lib/radio/radio.test.js.map +1 -0
  164. package/target/lib/select/select-option.element.d.ts +11 -0
  165. package/target/lib/select/select-option.element.js +58 -0
  166. package/target/lib/select/select-option.element.js.map +1 -0
  167. package/target/lib/select/select.element.d.ts +16 -0
  168. package/target/lib/select/select.element.js +144 -0
  169. package/target/lib/select/select.element.js.map +1 -0
  170. package/target/lib/select/select.stories.d.ts +12 -0
  171. package/target/lib/select/select.stories.js +26 -0
  172. package/target/lib/select/select.stories.js.map +1 -0
  173. package/target/lib/select/select.test.d.ts +2 -0
  174. package/target/lib/select/select.test.js +89 -0
  175. package/target/lib/select/select.test.js.map +1 -0
  176. package/target/lib/tag/tag.element.d.ts +10 -0
  177. package/target/lib/tag/tag.element.js +66 -0
  178. package/target/lib/tag/tag.element.js.map +1 -0
  179. package/target/lib/tag/tag.stories.d.ts +19 -0
  180. package/target/lib/tag/tag.stories.js +25 -0
  181. package/target/lib/tag/tag.stories.js.map +1 -0
  182. package/target/lib/tag/tag.test.d.ts +1 -0
  183. package/target/lib/tag/tag.test.js +11 -0
  184. package/target/lib/tag/tag.test.js.map +1 -0
  185. package/target/lib.d.ts +13 -0
  186. package/target/lib.js +14 -0
  187. package/target/lib.js.map +1 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Noctua Technology
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # USWDS
2
+
3
+ An implementaiton of the US Web Design System using web components.
4
+
5
+ https://designsystem.digital.gov/
6
+
7
+ ## Development
8
+
9
+ ### Install
10
+
11
+ ```sh
12
+ npm i
13
+ ```
14
+
15
+ ### Run Storybook
16
+
17
+ ```sh
18
+ npm run storybook
19
+ ```
20
+
21
+ ### Generate a new element
22
+
23
+ ```sh
24
+ npm run gen element
25
+ ```
package/package.json ADDED
@@ -0,0 +1,89 @@
1
+ {
2
+ "name": "@noctuatech/uswds",
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "workspaces": [
6
+ "packages/**"
7
+ ],
8
+ "main": "./target/lib.js",
9
+ "module": "./target/lib.js",
10
+ "exports": {
11
+ ".": "./target/lib.js",
12
+ "./*": "./target/lib/*"
13
+ },
14
+ "files": [
15
+ "src",
16
+ "target"
17
+ ],
18
+ "scripts": {
19
+ "test": "wireit",
20
+ "build": "wireit",
21
+ "storybook": "storybook dev -p 6006",
22
+ "build-storybook": "wireit",
23
+ "gen": "plop",
24
+ "prepare": "husky"
25
+ },
26
+ "wireit": {
27
+ "test": {
28
+ "command": "wtr",
29
+ "dependencies": [
30
+ "tsc"
31
+ ]
32
+ },
33
+ "build": {
34
+ "dependencies": [
35
+ "tsc",
36
+ "build-storybook"
37
+ ]
38
+ },
39
+ "build-storybook": {
40
+ "command": "storybook build"
41
+ },
42
+ "tsc": {
43
+ "command": "tsc --build --pretty",
44
+ "clean": "if-file-deleted",
45
+ "files": [
46
+ "src/**",
47
+ "tsconfig.json"
48
+ ],
49
+ "output": [
50
+ "target/**",
51
+ "tsconfig.tsbuildinfo"
52
+ ],
53
+ "dependencies": [
54
+ "./packages/testing:build"
55
+ ]
56
+ }
57
+ },
58
+ "author": "",
59
+ "license": "ISC",
60
+ "description": "",
61
+ "dependencies": {
62
+ "@joist/di": "^4.0.0-next.26",
63
+ "@joist/element": "^4.0.0-next.26",
64
+ "@joist/observable": "^4.0.0-next.26",
65
+ "tslib": "2.8.1"
66
+ },
67
+ "devDependencies": {
68
+ "@open-wc/testing": "^4.0.0",
69
+ "@storybook/addon-essentials": "^8.4.4",
70
+ "@storybook/web-components": "^8.4.4",
71
+ "@storybook/web-components-vite": "^8.4.4",
72
+ "@types/mocha": "^10.0.7",
73
+ "@types/node": "^22.0.0",
74
+ "@web/test-runner": "^0.19.0",
75
+ "husky": "^9.0.11",
76
+ "js-beautify": "^1.15.1",
77
+ "lint-staged": "^15.2.2",
78
+ "lit": "^3.2.1",
79
+ "mocha": "^11.0.0",
80
+ "plop": "^4.0.1",
81
+ "prettier": "^3.3.3",
82
+ "storybook": "^8.4.4",
83
+ "typescript": "^5.6.3",
84
+ "wireit": "^0.14.9"
85
+ },
86
+ "lint-staged": {
87
+ "*.{js,ts,css,md}": "prettier --write"
88
+ }
89
+ }
@@ -0,0 +1,33 @@
1
+ import { USAIcon } from "../icon/icon-types.js";
2
+
3
+ export const USA_ALERT_TYPES = [
4
+ "info",
5
+ "warning",
6
+ "success",
7
+ "error",
8
+ "emergency",
9
+ ] as const;
10
+
11
+ export type USAAlertType = (typeof USA_ALERT_TYPES)[number];
12
+
13
+ export interface USAAlert {
14
+ icon: USAIcon;
15
+ }
16
+
17
+ export const USA_ALERT_CONFIG: Record<USAAlertType, USAAlert> = {
18
+ info: {
19
+ icon: "info",
20
+ },
21
+ warning: {
22
+ icon: "warning",
23
+ },
24
+ success: {
25
+ icon: "check_circle",
26
+ },
27
+ error: {
28
+ icon: "error",
29
+ },
30
+ emergency: {
31
+ icon: "error",
32
+ },
33
+ };
@@ -0,0 +1,105 @@
1
+ import { attr, css, element, html, query } from "@joist/element";
2
+
3
+ import { USA_ALERT_CONFIG, USAAlertType } from "./alert-types.js";
4
+
5
+ declare global {
6
+ interface HTMLElementTagNameMap {
7
+ "usa-alert": USAAlertElement;
8
+ }
9
+ }
10
+
11
+ @element({
12
+ tagName: "usa-alert",
13
+ shadowDom: [
14
+ css`
15
+ :host {
16
+ display: block;
17
+ border-left: 0.5rem solid #adadad;
18
+ padding: 1rem 1.2rem;
19
+ color: #1b1b1b;
20
+ margin-bottom: 1rem;
21
+ }
22
+
23
+ :host([type="info"]) {
24
+ border-left-color: #00bde3;
25
+ background-color: #e7f6f8;
26
+ }
27
+
28
+ :host([type="warning"]) {
29
+ background-color: #faf3d1;
30
+ border-left-color: #ffbe2e;
31
+ }
32
+
33
+ :host([type="success"]) {
34
+ background-color: #ecf3ec;
35
+ border-left-color: #00a91c;
36
+ }
37
+
38
+ :host([type="error"]) {
39
+ background-color: #f4e3db;
40
+ border-left-color: #d54309;
41
+ }
42
+
43
+ :host([type="emergency"]) {
44
+ background-color: #9c3d10;
45
+ border-left-color: #9c3d10;
46
+ color: #fff;
47
+ }
48
+
49
+ :host([type="emergency"]) ::slotted(*) {
50
+ color: #fff;
51
+ }
52
+
53
+ .alert-heading {
54
+ display: grid;
55
+ grid-template-columns: auto 1fr;
56
+ align-items: center;
57
+ }
58
+
59
+ usa-icon {
60
+ margin: -0.28rem 0.75rem 0 0;
61
+ }
62
+
63
+ #heading::slotted(*) {
64
+ font-family:
65
+ Source Sans Pro Web,
66
+ Helvetica Neue,
67
+ Helvetica,
68
+ Roboto,
69
+ Arial,
70
+ sans-serif;
71
+ font-size: 1.33rem;
72
+ line-height: 0.9;
73
+ margin-top: 0;
74
+ margin-bottom: 0.5rem;
75
+ }
76
+ `,
77
+ html`
78
+ <div class="alert-heading">
79
+ <usa-icon icon="check_circle"></usa-icon>
80
+
81
+ <div>
82
+ <slot id="heading" name="heading"></slot>
83
+ </div>
84
+
85
+ <div class="spacer"></div>
86
+
87
+ <div>
88
+ <slot></slot>
89
+ </div>
90
+ </div>
91
+ `,
92
+ ],
93
+ })
94
+ export class USAAlertElement extends HTMLElement {
95
+ @attr()
96
+ accessor type: USAAlertType = "info";
97
+
98
+ #icon = query("usa-icon");
99
+
100
+ attributeChangedCallback() {
101
+ const icon = this.#icon();
102
+
103
+ icon.icon = USA_ALERT_CONFIG[this.type].icon;
104
+ }
105
+ }
@@ -0,0 +1,63 @@
1
+ import type { Meta, StoryObj } from "@storybook/web-components";
2
+ import { html } from "lit";
3
+
4
+ import type { USAAlertElement } from "./alert.element.js";
5
+
6
+ // More on how to set up stories at: https://storybook.js.org/docs/writing-stories
7
+ const meta = {
8
+ title: "usa-alert",
9
+ tags: ["autodocs"],
10
+ argTypes: {},
11
+ args: {},
12
+ } satisfies Meta<USAAlertElement>;
13
+
14
+ export default meta;
15
+
16
+ type Story = StoryObj<USAAlertElement>;
17
+
18
+ // More on writing stories with args: https://storybook.js.org/docs/writing-stories/args
19
+ export const Primary: Story = {
20
+ render() {
21
+ return html`
22
+ <usa-alert type="info">
23
+ <h3 slot="heading">Informative status</h3>
24
+
25
+ Lorem ipsum dolor sit amet,&nbsp;
26
+ <usa-link href="#">consectetur adipiscing</usa-link>&nbsp;elit, sed do
27
+ eiusmod.
28
+ </usa-alert>
29
+
30
+ <usa-alert type="warning">
31
+ <h3 slot="heading">Warning status</h3>
32
+
33
+ Lorem ipsum dolor sit amet,&nbsp;
34
+ <usa-link href="#">consectetur adipiscing</usa-link>&nbsp;elit, sed do
35
+ eiusmod.
36
+ </usa-alert>
37
+
38
+ <usa-alert type="success">
39
+ <h3 slot="heading">Success status</h3>
40
+
41
+ Lorem ipsum dolor sit amet,&nbsp;
42
+ <usa-link href="#">consectetur adipiscing</usa-link>&nbsp;elit, sed do
43
+ eiusmod.
44
+ </usa-alert>
45
+
46
+ <usa-alert type="error">
47
+ <h3 slot="heading">Error status</h3>
48
+
49
+ Lorem ipsum dolor sit amet,&nbsp;
50
+ <usa-link href="#">consectetur adipiscing</usa-link>&nbsp;elit, sed do
51
+ eiusmod.
52
+ </usa-alert>
53
+
54
+ <usa-alert type="emergency">
55
+ <h3 slot="heading">Emergency status</h3>
56
+
57
+ Lorem ipsum dolor sit amet,&nbsp;
58
+ <usa-link href="#">consectetur adipiscing</usa-link>&nbsp;elit, sed do
59
+ eiusmod.
60
+ </usa-alert>
61
+ `;
62
+ },
63
+ };
@@ -0,0 +1,23 @@
1
+ import "./alert.element.js";
2
+
3
+ import { fixture, html, assert } from "@open-wc/testing";
4
+
5
+ import { USA_ALERT_TYPES } from "./alert-types.js";
6
+
7
+ describe("usa-alert", () => {
8
+ for (let alert of USA_ALERT_TYPES) {
9
+ it(`should be accessible: ${alert}`, async () => {
10
+ const form = await fixture(html`
11
+ <usa-alert type=${alert}>
12
+ <h3 slot="heading">Status</h3>
13
+
14
+ Lorem ipsum dolor sit amet,&nbsp;
15
+ <usa-link href="#">consectetur adipiscing</usa-link>&nbsp;elit, sed do
16
+ eiusmod.
17
+ </usa-alert>
18
+ `);
19
+
20
+ return assert.isAccessible(form);
21
+ });
22
+ }
23
+ });
@@ -0,0 +1,224 @@
1
+ import { attr, css, element, html, listen, query } from "@joist/element";
2
+
3
+ declare global {
4
+ interface HTMLElementTagNameMap {
5
+ "usa-button": USAButtonElement;
6
+ }
7
+ }
8
+
9
+ export const BUTTON_VARIANTS = [
10
+ "primary",
11
+ "secondary",
12
+ "cool",
13
+ "warm",
14
+ "outline",
15
+ ] as const;
16
+
17
+ export type ButtonVariant = (typeof BUTTON_VARIANTS)[number];
18
+
19
+ @element({
20
+ tagName: "usa-button",
21
+ shadowDom: [
22
+ css`
23
+ :host {
24
+ display: contents;
25
+ }
26
+
27
+ .usa-button {
28
+ box-sizing: border-box;
29
+ font-family:
30
+ Source Sans Pro Web,
31
+ Helvetica Neue,
32
+ Helvetica,
33
+ Roboto,
34
+ Arial,
35
+ sans-serif;
36
+ font-size: 1.06rem;
37
+ line-height: 0.9;
38
+ color: white;
39
+ background-color: #005ea2;
40
+ -webkit-appearance: none;
41
+ -moz-appearance: none;
42
+ appearance: none;
43
+ align-items: center;
44
+ border: 0;
45
+ border-radius: 0.25rem;
46
+ cursor: pointer;
47
+ -moz-column-gap: 0.5rem;
48
+ column-gap: 0.5rem;
49
+ display: inline-flex;
50
+ font-weight: 700;
51
+ justify-content: center;
52
+ padding: 0.75rem 1.25rem;
53
+ text-align: center;
54
+ text-decoration: none;
55
+ width: 100%;
56
+ cursor: pointer;
57
+ }
58
+
59
+ @media all and (min-width: 30em) {
60
+ .usa-button {
61
+ width: auto;
62
+ }
63
+ }
64
+
65
+ .usa-button:visited {
66
+ color: white;
67
+ }
68
+
69
+ .usa-button:hover {
70
+ color: white;
71
+ background-color: #1a4480;
72
+ border-bottom: 0;
73
+ text-decoration: none;
74
+ }
75
+
76
+ .usa-button:active {
77
+ color: white;
78
+ background-color: #162e51;
79
+ }
80
+
81
+ .usa-button:not([disabled]):focus {
82
+ outline-offset: 0.25rem;
83
+ }
84
+
85
+ .usa-button:disabled {
86
+ color: #454545;
87
+ background-color: #c9c9c9;
88
+ cursor: not-allowed;
89
+ opacity: 1;
90
+ }
91
+
92
+ .usa-button:disabled:hover,
93
+ .usa-button:disabled:active,
94
+ .usa-button:disabled:focus {
95
+ color: #454545;
96
+ background-color: #c9c9c9;
97
+ }
98
+
99
+ .usa-button:focus {
100
+ outline: 0.25rem solid #2491ff;
101
+ outline-offset: 0;
102
+ }
103
+
104
+ /** Secondary */
105
+ :host([variant="secondary"]) .usa-button {
106
+ color: #fff;
107
+ background-color: #d83933;
108
+ }
109
+
110
+ :host([variant="secondary"]) .usa-button:hover {
111
+ background-color: #b50909;
112
+ }
113
+
114
+ :host([variant="secondary"]) .usa-button:active {
115
+ background-color: #8b0a03;
116
+ }
117
+
118
+ /** cool */
119
+ :host([variant="cool"]) .usa-button {
120
+ color: #1b1b1b;
121
+ background-color: #00bde3;
122
+ }
123
+
124
+ :host([variant="cool"]) .usa-button:hover {
125
+ background-color: #28a0cb;
126
+ }
127
+
128
+ :host([variant="cool"]) .usa-button:active {
129
+ color: #fff;
130
+ background-color: #07648d;
131
+ }
132
+
133
+ /** warm */
134
+ :host([variant="warm"]) .usa-button {
135
+ color: #1b1b1b;
136
+ background-color: #fa9441;
137
+ }
138
+
139
+ :host([variant="warm"]) .usa-button:hover {
140
+ color: #fff;
141
+ background-color: #c05600;
142
+ }
143
+
144
+ :host([variant="warm"]) .usa-button:active {
145
+ color: #fff;
146
+ background-color: #775540;
147
+ }
148
+
149
+ /** outline */
150
+ :host([variant="outline"]) .usa-button {
151
+ background-color: transparent;
152
+ box-shadow: inset 0 0 0 2px #005ea2;
153
+ color: #005ea2;
154
+ }
155
+
156
+ :host([variant="outline"]) .usa-button:hover {
157
+ box-shadow: inset 0 0 0 2px #1a4480;
158
+ color: #1a4480;
159
+ }
160
+
161
+ :host([variant="outline"]) .usa-button:active {
162
+ box-shadow: inset 0 0 0 2px #162e51;
163
+ color: #162e51;
164
+ }
165
+ `,
166
+ html`
167
+ <button class="usa-button">
168
+ <slot></slot>
169
+ </button>
170
+ `,
171
+ ],
172
+ })
173
+ export class USAButtonElement extends HTMLElement {
174
+ static formAssociated = true;
175
+
176
+ @attr()
177
+ accessor type: "button" | "submit" | "reset" = "button";
178
+
179
+ @attr()
180
+ accessor disabled = false;
181
+
182
+ @attr()
183
+ accessor variant: ButtonVariant = "primary";
184
+
185
+ #internals = this.attachInternals();
186
+ #button = query("button");
187
+
188
+ @listen("keydown", () => document.body)
189
+ onKeyDown(e: KeyboardEvent) {
190
+ if (this.type === "submit") {
191
+ if (e.key.toUpperCase() === "ENTER") {
192
+ this.#handleForm();
193
+ }
194
+ }
195
+ }
196
+
197
+ @listen("click")
198
+ onInternalClick() {
199
+ this.#handleForm();
200
+ }
201
+
202
+ attributeChangedCallback() {
203
+ const button = this.#button();
204
+ button.type = this.type;
205
+ button.disabled = this.disabled;
206
+ }
207
+
208
+ #handleForm() {
209
+ const { form } = this.#internals;
210
+
211
+ if (form) {
212
+ if (this.type === "submit") {
213
+ const btn = document.createElement("button");
214
+ btn.type = "submit";
215
+ form.append(btn);
216
+
217
+ btn.click();
218
+ btn.remove();
219
+ } else if (this.type === "reset") {
220
+ form.reset();
221
+ }
222
+ }
223
+ }
224
+ }
@@ -0,0 +1,34 @@
1
+ import type { Meta, StoryObj } from "@storybook/web-components";
2
+ import { html } from "lit";
3
+
4
+ import { BUTTON_VARIANTS, type USAButtonElement } from "./button.element.js";
5
+
6
+ // More on how to set up stories at: https://storybook.js.org/docs/writing-stories
7
+ const meta = {
8
+ title: "usa-button",
9
+ tags: ["autodocs"],
10
+ render(args) {
11
+ return html`
12
+ <div style="display: inline-flex; flex-direction: column; gap: 1rem">
13
+ ${BUTTON_VARIANTS.map(
14
+ (variant, i) =>
15
+ html`<usa-button variant=${variant}>Hello World</usa-button> ${i ===
16
+ BUTTON_VARIANTS.length - 1
17
+ ? ""
18
+ : "\n\n"}`
19
+ )}
20
+ </div>
21
+ `;
22
+ },
23
+ argTypes: {},
24
+ args: {},
25
+ } satisfies Meta<USAButtonElement>;
26
+
27
+ export default meta;
28
+
29
+ type Story = StoryObj<USAButtonElement>;
30
+
31
+ // More on writing stories with args: https://storybook.js.org/docs/writing-stories/args
32
+ export const Primary: Story = {
33
+ args: {},
34
+ };
@@ -0,0 +1,17 @@
1
+ import "./button.element.js";
2
+
3
+ import { fixture, html, assert } from "@open-wc/testing";
4
+
5
+ import { BUTTON_VARIANTS, USAButtonElement } from "./button.element.js";
6
+
7
+ describe("usa-button", () => {
8
+ for (let variant of BUTTON_VARIANTS) {
9
+ it("should be accessible", async () => {
10
+ const button = await fixture<USAButtonElement>(html`
11
+ <usa-button variant=${variant}>Hello World</usa-button>
12
+ `);
13
+
14
+ return assert.isAccessible(button);
15
+ });
16
+ }
17
+ });