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

toggle reverse on useTransitions #1896

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion demo/src/sandboxes/chain/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export default function App() {
from: { opacity: 0, scale: 0 },
enter: { opacity: 1, scale: 1 },
leave: { opacity: 0, scale: 0 },
toggleReverse: true
})

// This will orchestrate the two animations above, comment the last arg and it creates a sequence
Expand All @@ -51,7 +52,7 @@ export default function App() {
className={styles.item}
style={{ ...style, background: item.css }}
/>
))}
), !open)}
</animated.div>
</div>
)
Expand Down
10 changes: 8 additions & 2 deletions packages/core/src/hooks/useTransition.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export function useTransition(
expires = true,
exitBeforeEnter = false,
onDestroyed,
toggleReverse = false,
ref: propsRef,
config: propsConfig,
}: UseTransitionProps<any> = propsFn ? propsFn() : props
Expand Down Expand Up @@ -206,6 +207,11 @@ export function useTransition(
const exitingTransitions = useRef(new Map<TransitionState, Change>())

const forceChange = useRef(false)

if (toggleReverse) {
transitions.reverse()
}

each(transitions, (t, i) => {
const key = t.key
const prevPhase = t.phase
Expand Down Expand Up @@ -425,9 +431,9 @@ export function useTransition(
reset ? void 0 : deps
)

const renderTransitions: TransitionFn = render => (
const renderTransitions: TransitionFn = (render, reverse = false) => (
<>
{transitions.map((t, i) => {
{(reverse ? transitions.reverse() : transitions).map((t, i) => {
const { springs } = changes.get(t) || t.ctrl
const elem: any = render({ ...springs }, t.item, t, i)
return elem && elem.type ? (
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/types/transition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export type UseTransitionProps<Item = any> = Merge<
leave?: TransitionTo<Item>
keys?: ItemKeys<Item>
sort?: (a: Item, b: Item) => number
toggleReverse?: boolean
trail?: number
exitBeforeEnter?: boolean
/**
Expand Down Expand Up @@ -111,7 +112,7 @@ export type ItemKeys<T = any> = OneOrMore<Key> | ((item: T) => Key) | null

/** The function returned by `useTransition` */
export interface TransitionFn<Item = any, State extends Lookup = Lookup> {
(render: TransitionRenderFn<Item, State>): JSX.Element
(render: TransitionRenderFn<Item, State>, reverse?: boolean): JSX.Element
}

export interface TransitionRenderFn<Item = any, State extends Lookup = Lookup> {
Expand Down