A declarative shared element transition library for React.
$ npm install --save springfield
OR
$ yarn add springfield
The simplest usage of springfield
is to wrap 2 DOM elements with <SharedElement>
component,
specifying identical "logicalId" and different "instanceId" to them:
<SharedElement logicalId="user" instanceId="icon">
<div className="user-icon" />
</SharedElement>
// and in a distinct place
<SharedElement logicalId="user" instanceId="avatar" isTarget>
<div className="user-avatar" />
</SharedElement>
springfield
works by rendering DOM elements with appropriate inline styles.
These styles typically contain CSS opacity
transform
transition
.
We recommended to read FLIP Your Animations first.
springfield
is but first-last-invert-play
animation implemented for React.
In the Getting Started example, when <div className="user-avatar" />
comes into VDOM tree,
springfield
renders the <div />
inside with a few inline styles, to cause the transition.
Details:
- render with
{ opacity: 0 }
to obtain its last position, where it should be when transition ends. - find a position snapshot of same
logicalId
but differentinstanceId
, and use it as the first position - render with a inverted transform
{ transform: ... }
to make the real element appear at first position - after the inverted transform gets layouted, render with
{ transition: ... }
(unsettransform
and settransition
) to kick off the transition - reset style to
undefined
aftertransitionend
event fired on the DOM element.
SharedElement
accepts a optional transition
property (string).
When specified, it will be used instead of default all 0.3s ease-in
;
SharedElement
supports the render-function-as-children idiom.
<SharedElement logicalId="user" instanceId="icon">
<div className="user-icon" />
</SharedElement>;
/* is equalivent to */
<SharedElement logicalId="user" instanceId="icon">
{(style, callbacks, phase, ref) => <div className="user-icon" style={style} ref={ref} />}
</SharedElement>;
See RenderPropsChildren type for explanation of these parameters.
The default snapshot strategy (what position to use as starti of transition) and styles (scale / transform only)
can be overridden by passing an object that implemented SpringfieldDelegate API to SpringfieldContext
.
See defaultSpringfieldDelegate for the default implementation.
Feel free to drop issues / PRs if you want to do something.
We named it springfield, because in a shared element transition, element moves as if it were pulled by an invisible spring-like field ---- and this imagination constitutes a bad pun joke.
MIT