Skip to content
Which screen

Settings

Vaun Blulab.vaunb.lu
NotificationsOn
Appearance
LanguageEnglish
SoundsOff
Scaled from the toptoggle drifts 17px

Settings

Vaun Blulab.vaunb.lu
NotificationsOn
Appearance
LanguageEnglish
SoundsOff
Scaled from the toggletoggle drifts 0px

Study

Scale the page, not what you press

Hold scales the page to 0.93 from the pressed toggle's center, with the toggle in its own layer, so it drifts 0px instead of 17px.

Holding the Appearance toggle in Hour 88 shrinks the page to 0.93 and blurs it 2px, from the top. The toggle is part of the page, so it shrinks toward the top too: 17px up and to the left. Let go once it has moved and the pointer isn’t over it anymore, so the release can miss the thing you pressed.

Scale from what’s pressed instead: set the transform origin to the toggle’s center, and a point at the origin doesn’t move (drift = (1 − scale) × distance from origin, and the distance is 0). Then lift the toggle into its own layer above the page, so it keeps its size and stays sharp while everything around it recedes.

Credits
dzzgnr
Original
The first version
"use client"

import { useRef, useState } from "react"
import { driftOf, NO_PRESS, type Origin, ORIGINS, type Press, pressAt } from "../../_lib/press"
import { useClock } from "../../_lib/use-clock"
import { useReducedMotion } from "../../_lib/use-reduced-motion"
import { PressCompareOriginSwitch } from "./nested/origin-switch"
import { PressCompareScreen } from "./nested/screen"

type Side = { press: Press; dark: boolean }
const START: Side = { press: NO_PRESS, dark: false }

/**
 * The Hour 88 menu twice. Hold the Light/Dark toggle on each: on the left the page scales from the
 * top and the toggle slides out from under the pointer, so letting go there often misses it; on the
 * right the page scales from the toggle and it stays put.
 */
function PressCompare() {
  const reduced = useReducedMotion()
  const root = useRef<HTMLDivElement>(null)
  const { now, nowRef } = useClock(root, reduced)
  const [sides, setSides] = useState<Record<Origin, Side>>({ top: START, pressed: START })
  /* Phones show one screen at a time, so the one you hold and its caption fit on screen. */
  const [shown, setShown] = useState<Origin>("top")

  const update = (origin: Origin, next: (side: Side) => Side) => setSides((all) => ({ ...all, [origin]: next(all[origin]) }))
  const pressChange = (origin: Origin) => (down: boolean) =>
    update(origin, (side) => {
      if (side.press.down === down) return side
      const at = nowRef.current
      return { ...side, press: { down, at, from: pressAt(side.press, at) } }
    })

  return (
    <div ref={root} className="flex flex-col items-center gap-6">
      <PressCompareOriginSwitch value={shown} onChange={setShown} />
      <div className="flex items-start justify-center gap-10">
      {ORIGINS.map(({ id, label, detail }) => (
        <figure key={id} className={`flex flex-col items-center gap-3 ${id === shown ? "" : "max-sm:hidden"}`}>
          <PressCompareScreen
            origin={id}
            press={reduced ? 0 : pressAt(sides[id].press, now)}
            darkness={sides[id].dark ? 1 : 0}
            animate
            onPressChange={pressChange(id)}
            onToggle={() => update(id, (side) => ({ ...side, dark: !side.dark }))}
          />
          <figcaption className="flex flex-col items-center gap-0.5 text-center font-mono text-xs">
            <span className="text-ink">{label}</span>
            <span className="text-ink-muted tabular-nums">
              {detail} {Math.round(driftOf(id))}px
            </span>
          </figcaption>
        </figure>
      ))}
      </div>
    </div>
  )
}

PressCompare.Screen = PressCompareScreen
PressCompare.OriginSwitch = PressCompareOriginSwitch

export { PressCompare }
All entries