Skip to content

Commit 39c3f79

Browse files
committed
fix(ui): Theme switching - disable motion during the change
1 parent 0872a8b commit 39c3f79

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

packages/ui/src/context/theme.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import type { ReactNode } from 'react';
22
import React, { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react';
33
import { useCookie } from 'react-use';
4+
import { wait } from '@bundle-stats/utils';
5+
6+
// classList required timeout to allow to disable motion during the switch
7+
const CLASSLIST_UPDATE_TIMEOUT = 10;
48

59
type ThemeName = 'light' | 'dark';
610

@@ -18,17 +22,21 @@ export const ThemeProvider = ({ children }: { children: ReactNode }) => {
1822
const [cookieValue, setCookieValue] = useCookie('theme');
1923
const [name, setName] = useState<ThemeName>(cookieValue === 'dark' ? 'dark' : 'light');
2024

21-
const updateClassList = useCallback((newTheme: ThemeName) => {
25+
const updateClassList = useCallback(async (newTheme: ThemeName) => {
2226
const htmlElm = document.querySelector('html');
2327

2428
htmlElm?.classList.add('no-motion');
2529

30+
await wait(CLASSLIST_UPDATE_TIMEOUT);
31+
2632
if (newTheme === 'dark') {
2733
htmlElm?.classList.replace('light-theme', 'dark-theme');
2834
} else {
2935
htmlElm?.classList.replace('dark-theme', 'light-theme');
3036
}
3137

38+
await wait(CLASSLIST_UPDATE_TIMEOUT);
39+
3240
htmlElm?.classList.remove('no-motion');
3341
}, []);
3442

packages/ui/src/css/variables.css

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,7 @@
285285
--entry-info-top: var(--header-height);
286286
}
287287

288-
.no-motion {
289-
--transition-duration-in: 0s;
290-
--transition-duration-out: 0s;
291-
--transition-in: none;
292-
--transition-out: none;
288+
.no-motion * {
289+
transition: none !important;
290+
animation: none !important;
293291
}

packages/utils/src/utils/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export * from './insights';
55
export * from './file-types';
66
export * from './format';
77
export * from './metrics';
8+
export * from './wait';

0 commit comments

Comments
 (0)