-
Notifications
You must be signed in to change notification settings - Fork 30
feat(components): Allow Configuration of AnimatedPresence #2898
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,15 @@ | ||
| import type { PropsWithChildren } from "react"; | ||
| import React, { Children, useEffect } from "react"; | ||
| import type { tokens } from "@jobber/design"; | ||
| import { AnimatePresence, motion, useReducedMotion } from "framer-motion"; | ||
| import { | ||
| TIMING_BASE, | ||
| TIMING_LOADING, | ||
| TIMING_LOADING_EXTENDED, | ||
| TIMING_QUICK, | ||
| TIMING_SLOW, | ||
| TIMING_SLOWER, | ||
| TIMING_SLOWEST, | ||
| fade, | ||
| fromBottom, | ||
| fromLeft, | ||
|
|
@@ -15,6 +21,16 @@ import { | |
| } from "./AnimatedPresence.transitions"; | ||
| import { usePreviousValue } from "./hooks/usePreviousValue"; | ||
|
|
||
| const timingMap = { | ||
| "timing-base": TIMING_BASE, | ||
| "timing-quick": TIMING_QUICK, | ||
| "timing-slow": TIMING_SLOW, | ||
| "timing-slower": TIMING_SLOWER, | ||
| "timing-slowest": TIMING_SLOWEST, | ||
| "timing-loading": TIMING_LOADING, | ||
| "timing-loading--extended": TIMING_LOADING_EXTENDED, | ||
| }; | ||
|
|
||
| const transitions = { | ||
| fromBottom, | ||
| fromTop, | ||
|
|
@@ -26,18 +42,46 @@ const transitions = { | |
| fade, | ||
| }; | ||
|
|
||
| const exitBehaviorMap: Record<ExitBehavior, "wait" | "sync" | "popLayout"> = { | ||
| overlap: "sync", | ||
| replace: "popLayout", | ||
| sequential: "wait", | ||
| }; | ||
|
|
||
| type Timing = { | ||
| [K in keyof typeof tokens]: K extends `timing-${string}` ? K : never; | ||
| }[keyof typeof tokens]; | ||
|
|
||
| export type AnimatedPresenceTransitions = keyof typeof transitions; | ||
|
|
||
| type ExitBehavior = "overlap" | "replace" | "sequential"; | ||
|
|
||
| interface AnimatedPresenceProps extends Required<PropsWithChildren> { | ||
| /** | ||
| * The type of transition you can use. | ||
| */ | ||
| readonly transition?: AnimatedPresenceTransitions; | ||
| /** | ||
| * The timing of the animation. | ||
| */ | ||
| readonly timing?: Timing; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not 100% on this interface you'll be passing values like "timing-slow" rather than just "slow" buut it is derived from the tokens which is nice. the only time it might become a hindrance is if we change the token values, the props would need to get fixed in the invocations. |
||
|
|
||
| /** | ||
| * The mode of the animation. | ||
| * | ||
| * @default "popLayout" | ||
| */ | ||
| readonly exitBehavior?: ExitBehavior; | ||
|
|
||
| /** | ||
| * Whether or not to animate the children on mount. By default it's set to false. | ||
| */ | ||
| readonly initial?: boolean; | ||
|
|
||
| /** | ||
| * Callback called when all exiting elements have completed their animation. | ||
| */ | ||
| readonly onExitComplete?: () => void; | ||
| } | ||
|
|
||
| export function AnimatedPresence(props: AnimatedPresenceProps) { | ||
|
|
@@ -53,7 +97,10 @@ export function AnimatedPresence(props: AnimatedPresenceProps) { | |
| function InternalAnimatedPresence({ | ||
| transition = "fromTop", | ||
| initial = false, | ||
| exitBehavior = "replace", | ||
| children, | ||
| timing = "timing-base", | ||
| onExitComplete, | ||
| }: AnimatedPresenceProps) { | ||
| const transitionVariation = transitions[transition]; | ||
| const hasInitialTransition = "initial" in transitionVariation; | ||
|
|
@@ -69,7 +116,11 @@ function InternalAnimatedPresence({ | |
| }, [childCount]); | ||
|
|
||
| return ( | ||
| <AnimatePresence initial={initial} mode="popLayout"> | ||
| <AnimatePresence | ||
| initial={initial} | ||
| mode={exitBehaviorMap[exitBehavior]} | ||
| onExitComplete={onExitComplete} | ||
| > | ||
| {Children.map( | ||
| children, | ||
| (child, i) => | ||
|
|
@@ -81,7 +132,7 @@ function InternalAnimatedPresence({ | |
| animate="visible" | ||
| exit="hidden" | ||
| transition={{ | ||
| duration: TIMING_BASE, | ||
| duration: timingMap[timing], | ||
| delay: generateDelayTime(i), | ||
| }} | ||
| > | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let me know if these are good, clear names.
I had a couple other ideas to describe "overlap" (crossfade)
and "immediate" instead of "replace"
the actual framer motion values they map to are:
and here's their descriptions