Skip to content

useTheme

根据传入的theme类型来来触发对应的事件。

基础用法

tsx
import { useState } from'react'
import { useTheme, ThemeType } from 'ono-react-element'

function App() {
    const [theme, setTheme] = useState<ThemeType>('light')

    useTheme({
        theme, // 当传入为system时,会根据系统的主题来切换
        onDark: ()=> document.body.classList.add('dark'),
        onLight: ()=> document.body.classList.remove('dark'),
    })

    return (
        <div>
            <button onClick={() => setTheme('dark')}>切换为暗黑模式</button>
            <button onClick={() => setTheme('light')}>切换为浅色模式</button>
            <button onClick={() => setTheme('system')}>切换为跟随系统</button>
        </div>
    )
}

export default App

API

通用属性参考:通用属性

参数说明类型默认值是否必填
params传入的参数useThemeParams-

useThemeParams

参数说明类型默认值是否必填
theme主题类型ThemeType-
onDark暗黑模式切换事件回调函数() => void-
onLight浅色模式切换事件回调函数() => void-

ThemeType

参数说明类型默认值是否必填
ThemeType主题类型light|dark|system-