import { Atom } from "@effect-atom/atom-react"
import { BrowserKeyValueStore } from "@effect/platform-browser"
import { Schema as S } from "effect"
export const themeAtom = Atom.kvs({
runtime: Atom.runtime(BrowserKeyValueStore.layerLocalStorage),
key: "theme",
schema: S.Literal("dark", "light"),
defaultValue: () => matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light",
}).pipe(
Atom.keepAlive,
Atom.tap((_get, theme) => {
const { classList } = document.documentElement
classList.remove(theme === "light" ? "dark" : "light")
classList.add(theme)
}),
)
import { Atom } from "@effect-atom/atom-react"
import { BrowserKeyValueStore } from "@effect/platform-browser"
import { Schema as S } from "effect"
export const themeAtom = Atom.kvs({
runtime: Atom.runtime(BrowserKeyValueStore.layerLocalStorage),
key: "theme",
schema: S.Literal("dark", "light"),
defaultValue: () => matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light",
}).pipe(
Atom.keepAlive,
Atom.tap((_get, theme) => {
const { classList } = document.documentElement
classList.remove(theme === "light" ? "dark" : "light")
classList.add(theme)
}),
)