Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create useCSSTransition() #7876

Merged
merged 3 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/vkui/src/lib/animation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@
export { rubberbandIfOutOfBounds } from './rubberbandIfOutOfBounds';
export { animationFadeClassNames } from './fades';
export { transformOriginClassNames } from './transformOrigin';
export {
type UseCSSTransitionState,
type UseCSSTransitionOptions,
type UseCSSTransition,
useCSSTransition,

Check warning on line 10 in packages/vkui/src/lib/animation/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/vkui/src/lib/animation/index.ts#L10

Added line #L10 was not covered by tests
} from './useCSSTransition';
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
.host {
--css-transition-duration: 1s;

inline-size: 50vh;
block-size: 50vh;
background-color: var(--vkui--color_background_accent);
border-radius: 8px;
}

.appear {
opacity: 0;
}

.appearing {
opacity: 1;
transition: opacity var(--css-transition-duration) ease-in-out;
}

.appeared {
opacity: 1;
}

.enter {
opacity: 0;
transform: translateY(-25%) rotate(25deg);
transform-origin: center center;
}

.entering {
opacity: 1;
transform: translateY(0) rotate(0deg);
transform-origin: center center;
transition:
transform var(--css-transition-duration) ease-in-out,
opacity var(--css-transition-duration) ease-in-out;
}

.entered {
opacity: 1;
}

.exit {
opacity: 1;
transform: translateY(0) rotate(0deg);
}

.exiting {
opacity: 0;
transform: translateY(-25%) rotate(25deg);
transform-origin: center center;
transition:
transform var(--css-transition-duration) ease-in-out,
opacity var(--css-transition-duration) ease-in-out;
}

.exited {
opacity: 0;
transform: translateY(-25%) rotate(25deg);
}
172 changes: 172 additions & 0 deletions packages/vkui/src/lib/animation/useCSSTransition.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/* eslint-disable no-console, import/no-default-export */
'use client';

import { type Meta, type StoryObj } from '@storybook/react';
import { classNames } from '@vkontakte/vkjs';
import { CanvasFullLayout, DisableCartesianParam } from '../../storybook/constants';
import type { CSSCustomProperties } from '../../types';
import {
useCSSTransition,
type UseCSSTransitionOptions,
type UseCSSTransitionState,
} from './useCSSTransition';
import styles from './useCSSTransition.stories.module.css';

interface DemoProps extends UseCSSTransitionOptions {
in: boolean;
duration: number;
}

const story: Meta<DemoProps> = {
title: 'DevTools/useCSSTransition',
tags: ['test'], // скрываем из публичной документации, т.к. хук внутренний
component: () => <div />,
parameters: { ...CanvasFullLayout, ...DisableCartesianParam },
argTypes: {
in: { control: { type: 'boolean' } },
enableAppear: { control: { type: 'boolean' } },
enableEnter: { control: { type: 'boolean' } },
enableExit: { control: { type: 'boolean' } },
duration: {
control: {
type: 'number',
},
table: {
defaultValue: {
summary: '⚠️ Это параметр данного Story',
},
},
},
},
args: {
duration: 1,
in: true,
enableAppear: true,
enableEnter: true,
enableExit: true,
onEnter(appear) {
console.log('onEnter', appear);
},
onEntering(appear) {
console.log('onEntering', appear);
},
onEntered(propertyName, appear) {
console.log('onEntered', propertyName, appear);
},
onExit() {
console.log('onExit');
},
onExiting() {
console.log('onExiting');
},
onExited(propertyName) {
console.log('onExited', propertyName);
},
},
};

export default story;

const transitionClassNames = {
appear: styles.appear,
appearing: styles.appearing,
appeared: styles.appeared,
enter: styles.enter,
entering: styles.entering,
entered: styles.entered,
exit: styles.exit,
exiting: styles.exiting,
exited: styles.exited,
};

export const WithClassNameAttribute: StoryObj<DemoProps> = {
render: function Render({ in: inProp, duration, ...restProps }) {
const [state, { ref, onTransitionEnd }] = useCSSTransition<HTMLDivElement>(inProp, restProps);

if (state === 'exited') {
return <div />;
}

return (
<div
ref={ref}
className={classNames(styles.host, transitionClassNames[state])}
style={
duration
? ({ '--css-transition-duration': `${duration}s` } as unknown as CSSCustomProperties)
: undefined
}
onTransitionEnd={onTransitionEnd}
/>
);
},
};

const getTransition = (state: UseCSSTransitionState, duration = 1) =>
({
appear: {
opacity: 0,
},

appearing: {
opacity: 1,
transition: `opacity ${duration}s ease-in-out`,
},

appeared: {
opacity: 1,
},

enter: {
opacity: 0,
transform: 'translateY(-25%) rotate(25deg)',
transformOrigin: 'center center',
},

entering: {
opacity: 1,
transform: 'translateY(0) rotate(0deg)',
transformOrigin: 'center center',
transition: `transform ${duration}s ease-in-out, opacity ${duration}s ease-in-out`,
},

entered: {
opacity: 1,
},

exit: {
opacity: 1,
transform: 'translateY(0) rotate(0deg)',
},

exiting: {
opacity: 0,
transform: 'translateY(-25%) rotate(25deg)',
transformOrigin: 'center center',
transition: `transform ${duration}s ease-in-out, opacity ${duration}s ease-in-out`,
},

exited: {
opacity: 0,
transform: 'translateY(-25%) rotate(25deg)',
},
})[state];

export const WithStyleAttribute: StoryObj<DemoProps> = {
render: function Render({ in: inProp, duration, ...restProps }) {
const [state, { ref, onTransitionEnd }] = useCSSTransition<HTMLDivElement>(inProp, restProps);

if (state === 'exited') {
return <div />;
}

return (
<div
ref={ref}
className={styles.host}
style={getTransition(state, duration)}
onTransitionEnd={onTransitionEnd}
/>
);
},
};
Loading