主题切换效果
在浅色和深色主题之间切换时的动画过渡。
哦我太喜欢这个了,必须把它搬到 orcdev.com 上。
OrcDev, 8bitcn.com 创作者
使用场景
- 站点标题中的明暗切换。
- 投资组合或营销网站的主题更改。
浏览器兼容性
该组件使用视图转换API。在生产中使用之前,请检查 MDN 上最新的浏览器兼容性。
安装
三角形
pnpm dlx shadcn@latest add @yourname/theme-toggle-effect-triangle
三角形模糊
pnpm dlx shadcn@latest add @yourname/theme-toggle-effect-triangle-blur
圆圈
pnpm dlx shadcn@latest add @yourname/theme-toggle-effect-circle
圆圈模糊
pnpm dlx shadcn@latest add @yourname/theme-toggle-effect-circle-blur
圆圈模糊(左上角)
pnpm dlx shadcn@latest add @yourname/theme-toggle-effect-circle-blur-top-left
多边形
pnpm dlx shadcn@latest add @yourname/theme-toggle-effect-polygon
多边形渐变
pnpm dlx shadcn@latest add @yourname/theme-toggle-effect-polygon-gradient
用法
用 document.startViewTransition 包裹你的主题设置器以触发效果:
const { setTheme } = useTheme()
function toggleTheme(theme: string) {
if (!document.startViewTransition) {
setTheme(theme)
return
}
document.startViewTransition(() => setTheme(theme))
}致谢
参考
License and usage
- Free to use and modify in both personal and commercial projects.
- Attribution is appreciated but not required.
- Released under the MIT License.
Every component here is built and maintained by one person. If you enjoy using them, you can sponsor my work.