123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585 |
- import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
- import _inheritsLoose from "@babel/runtime/helpers/esm/inheritsLoose";
- import PropTypes from 'prop-types';
- import React from 'react';
- import ReactDOM from 'react-dom';
- import config from './config';
- import { timeoutsShape } from './utils/PropTypes';
- import TransitionGroupContext from './TransitionGroupContext';
- export var UNMOUNTED = 'unmounted';
- export var EXITED = 'exited';
- export var ENTERING = 'entering';
- export var ENTERED = 'entered';
- export var EXITING = 'exiting';
- /**
- * The Transition component lets you describe a transition from one component
- * state to another _over time_ with a simple declarative API. Most commonly
- * it's used to animate the mounting and unmounting of a component, but can also
- * be used to describe in-place transition states as well.
- *
- * ---
- *
- * **Note**: `Transition` is a platform-agnostic base component. If you're using
- * transitions in CSS, you'll probably want to use
- * [`CSSTransition`](https://reactcommunity.org/react-transition-group/css-transition)
- * instead. It inherits all the features of `Transition`, but contains
- * additional features necessary to play nice with CSS transitions (hence the
- * name of the component).
- *
- * ---
- *
- * By default the `Transition` component does not alter the behavior of the
- * component it renders, it only tracks "enter" and "exit" states for the
- * components. It's up to you to give meaning and effect to those states. For
- * example we can add styles to a component when it enters or exits:
- *
- * ```jsx
- * import { Transition } from 'react-transition-group';
- *
- * const duration = 300;
- *
- * const defaultStyle = {
- * transition: `opacity ${duration}ms ease-in-out`,
- * opacity: 0,
- * }
- *
- * const transitionStyles = {
- * entering: { opacity: 1 },
- * entered: { opacity: 1 },
- * exiting: { opacity: 0 },
- * exited: { opacity: 0 },
- * };
- *
- * const Fade = ({ in: inProp }) => (
- * <Transition in={inProp} timeout={duration}>
- * {state => (
- * <div style={{
- * ...defaultStyle,
- * ...transitionStyles[state]
- * }}>
- * I'm a fade Transition!
- * </div>
- * )}
- * </Transition>
- * );
- * ```
- *
- * There are 4 main states a Transition can be in:
- * - `'entering'`
- * - `'entered'`
- * - `'exiting'`
- * - `'exited'`
- *
- * Transition state is toggled via the `in` prop. When `true` the component
- * begins the "Enter" stage. During this stage, the component will shift from
- * its current transition state, to `'entering'` for the duration of the
- * transition and then to the `'entered'` stage once it's complete. Let's take
- * the following example (we'll use the
- * [useState](https://reactjs.org/docs/hooks-reference.html#usestate) hook):
- *
- * ```jsx
- * function App() {
- * const [inProp, setInProp] = useState(false);
- * return (
- * <div>
- * <Transition in={inProp} timeout={500}>
- * {state => (
- * // ...
- * )}
- * </Transition>
- * <button onClick={() => setInProp(true)}>
- * Click to Enter
- * </button>
- * </div>
- * );
- * }
- * ```
- *
- * When the button is clicked the component will shift to the `'entering'` state
- * and stay there for 500ms (the value of `timeout`) before it finally switches
- * to `'entered'`.
- *
- * When `in` is `false` the same thing happens except the state moves from
- * `'exiting'` to `'exited'`.
- */
-
- var Transition =
- /*#__PURE__*/
- function (_React$Component) {
- _inheritsLoose(Transition, _React$Component);
-
- function Transition(props, context) {
- var _this;
-
- _this = _React$Component.call(this, props, context) || this;
- var parentGroup = context; // In the context of a TransitionGroup all enters are really appears
-
- var appear = parentGroup && !parentGroup.isMounting ? props.enter : props.appear;
- var initialStatus;
- _this.appearStatus = null;
-
- if (props.in) {
- if (appear) {
- initialStatus = EXITED;
- _this.appearStatus = ENTERING;
- } else {
- initialStatus = ENTERED;
- }
- } else {
- if (props.unmountOnExit || props.mountOnEnter) {
- initialStatus = UNMOUNTED;
- } else {
- initialStatus = EXITED;
- }
- }
-
- _this.state = {
- status: initialStatus
- };
- _this.nextCallback = null;
- return _this;
- }
-
- Transition.getDerivedStateFromProps = function getDerivedStateFromProps(_ref, prevState) {
- var nextIn = _ref.in;
-
- if (nextIn && prevState.status === UNMOUNTED) {
- return {
- status: EXITED
- };
- }
-
- return null;
- }; // getSnapshotBeforeUpdate(prevProps) {
- // let nextStatus = null
- // if (prevProps !== this.props) {
- // const { status } = this.state
- // if (this.props.in) {
- // if (status !== ENTERING && status !== ENTERED) {
- // nextStatus = ENTERING
- // }
- // } else {
- // if (status === ENTERING || status === ENTERED) {
- // nextStatus = EXITING
- // }
- // }
- // }
- // return { nextStatus }
- // }
-
-
- var _proto = Transition.prototype;
-
- _proto.componentDidMount = function componentDidMount() {
- this.updateStatus(true, this.appearStatus);
- };
-
- _proto.componentDidUpdate = function componentDidUpdate(prevProps) {
- var nextStatus = null;
-
- if (prevProps !== this.props) {
- var status = this.state.status;
-
- if (this.props.in) {
- if (status !== ENTERING && status !== ENTERED) {
- nextStatus = ENTERING;
- }
- } else {
- if (status === ENTERING || status === ENTERED) {
- nextStatus = EXITING;
- }
- }
- }
-
- this.updateStatus(false, nextStatus);
- };
-
- _proto.componentWillUnmount = function componentWillUnmount() {
- this.cancelNextCallback();
- };
-
- _proto.getTimeouts = function getTimeouts() {
- var timeout = this.props.timeout;
- var exit, enter, appear;
- exit = enter = appear = timeout;
-
- if (timeout != null && typeof timeout !== 'number') {
- exit = timeout.exit;
- enter = timeout.enter; // TODO: remove fallback for next major
-
- appear = timeout.appear !== undefined ? timeout.appear : enter;
- }
-
- return {
- exit: exit,
- enter: enter,
- appear: appear
- };
- };
-
- _proto.updateStatus = function updateStatus(mounting, nextStatus) {
- if (mounting === void 0) {
- mounting = false;
- }
-
- if (nextStatus !== null) {
- // nextStatus will always be ENTERING or EXITING.
- this.cancelNextCallback();
- var node = ReactDOM.findDOMNode(this);
-
- if (nextStatus === ENTERING) {
- this.performEnter(node, mounting);
- } else {
- this.performExit(node);
- }
- } else if (this.props.unmountOnExit && this.state.status === EXITED) {
- this.setState({
- status: UNMOUNTED
- });
- }
- };
-
- _proto.performEnter = function performEnter(node, mounting) {
- var _this2 = this;
-
- var enter = this.props.enter;
- var appearing = this.context ? this.context.isMounting : mounting;
- var timeouts = this.getTimeouts();
- var enterTimeout = appearing ? timeouts.appear : timeouts.enter; // no enter animation skip right to ENTERED
- // if we are mounting and running this it means appear _must_ be set
-
- if (!mounting && !enter || config.disabled) {
- this.safeSetState({
- status: ENTERED
- }, function () {
- _this2.props.onEntered(node);
- });
- return;
- }
-
- this.props.onEnter(node, appearing);
- this.safeSetState({
- status: ENTERING
- }, function () {
- _this2.props.onEntering(node, appearing);
-
- _this2.onTransitionEnd(node, enterTimeout, function () {
- _this2.safeSetState({
- status: ENTERED
- }, function () {
- _this2.props.onEntered(node, appearing);
- });
- });
- });
- };
-
- _proto.performExit = function performExit(node) {
- var _this3 = this;
-
- var exit = this.props.exit;
- var timeouts = this.getTimeouts(); // no exit animation skip right to EXITED
-
- if (!exit || config.disabled) {
- this.safeSetState({
- status: EXITED
- }, function () {
- _this3.props.onExited(node);
- });
- return;
- }
-
- this.props.onExit(node);
- this.safeSetState({
- status: EXITING
- }, function () {
- _this3.props.onExiting(node);
-
- _this3.onTransitionEnd(node, timeouts.exit, function () {
- _this3.safeSetState({
- status: EXITED
- }, function () {
- _this3.props.onExited(node);
- });
- });
- });
- };
-
- _proto.cancelNextCallback = function cancelNextCallback() {
- if (this.nextCallback !== null) {
- this.nextCallback.cancel();
- this.nextCallback = null;
- }
- };
-
- _proto.safeSetState = function safeSetState(nextState, callback) {
- // This shouldn't be necessary, but there are weird race conditions with
- // setState callbacks and unmounting in testing, so always make sure that
- // we can cancel any pending setState callbacks after we unmount.
- callback = this.setNextCallback(callback);
- this.setState(nextState, callback);
- };
-
- _proto.setNextCallback = function setNextCallback(callback) {
- var _this4 = this;
-
- var active = true;
-
- this.nextCallback = function (event) {
- if (active) {
- active = false;
- _this4.nextCallback = null;
- callback(event);
- }
- };
-
- this.nextCallback.cancel = function () {
- active = false;
- };
-
- return this.nextCallback;
- };
-
- _proto.onTransitionEnd = function onTransitionEnd(node, timeout, handler) {
- this.setNextCallback(handler);
- var doesNotHaveTimeoutOrListener = timeout == null && !this.props.addEndListener;
-
- if (!node || doesNotHaveTimeoutOrListener) {
- setTimeout(this.nextCallback, 0);
- return;
- }
-
- if (this.props.addEndListener) {
- this.props.addEndListener(node, this.nextCallback);
- }
-
- if (timeout != null) {
- setTimeout(this.nextCallback, timeout);
- }
- };
-
- _proto.render = function render() {
- var status = this.state.status;
-
- if (status === UNMOUNTED) {
- return null;
- }
-
- var _this$props = this.props,
- children = _this$props.children,
- childProps = _objectWithoutPropertiesLoose(_this$props, ["children"]); // filter props for Transtition
-
-
- delete childProps.in;
- delete childProps.mountOnEnter;
- delete childProps.unmountOnExit;
- delete childProps.appear;
- delete childProps.enter;
- delete childProps.exit;
- delete childProps.timeout;
- delete childProps.addEndListener;
- delete childProps.onEnter;
- delete childProps.onEntering;
- delete childProps.onEntered;
- delete childProps.onExit;
- delete childProps.onExiting;
- delete childProps.onExited;
-
- if (typeof children === 'function') {
- // allows for nested Transitions
- return React.createElement(TransitionGroupContext.Provider, {
- value: null
- }, children(status, childProps));
- }
-
- var child = React.Children.only(children);
- return (// allows for nested Transitions
- React.createElement(TransitionGroupContext.Provider, {
- value: null
- }, React.cloneElement(child, childProps))
- );
- };
-
- return Transition;
- }(React.Component);
-
- Transition.contextType = TransitionGroupContext;
- Transition.propTypes = process.env.NODE_ENV !== "production" ? {
- /**
- * A `function` child can be used instead of a React element. This function is
- * called with the current transition status (`'entering'`, `'entered'`,
- * `'exiting'`, `'exited'`), which can be used to apply context
- * specific props to a component.
- *
- * ```jsx
- * <Transition in={this.state.in} timeout={150}>
- * {state => (
- * <MyComponent className={`fade fade-${state}`} />
- * )}
- * </Transition>
- * ```
- */
- children: PropTypes.oneOfType([PropTypes.func.isRequired, PropTypes.element.isRequired]).isRequired,
-
- /**
- * Show the component; triggers the enter or exit states
- */
- in: PropTypes.bool,
-
- /**
- * By default the child component is mounted immediately along with
- * the parent `Transition` component. If you want to "lazy mount" the component on the
- * first `in={true}` you can set `mountOnEnter`. After the first enter transition the component will stay
- * mounted, even on "exited", unless you also specify `unmountOnExit`.
- */
- mountOnEnter: PropTypes.bool,
-
- /**
- * By default the child component stays mounted after it reaches the `'exited'` state.
- * Set `unmountOnExit` if you'd prefer to unmount the component after it finishes exiting.
- */
- unmountOnExit: PropTypes.bool,
-
- /**
- * Normally a component is not transitioned if it is shown when the
- * `<Transition>` component mounts. If you want to transition on the first
- * mount set `appear` to `true`, and the component will transition in as soon
- * as the `<Transition>` mounts.
- *
- * > **Note**: there are no special appear states like `appearing`/`appeared`, this prop
- * > only adds an additional enter transition. However, in the
- * > `<CSSTransition>` component that first enter transition does result in
- * > additional `.appear-*` classes, that way you can choose to style it
- * > differently.
- */
- appear: PropTypes.bool,
-
- /**
- * Enable or disable enter transitions.
- */
- enter: PropTypes.bool,
-
- /**
- * Enable or disable exit transitions.
- */
- exit: PropTypes.bool,
-
- /**
- * The duration of the transition, in milliseconds.
- * Required unless `addEndListener` is provided.
- *
- * You may specify a single timeout for all transitions:
- *
- * ```jsx
- * timeout={500}
- * ```
- *
- * or individually:
- *
- * ```jsx
- * timeout={{
- * appear: 500,
- * enter: 300,
- * exit: 500,
- * }}
- * ```
- *
- * - `appear` defaults to the value of `enter`
- * - `enter` defaults to `0`
- * - `exit` defaults to `0`
- *
- * @type {number | { enter?: number, exit?: number, appear?: number }}
- */
- timeout: function timeout(props) {
- var pt = timeoutsShape;
- if (!props.addEndListener) pt = pt.isRequired;
-
- for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
- args[_key - 1] = arguments[_key];
- }
-
- return pt.apply(void 0, [props].concat(args));
- },
-
- /**
- * Add a custom transition end trigger. Called with the transitioning
- * DOM node and a `done` callback. Allows for more fine grained transition end
- * logic. **Note:** Timeouts are still used as a fallback if provided.
- *
- * ```jsx
- * addEndListener={(node, done) => {
- * // use the css transitionend event to mark the finish of a transition
- * node.addEventListener('transitionend', done, false);
- * }}
- * ```
- */
- addEndListener: PropTypes.func,
-
- /**
- * Callback fired before the "entering" status is applied. An extra parameter
- * `isAppearing` is supplied to indicate if the enter stage is occurring on the initial mount
- *
- * @type Function(node: HtmlElement, isAppearing: bool) -> void
- */
- onEnter: PropTypes.func,
-
- /**
- * Callback fired after the "entering" status is applied. An extra parameter
- * `isAppearing` is supplied to indicate if the enter stage is occurring on the initial mount
- *
- * @type Function(node: HtmlElement, isAppearing: bool)
- */
- onEntering: PropTypes.func,
-
- /**
- * Callback fired after the "entered" status is applied. An extra parameter
- * `isAppearing` is supplied to indicate if the enter stage is occurring on the initial mount
- *
- * @type Function(node: HtmlElement, isAppearing: bool) -> void
- */
- onEntered: PropTypes.func,
-
- /**
- * Callback fired before the "exiting" status is applied.
- *
- * @type Function(node: HtmlElement) -> void
- */
- onExit: PropTypes.func,
-
- /**
- * Callback fired after the "exiting" status is applied.
- *
- * @type Function(node: HtmlElement) -> void
- */
- onExiting: PropTypes.func,
-
- /**
- * Callback fired after the "exited" status is applied.
- *
- * @type Function(node: HtmlElement) -> void
- */
- onExited: PropTypes.func // Name the function so it is clearer in the documentation
-
- } : {};
-
- function noop() {}
-
- Transition.defaultProps = {
- in: false,
- mountOnEnter: false,
- unmountOnExit: false,
- appear: false,
- enter: true,
- exit: true,
- onEnter: noop,
- onEntering: noop,
- onEntered: noop,
- onExit: noop,
- onExiting: noop,
- onExited: noop
- };
- Transition.UNMOUNTED = 0;
- Transition.EXITED = 1;
- Transition.ENTERING = 2;
- Transition.ENTERED = 3;
- Transition.EXITING = 4;
- export default Transition;
|