fix(component): correct notification theme when theme toggled (#8570)

close AF-1526
This commit is contained in:
CatsJuice
2024-10-22 03:15:33 +00:00
parent 21d3b5084a
commit ed511f8d29
2 changed files with 15 additions and 8 deletions

View File

@@ -1,12 +1,12 @@
import './polyfill'; import './polyfill';
import '../src/theme'; import '../src/theme';
import './preview.css'; import './preview.css';
import { ThemeProvider } from 'next-themes';
import type { ComponentType } from 'react';
import type { Preview } from '@storybook/react';
import React, { useEffect } from 'react';
import { ConfirmModalProvider } from '../src/ui/modal/confirm-modal'; import { ConfirmModalProvider } from '../src/ui/modal/confirm-modal';
import { ThemeProvider, useTheme as useNextTheme } from 'next-themes';
import type { ComponentType } from 'react';
import React, { useEffect } from 'react';
import type { Preview } from '@storybook/react';
import { setupGlobal } from '@affine/env/global'; import { setupGlobal } from '@affine/env/global';
setupGlobal(); setupGlobal();
@@ -34,19 +34,22 @@ export const globalTypes = {
}, },
}; };
const useTheme = context => { const ThemeToggle = ({ context }) => {
const { theme } = context.globals; const { theme } = context.globals;
const { setTheme } = useNextTheme();
useEffect(() => { useEffect(() => {
document.documentElement.setAttribute('data-theme', theme); setTheme(theme);
}, [theme]); }, [theme]);
return null;
}; };
export const decorators = [ export const decorators = [
(Story: ComponentType, context) => { (Story: ComponentType, context) => {
useTheme(context);
return ( return (
<ThemeProvider themes={['dark', 'light']} enableSystem={true}> <ThemeProvider themes={['dark', 'light']} enableSystem={true}>
<ThemeToggle context={context} />
<ConfirmModalProvider> <ConfirmModalProvider>
<Story {...context} /> <Story {...context} />
</ConfirmModalProvider> </ConfirmModalProvider>

View File

@@ -1,4 +1,5 @@
import { assignInlineVars } from '@vanilla-extract/dynamic'; import { assignInlineVars } from '@vanilla-extract/dynamic';
import { useTheme } from 'next-themes';
import { type CSSProperties, useMemo } from 'react'; import { type CSSProperties, useMemo } from 'react';
import { Toaster } from 'sonner'; import { Toaster } from 'sonner';
@@ -7,6 +8,8 @@ import type { NotificationCenterProps } from '../types';
export function DesktopNotificationCenter({ export function DesktopNotificationCenter({
width = 380, width = 380,
}: NotificationCenterProps) { }: NotificationCenterProps) {
const theme = useTheme();
const resolvedTheme = theme.resolvedTheme as 'light' | 'dark';
const style = useMemo(() => { const style = useMemo(() => {
return { return {
...assignInlineVars({ ...assignInlineVars({
@@ -32,6 +35,7 @@ export function DesktopNotificationCenter({
className="affine-notification-center" className="affine-notification-center"
style={style} style={style}
toastOptions={toastOptions} toastOptions={toastOptions}
theme={resolvedTheme}
/> />
); );
} }