Skip to content

Commit ad01a8d

Browse files
authored
[Docs Site] Interactive checkbox component (#24361)
* [Docs Site] Interactive checkbox component * Update src/components/Checkbox.astro * Update src/components/Checkbox.astro
1 parent 7ec4ea0 commit ad01a8d

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

src/components/Checkbox.astro

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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>

src/components/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export { Icon as AstroIcon } from "astro-icon/components";
77
export { default as AnchorHeading } from "./AnchorHeading.astro";
88
export { default as APIRequest } from "./APIRequest.astro";
99
export { default as AvailableNotifications } from "./AvailableNotifications.astro";
10+
export { default as Checkbox } from "./Checkbox.astro";
1011
export { default as CompatibilityFlag } from "./CompatibilityFlag.astro";
1112
export { default as CompatibilityFlags } from "./CompatibilityFlags.astro";
1213
export { default as ComponentUsage } from "./ComponentUsage.astro";
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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.

0 commit comments

Comments
 (0)