sdxc

[ @sdxc/u/general ]

will-change

Applies will-change, hinting the browser to optimize for an upcoming animation on the given property/properties before it starts.

import { willChange } from "@sdxc/u/general";

Quick reference

Every documented call, beside the CSS it emits.

CallCSS
u.willChange("transform")css({ willChange: "transform" })
u.willChange("opacity, transform")css({ willChange: "opacity, transform" })

Examples

Basic example

Use willChange(property) to tell the browser which property is about to animate, so it can prepare before the change starts. Hover the card:

Spring collection · 24 new pieces
<div mix={[willChange("transform"), transition("transform"), hover(scale(1.05)), ...]}>
	Spring collection · 24 new pieces
</div>

Several properties

List several properties separated by commas, like willChange("opacity, transform"), when an animation changes more than one. Hover the photo:

<div
	mix={[
		willChange("opacity, transform"),
		transition("opacity, transform"),
		hover([opacity(80), scale(1.05)]),
		is(64),
		bs(40),
		...,
	]}
/>

Applying on a state

Wrap the call in a state utility to scope it to one selector.

u.hover(u.willChange("transform"))

Responsive design

Wrap the call in a container query to scope it to one width.

u.at("md", u.willChange("transform"))

Signature

u.willChange(value: WillChangeValue)

Written by Sergio Xalambrí. Follow @sergiodxa for new packages, or sponsor the work.