Skip to content

Commit

Permalink
feat: initial={false} (#12)
Browse files Browse the repository at this point in the history
* yeet

* Update CHANGELOG.md
  • Loading branch information
judehunter authored Sep 8, 2023
1 parent 75f72a3 commit 2bd9326
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Added

- `initial={false}`, allows you to skip the initial animation when the component mounts.

In short, it takes the first value of `animate` and applies it as styles immediately.

```jsx
return (
<m.div
initial={false}
animate={{width}}
/>
/* equivalent to initial={{width}} */
)
```

## 0.0.2 - 2023-08-12

### Added
Expand Down
11 changes: 8 additions & 3 deletions packages/motioned/src/m.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
matchAgainstVariants,
matchAnimatePropertyNameToCssVariableName,
mapEasingOrEasingList,
stripTransition,
} from './utils.js';
import {
AnimateOptions,
Expand Down Expand Up @@ -227,25 +228,29 @@ const makeMElem = <TElement extends keyof React.JSX.IntrinsicElements>(
variants,
...rest
}: {
initial?: AnimateProperties | NoInfer<TVariants>;
initial?: AnimateProperties | NoInfer<TVariants> | false;
animate: AnimateOptions | NoInfer<TVariants>;
variants?: Variants<TVariants>;
ref?: React.Ref<ElementTypeOf<TElement>>;
} & ComponentProps<TElement>,
ref: ForwardedRef<TElement>,
) => {
const initialOrAnimate = stripTransition(
initial === false ? animate : initial,
);
const memodMatchedInitial = useMemo(
() =>
initial
initialOrAnimate
? animatePropertiesToStyle(
matchAgainstVariants(
variants,
initial as AnimateOptions | string,
initialOrAnimate as AnimateOptions | string,
) as AnimateProperties,
)
: {},
[],
);

const elem = useRef<HTMLDivElement>(null!);

useAnimation(elem, animate, variants);
Expand Down
9 changes: 9 additions & 0 deletions packages/motioned/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,12 @@ export const bezierDefinitionToEasing = (
}
return `cubic-bezier(${definition.join(',')})`;
};

export const stripTransition = <
T extends AnimateOptions | AnimateProperties | undefined,
>(
x: T,
): Omit<T, 'transition'> => {
const { transition, ...rest }: any = x ?? {};
return rest;
};
2 changes: 1 addition & 1 deletion packages/website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev --port 3001",
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
Expand Down

0 comments on commit 2bd9326

Please sign in to comment.