Layout animations
layout animates an element from where it was to where the layout puts it now: a new size, a new place in a list, the other end of a flex row. The CSS makes the change at once, and Wardic plays it with transforms, so it stays smooth whatever changed.
import { domAnimation, layout } from "@wardic/solid"
import { elements, makeWard } from "@wardic/solid/hooks"
import { createSignal } from "solid-js"
// This page lays out: its own Wardic instance brings `layout`, so pages that don't never load it.
const { w } = makeWard({ features: [domAnimation, layout], elements })
/** A switch: the stylesheet moves its knob across, and `layout` animates the move. */
export default function Switch() {
const [on, setOn] = createSignal(false)
return (
<button class="sample-switch" role="switch" aria-checked={on() ? "true" : "false"} aria-label="Lights" onClick={() => setOn(!on())}>
{/* Nothing re-renders in Solid: `layoutDependency` says when to look for a move. */}
<w.span class="sample-knob" layout layoutDependency={on()} transition={{ type: "spring", stiffness: 500, damping: 30 }} />
</button>
)
}import { createWardic, domAnimation, layout } from "@wardic/solid/v1"
import { createSignal } from "solid-js"
// This page lays out: its own Wardic instance brings `layout`, so pages that don't never load it.
const { w } = createWardic([domAnimation, layout])
/** A switch: the stylesheet moves its knob across, and `layout` animates the move. */
export default function Switch() {
const [on, setOn] = createSignal(false)
return (
<button class="sample-switch" role="switch" aria-checked={on() ? "true" : "false"} aria-label="Lights" onClick={() => setOn(!on())}>
{/* Nothing re-renders in Solid: `layoutDependency` says when to look for a move. */}
<w.span class="sample-knob" layout layoutDependency={on()} transition={{ type: "spring", stiffness: 500, damping: 30 }} />
</button>
)
}import { DomLive } from "@wardic/dom"
import * as Schema from "effect/Schema"
import type { Html, HtmlBuilder } from "foldkit/html"
import { defineMessageUnion } from "foldkit/message"
import { Features, Wardic } from "@wardic/foldkit"
// This app lays out: its Handle names `layout` among its Features.
const wardic = Wardic.make({ env: DomLive, features: [Features.domAnimation, Features.layout] })
const Model = Schema.Struct({ on: Schema.Boolean })
type Model = typeof Model.Type
const Message = defineMessageUnion({ Flip: {} })
type Message = typeof Message.Type
export const init = (): Model => ({ on: false })
export const update = (model: Model, message: Message) => Message.match(message, { Flip: () => ({ model: { on: !model.on } }) })
/** A switch: the stylesheet moves its knob across, and `Layout` animates the move. */
export const view = (model: Model, h: HtmlBuilder<Message>): Html => {
const w = wardic.html(h)
return h.button([h.Class("sample-switch"), h.Role("switch"), h.AriaChecked(model.on), h.AriaLabel("Lights"), h.OnClick(Message.Flip())], [
// The patch changes `aria-checked` alone, which no layout pass watches: `LayoutDependency` says when to look for a move.
w.span([h.Class("sample-knob"), w.Layout(true), w.LayoutDependency(model.on), w.Transition({ type: "spring", stiffness: 500, damping: 30 })]),
])
}import { domMax, LazyMotion } from "motion/react"
import * as m from "motion/react-m"
import { useState } from "react"
/** A switch: the stylesheet moves its knob across, and `layout` animates the move. */
export default function Switch() {
const [on, setOn] = useState(false)
return (
// This page lays out, so it brings `domMax`, Motion's layout and drag, itself: pages that don't never load it.
<LazyMotion features={domMax}>
<button className="sample-switch" role="switch" aria-checked={on} aria-label="Lights" onClick={() => setOn(!on)}>
<m.span className="sample-knob" layout transition={{ type: "spring", stiffness: 500, damping: 30 }} />
</button>
</LazyMotion>
)
}What starts one
Nothing re-renders in Solid, so Wardic doesn't look for a layout change after every update, as Motion does after every render. It measures at the end of a task in which:
- an element with
layoutor alayoutIdmounted; - one of their sizes changed, a child's or an ancestor's included;
- a
layoutDependencychanged: give it whatever moves the element, as the switch above giveson(); - the app made the change inside
animateLayout(fn), fromuseAnimateLayout(). That's for a change nothing else sees, such as a plain list sorted:
const animateLayout = useAnimateLayout()
const sort = () => animateLayout(() => setItems(sorted(items())))Each time it measures every layout element, so the ones a change pushes aside move too. To hear when the animations it started have ended:
- On Solid 2, it returns a promise, which resolves then:
await animateLayout(fn). - On Solid 1,
animateLayoutreturns whatfnreturns, and callsonSettledin its options:animateLayout(fn, { onSettled }).
Part of the change
layout="position" animates the move alone: the size changes at once. "size" animates the size alone, "x" and "y" one axis, and "preserve-aspect" the position unless the width-to-height ratio changes.
A layout animation stretches the element as it goes. border-radius and box-shadow are corrected for it, so a rounded card stays round, and a child with layout of its own isn't stretched with it.
One element, two places
Two elements with the same layoutId are one element in two places: as one mounts while the other leaves, it flies from where the other was.
import { domAnimation, layout } from "@wardic/solid"
import { elements, makeWard } from "@wardic/solid/hooks"
import { createSignal, For, Show } from "solid-js"
// This page lays out: its own Wardic instance brings `layout`, so pages that don't never load it.
const { w } = makeWard({ features: [domAnimation, layout], elements })
const TABS = ["Bowls", "Plates", "Cups"]
/** Tabs with one underline: each tab's has the same `layoutId`, so it flies to the tab that's picked. */
export default function Tabs() {
const [picked, setPicked] = createSignal("Bowls")
return (
<div class="sample-tabs" role="tablist" aria-label="Shelves">
<For each={TABS}>
{(name) => (
<button class="sample-tab" role="tab" aria-selected={picked() === name ? "true" : "false"} onClick={() => setPicked(name)}>
{name}
<Show when={picked() === name}>
<w.span class="sample-underline" layoutId="underline" />
</Show>
</button>
)}
</For>
</div>
)
}import { createWardic, domAnimation, layout } from "@wardic/solid/v1"
import { createSignal, For, Show } from "solid-js"
// This page lays out: its own Wardic instance brings `layout`, so pages that don't never load it.
const { w } = createWardic([domAnimation, layout])
const TABS = ["Bowls", "Plates", "Cups"]
/** Tabs with one underline: each tab's has the same `layoutId`, so it flies to the tab that's picked. */
export default function Tabs() {
const [picked, setPicked] = createSignal("Bowls")
return (
<div class="sample-tabs" role="tablist" aria-label="Shelves">
<For each={TABS}>
{(name) => (
<button class="sample-tab" role="tab" aria-selected={picked() === name ? "true" : "false"} onClick={() => setPicked(name)}>
{name}
<Show when={picked() === name}>
<w.span class="sample-underline" layoutId="underline" />
</Show>
</button>
)}
</For>
</div>
)
}import { DomLive } from "@wardic/dom"
import * as Schema from "effect/Schema"
import type { Html, HtmlBuilder } from "foldkit/html"
import { defineMessageUnion } from "foldkit/message"
import { Features, Wardic } from "@wardic/foldkit"
// This app lays out: its Handle names `layout` among its Features.
const wardic = Wardic.make({ env: DomLive, features: [Features.domAnimation, Features.layout] })
const TABS = ["Bowls", "Plates", "Cups"]
const Model = Schema.Struct({ picked: Schema.String })
type Model = typeof Model.Type
const Message = defineMessageUnion({ Pick: { name: Schema.String } })
type Message = typeof Message.Type
export const init = (): Model => ({ picked: "Bowls" })
export const update = (_model: Model, message: Message) => Message.match(message, { Pick: ({ name }) => ({ model: { picked: name } }) })
/** Tabs with one underline: each tab's has the same `LayoutId`, so it flies to the tab that's picked. */
export const view = (model: Model, h: HtmlBuilder<Message>): Html => {
const w = wardic.html(h)
return h.div(
[h.Class("sample-tabs"), h.Role("tablist"), h.AriaLabel("Shelves")],
TABS.map((name) =>
h.button([h.Class("sample-tab"), h.Role("tab"), h.AriaSelected(model.picked === name), h.OnClick(Message.Pick({ name }))], [
name,
...(model.picked === name ? [w.keyed("span")("underline", [h.Class("sample-underline"), w.LayoutId("underline")])] : []),
]),
),
)
}import { domMax, LazyMotion } from "motion/react"
import * as m from "motion/react-m"
import { useState } from "react"
const TABS = ["Bowls", "Plates", "Cups"]
/** Tabs with one underline: each tab's has the same `layoutId`, so it flies to the tab that's picked. */
export default function Tabs() {
const [picked, setPicked] = useState("Bowls")
return (
// This page lays out, so it brings `domMax`, Motion's layout and drag, itself: pages that don't never load it.
<LazyMotion features={domMax}>
<div className="sample-tabs" role="tablist" aria-label="Shelves">
{TABS.map((name) => (
<button key={name} className="sample-tab" role="tab" aria-selected={picked === name} onClick={() => setPicked(name)}>
{name}
{picked === name && <m.span className="sample-underline" layoutId="underline" />}
</button>
))}
</div>
</LazyMotion>
)
}<LayoutGroup id="…"> puts an id before each layoutId made under it, so two copies of the same tabs keep their own underline.
Transitions and timing
A layout animation runs on transition's layout entry, else the element's transition, else Motion's 0.45 s tween. A path, such as arc(), curves a move of 20 px or more.
The scroll of every scrolling element around it, the page's under position: fixed, and how far a position: sticky element has stuck are accounted for on their own, with no layoutScroll or layoutRoot: a page scroll never reads as a move.
- On Solid 2, the element's ward gives
layoutStartandlayoutComplete:await card.next("layoutComplete"). - On Solid 1,
onLayoutAnimationStartandonLayoutAnimationCompleteare called as one starts and lands.
The Feature
Layout animations are the layout Feature. A page that lays out makes a Wardic instance with it, makeWard({ features: [domAnimation, layout], elements }) (on Solid 1, createWardic([domAnimation, layout])), as the demos do, so pages that don't never load it; its useAnimateLayout brings it too. <Reorder> brings it itself. <LayoutGroup> brings it only for its own layoutDependency, so the layout elements under it still need it from their instance or a section. See Shipping less.
In the storefront
The storefront's examples that show it, live and with their code:
- A product opens into its detail view: Tap a piece and its card grows into the full view, photo, name and price carried across. (on Foldkit)
- Filter the shop, switch the view: Pick a category and the products that stay slide together; switch to a list and every card reshapes into a row. (on Foldkit)
- Tabs on a product page: Switch between a piece's description, details and delivery, and the underline slides to the tab you chose. (on Foldkit)
- Add to cart, and see it's done: The button says what it's doing, adding then added, and makes room for a way to the cart. (on Foldkit)
- Questions that open in place: Open an answer, and its card grows to fit while the questions below slide down to make room. Open all, or close all, and they change at once. (on Foldkit)
- A cart drawer that keeps up: Change a quantity or remove a piece, and the lines below close up at once, before the store has answered. (on Foldkit)
- Order history in a scrolling panel: Sort your orders and each one glides to its new place; open one and only the orders below it make room. (on Foldkit)
- From the catalogue to a piece's own page: Open a piece and its photo flies from the catalogue into its own page; go back and it flies home. The two are routes, so the browser's back and forward do the same. (on Foldkit)
- Sort the shop with Kobalte's select: Pick an order: the list grows out of its button, and the pieces slide to their new places. (on Foldkit)
- A piece's details in Kobalte's tabs: Switch between a piece's size, material and care: the underline slides to the tab, whether you click or use the arrow keys. (on Foldkit)