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.
chore: auto merge branches (ant-design#44739)
chore: merge master into feature
- Loading branch information
Showing
30 changed files
with
2,307 additions
and
1,733 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
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
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
73 changes: 73 additions & 0 deletions
73
.dumi/pages/index/components/PreviewBanner/useMouseTransform.tsx
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,73 @@ | ||
import React, { startTransition } from 'react'; | ||
import { ConfigProvider } from 'antd'; | ||
|
||
const getTransformRotateStyle = ( | ||
event: React.MouseEvent<HTMLDivElement, MouseEvent>, | ||
currentTarget: EventTarget & HTMLDivElement, | ||
multiple: number, | ||
isRTL: boolean, | ||
): string => { | ||
const box = currentTarget?.getBoundingClientRect(); | ||
const calcX = -(event.clientY - box.y - box.height / 2) / multiple; | ||
const calcY = (event.clientX - box.x - box.width / 2) / multiple; | ||
return isRTL | ||
? `rotate3d(${24 + calcX}, ${83 + calcY}, -45, 57deg)` | ||
: `rotate3d(${24 + calcX}, ${-83 + calcY}, 45, 57deg)`; | ||
}; | ||
|
||
const useMouseTransform = ({ transitionDuration = 500, multiple = 36 } = {}) => { | ||
const [componentsBlockStyle, setComponentsBlockStyle] = React.useState<React.CSSProperties>({}); | ||
|
||
const { direction } = React.useContext(ConfigProvider.ConfigContext); | ||
|
||
const isRTL = direction === 'rtl'; | ||
|
||
const onMouseMove: React.MouseEventHandler<HTMLDivElement> = (event) => { | ||
const { currentTarget } = event; | ||
startTransition(() => { | ||
setComponentsBlockStyle((style) => ({ | ||
...style, | ||
transform: getTransformRotateStyle(event, currentTarget, multiple, isRTL), | ||
})); | ||
}); | ||
}; | ||
|
||
const onMouseEnter: React.MouseEventHandler<HTMLDivElement> = () => { | ||
startTransition(() => { | ||
setComponentsBlockStyle((style) => ({ | ||
...style, | ||
transition: `transform ${transitionDuration / 1000}s`, | ||
})); | ||
}); | ||
|
||
setTimeout(() => { | ||
startTransition(() => { | ||
setComponentsBlockStyle((style) => ({ | ||
...style, | ||
transition: '', | ||
})); | ||
}); | ||
}, transitionDuration); | ||
}; | ||
|
||
const onMouseLeave: React.MouseEventHandler<HTMLDivElement> = () => { | ||
startTransition(() => { | ||
setComponentsBlockStyle((style) => ({ | ||
...style, | ||
transition: `transform ${transitionDuration / 1000}s`, | ||
transform: '', | ||
})); | ||
}); | ||
}; | ||
|
||
return [ | ||
componentsBlockStyle, | ||
{ | ||
onMouseMove, | ||
onMouseEnter, | ||
onMouseLeave, | ||
}, | ||
] as const; | ||
}; | ||
|
||
export default useMouseTransform; |
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
Oops, something went wrong.