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

Create useFlip hook #183 #215

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions packages/react-animation/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './useExposeAnimation/useExposeAnimation.js';
export * from './useExposedAnimation/useExposedAnimation.js';
export * from './useExposedAnimations/useExposedAnimations.js';
export * from './useScrollAnimation/useScrollAnimation.js';
export * from './useFlip/useFlip.js';
46 changes: 46 additions & 0 deletions packages/react-animation/src/useFlip/useFlip.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Meta, Canvas } from '@storybook/blocks';
import * as stories from './useFlip.stories';

<Meta title="components/useFlip" of={stories} />

# useFlip
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unclear from the (non-existing) api docs:

  • what I can pass as first parameter (can it be both an element, or also a ref around an element?)
  • what I can pass as second parameter (some gsap flip props?)
  • If the second param is optional or required.


Use to flip a components state from one value to another.

```tsx
function MyComponent(): ReactElement {
const [isFlipped, toggle] = useToggle(false);

const divRef = useRef<HTMLDivElement>(null);
useFlip(divRef, flipOptions);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flipOptions is not defined anywhere


useEventListener(globalThis.document, 'click', () => {
toggle();
});

return (
<div
ref={divRef}
style={
isFlipped
? {
...styles,
position: 'absolute',
insetBlock: 0,
insetInlineStart: '50%',
insetInlineEnd: 0,
}
: {
...styles,
inlineSize: 150,
blockSize: 150,
}
}
/>
);
}
```

### Demo

<Canvas of={stories.Default} />
63 changes: 63 additions & 0 deletions packages/react-animation/src/useFlip/useFlip.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* eslint-disable react/jsx-no-literals */
import { useEventListener, useToggle } from '@mediamonks/react-hooks';
import type { Meta, StoryObj } from '@storybook/react';
import { useRef, type CSSProperties, type ReactElement } from 'react';
import { useFlip } from './useFlip.js';

const meta = {
title: 'hooks/useFlip',
} as Meta;

export default meta;

type Story = StoryObj<typeof meta>;

const flipOptions = {
ease: 'power2.inOut',
} satisfies Flip.FromToVars;

const styles = {
backgroundColor: 'royalblue',
} satisfies CSSProperties;

export const Default: Story = {
render(): ReactElement {
const [isFlipped, toggle] = useToggle(false);
const div1Ref = useRef<HTMLDivElement>(null);
const div2Ref = useRef<HTMLDivElement>(null);

useFlip(div1Ref, flipOptions);
useFlip(div2Ref, flipOptions);

useEventListener(globalThis.document, 'click', () => {
toggle();
});

return (
<>
<p>
Click to rerender, the box will fill the right side of the screen using position absolute.
</p>
<div
ref={div1Ref}
style={
isFlipped
? {
...styles,
position: 'absolute',
insetBlock: 0,
insetInlineStart: '50%',
insetInlineEnd: 0,
}
: {
...styles,
inlineSize: 150,
blockSize: 150,
}
}
/>
<p ref={div2Ref}>The element renders inline</p>
</>
);
},
};
26 changes: 26 additions & 0 deletions packages/react-animation/src/useFlip/useFlip.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { unref, type Unreffable } from '@mediamonks/react-hooks';
import gsap from 'gsap';
import Flip from 'gsap/Flip';
import { useEffect, useRef, type MutableRefObject } from 'react';

gsap.registerPlugin(Flip);

export function useFlip(
ref: Unreffable<HTMLElement | null>,
flipStateVariables: Flip.FromToVars = {},
): MutableRefObject<Flip.FlipState | undefined> {
const flipStateRef = useRef<Flip.FlipState>();

// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (globalThis.window !== undefined) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be useful (for others, or future self), to add some comments explaining how/why this works.

I assume that:

  • the if executes before potentially new style vars are set, capturing the "current" state.
  • the useEffect executes after applying and rendering the new style vars, and then transitioning to them from the previously captured state.

flipStateRef.current = Flip.getState(unref(ref));
}

useEffect(() => {
if (flipStateRef.current) {
Flip.from(flipStateRef.current, flipStateVariables);
}
});

return flipStateRef;
}
Loading