Dashboard motion: round dashboards page up and down, the ring eases to a notch or a reading, the slider tile to a value set elsewhere; reduced motion honoured by every tween; motion spec in the docs
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
5b2769c141
commit
b500af84ec
@@ -1,9 +1,15 @@
|
||||
import { Power } from "lucide-react"
|
||||
import { AnimatePresence, motion, type Variants } from "motion/react"
|
||||
import {
|
||||
AnimatePresence,
|
||||
motion,
|
||||
useIsPresent,
|
||||
type Variants,
|
||||
} from "motion/react"
|
||||
import { useEffect, useRef, useState } from "react"
|
||||
import { ErrorBoundary } from "react-error-boundary"
|
||||
|
||||
import type { WidgetDef } from "@/client"
|
||||
import { fadeIn } from "@/lib/motion"
|
||||
import { useShortcuts } from "@/lib/shortcuts"
|
||||
import { useClock } from "./ClockWidget"
|
||||
import type { Dashboard } from "./DashboardView"
|
||||
@@ -70,11 +76,77 @@ const lastFace = new Map<string, number>()
|
||||
/** The dashboard a panel showed last, so landing on another can say which. */
|
||||
let lastShown: string | null = null
|
||||
|
||||
/** How far a face or a dashboard slides in as it is paged to, in canvas
|
||||
* pixels, fading in as it comes. */
|
||||
const PAGE_SLIDE = 48
|
||||
|
||||
/** A face arrives from the side it was paged towards. */
|
||||
const TURN: Variants = {
|
||||
enter: (way: number) => ({ opacity: 0, x: way * 48 }),
|
||||
enter: (way: number) => ({ opacity: 0, x: way * PAGE_SLIDE }),
|
||||
shown: { opacity: 1, x: 0 },
|
||||
leave: (way: number) => ({ opacity: 0, x: way * -48 }),
|
||||
leave: (way: number) => ({ opacity: 0, x: way * -PAGE_SLIDE }),
|
||||
}
|
||||
|
||||
/** And a dashboard from below or above, as the faces do from the side. */
|
||||
const RISE: Variants = {
|
||||
enter: (way: number) => ({ opacity: 0, y: way * PAGE_SLIDE }),
|
||||
shown: { opacity: 1, y: 0 },
|
||||
leave: (way: number) => ({ opacity: 0, y: way * -PAGE_SLIDE }),
|
||||
}
|
||||
|
||||
/**
|
||||
* A panel's round dashboards, one at a time, paged up and down.
|
||||
*
|
||||
* The next one rises in from below as the last leaves upwards — the way a
|
||||
* swipe up moved them — and the previous one comes down from above. One that
|
||||
* arrives without a turn, by its address, only fades in.
|
||||
*/
|
||||
export function RoundPanel({
|
||||
dashboard,
|
||||
size,
|
||||
onDashboard,
|
||||
}: {
|
||||
dashboard: Dashboard
|
||||
size: number
|
||||
onDashboard?: (by: number) => void
|
||||
}) {
|
||||
const { look } = useLook()
|
||||
// Read between renders: the turn asked for, and the side the dashboard it
|
||||
// brings arrives from once it has loaded.
|
||||
const turned = useRef(0)
|
||||
const shown = useRef(dashboard.name)
|
||||
const way = useRef(0)
|
||||
if (shown.current !== dashboard.name) {
|
||||
shown.current = dashboard.name
|
||||
way.current = turned.current
|
||||
turned.current = 0
|
||||
}
|
||||
return (
|
||||
<AnimatePresence initial={false} custom={way.current}>
|
||||
<motion.div
|
||||
key={dashboard.name}
|
||||
custom={way.current}
|
||||
variants={RISE}
|
||||
initial="enter"
|
||||
animate="shown"
|
||||
exit="leave"
|
||||
transition={LOOK[look].spring}
|
||||
className="absolute inset-0"
|
||||
>
|
||||
<RoundView
|
||||
dashboard={dashboard}
|
||||
size={size}
|
||||
onDashboard={
|
||||
onDashboard &&
|
||||
((by) => {
|
||||
turned.current = by
|
||||
onDashboard(by)
|
||||
})
|
||||
}
|
||||
/>
|
||||
</motion.div>
|
||||
</AnimatePresence>
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -124,6 +196,8 @@ export function RoundView({
|
||||
was.current = index
|
||||
}
|
||||
const swiped = useRef(false)
|
||||
// A dashboard on its way out keeps its keys to itself.
|
||||
const present = useIsPresent()
|
||||
const [announce, setAnnounce] = useState(
|
||||
() =>
|
||||
Boolean(onDashboard) &&
|
||||
@@ -148,11 +222,12 @@ export function RoundView({
|
||||
onFace?.(next)
|
||||
}
|
||||
|
||||
const keys = !editing && present
|
||||
useShortcuts({
|
||||
pagedown: () => (editing ? false : page(index + 1)),
|
||||
pageup: () => (editing ? false : page(index - 1)),
|
||||
"shift+pagedown": () => (onDashboard && !editing ? onDashboard(1) : false),
|
||||
"shift+pageup": () => (onDashboard && !editing ? onDashboard(-1) : false),
|
||||
pagedown: () => (keys ? page(index + 1) : false),
|
||||
pageup: () => (keys ? page(index - 1) : false),
|
||||
"shift+pagedown": () => (onDashboard && keys ? onDashboard(1) : false),
|
||||
"shift+pageup": () => (onDashboard && keys ? onDashboard(-1) : false),
|
||||
})
|
||||
|
||||
/** A drag that ends far enough away is a swipe, measured in canvas pixels
|
||||
@@ -270,9 +345,10 @@ export function RoundView({
|
||||
{announce ? (
|
||||
<motion.div
|
||||
key="announce"
|
||||
initial={{ opacity: 0 }}
|
||||
animate={{ opacity: 1 }}
|
||||
exit={{ opacity: 0 }}
|
||||
variants={fadeIn}
|
||||
initial="hidden"
|
||||
animate="visible"
|
||||
exit="exit"
|
||||
className="pointer-events-none absolute inset-0 z-10 flex items-center justify-center"
|
||||
>
|
||||
<output
|
||||
|
||||
Reference in New Issue
Block a user