Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
react-router-transitions-test
Advanced tools
THIS IS A TEST, I DO NOT OWN THIS CODE, NOR DID I WRITE ANY OF IT. I NEED TO HAVE THIS FORKED BRANCH AND IT WONT INSTALL FROM GIT FOR SOME REASON SO I'M TRYING TO PUBLISH IT TO NPM.
Brings transitions to react-router.
npm install --save react-router-transitions
The main goal of this module is to handle history navigation by providing the correct animation if the user goes back or goes forward. It is a requirement especially on mobile to provide a great user experience.
The requirement to use this module is to set up transition context at the root level
of your application using useTransitions
.
Then you can enable transitions at several levels of your application using withTransition
.
In a simple application, you should only wrap your root component.
import React from 'react';
import {Router, Route, browserHistory, applyRouterMiddleware} from 'react-router';
import {useTransitions, withTransition} from 'react-router-transitions';
import CSSTransitionGroup from 'react-transition-group';
import App from './App';
import Home from './Home';
import AboutUs from './AboutUs';
export default () => (
<Router
history={browserHistory}
render={applyRouterMiddleware(useTransitions({
TransitionGroup: ReactCSSTransitionGroup,
defaultTransition: {
transitionName: 'fade',
transitionEnterTimeout: 500,
transitionLeaveTimeout: 300
}
}))}
>
<Route path="/" component={withTransition(App)}>
<Route path="home" component={Home} />
<Route path="about-us" component={AboutUs} />
</Route>
</Router>
);
If you are familiar with react-router, you have probably already used methods like router.push
or router.replace
.
Starting now, you should not use them to navigate into your application.
You have to think your navigation in a view logic, just like in a mobile application.
To make it possible, a new property is now accessible in the transitionRouter
context. This property has three main methods: show
, dismiss
and swap
.
Go to a location using a show
animation.
You have to use this method each time you want to display a new view.
It adds a new entry in the history to give the user the ability to use the back button.
Internally, it uses the router.push
method.
Go to a location using a dismiss
animation.
You have to use this method when you want to hide a view (e.g. simulate a close or a go back).
The options
argument accept an object.
depth
: default to 1. Is the number of history records you want to revert.It doesn't add a new entry in the history.
Internally, if we have an history, it uses the router.go(-n)
method, otherwise it uses transitionRouter.swap
with a dismiss
action.
Go to a location using the default transition or the transition specified in the second argument. You have to use this method if you want to change route without affecting the history (e.g. tabs, accordion, etc).
It doesn't add a new entry in the history.
Internally, it merges the new location with the current one and uses router.replace
.
The transitionAction
is added to the location state if specified. Available values are "show" or "dismiss".
You can specify a transition associated with a view directly in the location state.
You have to specify two transition configurations: showTransition
and dismissTransition
.
If you specify these properties, they will be the transition used to show or dismiss the view.
import React, {Component} from 'react';
import PropTypes from 'prop-types';
export default class Home extends Component {
static contextTypes = {
transitionRouter: PropTypes.object,
};
onClickAboutUs(event) {
event.preventDefault();
this.context.transitionRouter.show({
pathname: '/about-us',
state: {
showTransition: {
transitionName: 'show-from-top',
transitionEnterTimeout: 500,
transitionLeaveTimeout: 300,
},
dismissTransition: {
transitionName: 'dismiss-from-top',
transitionEnterTimeout: 500,
transitionLeaveTimeout: 300,
},
},
});
}
render() {
return (
<div className="home">
<a href="/about-us" onClick={this.onClickAboutUs}>
About us
</a>
</div>
);
}
}
Available options are:
TransitionGroup
: The transition group component used to make transition. You can use any kind of transition group, ReactCSSTransitionGroup being the default.
Transition specified in showTransition
, dismissTransition
or defaultTransition
are the properties used to render your TransitionGroup
.defaultTransition
: The default transition applied on the component. The default transition is applied
when no transition is specified or when we can't determine the type of transition to apply.onShow
: Hook function called after a "show" action has been requested.onDismiss
: Hook function called after a "dismiss" action has been requested.getComponentKey
: Function used to generate the component key. Defaults to the complete route path.This method has to be called in the render method of the Router component.
<Router
history={browserHistory}
render={applyRouterMiddleware(useTransitions({
TransitionGroup: ReactCSSTransitionGroup,
defaultTransition: {
transitionName: 'fade',
transitionEnterTimeout: 500,
transitionLeaveTimeout: 300
}
}))}
/>
Enable transitions in the component (children of the component will be animated).
The config provided is merged with the config provided into useTransitions
.
In a simple application, this high order component has to be applied at the root level.
<Route path="/" component={withTransition(App)}>
<Route path="home" component={Home} />
<Route path="about-us" component={AboutUs} />
</Route>
You can use hooks to determine transitions automatically without having to specify it in every "show" or "dismiss".
Example:
<Router
history={browserHistory}
render={applyRouterMiddleware(useTransitions({
TransitionGroup: ReactCSSTransitionGroup,
onShow(prevState, nextState, replaceTransition) {
replaceTransition({
transitionName: `show-${nextState.children.props.route.transition}`,
transitionEnterTimeout: 500,
transitionLeaveTimeout: 300,
});
},
onDismiss(prevState, nextState, replaceTransition) {
replaceTransition({
transitionName: `dismiss-${prevState.children.props.route.transition}`,
transitionEnterTimeout: 500,
transitionLeaveTimeout: 300,
});
},
defaultTransition: {
transitionName: 'fade',
transitionEnterTimeout: 500,
transitionLeaveTimeout: 300,
},
}))}
>
<Route path="/" component={withTransition(App)}>
<Route path="home" component={Home} transition="from-right" />
<Route path="about-us" component={AboutUs} transition="from-top" />
</Route>
</Router>
If the animation is not played, one of the reason could be that your key is not different from the key of the last location.
To fix that, you can create a custom component by specifying getComponentKey
in the configuration.
By default, the key used is the full path to the current route. Params are ignored.
Example:
<Route path="books/:id" component={withTransition(BooksDetail, {
getComponentKey(child, props) {
return `books/${child.props.routeParams.id}`;
}
})}
React router transitions work with every histories available in react-router and available in history.
The only requirement to use this module with a custom history is to set an "action" and a "key" property in location. You can refer to history to see examples.
MIT
FAQs
Brings transitions to react-router
The npm package react-router-transitions-test receives a total of 1 weekly downloads. As such, react-router-transitions-test popularity was classified as not popular.
We found that react-router-transitions-test demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.