Skip to content

Commit

Permalink
add useSynchronizedAnimation to storybook, and export
Browse files Browse the repository at this point in the history
  • Loading branch information
Barsnes committed Jun 25, 2024
1 parent b7cc4ce commit bbc79f9
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 3 deletions.
1 change: 1 addition & 0 deletions apps/storybook/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ const preview: Preview = {
'Kom i gang',
'Endringslogger',
'Komponenter',
'Hooks',
'Experimental',
'Primitives',
'Avviklet',
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { useMediaQuery } from './useMediaQuery';
export { usePrevious } from './usePrevious';
export { useSynchronizedAnimation } from './useSynchronizedAnimation';
export { useSynchronizedAnimation } from './useSynchronizedAnimation/useSynchronizedAnimation';
export { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect';
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Meta, Primary, ArgTypes } from '@storybook/blocks';

import * as SyncStories from './useSynchronizedAnimation.stories';
import { useSynchronizedAnimation } from '../';

<Meta of={SyncStories} />

# useSynchronizedAnimation

En react hook for å synkronisere CSS animasjoner på tvers av flere elementer.

<Primary />

Du sender inn navnet på animasjonen du vil synkronisere.
Hooken returnerer en `ref`, som du sender til elementet ditt.
Animasjonen på elementet ditt blir synkronsisert til den første animasjonen med samme navn på siden.

## Bruk

```tsx
const SyncedBox = () => {
const ref = useSynchronizedAnimation<HTMLDivElement>('spin');

return <div ref={ref} />;
};
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import type { Meta, StoryFn } from '@storybook/react';
import { useState } from 'react';

import { Button } from '../../components';

import { useSynchronizedAnimation } from './useSynchronizedAnimation';

const decorators = [
(Story: StoryFn) => (
<div
style={{
display: 'flex',
flexDirection: 'column',
gap: '20px',
alignItems: 'center',
justifyContent: 'center',
padding: '20px',
}}
>
<Story />
</div>
),
];

const meta: Meta = {
title: 'Hooks/useSynchronizedAnimation',
decorators,
};

export default meta;

const boxStyle = {
width: '30px',
height: '30px',
backgroundColor: 'red',
};

const SyncedBox = () => {
const ref = useSynchronizedAnimation<HTMLDivElement>('spin');

return (
<div
ref={ref}
style={{
animation: 'spin 2s linear infinite',
...boxStyle,
}}
/>
);
};

export const TestSync = () => {
const [count, setCount] = useState(1);

return (
<>
<Button onClick={() => setCount(count + 1)}>Ny boks</Button>
<div
style={{
display: 'flex',
gap: '10px',
maxWidth: '300px',
flexWrap: 'wrap',
}}
>
{Array.from({ length: count }).map((_, i) => (
<SyncedBox key={i} />
))}
</div>

<style>
{`
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
`}
</style>
</>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,20 @@

import { useRef } from 'react';

import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect';

import { useIsomorphicLayoutEffect } from '../useIsomorphicLayoutEffect';

/**
* Synchronizes the css animation of multiple elements with the same `animationName`.
*
* @example
* ```tsx
* const ref = useSynchronizedAnimation<HTMLDivElement>('spin');
*
* <div
* ref={ref}
* />
* ```
*/
export function useSynchronizedAnimation<T>(animationName: string) {
const ref = useRef<T>(null);

Expand Down
1 change: 1 addition & 0 deletions packages/react/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './components';
export { useSynchronizedAnimation } from './hooks';

0 comments on commit bbc79f9

Please sign in to comment.