Shipping less
A w.* element animates its values with nothing else: initial, animate, exit, variants and transitions. Each thing it does beyond that, a gesture, layout or a colour space, is a Feature: code a page ships only if it asks for it.
The Features
| Feature | What it brings |
|---|---|
hover | whileHover, onHoverStart and onHoverEnd |
press | whileTap, onTap, onTapStart and onTapCancel, from the pointer and the keyboard |
focus | whileFocus |
inView | whileInView, viewport, onViewportEnter and onViewportLeave |
pan | onPan, onPanStart, onPanSessionStart and onPanEnd |
drag | drag, its constraints, the glide on release, and dragControls |
layout | layout, layoutId, layoutScroll and layoutRoot |
colors | oklch(), oklab(), lab(), lch(), hwb() and color(). Hex, rgb(), hsl() and colour names need nothing. |
scroll | a container's scroll, for createScroll, which brings it itself |
On Solid 2, the gesture Features bring the ward's events too, such as "hover" and "tap". Motion's two sets are here under the same names: domAnimation is hover, press, focus and inView, and domMax is every Feature an element's props use, all but scroll. Each is a list, so [domAnimation, layout] works wherever Features go.
A Wardic instance
Choose an app's Features once, in a module of its own, and import its w wherever you'd import the binding's:
// src/lib/wardic.ts
import { domAnimation } from "@wardic/solid"
import { elements, makeWard } from "@wardic/solid/hooks"
export const { w } = makeWard({ features: domAnimation, elements })makeWard makes a Wardic instance (on Solid 1, createWardic(domAnimation)): a w whose elements bring those Features wherever they render, and take their props alone. On this one, <w.div drag /> is a type error, Type 'boolean' is not assignable to type 'NeedsFeature<"drag">', where the binding's w would take the prop and do nothing.
A page that drags or lays out makes an instance of its own:
// src/lib/wardic-drag.ts: for the pages that drag
import { domAnimation, drag } from "@wardic/solid"
import { elements, makeWard } from "@wardic/solid/hooks"
export const { w } = makeWard({ features: [domAnimation, drag], elements })The module that makes an instance ships its Features to every page that imports it, so give a page's Features a module only its pages import: the pages that don't drag never load drag. An instance with layout returns the layout hooks too, useAnimateLayout, createInstantLayoutTransition and createResetProjection, which bring it.
Loaded later
An entry in the list can be a load:
// drag-feature.ts holds `export { drag } from "@wardic/solid"`, so the import is a chunk of its own.
export const { w } = makeWard({ features: [domAnimation, () => import("./drag-feature.ts").then((m) => m.drag)], elements })It starts in the browser the first time one of the instance's elements or wards is made, or one of its hooks is called, and once for the instance, however many pages use it. While it loads:
- Values animate at once, and each Feature takes effect as it arrives. A drag tried before
draghas arrived does nothing, and isn't replayed. - An element with a layout prop starts laying out when
layoutarrives: it animates its next layout change. - The server loads nothing: it renders each element at its starting style, as it always does.
For a section
An instance's Features are named where its module is written. A section gives Features in the three cases that can't be: <WardicProvider features> to everything under it, and, on Solid 1, <LazyWardic features> to a part of the tree. On Solid 2 a part of the tree gets its own with a WardicProvider of its own, a page of its own inside the page, and a hook ward brings each Feature the first time it uses it. The binding's own w, imported from @wardic/solid or @wardic/solid/v1, has those of its section, its provider's and, on Solid 1, every LazyWardic's between them; it takes every prop, and a prop whose Feature isn't there does nothing. An instance's elements have them too, beside their own.
- A subtree you don't make: a component library's elements are its own
w.*, or the binding's; aLazyWardicaround them (Solid 1), or aWardicProviderof their own (Solid 2), gives them what they use. - Features handed over at runtime: a page that has a Feature can hand it to a part of the shell that doesn't, as a cart's fly-in given the
layoutof the page a piece came from: on Solid 1, aLazyWardicgiven it, so the shell, on every page, never importslayoutitself. On Solid 2 the fly-in is a hook ward, which bringslayoutwhen it's used. - A load at each render: a section whose
featuresis a function loads as it renders, every time it renders. Rendered on a press, it loads on that press, as below; an instance's load runs once.
import { DomLive } from "@wardic/dom"
import { w, WardicProvider } from "@wardic/solid"
import { createSignal, Show } from "solid-js"
/** A ball that drags once you ask: the page fetches `drag` on the press, not with itself. */
export default function Shipping() {
const [wanted, setWanted] = createSignal(false)
// How long `drag` took to arrive, from the press: absent until it has.
const [arrivedIn, setArrivedIn] = createSignal<number>()
let arena!: HTMLDivElement
let pressedAt = 0
// The provider's Features, as a function: it runs as the provider renders, so on the press.
const loadDrag = async () => {
const { drag } = await import("./drag-feature.ts")
setArrivedIn(Math.round(performance.now() - pressedAt))
return drag
}
const load = () => {
pressedAt = performance.now()
setWanted(true)
}
return (
<div class="sample-stack">
<button class="sample-button" disabled={wanted()} onClick={load}>
Load drag
</button>
<div class="sample-arena" ref={arena}>
<Show when={wanted()} fallback={<div class="sample-ball" />}>
<WardicProvider env={DomLive} features={loadDrag}>
<w.div class="sample-ball" drag dragConstraints={() => arena} whileDrag={{ scale: 1.15 }} />
</WardicProvider>
</Show>
</div>
<p class="sample-note" aria-live="polite">
<Show when={wanted()} fallback={<>No drag yet: the ball stays put.</>}>
<Show when={arrivedIn() !== undefined} fallback={<>Loading drag…</>}>
Drag arrived in {arrivedIn()} ms.
</Show>
</Show>
</p>
</div>
)
}import { LazyWardic, w } from "@wardic/solid/v1"
import { createSignal, Show } from "solid-js"
/** A ball that drags once you ask: the page fetches `drag` on the press, not with itself. */
export default function Shipping() {
const [wanted, setWanted] = createSignal(false)
// How long `drag` took to arrive, from the press: absent until it has.
const [arrivedIn, setArrivedIn] = createSignal<number>()
let arena!: HTMLDivElement
let pressedAt = 0
// The section's Features, as a function: it runs as the section renders, so on the press.
const loadDrag = async () => {
const { drag } = await import("./drag-feature-solid1.ts")
setArrivedIn(Math.round(performance.now() - pressedAt))
return drag
}
const load = () => {
pressedAt = performance.now()
setWanted(true)
}
return (
<div class="sample-stack">
<button class="sample-button" disabled={wanted()} onClick={load}>
Load drag
</button>
<div class="sample-arena" ref={arena}>
<Show when={wanted()} fallback={<div class="sample-ball" />}>
<LazyWardic features={loadDrag}>
<w.div class="sample-ball" drag dragConstraints={() => arena} whileDrag={{ scale: 1.15 }} />
</LazyWardic>
</Show>
</div>
<p class="sample-note" aria-live="polite">
<Show when={wanted()} fallback={<>No drag yet: the ball stays put.</>}>
<Show when={arrivedIn() !== undefined} fallback={<>Loading drag…</>}>
Drag arrived in {arrivedIn()} ms.
</Show>
</Show>
</p>
</div>
)
}import { DomLive } from "@wardic/dom"
import * as Effect from "effect/Effect"
import * as Schema from "effect/Schema"
import type { Html, HtmlBuilder } from "foldkit/html"
import { defineMessageUnion } from "foldkit/message"
import * as Mount from "foldkit/mount"
import { Wardic } from "@wardic/foldkit"
// Settled once `drag` has arrived: the load below resolves it, and the arena's Mount awaits it.
let arrived!: () => void
const arrival = new Promise<void>((resolve) => (arrived = resolve))
// The Handle's Features as a load: fetched by the first `w.*` element to mount, not with the app. An
// element mounted before `drag` arrives starts its drag once it has.
const wardic = Wardic.make({
env: DomLive,
features: () =>
import("./foldkit-drag-feature.ts").then((m) => {
arrived()
return m.drag
}),
})
const Model = Schema.Struct({ wanted: Schema.Boolean, arrivedIn: Schema.NullOr(Schema.Number) })
type Model = typeof Model.Type
const Message = defineMessageUnion({ Load: {}, Arrived: { ms: Schema.Number } })
type Message = typeof Message.Type
/** The arena's Mount: how long `drag` took to arrive, from the press that mounted the ball, told to `update`. */
const Arrival = Mount.define("DragArrival", {
messages: [Message.Arrived],
execute: () =>
Effect.gen(function* () {
const pressedAt = performance.now()
yield* Effect.promise(() => arrival)
return Message.Arrived({ ms: Math.round(performance.now() - pressedAt) })
}),
})
export const init = (): Model => ({ wanted: false, arrivedIn: null })
export const update = (model: Model, message: Message) =>
Message.match(message, {
Load: () => ({ model: { ...model, wanted: true } }),
Arrived: ({ ms }) => ({ model: { ...model, arrivedIn: ms } }),
})
const noteOf = (model: Model): string => {
if (!model.wanted) return "No drag yet: the ball stays put."
if (model.arrivedIn === null) return "Loading drag…"
return `Drag arrived in ${model.arrivedIn} ms.`
}
/** A ball that drags once you ask: until the press the ball is a plain element, and nothing of `drag` has been fetched. */
export const view = (model: Model, h: HtmlBuilder<Message>): Html => {
const w = wardic.html(h)
return h.div([h.Class("sample-stack")], [
h.button([h.Class("sample-button"), h.Disabled(model.wanted), h.OnClick(Message.Load())], ["Load drag"]),
// The arena is keyed by the press, so its Mount starts with the ball, the first `w.*` element, whose mount starts the load.
h.keyed("div")(String(model.wanted), [h.Class("sample-arena"), h.OnMount(Arrival())], [
model.wanted
? w.div([h.Class("sample-ball"), w.Drag(), w.DragConstraints("parent"), w.WhileDrag({ scale: 1.15 })])
: h.div([h.Class("sample-ball")]),
]),
h.p([h.Class("sample-note"), h.AriaLive("polite")], [noteOf(model)]),
])
}import { LazyMotion } from "motion/react"
import * as m from "motion/react-m"
import { useRef, useState } from "react"
/** A ball that drags once you ask: the page fetches Motion's Features on the press, not with itself. */
export default function Shipping() {
const [wanted, setWanted] = useState(false)
// How long the Features took to arrive, from the press: absent until they have.
const [arrivedIn, setArrivedIn] = useState<number>()
const arena = useRef<HTMLDivElement>(null)
const pressedAt = useRef(0)
// Motion's drag comes in `domMax`, with layout: they load for the whole page.
const loadFeatures = async () => {
const features = (await import("./motion-features.ts")).default
setArrivedIn(Math.round(performance.now() - pressedAt.current))
return features
}
const load = () => {
pressedAt.current = performance.now()
setWanted(true)
}
let status = "No drag yet: the ball stays put."
if (wanted) status = "Loading drag…"
if (arrivedIn !== undefined) status = `Drag arrived in ${arrivedIn} ms.`
return (
<div className="sample-stack">
<button className="sample-button" disabled={wanted} onClick={load}>
Load drag
</button>
<div className="sample-arena" ref={arena}>
{wanted ? (
<LazyMotion features={loadFeatures}>
<m.div className="sample-ball" drag dragConstraints={arena} whileDrag={{ scale: 1.15 }} />
</LazyMotion>
) : (
<div className="sample-ball" />
)}
</div>
<p className="sample-note" aria-live="polite">
{status}
</p>
</div>
)
}Components and hooks that need a Feature bring it: Reorder.Item brings drag, layout and the gestures, Reorder.Group brings layout, and createScroll brings scroll. LayoutGroup brings layout for its own layoutDependency only: a layout element under it still needs layout from its instance or its section.
A Feature left out
On an instance's element, a prop of a Feature the instance hasn't got is a type error that names the Feature, and so, on Solid 2, is listening for its event on the instance's ward.
On the binding's w, the prop does nothing: whileHover without hover doesn't play, drag without drag doesn't drag, and an oklch() colour without colors isn't read as a colour. In development, Wardic warns once for each Feature, naming it and where to add it.
Every Feature at once
wardic.* is the element with every Feature, whatever its section has, and so are wardic.create and the ward that isn't w's: ward() on Solid 2, createWard on Solid 1. It's handy for trying something out, and heavy for a page. strict on a WardicProvider, or on Solid 1 a LazyWardic, makes one throw in development, so none reaches a page by accident.
What it saves
Measured on Solid 1's binding for 1.1, built as an app ships and gzipped, each over the Slim element alone, 94 KB for the whole app:
| Feature | Size |
|---|---|
hover | 5 KB, the input every gesture but inView stands on included |
press, focus and inView, after hover | 2 KB |
drag, after domAnimation | 7 KB |
layout | 23 KB |
colors | 4.5 KB |
Every Feature, a wardic.* element | 40 KB |
In the storefront
The storefront's examples that show it, live and with their code:
- Rearrange the room: drag loads when you ask: The room's pieces stay put until you press Rearrange. Only then does the page fetch Wardic's drag, and the card says how long it took to arrive. (on Foldkit)