Scroll
createScroll() gives how far something is scrolled, as values that change as it scrolls. Given to an element's style, they move it with the scroll itself, with no animation of their own.
import { createScroll } from "@wardic/solid"
import { w } from "~/lib/wardic.ts"
import { For } from "solid-js"
const LINES = [
"The kiln is cooling.",
"Twelve bowls went in this morning, glazed in lagoon blue.",
"By evening the glaze has run and pooled where the walls meet the foot.",
"Each one comes out a little different from the last.",
"Two have crazed: fine lines all over, like ice on a pond.",
"One cracked in the cooling, and goes on the shelf by the door.",
"The rest are wrapped in paper and packed for the morning post.",
]
/** A panel's scroll, shown as a bar across its top: the bar is the scroll itself, with no animation of its own. */
export default function Story() {
let panel!: HTMLDivElement
// How far the panel is scrolled, from 0 to 1. Without `container`, it's the page's.
const { scrollYProgress } = createScroll({ container: () => panel })
return (
<div class="sample-scroller">
<w.div class="sample-progress" style={{ scaleX: scrollYProgress }} />
<div class="sample-scroll" ref={panel} tabindex="0" aria-label="From the studio">
<For each={LINES}>{(line) => <p>{line}</p>}</For>
</div>
</div>
)
}import { createScroll } from "@wardic/solid/v1"
import { w } from "~/lib/wardic.ts"
import { For } from "solid-js"
const LINES = [
"The kiln is cooling.",
"Twelve bowls went in this morning, glazed in lagoon blue.",
"By evening the glaze has run and pooled where the walls meet the foot.",
"Each one comes out a little different from the last.",
"Two have crazed: fine lines all over, like ice on a pond.",
"One cracked in the cooling, and goes on the shelf by the door.",
"The rest are wrapped in paper and packed for the morning post.",
]
/** A panel's scroll, shown as a bar across its top: the bar is the scroll itself, with no animation of its own. */
export default function Story() {
let panel!: HTMLDivElement
// How far the panel is scrolled, from 0 to 1. Without `container`, it's the page's.
const { scrollYProgress } = createScroll({ container: () => panel })
return (
<div class="sample-scroller">
<w.div class="sample-progress" style={{ scaleX: scrollYProgress }} />
<div class="sample-scroll" ref={panel} tabindex="0" aria-label="From the studio">
<For each={LINES}>{(line) => <p>{line}</p>}</For>
</div>
</div>
)
}import { DomLive } from "@wardic/dom"
import * as Effect from "effect/Effect"
import * as Atom from "effect/reactivity/Atom"
import * as AtomRegistry from "effect/reactivity/AtomRegistry"
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 { Features, Wardic } from "@wardic/foldkit"
// `Wardic.scroll` reads on the `scroll` Feature: the Handle names it.
const wardic = Wardic.make({ env: DomLive, features: [Features.scroll] })
const LINES = [
"The kiln is cooling.",
"Twelve bowls went in this morning, glazed in lagoon blue.",
"By evening the glaze has run and pooled where the walls meet the foot.",
"Each one comes out a little different from the last.",
"Two have crazed: fine lines all over, like ice on a pond.",
"One cracked in the cooling, and goes on the shelf by the door.",
"The rest are wrapped in paper and packed for the morning post.",
]
// The Live value the bar binds, an Atom at module scope: how far the panel is scrolled, from 0 to 1.
const progress = Atom.make(0)
const Model = Schema.Struct({})
type Model = typeof Model.Type
const Message = defineMessageUnion({ Reading: {} })
type Message = typeof Message.Type
export const init = (): Model => ({})
export const update = (model: Model, message: Message) => Message.match(message, { Reading: () => ({ model }) })
/** The panel's Mount: its scroll as Live values (`Wardic.scroll`), fed into `progress` while the panel is on the page. Without `container`, it's the page's. */
const Read = Mount.define("Read", {
messages: [Message.Reading],
execute: ({ element }) =>
Effect.provide(
Wardic.provide(
Effect.gen(function* () {
const { scrollYProgress } = yield* Effect.orDie(Wardic.scroll({ container: element }))
const registry = yield* AtomRegistry.AtomRegistry
const stop = registry.subscribe(scrollYProgress, (at) => registry.set(progress, at), { immediate: true })
yield* Effect.addFinalizer(() => Effect.sync(stop))
return Message.Reading()
}),
),
wardic.resources,
),
})
/** A panel's scroll, shown as a bar across its top: the bar is the scroll itself, with no animation of its own. */
export const view = (_model: Model, h: HtmlBuilder<Message>): Html => {
const w = wardic.html(h)
return h.div([h.Class("sample-scroller")], [
w.div([h.Class("sample-progress"), w.Style({ scaleX: progress })]),
h.div([h.Class("sample-scroll"), h.OnMount(Read()), h.Tabindex(0), h.AriaLabel("From the studio")], LINES.map((line) => h.p([], [line]))),
])
}import { useScroll } from "motion/react"
import * as m from "motion/react-m"
import { useRef } from "react"
const LINES = [
"The kiln is cooling.",
"Twelve bowls went in this morning, glazed in lagoon blue.",
"By evening the glaze has run and pooled where the walls meet the foot.",
"Each one comes out a little different from the last.",
"Two have crazed: fine lines all over, like ice on a pond.",
"One cracked in the cooling, and goes on the shelf by the door.",
"The rest are wrapped in paper and packed for the morning post.",
]
/** A panel's scroll, shown as a bar across its top: the bar is the scroll itself, with no animation of its own. */
export default function Story() {
const panel = useRef<HTMLDivElement>(null)
// How far the panel is scrolled, from 0 to 1. Without `container`, it's the page's.
const { scrollYProgress } = useScroll({ container: panel })
return (
<div className="sample-scroller">
<m.div className="sample-progress" style={{ scaleX: scrollYProgress }} />
<div className="sample-scroll" ref={panel} tabIndex={0} aria-label="From the studio">
{LINES.map((line) => (
<p key={line}>{line}</p>
))}
</div>
</div>
)
}What it gives
createScroll returns Motion's four values, each an accessor:
scrollXandscrollY: how far it's scrolled, in pixels.scrollXProgressandscrollYProgress: the same, from 0 at the start to 1 at the end.
Without options it's the page's scroll. container is a scrolling element of your own, given as an accessor of its ref, as above; until the ref is there, every value reads 0.
They're set in the scroll's own event, so a value worked out from one paints in the frame that shows the scroll.
An element's way through
With a target, the progress is the target's way through the container, between two offsets, in Motion's terms: each pairs an edge of the target with an edge of the container.
let card!: HTMLElement
// 0 as the card's top meets the bottom of the view, 1 as its bottom leaves the top.
const { scrollYProgress } = createScroll({ target: () => card, offset: ["start end", "end start"] })
const y = createTransform(scrollYProgress, [0, 1], [60, -60])
<w.figure ref={card} style={{ y }} />axis is the axis the offsets read, "y" unless it's "x".
Shaping it
A scroll value is a live value, so every live value works on it:
createTransform(scrollYProgress, [0, 1], ["0%", "100%"])maps it, to any unit or colour.createSpring(scrollYProgress)smooths it, so the effect trails the scroll a little.createVelocity(scrollY)is how fast it's scrolling, to skew or blur with the speed.
Drawn with the scroll
Browsers scroll on a thread of their own, so the scroll's event can reach the page a frame after the scroll shows, and later still while the page is busy. Where the browser has native scroll timelines (Chrome and Edge 115+, Safari 26+, Firefox 159+), Wardic hands a scroll value to the browser instead, as a scroll-driven animation, so it moves with the scroll itself. There's nothing to turn on, and everywhere else Wardic draws the same value itself.
const { scrollYProgress } = createScroll({ container: () => panel })
// Drawn with the scroll: a value itself, or one createTransform of it through ranges.
<w.div style={{ scaleX: scrollYProgress }} />
<w.div style={{ opacity: createTransform(scrollYProgress, [0, 0.5], [0, 1]) }} />
// Drawn by Wardic each frame, from the scroll's event: a function, a spring, a velocity.
<w.div style={{ opacity: createTransform(scrollYProgress, (p) => 1 - p) }} />
<w.div style={{ scaleX: createSpring(scrollYProgress) }} />- The values:
scrollXProgressandscrollYProgresswith nooffset, atarget's progress between two stops whose container edges arestartorend, as Motion's are, andscrollXorscrollYthrough onecreateTransform. - The keys:
opacity,filter,clipPath, atransformstring, and a transform key (scaleX,y,rotate…) while it's the element's only one. With a second, or a layout animation, Wardic draws them all, and hands the key back once it's alone again. - Anything else that sets the key takes it back, from the frame it starts. An
animatetarget or a gesture holds it while it sets the key; then the key returns to its scroll value, and the scroll draws it again. Ananimate()call keeps it where it leaves it, as it does any key. - Nothing else changes: the values still change in the scroll's event, Wardic still writes the key's inline style,
onUpdateis still told, andelement.getAnimations()lists the scroll-driven animation.
Once it's in view
To play an animation as an element scrolls into view, rather than follow the scroll, use whileInView with viewport={{ once: true }}: see Gestures.
In the storefront
The storefront's examples that show it, live and with their code:
- A reading bar for the studio's story: Scroll through how the Lagoon glaze is made, and a bar along the top fills as you read, the minutes left beside it. (on Foldkit)
- A lookbook with depth: Scroll the lookbook and each collection's photo drifts slower than its banner and its words faster, as if they sat at different depths. (on Foldkit)