mirror of
https://github.com/alrayyes/wiki.git
synced 2024-11-23 12:06:22 +00:00
e58c217de1
* feat: support checkbox Signed-off-by: Aaron <29749331+aarnphm@users.noreply.github.com> * chore: apply review from jacky --------- Signed-off-by: Aaron <29749331+aarnphm@users.noreply.github.com>
23 lines
754 B
TypeScript
23 lines
754 B
TypeScript
import { getFullSlug } from "../../util/path"
|
|
|
|
const checkboxId = (index: number) => `${getFullSlug(window)}-checkbox-${index}`
|
|
|
|
document.addEventListener("nav", () => {
|
|
const checkboxes = document.querySelectorAll(
|
|
"input.checkbox-toggle",
|
|
) as NodeListOf<HTMLInputElement>
|
|
checkboxes.forEach((el, index) => {
|
|
const elId = checkboxId(index)
|
|
|
|
const switchState = (e: Event) => {
|
|
const newCheckboxState = (e.target as HTMLInputElement)?.checked ? "true" : "false"
|
|
localStorage.setItem(elId, newCheckboxState)
|
|
}
|
|
|
|
el.addEventListener("change", switchState)
|
|
window.addCleanup(() => el.removeEventListener("change", switchState))
|
|
if (localStorage.getItem(elId) === "true") {
|
|
el.checked = true
|
|
}
|
|
})
|
|
})
|