Animations
YPY does not animate anything by itself. It puts classes on the elements during a swap and leaves the motion to CSS. Three ways to use them follow, from the simplest to the one that needs a browser feature.
The classes are ypy-swapping (on the swap target, until the content is inserted), ypy-settling (on the target,
during the settle delay) and ypy-added (on new content, before it settles). See
CSS transitions and CSS classes.
The page
1. Fade out and fade in
ypy.css has two opt-in classes. ypy-fade makes the swap target fade out while it is in the swapping state and fade
back in when the state ends. ypy-fade-in makes new content fade in, which suits content added next to what is already
there.
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/ypy-develop/[email protected]/dist/ypy.css">
<button ypy-get="/quote" ypy-target="#quote" ypy-swap="innerHTML swap:200ms">Another quote</button>
<blockquote id="quote" class="ypy-fade"><p>Start here.</p></blockquote>swap:200ms delays the swap, so ypy-swapping stays on #quote for 200 ms and the fade-out has time to run. For a list
that grows, put ypy-fade-in on each new item and use ypy-swap="beforeend".
2. Animate a class that changed
When the response holds an element with the same id and tag as one it replaces, YPY first inserts the new element with
the old class, style, width and height, and applies the new values after the settle delay. A CSS transition
then runs between the two. It works with innerHTML and outerHTML.
.meter { height: 8px; background: var(--accent); transition: width 500ms ease-out; }
.meter.level-1 { width: 20%; }
.meter.level-3 { width: 60%; }
.meter.level-5 { width: 100%; }<div id="panel">
<div id="meter" class="meter level-1"></div>
<button ypy-get="/meter/3" ypy-target="#panel" ypy-swap="innerHTML settle:50ms">Raise</button>
</div>3. View transitions
The ypy-transitions extension runs a swap inside document.startViewTransition(), so the browser animates between the
old and the new state of the page.
<a href="/photos/2" ypy-get="/photos/2" ypy-target="#gallery" ypy-swap="innerHTML transition:true">Next photo</a>::view-transition-group(root) { animation-duration: 150ms; }What the server answers
Nothing here is special to the server: it answers an ordinary fragment with Vary: YPY-Request.
| Request | Status | Body |
|---|---|---|
GET /quote |
200 |
<p>Make it work, then make it fast.</p> |
GET /meter/3 |
200 |
The same two elements with the new class: <div id="meter" class="meter level-3"></div> and the button |
GET /photos/2 |
200 |
The next photo markup, for example <img src="/photos/2.jpg" alt="Harbour at dawn" width="800" height="533"> |
For the class change, keep the id and the tag of #meter the same in every answer, and change only the class.
How it works
- The core sets and removes the state classes. You write the CSS that reacts to them.
swap:andsettle:are modifiers ofypy-swap. They give a transition time to run.-
ypy-addedlasts only until the settle step ends, which can be a single frame.ypy-fade-indoes not depend on it: it starts from@starting-style. -
The time and the curve come from
--ypy-durationand--ypy-easing. Withprefers-reduced-motion: reducethe duration is zero. -
transition:trueasks for a view transition on one swap, andconfig.transitionsturns it on for all. A browser without the API, or a visitor who prefers reduced motion, gets the same swap with no animation.ypy:before:viewTransitionandypy:after:viewTransitionfire around it.
Variations
-
Prefer a class to a
styleattribute for the new value: a strict CSP blocks inline styles. See security. - Animate a removal:
ypy-swap="delete swap:300ms"keepsypy-swappingon the target for 300 ms before it goes.
See also
ypy-transitionsypy-swapfor every modifier- Delete row