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 banner mouse effect animation (ant-design#44711)
* docs: add banner mouse effect animation * refactor to useRef * refactor to react way * refactor to hooks * type: add type * fix: fix error * fix: fix error * fix: update RTL --------- Co-authored-by: 栗嘉男 <574980606@qq.com>
- Loading branch information
1 parent
bdf8c43
commit 4489eec
Showing
5 changed files
with
110 additions
and
25 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
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