File tree Expand file tree Collapse file tree 3 files changed +90
-0
lines changed
content/docs/style-guide/components Expand file tree Collapse file tree 3 files changed +90
-0
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ import { z } from " astro:schema" ;
3+
4+ const props = z .object ({
5+ label: z .string (),
6+ });
7+
8+ const { label } = props .parse (Astro .props );
9+ ---
10+
11+ <check-box>
12+ <label class =" block" >
13+ <input type =" checkbox" />
14+ { label }
15+ </label >
16+ </check-box>
17+
18+ <script >
19+ function getStorage() {
20+ const raw = localStorage.getItem("checkboxes");
21+
22+ if (!raw) return [];
23+
24+ let json;
25+ try {
26+ json = JSON.parse(raw);
27+ } catch {
28+ localStorage.removeItem("checkboxes");
29+ return [];
30+ }
31+
32+ return json;
33+ }
34+
35+ function updateStorage(label: string, checked: boolean) {
36+ const key = `${window.location.pathname}${label}`;
37+ const storage = getStorage();
38+
39+ if (checked) {
40+ storage.push(key);
41+ } else {
42+ storage.splice(storage.indexOf(key), 1);
43+ }
44+
45+ localStorage.setItem("checkboxes", JSON.stringify(storage));
46+ }
47+
48+ class Checkbox extends HTMLElement {
49+ connectedCallback() {
50+ const input = this.querySelector("input") as HTMLInputElement;
51+ const label = (
52+ this.querySelector("label") as HTMLLabelElement
53+ ).innerText.trim();
54+
55+ input.checked = getStorage().includes(
56+ `${window.location.pathname}${label}`,
57+ );
58+
59+ input.addEventListener("change", () => {
60+ updateStorage(label, input.checked);
61+ });
62+ }
63+ }
64+
65+ customElements.define("check-box", Checkbox);
66+ </script >
Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ export { Icon as AstroIcon } from "astro-icon/components";
77export { default as AnchorHeading } from "./AnchorHeading.astro" ;
88export { default as APIRequest } from "./APIRequest.astro" ;
99export { default as AvailableNotifications } from "./AvailableNotifications.astro" ;
10+ export { default as Checkbox } from "./Checkbox.astro" ;
1011export { default as CompatibilityFlag } from "./CompatibilityFlag.astro" ;
1112export { default as CompatibilityFlags } from "./CompatibilityFlags.astro" ;
1213export { default as ComponentUsage } from "./ComponentUsage.astro" ;
Original file line number Diff line number Diff line change 1+ ---
2+ title : Checkbox
3+ styleGuide :
4+ component : Checkbox
5+ ---
6+
7+ import { Type , MetaInfo } from " ~/components" ;
8+
9+ The ` Checkbox ` component is a custom checkbox that persists its state in local storage.
10+
11+ ``` mdx live
12+ import { Checkbox } from " ~/components" ;
13+
14+ <Checkbox label = " Step one" />
15+ <Checkbox label = " Step two" />
16+ <Checkbox label = " Step three" />
17+ ```
18+
19+ ## Props
20+
21+ - ` label ` <Type text = " string" /> <MetaInfo text = " required" />
22+
23+ The label for the checkbox.
You can’t perform that action at this time.
0 commit comments