Gestures

Each gesture has a target that applies while it's on: whileHover, whileTap, whileFocus, whileInView, and whileDrag for drag. When it ends, the element goes back to its animate.

Live demo
gestures/solid2.tsx
import { createWard } from "~/lib/wardic.ts"
import { createEffect, createSignal } from "solid-js"

/** A button that answers hover, press and keyboard focus, and counts its taps. */
export default function Gestures() {
  const [taps, setTaps] = createSignal(0)
  // The button's ward: its motion under each gesture, and its taps, heard.
  const button = createWard({ whileHover: { scale: 1.06 }, whileTap: { scale: 0.94 }, whileFocus: { y: -2 } })
  createEffect(button.when("tap"), () => {
    setTaps((count) => count + 1)
  })
  return (
    <div class="sample-stack">
      <button class="sample-button" {...button}>
        Tap me
      </button>
      <p class="sample-note">Tapped {taps()} times</p>
    </div>
  )
}
gestures/solid1.tsx
import { w } from "~/lib/wardic.ts"
import { createSignal } from "solid-js"

/** A button that answers hover, press and keyboard focus, and counts its taps. */
export default function Gestures() {
  const [taps, setTaps] = createSignal(0)
  return (
    <div class="sample-stack">
      <w.button class="sample-button" whileHover={{ scale: 1.06 }} whileTap={{ scale: 0.94 }} whileFocus={{ y: -2 }} onTap={() => setTaps(taps() + 1)}>
        Tap me
      </w.button>
      <p class="sample-note">Tapped {taps()} times</p>
    </div>
  )
}
gestures/foldkit.ts
import * as Schema from "effect/Schema"
import type { Html, HtmlBuilder } from "foldkit/html"
import { defineMessageUnion } from "foldkit/message"
import { wardic } from "~/lib/wardic.ts"

const Model = Schema.Struct({ taps: Schema.Number })
type Model = typeof Model.Type
const Message = defineMessageUnion({ Tapped: {} })
type Message = typeof Message.Type

export const init = (): Model => ({ taps: 0 })
export const update = (model: Model, message: Message) => Message.match(message, { Tapped: () => ({ model: { taps: model.taps + 1 } }) })

/** A button that answers hover, press and keyboard focus, and counts its taps. */
export const view = (model: Model, h: HtmlBuilder<Message>): Html => {
  const w = wardic.html(h)
  return h.div([h.Class("sample-stack")], [
    // The button's own attributes animate it, and `OnTap` sends a Message at each tap.
    w.button([h.Class("sample-button"), w.WhileHover({ scale: 1.06 }), w.WhileTap({ scale: 0.94 }), w.WhileFocus({ y: -2 }), w.OnTap(Message.Tapped())], ["Tap me"]),
    h.p([h.Class("sample-note")], [`Tapped ${model.taps} times`]),
  ])
}
gestures/motion.tsx
import * as m from "motion/react-m"
import { useState } from "react"

/** A button that answers hover, press and keyboard focus, and counts its taps. */
export default function Gestures() {
  const [taps, setTaps] = useState(0)
  return (
    <div className="sample-stack">
      <m.button className="sample-button" whileHover={{ scale: 1.06 }} whileTap={{ scale: 0.94 }} whileFocus={{ y: -2 }} onTap={() => setTaps((count) => count + 1)}>
        Tap me
      </m.button>
      <p className="sample-note">Tapped {taps} times</p>
    </div>
  )
}

Press

whileTap applies while the pointer is down on the element, and a tap is a press released over it. The keyboard presses too: Enter, and Space as a button takes it. An element that can't take focus gets tabIndex = 0, so a keyboard can reach it.

A press inside a pressable element presses both, as in Motion. propagate={{ tap: false }} keeps an element's press from reaching its ancestors.

Focus

whileFocus applies to the keyboard's focus (:focus-visible), not a pointer's click.

In view

whileInView applies while the element is in the viewport. viewport tunes it: once keeps it on after the first time, margin grows or shrinks the area, amount is how much of the element must show, and root watches a scrolling element in place of the page.

<w.section initial={{ opacity: 0 }} whileInView={{ opacity: 1 }} viewport={{ once: true, amount: 0.4 }} />

Gestures as state

A sensor reads a gesture as a signal, for what isn't an animation: text that says what's under the pointer, or a value a drag sets. createHover, createPress, createFocus and createDrag each take what they read first:

  • A ward: createHover(card) reads the ward's own hover, so nothing listens to its element twice. createDrag(card) gives each sample of the ward's own drag, null before the first.
  • An element: createHover(el) reads that element.
  • Nothing: createHover() is a sensor to give to an element's ref, and its reading: <div ref={hovered} />, then hovered().
Live demo
sensors/solid2.tsx
import { createDrag, createHover, domAnimation, drag } from "@wardic/solid"
import { elements, makeWard } from "@wardic/solid/hooks"
import { Show } from "solid-js"

// This page drags: its own Wardic instance brings `drag`, so pages that don't never load it.
const { w } = makeWard({ features: [domAnimation, drag], elements })

/** A card's hover and drag, read as state: the sensors hear the card's own gestures, through its ward. */
export default function Sensors() {
  const card = w.ward({ whileHover: { scale: 1.04 }, drag: "x", dragSnapToOrigin: true, dragElastic: 0.3 })
  const hovered = createHover(card)
  const dragged = createDrag(card)
  return (
    <div class="sample-stack">
      <div class="sample-card" {...card}>
        Drag me
      </div>
      <p class="sample-note">
        <Show when={hovered()} fallback="Away">
          Over
        </Show>
        , dragged {Math.round(dragged()?.offset.x ?? 0)}px
      </p>
    </div>
  )
}
sensors/solid1.tsx
import { createDrag, createHover, createWardic, domAnimation, drag } from "@wardic/solid/v1"
import { createSignal, Show } from "solid-js"

// This page drags: its own Wardic instance brings `drag`, so pages that don't never load it.
const { w } = createWardic([domAnimation, drag])

/** A card's hover and drag, read as state: Solid 1's sensors take the element itself. */
export default function Sensors() {
  const [card, setCard] = createSignal<HTMLElement>()
  return (
    <div class="sample-stack">
      <w.div class="sample-card" ref={setCard} whileHover={{ scale: 1.04 }} drag="x" dragSnapToOrigin dragElastic={0.3}>
        Drag me
      </w.div>
      <Show when={card()}>{(element) => <Readout card={element()} />}</Show>
    </div>
  )
}

/** The card's hover and drag, read where its element exists. */
function Readout(props: { readonly card: HTMLElement }) {
  const hovered = createHover(props.card)
  // `claim: false`: the card's own drag moves it, and this only reads the gesture.
  const dragged = createDrag(props.card, { claim: false })
  return (
    <p class="sample-note">
      <Show when={hovered()} fallback="Away">
        Over
      </Show>
      , dragged {Math.round(dragged()?.offset.x ?? 0)}px
    </p>
  )
}

On Solid 1, a sensor takes the element itself, so it's made where the element exists: in a component the element is given to.

One element, several refs

Solid 2's ref takes a list, so one element goes to a ward and to sensors at once: ref={[card, hovered, pressed]}. Each is given the element, in order. A ward spread on the element with {...card} gives its own ref, which a ref after the spread replaces, so the ward goes in the list too.

Live demo
ref-arrays/solid2.tsx
import { createHover, createPress } from "@wardic/solid"
import { w } from "~/lib/wardic.ts"

/** One button given to a ward and to two sensors at once, through a list of refs. */
export default function RefArrays() {
  const card = w.ward({ whileHover: { scale: 1.05 }, whileTap: { scale: 0.95 } })
  const hovered = createHover()
  const pressed = createPress()
  return (
    <div class="sample-stack">
      <button class="sample-button" type="button" {...card} ref={[card, hovered, pressed]}>
        Press me
      </button>
      <p class="sample-flags">
        <span class={{ on: hovered() }}>Hovered</span>
        <span class={{ on: pressed() }}>Pressed</span>
      </p>
    </div>
  )
}

Hearing a gesture

  • On Solid 2, the element's ward gives them: make one with w.ward(), give it to the element as wardRef, then await button.next("tap"), or run an effect over button.when("tap").
  • On Solid 1, an element takes Motion's callbacks: onHoverStart, onHoverEnd, onTapStart, onTap, onTapCancel, onViewportEnter, onViewportLeave and the drag and pan ones, each (event, info), with info.point in the viewport's coordinates.

The Features they need

Hover, press, focus and in-view are domAnimation's, which an app's Wardic instance usually has. Each is also a Feature of its own (hover, press, focus, inView), for a page that needs only one.

In the storefront

The storefront's examples that show it, live and with their code: