Skip to content

Commit

Permalink
site: use vanilla-tilt.js for banner motion
Browse files Browse the repository at this point in the history
  • Loading branch information
afc163 committed Nov 22, 2023
1 parent 3233ac4 commit 5aafe6d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 83 deletions.
11 changes: 8 additions & 3 deletions .dumi/pages/index/components/PreviewBanner/ComponentsBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import { createStyles } from 'antd-style';
import classNames from 'classnames';

import Tilt from './Tilt';
import useLocale from '../../../../hooks/useLocale';

const { _InternalPanelDoNotUseOrYouWillBeFired: ModalPanel } = Modal;
Expand Down Expand Up @@ -79,7 +80,7 @@ const useStyle = createStyles(({ token, css }) => {
display: flex;
flex-direction: column;
row-gap: ${gap}px;
opacity: 0.65;
opacity: 0.8;
`,

flex: css`
Expand Down Expand Up @@ -117,7 +118,11 @@ const ComponentsBlock: React.FC<ComponentsBlockProps> = (props) => {
const { styles } = useStyle();

return (
<div className={classNames(className, styles.holder)} style={style}>
<Tilt
options={{ max: 20, glare: true, scale: 1 }}
className={classNames(className, styles.holder)}
style={style}
>
<ModalPanel title="Ant Design 5.0" width="100%">
{locale.text}
</ModalPanel>
Expand Down Expand Up @@ -252,7 +257,7 @@ const ComponentsBlock: React.FC<ComponentsBlockProps> = (props) => {
<InternalTooltip title={locale.hello} placement="topLeft" className={styles.noMargin} />

<Alert message="Ant Design love you!" type="success" />
</div>
</Tilt>
);
};

Expand Down
33 changes: 33 additions & 0 deletions .dumi/pages/index/components/PreviewBanner/Tilt.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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;
16 changes: 9 additions & 7 deletions .dumi/pages/index/components/PreviewBanner/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import useLocale from '../../../../hooks/useLocale';
import SiteContext from '../../../../theme/slots/SiteContext';
import * as utils from '../../../../theme/utils';
import { GroupMask } from '../Group';
import useMouseTransform from './useMouseTransform';

const ComponentsBlock = React.lazy(() => import('./ComponentsBlock'));

Expand All @@ -28,7 +27,6 @@ const locales = {
const useStyle = () => {
const { direction } = React.useContext(ConfigProvider.ConfigContext);
const isRTL = direction === 'rtl';

return createStyles(({ token, css, cx }) => {
const textShadow = `0 0 3px ${token.colorBgContainer}`;

Expand All @@ -37,7 +35,9 @@ const useStyle = () => {
inset: 0;
backdrop-filter: blur(4px);
opacity: 1;
transition: opacity 1s ease;
background: rgba(255, 255, 255, 0.2);
transition: all 1s ease;
pointer-events: none;
`);

return {
Expand Down Expand Up @@ -114,10 +114,8 @@ const PreviewBanner: React.FC<PreviewBannerProps> = (props) => {
const { pathname, search } = useLocation();
const isZhCN = utils.isZhCN(pathname);

const [componentsBlockStyle, mouseEvents] = useMouseTransform();

return (
<GroupMask {...mouseEvents}>
<GroupMask>
{/* Image Left Top */}
<img
style={{ position: 'absolute', left: isMobile ? -120 : 0, top: 0, width: 240 }}
Expand All @@ -134,7 +132,11 @@ const PreviewBanner: React.FC<PreviewBannerProps> = (props) => {
<div className={styles.holder}>
{/* Mobile not show the component preview */}
<Suspense fallback={null}>
{!isMobile && <ComponentsBlock className={styles.block} style={componentsBlockStyle} />}
{isMobile ? null : (
<div className={styles.block}>
<ComponentsBlock />
</div>
)}
</Suspense>
<div className={styles.mask} />

Expand Down
73 changes: 0 additions & 73 deletions .dumi/pages/index/components/PreviewBanner/useMouseTransform.tsx

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@
"typedoc": "^0.25.0",
"typescript": "~5.2.2",
"vanilla-jsoneditor": "^0.18.0",
"vanilla-tilt": "^1.8.1",
"webpack-bundle-analyzer": "^4.1.0",
"xhr-mock": "^2.4.1"
},
Expand Down

0 comments on commit 5aafe6d

Please sign in to comment.