forked from ant-design/ant-design
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add theme toggle animation (ant-design#44655)
* docs: add theme toggle animation * fix: add compatibility judgment * refactor: optimization code * fix: server document not found * fix: animation lag * fix: transition issue * fix: scroll bar theme color
- Loading branch information
Showing
2 changed files
with
140 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import { useEffect, useRef } from 'react'; | ||
import { removeCSS, updateCSS } from 'rc-util/lib/Dom/dynamicCSS'; | ||
|
||
import theme from '../../components/theme'; | ||
|
||
const viewTransitionStyle = ` | ||
::view-transition-old(root), | ||
::view-transition-new(root) { | ||
animation: none; | ||
mix-blend-mode: normal; | ||
} | ||
.dark::view-transition-old(root) { | ||
z-index: 1; | ||
} | ||
.dark::view-transition-new(root) { | ||
z-index: 999; | ||
} | ||
::view-transition-old(root) { | ||
z-index: 999; | ||
} | ||
::view-transition-new(root) { | ||
z-index: 1; | ||
} | ||
`; | ||
|
||
const useThemeAnimation = () => { | ||
const { | ||
token: { colorBgElevated }, | ||
} = theme.useToken(); | ||
|
||
const animateRef = useRef<{ | ||
colorBgElevated: string; | ||
}>({ | ||
colorBgElevated, | ||
}); | ||
|
||
const startAnimationTheme = (clipPath: string[], isDark: boolean) => { | ||
updateCSS( | ||
` | ||
* { | ||
transition: none !important; | ||
} | ||
`, | ||
'disable-transition', | ||
); | ||
|
||
document.documentElement | ||
.animate( | ||
{ | ||
clipPath: isDark ? [...clipPath].reverse() : clipPath, | ||
}, | ||
{ | ||
duration: 500, | ||
easing: 'ease-in', | ||
pseudoElement: isDark ? '::view-transition-old(root)' : '::view-transition-new(root)', | ||
}, | ||
) | ||
.addEventListener('finish', () => { | ||
removeCSS('disable-transition'); | ||
}); | ||
}; | ||
|
||
const toggleAnimationTheme = (event: MouseEvent, isDark: boolean) => { | ||
// @ts-ignore | ||
if (!(event && typeof document.startViewTransition === 'function')) return; | ||
const x = event.clientX; | ||
const y = event.clientY; | ||
const endRadius = Math.hypot(Math.max(x, innerWidth - x), Math.max(y, innerHeight - y)); | ||
updateCSS( | ||
` | ||
[data-prefers-color='dark'] { | ||
color-scheme: light !important; | ||
} | ||
[data-prefers-color='light'] { | ||
color-scheme: dark !important; | ||
} | ||
`, | ||
'color-scheme', | ||
); | ||
document | ||
// @ts-ignore | ||
.startViewTransition(async () => { | ||
// wait for theme change end | ||
while (colorBgElevated === animateRef.current.colorBgElevated) { | ||
// eslint-disable-next-line no-await-in-loop | ||
await new Promise((resolve) => { | ||
setTimeout(resolve, 1000 / 60); | ||
}); | ||
} | ||
const root = document.documentElement; | ||
root.classList.remove(isDark ? 'dark' : 'light'); | ||
root.classList.add(isDark ? 'light' : 'dark'); | ||
}) | ||
.ready.then(() => { | ||
const clipPath = [ | ||
`circle(0px at ${x}px ${y}px)`, | ||
`circle(${endRadius}px at ${x}px ${y}px)`, | ||
]; | ||
removeCSS('color-scheme'); | ||
startAnimationTheme(clipPath, isDark); | ||
}); | ||
}; | ||
|
||
// inject transition style | ||
useEffect(() => { | ||
// @ts-ignore | ||
if (typeof document.startViewTransition === 'function') { | ||
updateCSS(viewTransitionStyle, 'view-transition-style'); | ||
} | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (colorBgElevated !== animateRef.current.colorBgElevated) { | ||
animateRef.current.colorBgElevated = colorBgElevated; | ||
} | ||
}, [colorBgElevated]); | ||
|
||
return toggleAnimationTheme; | ||
}; | ||
|
||
export default useThemeAnimation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters