Live values

Solid 2

Announcements that take turns

The strip above the shop shows one message at a time, a bar filling under it, and holds while you read it or look away.

Live demo

Free delivery on orders over £60

Reduced motion: The messages fade without sliding; the bar still fills, as time passes, not as an animation of its own.

Why

A strip of news above a shop has more to say than fits, and a visitor reads one line at a time. Taking turns shows each in full, and the bar under it says when the next comes. It holds while the pointer rests on it, and Pause stops it for good, so nobody loses a message halfway through reading it.

createCycle keeps the messages and steps to the next, the first after the last; each is a new child of <AnimatePresence mode="wait">, so the last fades out before the next fades in. createAnimationFrame runs every frame with the frame's delta, which adds up to each message's four seconds and fills the bar, a live value in its scaleX. createPageInView is false while the tab is in the background: then the time stands still, so a visitor coming back finds the message they left, not the one after.

The code

examples/announcements/Demo.tsx
import { AnimatePresence, createAnimationFrame, createCycle, createPageInView } from "@wardic/solid"
import { w } from "~/lib/wardic.ts"
import { createSignal, Show } from "solid-js"

const MESSAGES = [
  "Free delivery on orders over £60",
  "New: the Lagoon glaze, fired in small batches",
  "Every piece is wrapped by hand",
  "Free returns within 30 days",
] as const

// How long each message stays, in milliseconds of the page being seen.
const HOLD = 4000

// The message going fades up and out, then the next fades up in (mode="wait").
const message = {
  initial: { opacity: 0, y: 8 },
  animate: { opacity: 1, y: 0 },
  exit: { opacity: 0, y: -8 },
  transition: { type: "tween", duration: 0.2 },
} as const

export default function Announcements() {
  // One message at a time: `next()` steps to the following one, the first after the last.
  const [current, next] = createCycle(...MESSAGES)
  // Whether the page is shown: a tab in the background spends no time on messages nobody sees.
  const visible = createPageInView()
  const [hovered, setHovered] = createSignal(false)
  const [held, setHeld] = createSignal(false)
  // How far the message is through its time, 0 to 1: the bar under it, as a live value.
  const [progress, setProgress] = createSignal(0)
  let shown = 0

  const step = () => {
    shown = 0
    setProgress(0)
    next()
  }

  // Each frame, the message's time runs on by the frame's own `delta`, but only while someone can
  // read it: not while the page is hidden, the pointer rests on it, or it's paused.
  createAnimationFrame((_, delta) => {
    if (!visible() || hovered() || held()) return
    shown += delta
    if (shown >= HOLD) step()
    else setProgress(shown / HOLD)
  })

  return (
    <div class="announcements" onMouseEnter={() => setHovered(true)} onMouseLeave={() => setHovered(false)}>
      <div class="announcement">
        <AnimatePresence mode="wait">
          {/* keyed: each message is a new child, so the last one leaves before the next enters. */}
          <Show when={current()} keyed>
            {(text) => (
              <w.p class="announcement-message" {...message}>
                {text}
              </w.p>
            )}
          </Show>
        </AnimatePresence>
      </div>
      <w.div class="announcement-bar" style={{ scaleX: progress }} />
      <div class="announcement-controls">
        <button type="button" class="button quiet" aria-pressed={String(held()) as "true" | "false"} onClick={() => setHeld(!held())}>
          {held() ? "Play" : "Pause"}
        </button>
        <button type="button" class="button quiet" onClick={step}>
          Next
        </button>
      </div>
    </div>
  )
}