@vela-ui/react
Version:
Vela UI React components
3 lines (2 loc) • 785 B
JavaScript
"use client"
import{createContext as n,useContext as c,useEffect as T,useState as l}from"react";import{jsx as v}from"react/jsx-runtime";var u={theme:"system",setTheme:()=>null},s=n(u);function p({children:t,defaultTheme:m="system",storageKey:o="vela-ui-theme",...h}){let[r,i]=l(()=>localStorage.getItem(o)||m);T(()=>{let e=window.document.documentElement;if(e.classList.remove("light","dark"),r==="system"){let a=window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light";e.classList.add(a);return}e.classList.add(r)},[r]);let d={theme:r,setTheme:e=>{localStorage.setItem(o,e),i(e)}};return v(s.Provider,{...h,value:d,children:t})}var f=()=>{let t=c(s);if(t===void 0)throw new Error("useTheme must be used within a ThemeProvider");return t};export{s as a,p as b,f as c};