Skip to content
blogcssjs

PropFLIP

FLIP, but the invert is 4 custom properties and CSS does the play

AA
Nov 10, 202611 min read

mport "https://cdn.jsdelivr.net/npm/baseline-status"; @import url("https://cdn.jsdelivr.net/npm/open-props/easings.min.css"); @property --tx { syntax: " "; inherits: false; initial-value: 0; } @property --ty { syntax: " "; inherits: false; initial-value: 0; } @property --sx { syntax: " "; inherits: true; initial-value: 1; } @property --sy { syntax: " "; inherits: true; initial-value: 1; } figure.pf { justify-self: stretch; display: grid; gap: var(--size-3); } .pf-stage { container-type: inline-size; justify-self: stretch; display: grid; min-block-size: 11rem; justify-content: center; } :is(.pf-play, .pf-invert, .pf-easing, .pf-squish) .pf-stage > * { grid-area: 1 / 1; } .pf-why .pf-stage { min-block-size: 0; gap: var(--size-3); } .pf-box, .pf-ghost { --r: var(--radius-3); --p: var(--size-3); inline-size: 4rem; block-size: 4rem; place-self: start; border-radius: var(--r); } .pf-squish .pf-box { inline-size: 11rem; block-size: 4rem; padding: var(--p); } :is([data-last] > .pf-box, .pf-ghost.last) { place-self: end; inline-size: 65cqi; block-size: 6rem; } .pf-box { background: var(--text-2); color: var(--surface-1); box-shadow: var(--shadow-2); } .pf-ghost { display: grid; place-items: center; border: 2px dashed color-mix(in oklab, var(--text-2) 40%, transparent); } :is(.pf-ghost, .pf-why .pf-box, .pf-invert output) { font-size: var(--font-size-0); } .pf-box p { margin: 0; white-space: nowrap; line-height: var(--font-lineheight-1); } .PropFLIP { transform: translate(calc(var(--tx) * 1px), calc(var(--ty) * 1px)) scale(var(--sx), var(--sy)); transform-origin: top left; transition-property: --tx, --ty, --sx, --sy; transition-duration: var(--d-tx, 1.5s), var(--d-ty, 1.5s), var(--d-sx, 1.5s), var(--d-sy, 1.5s); transition-delay: 0s, .25s, .25s, 0s; transition-timing-function: var(--e-tx, var(--ease-spring-2)), var(--e-ty, var(--ease-spring-2)), var(--e-sx, var(--ease-spring-2)), var(--e-sy, var(--ease-spring-2)); } .fix-radius { border-radius: calc(var(--r) / var(--sx)) / calc(var(--r) / var(--sy)); } .fix-kids > * { transform-origin: top left; transform: translate(calc(var(--p) / var(--sx) - var(--p)), calc(var(--p) / var(--sy) - var(--p))) scale(calc(1 / var(--sx)), calc(1 / var(--sy))); } .pf-why .pf-box { display: grid; place-items: center; inline-size: 6rem; block-size: 3rem; transition: --tx .5s var(--ease-3), --x .5s var(--ease-3); } .pf-why .registered { transform: translateX(calc(var(--tx) * (100cqi - 100%))); } .pf-why .plain { transform: translateX(calc(var(--x, 0) * (100cqi - 100%))); } .pf-why:has(:checked) .pf-box { --tx: 1; --x: 1; } .pf-invert output { font-family: var(--font-mono); } .pf :is(.pf-actions, .pf-timing > div) { display: flex; flex-wrap: wrap; align-items: center; gap: var(--size-2) var(--size-4); } .pf-timing { display: grid; gap: var(--size-2); margin: 0; padding: 0; border: 0; font-size: var(--font-size-0); } .pf-timing legend { padding: 0; margin-block-end: var(--size-1); font-weight: var(--font-weight-6); } .pf-timing code { inline-size: 3.5rem; } .pf-timing input { flex: 1 1 6rem; min-inline-size: 0; } .pf-timing select { flex: 1 1 9rem; min-inline-size: 0; } .pf-timing output { inline-size: 2.5rem; font-variant-numeric: tabular-nums; } .pf button { padding: var(--size-2) var(--size-5); background: var(--surface-2); border: 1px solid var(--surface-3); border-radius: var(--radius-round); box-shadow: var(--shadow-2); color: var(--text-1); font-weight: var(--font-weight-6); cursor: pointer; } @media (prefers-reduced-motion: no-preference) { .pf button { transition: scale .2s var(--ease-3); } .pf button:active { scale: 95%; } } /* phones: button right under the stage, timing below, thumb-sized targets */ @media (max-width: 40rem) { .pf-actions { column-gap: var(--size-3); } .pf-timing { order: 1; } .pf.pf-invert label { justify-self: stretch; display: flex; align-items: center; gap: var(--size-3); } .pf.pf-invert input { flex: 1; min-inline-size: 0; } } @media (pointer: coarse) { .pf label:has(input) { display: inline-flex; align-items: center; gap: var(--size-2); min-block-size: 2.5rem; } .pf input[type="checkbox"] { inline-size: 1.25rem; block-size: 1.25rem; } .pf input[type="range"] { min-block-size: 1.75rem; } .pf-timing select { min-block-size: 2.25rem; } } @media (prefers-reduced-motion: reduce) { .PropFLIP, .pf-why .pf-box { transition-duration: 0s !important; } } Back on my FLIP obsessions: this time invert is 4 @property 's and play is a transition . Callin it PropFLIP cuz it flips with props ๐Ÿ˜… Flip it Punch it, mid-flight too! Maybe that doesn't look that impressive, but it's packing a punch: x , y , scalex and scaley get their own duration, easing, and delays interruptible layout animation border radius maintained CSS led The effect is thanks to @property and some JS. Layout doesn't animate # So, something changes layout right? A card moves to a new grid cell, a list reorders, a box grows. You want it to glide there, so you reach for: .box { transition : left .5 s , top .5 s , width .5 s , height .5 s ; } which runs layout every frame, aka janky. The height being in there alone is asking for trouble, everything below it has to move every frame too. Needless to say, animating layout related situations is tricky. Shameless plug here for my new CSS library Anchored Auto Layout , which can animate a very cool amount of layout changes. Also see AIM . Then there's transform , cheap to animate. But a transform says "move by this much", not "go to wherever layout puts me next". In FLIP fashion, what if we give CSS more of the work, what if we transition @property values inside transform ? FLIP it # Here the box's layout is already at Last . Drag it home: First Last Play At 0 the layout says Last but the pixels sit on First. Those 4 numbers are the typed props we'll set up. 4 typed props # Build the transform from 4 custom properties: .PropFLIP { transform : translate ( calc ( var (--tx) * 1 px ) , calc ( var (--ty) * 1 px )) scale ( var (--sx) , var (--sy)) ; transform-origin : top left ; } top left cuz that's where getBoundingClientRect() measures from, so the math stays simple. Register all 4 as <number> 's, with x and y defaulted to 0 and scale x and y to 1: @property --tx { syntax: "&#x3C;number > "; inherits: false; initial-value : 0; } @property --ty { syntax: "&#x3C;number > "; inherits: false; initial-value : 0; } @property --sx { syntax: "&#x3C;number > "; inherits: true; initial-value : 1; } @property --sy { syntax: "&#x3C;number > "; inherits: true; initial-value : 1; } The scales inherit on purpose, there's a trick/correction/perfection here we'll come back to ๐Ÿ‘€ Transitions rule # Interruptible animation is the dream, CSS transitions deliver. So next, lets turn each @property variable into a transition-able value: .PropFLIP { transition : --tx , --ty , --sx , --sy ; transition-duration : .75 s ; transition-timing-function : var (--ease-spring-3) ; } Since it's 4 separate properties, each one can have its own duration and easing. Different timing on x and y is how you get arcs and swings ๐Ÿคฏ .PropFLIP { transition-duration : .6 s , 1 s , .4 s , .4 s ; /* ๐Ÿ‘ˆ */ transition-timing-function : var (--ease-out-3) , /* --tx */ var (--ease-spring-3) , /* --ty */ โ€ฆ ; } Have a play with a few Open Props easings : Flip it Keep flipping There's a gotcha here we gotta talk about. We're animating scale, that means squishing stuff. Which usually isn't beautiful. UnSquish the Squish # Scale a box and you scale everything in it. Corners go oval and text goes squishy. Different timings per property make it worse. Got x and y scaling at different speeds, so the corners really stretch. The following demo lets you check a box to see the fix and see the problem. Slow it down if you're not seein it. Flip it Keep flipping Fixed corners The Fix: Set --r to your radius and divide it by the scale: .PropFLIP { border-radius : calc ( var (--r) / var (--sx)) / calc ( var (--r) / var (--sy)) ; } But I cheated a li'l bit, time to come clean. Text content, if you have text inside the box it stretches too. And it's not on the .PropFLIP class, it's stuff inside the box. You can fix it though! For direct children, set --p to your padding and they undo the parent's scale: .PropFLIP > * { transform-origin : top left ; transform : translate ( calc ( var (--p) / var (--sx) - var (--p)) , calc ( var (--p) / var (--sy) - var (--p)) ) scale ( calc (1 / var (--sx)) , calc (1 / var (--sy))) ; } That's why --sx and --sy inherit. Only direct children need the fix, their kids ride along unsquished. It does assume they sit at the box's padding edge, anything fancier is more math. The JS # Phew, you made it this far. Time for the classic FLIP JS ritual, with a new requirement to temporarily disable the transition. measure First turn the transition off change the DOM measure Last const first = el .getBoundingClientRect () el . style .transition = 'none' updateDOM () const last = el .getBoundingClientRect () Now we do the I from FLIP "Invert" and set our props. Translate is a difference, scale is a ratio: el . style .setProperty ( '--tx' , first .left - last .left) el . style .setProperty ( '--ty' , first .top - last .top) el . style .setProperty ( '--sx' , first .width / last .width) el . style .setProperty ( '--sy' , first .height / last .height) Then, enable transitions again and set the props back: el .offsetHeight // ๐Ÿ‘ˆ commit the lie el . style .transition = '' el . style .setProperty ( '--tx' , 0 ) el . style .setProperty ( '--ty' , 0 ) el . style .setProperty ( '--sx' , 1 ) el . style .setProperty ( '--sy' , 1 ) That offsetHeight line looks useless, but it's clutch, it forces the browser to compute style with the inverted values, so the transition has a "from". Delete it and nothing animates ๐Ÿ˜… All together: function animateLayout (el , updateDOM) { const first = el .getBoundingClientRect () el . style .transition = 'none' updateDOM () const last = el .getBoundingClientRect () el . style .setProperty ( '--tx' , first .left - last .left) el . style .setProperty ( '--ty' , first .top - last .top) el . style .setProperty ( '--sx' , first .width / last .width) el . style .setProperty ( '--sy' , first .height / last .height) el .offsetHeight el . style .transition = '' el . style .setProperty ( '--tx' , 0 ) el . style .setProperty ( '--ty' , 0 ) el . style .setProperty ( '--sx' , 1 ) el . style .setProperty ( '--sy' , 1 ) } That's the exact function running the demos: button . onclick = () => animateLayout (box , () => stage .toggleAttribute ( 'data-last' ) ) Is it fast? # It depends. Layout only happens once, up front, like any FLIP. But every frame still costs something: 4 custom properties change and transform gets recomputed. That's style recalc. --sx and --sy inherit, so every descendant gets invalidated too. As far as I know, no browser hands registered property animations to the compositor. It's a main thread job, so a busy main thread could easily mean a janky glide. For a handful of things (a card, a dialog, a short list) it's great. For hundreds of elements, or a page that's already struggling, reach for element.animate() on transform with absolute values. What I wish existed # I want typed custom properties that animate off the main thread . The browser knows --tx is a <number> . It knows the duration and the easing. If the only place that number lands is a transform , it seems like the compositor could take the whole thing. As far as I know, today it doesn't. Until then, PropFLIP is a trade. Way less JS, but maybe not always the performant choice. Same instinct as AIM though, let CSS do the playing . Check out the playground where I formulated this here on codepen . Lemme know how it holds up on your stuff ๐Ÿค“ With reduced motion on, the demos jump straight to the end. The Play slider still works. function animateLayout(el, updateDOM) { const first = el.getBoundingClientRect() el.style.transition = 'none' updateDOM() const last = el.getBoundingClientRect() el.style.setProperty('--tx', first.left - last.left) el.style.setProperty('--ty', first.top - last.top) el.style.setProperty('--sx', first.width / last.width) el.style.setProperty('--sy', first.height / last.height) el.offsetHeight el.style.transition = '' el.style.setProperty('--tx', 0) el.style.setProperty('--ty', 0) el.style.setProperty('--sx', 1) el.style.setProperty('--sy', 1) } const $ = (sel, root = document) => root.querySelector(sel) const reduced = matchMedia('(prefers-reduced-motion: reduce)').matches // one button, one flip function wire(fig) { const stage = $('.pf-stage', fig), box = $('.PropFLIP', fig) const flip = () => animateLayout(box, () => stage.toggleAttribute('data-last')) $('button', fig).onclick = flip return { box, flip } } wire($('.pf-play')) // timing per property, plus a flip-on-a-loop checkbox const EASINGS = ['ease-3', 'ease-out-3', 'ease-in-out-3', 'ease-spring-3', 'ease-spring-5', 'ease-elastic-out-3', 'ease-bounce-3', 'ease-squish-3'] const TIMING = { tx: [.45, 'ease-out-3'], ty: [1.25, 'ease-spring-3'], sx: [.25, 'ease-out-3'], sy: [1, 'ease-spring-3'] } const options = EASINGS.map(e => ` ${e} `).join('') function timing(fig) { const { box, flip } = wire(fig) const times = Object.fromEntries(Object.entries(TIMING).map(([k, [t]]) => [k, t])) const set = $('.pf-actions', fig).insertAdjacentElement('beforebegin', Object.assign(document.createElement('fieldset'), { className: 'pf-timing' })) set.innerHTML = ' Timing ' + Object.entries(TIMING).map(([k, [t, e]]) => ` --${k} ${t}s ${options} `).join('') for (const sel of set.querySelectorAll('select')) sel.value = TIMING[sel.dataset.k][1] const apply = el => { const k = el.dataset.k if (el.type === 'range') { times[k] = +el.value box.style.setProperty(`--d-${k}`, `${el.value}s`) el.nextElementSibling.value = `${el.value}s` } else box.style.setProperty(`--e-${k}`, `var(--${el.value})`) } set.querySelectorAll('input, select').forEach(apply) set.oninput = e => apply(e.target) const auto = $('.auto', fig) if (reduced) auto.disabled = true let timer, visible = true new IntersectionObserver(([e]) => visible = e.isIntersecting).observe(fig) const loop = () => { if (visible) flip() timer = setTimeout(loop, Math.max(...Object.values(times)) * 1000 + 400) } auto.onchange = () => { clearTimeout(timer) if (auto.checked) loop() } return box } timing($('.pf-easing')) const squish = $('.pf-squish') const box = timing(squish) $('.radius', squish).onchange = e => box.classList.toggle('fix-radius', e.target.checked) // the invert, by hand const invert = $('.pf-invert'), range = $('input', invert) const scrub = () => { const f = $('.pf-ghost', invert).getBoundingClientRect(), l = $('.last', invert).getBoundingClientRect() const mix = (from, to) => from + (to - from) * range.value const vars = { tx: mix(f.left - l.left, 0), ty: mix(f.top - l.top, 0), sx: mix(f.width / l.width, 1), sy: mix(f.height / l.height, 1) } for (const k in vars) $('.pf-box', invert).style.setProperty(`--${k}`, vars[k]) $('output', invert).value = Object.entries(vars).map(([k, v]) => `--${k}: ${+v.toFixed(2)}`).join(' ') } range.oninput = scrub new ResizeObserver(scrub).observe(invert)

Did you enjoy this article?

Recommend it โ€” Standard Reader surfaces well-loved writing to more readers across the network.

Across the AtmosphereDiscussions