diff --git a/apps/web/src/components/blocksuite/workspace-header/header-right-items/theme-mode-switch/index.tsx b/apps/web/src/components/blocksuite/workspace-header/header-right-items/theme-mode-switch/index.tsx index 250f352a95..5a9df22cf5 100644 --- a/apps/web/src/components/blocksuite/workspace-header/header-right-items/theme-mode-switch/index.tsx +++ b/apps/web/src/components/blocksuite/workspace-header/header-right-items/theme-mode-switch/index.tsx @@ -1,10 +1,15 @@ import { DarkModeIcon, LightModeIcon } from '@blocksuite/icons'; import { useTheme } from 'next-themes'; -import { useState } from 'react'; +import { useEffect, useState } from 'react'; import { StyledSwitchItem, StyledThemeModeSwitch } from './style'; export const ThemeModeSwitch = () => { const { setTheme, resolvedTheme } = useTheme(); + + useEffect(() => { + window.apis?.onThemeChange(resolvedTheme === 'dark' ? 'dark' : 'light'); + }, [resolvedTheme]); + const [isHover, setIsHover] = useState(false); return ( ; diff --git a/packages/component/src/theme/theme.css.ts b/packages/component/src/theme/theme.css.ts index c6868f3168..2339a0f0f8 100644 --- a/packages/component/src/theme/theme.css.ts +++ b/packages/component/src/theme/theme.css.ts @@ -1,9 +1,19 @@ import type { AffineCssVariables } from '@affine/component'; import { globalStyle } from '@vanilla-extract/css'; -import kebabCase from 'kebab-case'; import { darkTheme, lightTheme } from '../styles/theme'; +const camelToKebab = (s: string) => { + if (typeof s !== 'string') return ''; + return s + .replace(/-/g, '_') + .replace(/([a-z0-9])([A-Z])/g, '$1-$2') + .replace(/([A-Z])([A-Z])(?=[a-z])/g, '$1-$2') + .toLowerCase() + .replace(/(\D+)(\d+)$/g, '$1-$2') + .replace(/\s|_/g, '-'); +}; + globalStyle('body', { color: 'var(--affine-text-primary-color)', fontFamily: 'var(--affine-font-family)', @@ -14,7 +24,7 @@ globalStyle('body', { globalStyle('html', { vars: { ...Object.entries(lightTheme).reduce((variables, [key, value]) => { - variables[`--affine-${kebabCase(key)}` as keyof AffineCssVariables] = + variables[`--affine-${camelToKebab(key)}` as keyof AffineCssVariables] = value; return variables; }, {} as AffineCssVariables), @@ -24,7 +34,7 @@ globalStyle('html', { globalStyle('html[data-theme="dark"]', { vars: { ...Object.entries(darkTheme).reduce((variables, [key, value]) => { - variables[`--affine-${kebabCase(key)}` as keyof AffineCssVariables] = + variables[`--affine-${camelToKebab(key)}` as keyof AffineCssVariables] = value; return variables; }, {} as AffineCssVariables), diff --git a/yarn.lock b/yarn.lock index 46e0878300..046b8a4219 100644 --- a/yarn.lock +++ b/yarn.lock @@ -76,7 +76,6 @@ __metadata: concurrently: ^8.0.1 jest-mock: ^29.5.0 jotai: ^2.0.4 - kebab-case: ^1.0.2 lit: ^2.7.2 lottie-web: ^5.11.0 react: ^18.2.0 @@ -16667,13 +16666,6 @@ __metadata: languageName: node linkType: hard -"kebab-case@npm:^1.0.2": - version: 1.0.2 - resolution: "kebab-case@npm:1.0.2" - checksum: bf01164e11c544ee9b3aa1a91c2e7d6aa6b3356b834a5e885d43f36db14aae5d347cd4a298a566133321b82fde6bac6b597f148ac19bfd3d3fc81e1034a79729 - languageName: node - linkType: hard - "keyv@npm:^4.0.0, keyv@npm:^4.5.2": version: 4.5.2 resolution: "keyv@npm:4.5.2"