mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-15 00:29:12 +08:00
34 lines
784 B
TypeScript
34 lines
784 B
TypeScript
import React, { useEffect, useRef } from 'react';
|
|
import VanillaTilt, { type TiltOptions } from 'vanilla-tilt';
|
|
|
|
interface TiltProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
options?: TiltOptions;
|
|
}
|
|
|
|
// https://micku7zu.github.io/vanilla-tilt.js/index.html
|
|
const defaultTiltOptions = {
|
|
scale: 1.02,
|
|
max: 8,
|
|
speed: 1500,
|
|
glare: true,
|
|
'max-glare': 0.8,
|
|
};
|
|
|
|
const Tilt: React.FC<TiltProps> = ({ options, ...props }) => {
|
|
const node = useRef<HTMLDivElement>(null);
|
|
useEffect(() => {
|
|
if (node.current) {
|
|
VanillaTilt.init(node.current, {
|
|
...defaultTiltOptions,
|
|
...options,
|
|
});
|
|
}
|
|
return () => {
|
|
(node.current as any)?.vanillaTilt.destroy();
|
|
};
|
|
}, []);
|
|
return <div ref={node} {...props} />;
|
|
};
|
|
|
|
export default Tilt;
|