Skip to content
This repository has been archived by the owner on Sep 20, 2023. It is now read-only.

Commit

Permalink
feat: add trigger prop to TransitionLink.
Browse files Browse the repository at this point in the history
This prop takes an async function with the entering and exiting pages as it's arguments. This allows awaiting each node so that a single transition can be made using both nodes. It allows for flip animations between pages.
  • Loading branch information
Tyler Barnes committed Mar 26, 2019
1 parent 04f9e8b commit 08d303b
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/components/TransitionHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default class TransitionHandler extends Component {
transitionIdHistory,
inTransition,
updateContext,
triggerResolve,
e
}) => {
return (
Expand Down Expand Up @@ -61,6 +62,7 @@ export default class TransitionHandler extends Component {
exitProps,
pathname,
updateContext,
triggerResolve,
e
})
}
Expand All @@ -73,6 +75,7 @@ export default class TransitionHandler extends Component {
exitTrigger,
entryProps,
exitProps,
triggerResolve,
e
})
}
Expand Down
8 changes: 5 additions & 3 deletions src/components/TransitionLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const TransitionLink = ({
style,
className,
onClick,
trigger,
...rest
}) => {
return (
Expand All @@ -29,11 +30,12 @@ const TransitionLink = ({
to,
exit,
entry,
trigger,
...context
})
});

if (typeof onClick === 'function') {
onClick(event)
if (typeof onClick === "function") {
onClick(event);
}
}}
to={to} // use gatsby link so prefetching still happens. this is prevent defaulted in triggertransition
Expand Down
23 changes: 23 additions & 0 deletions src/context/InternalProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,29 @@ class InternalProvider extends Component {
updateContext: obj => this.setState(obj)
};

componentDidMount() {
let exitResolve;
const exitPromise = new Promise(resolve => {
exitResolve = resolve;
});

let entryResolve;
const entryPromise = new Promise(resolve => {
entryResolve = resolve;
});

this.state.updateContext({
triggerResolve: {
entry: entryResolve,
exit: exitResolve
},
pages: {
exit: exitPromise,
entry: entryPromise
}
});
}

render() {
return <Provider value={this.state}>{this.props.children}</Provider>;
}
Expand Down
8 changes: 8 additions & 0 deletions src/functions/onEnter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const onEnter = ({
entryTrigger,
entryProps,
exitProps,
triggerResolve,
pathname,
e
}) => {
Expand All @@ -17,6 +18,13 @@ const onEnter = ({

if (!inTransition) return;

const { trigger: removed, ...entryPropsTrimmed } = entryProps;

triggerResolve.entry({
...entryPropsTrimmed,
node
});

entryTrigger &&
typeof entryTrigger === "function" &&
entryTrigger({
Expand Down
8 changes: 8 additions & 0 deletions src/functions/onExit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@ const onExit = ({
exitTrigger,
entryProps,
exitProps,
triggerResolve,
e
}) => {
if (!inTransition) return;

const { trigger: removed, ...exitPropsTrimmed } = exitProps;

triggerResolve.exit({
...exitPropsTrimmed,
node
});

return (
exitTrigger &&
typeof exitTrigger === "function" &&
Expand Down
4 changes: 4 additions & 0 deletions src/utils/triggerTransition.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const triggerTransition = ({
entry = {},
inTransition,
transitionIdHistory,
pages,
trigger,
updateContext
}) => {
event.persist();
Expand All @@ -28,6 +30,8 @@ const triggerTransition = ({
exitState: {}
});

trigger(pages);

const {
length: exitLength = 0,
delay: exitDelay = 0,
Expand Down

0 comments on commit 08d303b

Please sign in to comment.