- 使用 Chakra UI 的颜色模式实现暗模式:
- 自动颜色模式:利用 Chakra UI 内置的颜色模式(Color Mode)功能,轻松实现亮/暗模式切换,并支持系统偏好设置。
useColorModeHook:在组件中使用useColorModeHook 来获取当前颜色模式和切换模式的函数。ColorModeScript:在应用的根部(例如_document.js或index.js)引入ColorModeScript,以防止页面加载时的闪烁问题。- 主题配置:在 Chakra UI 主题中定义
colors对象,为亮/暗模式提供不同的颜色值,确保组件在不同模式下显示正确。 - 示例:
import { useColorMode, Button, Box } from '@chakra-ui/react'; function ColorModeSwitcher() { const { colorMode, toggleColorMode } = useColorMode(); return ( <Box> <Button Toggle {colorMode === 'light' ? 'Dark' : 'Light'} Mode </Button> </Box> ); } - 持久化:Chakra UI 默认会将颜色模式偏好存储在 localStorage 中,确保用户下次访问时保持一致。
- 可访问性:确保亮/暗模式切换时,颜色对比度仍然满足可访问性标准。
Chakra UI Dark Mode Implementation
在构建 React 组件时使用 Chakra UI 的颜色模式实现暗模式
Chakra UI Dark Mode Implementation by liaosw97 · f0765d5
npx skillmds@latest add liaosw97/chakra-ui-dark-mode-implementation File contents
---name: chakra-ui-dark-mode-implementationdescription: 在构建 React 组件时使用 Chakra UI 的颜色模式实现暗模式---- **使用 Chakra UI 的颜色模式实现暗模式**: - **自动颜色模式**:利用 Chakra UI 内置的颜色模式(Color Mode)功能,轻松实现亮/暗模式切换,并支持系统偏好设置。 - **`useColorMode` Hook**:在组件中使用 `useColorMode` Hook 来获取当前颜色模式和切换模式的函数。 - **`ColorModeScript`**:在应用的根部(例如 `_document.js` 或 `index.js`)引入 `ColorModeScript`,以防止页面加载时的闪烁问题。 - **主题配置**:在 Chakra UI 主题中定义 `colors` 对象,为亮/暗模式提供不同的颜色值,确保组件在不同模式下显示正确。 - **示例**: ```jsx import { useColorMode, Button, Box } from '@chakra-ui/react'; function ColorModeSwitcher() { const { colorMode, toggleColorMode } = useColorMode(); return ( <Box> <Button onClick={toggleColorMode}> Toggle {colorMode === 'light' ? 'Dark' : 'Light'} Mode </Button> </Box> ); } ``` - **持久化**:Chakra UI 默认会将颜色模式偏好存储在 localStorage 中,确保用户下次访问时保持一致。 - **可访问性**:确保亮/暗模式切换时,颜色对比度仍然满足可访问性标准。
liaosw97/awesome-rules-skills/tree/main/plugins/chakra-ui/skills/chakra-ui---dark-mode-implementation commit f0765d508e
Frequently asked questions
Run npx skillmds@latest add liaosw97/chakra-ui-dark-mode-implementation in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
在构建 React 组件时使用 Chakra UI 的颜色模式实现暗模式 It is listed under Web & Frontend on SkillMD.
This skill has not completed SkillMD's automated safety review yet. Capability flags: docs only. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
liaosw97 (@liaosw97) published this skill. Their other Agent Skills are listed on their SkillMD profile.