Illustrations
Selection
Direction Aware Selection
A selectable tile list whose hover fill enters from the side your pointer came in and leaves the way it exited.
Plays on its own. Click to take over.
Install
Copies the source into components/illustrations/direction-aware-selection.tsx and installs what it needs.
$ pnpm dlx shadcn@latest add https://makeui.xyz/r/direction-aware-selection.jsonUsage
app/page.tsx
import { DirectionAwareSelection } from "@/components/illustrations/direction-aware-selection"
export default function Example() {
return (
<DirectionAwareSelection
options={["Design", "Motion", "Code", "Ship"]}
onChange={(v) => console.log(v)}
/>
)
}Source
Prefer to copy by hand? It's one file. Requires motion and the shadcn cn helper.
components/illustrations/direction-aware-selection.tsx
"use client"
import { useState } from "react"
import { AnimatePresence, motion } from "motion/react"
import { Check } from "lucide-react"
import { cn } from "@/lib/utils"
export type Side = "top" | "right" | "bottom" | "left"
type Props = {
options?: string[]
/** Controlled selection. Leave out to let it manage itself. */
value?: string
defaultValue?: string
onChange?: (value: string) => void
/** Drive the hover fill from outside (the docs demo uses this to play itself). */
preview?: { index: number; side: Side; exit?: Side } | null
className?: string
}
const offsets: Record<Side, { x: string; y: string }> = {
top: { x: "0%", y: "-101%" },
bottom: { x: "0%", y: "101%" },
left: { x: "-101%", y: "0%" },
right: { x: "101%", y: "0%" },
}
/** The edge of the box closest to the pointer. Normalised so wide tiles don't favour left/right. */
function nearestSide(e: React.PointerEvent<HTMLElement>): Side {
const r = e.currentTarget.getBoundingClientRect()
const x = (e.clientX - r.left) / r.width - 0.5
const y = (e.clientY - r.top) / r.height - 0.5
return Math.abs(x) > Math.abs(y) ? (x < 0 ? "left" : "right") : y < 0 ? "top" : "bottom"
}
/** Selectable tiles. The hover fill enters from the side the pointer came in and leaves the way it went out. */
export function DirectionAwareSelection({
options = ["Design", "Motion", "Code", "Ship"],
value,
defaultValue,
onChange,
preview,
className,
}: Props) {
const [internal, setInternal] = useState(defaultValue ?? options[0])
const selected = value ?? internal
const [hover, setHover] = useState<{ index: number; side: Side } | null>(null)
const [exitSide, setExitSide] = useState<Side>("left")
const active = preview ?? hover
// keep the exit side in step with a driven preview (state set during render is the supported pattern)
if (preview?.exit && preview.exit !== exitSide) setExitSide(preview.exit)
function leave(side: Side) {
setExitSide(side)
setHover(null)
}
return (
<div role="radiogroup" className={cn("grid w-72 grid-cols-2 gap-2", className)}>
{options.map((option, i) => {
const isSelected = option === selected
const side = active?.index === i ? active.side : null
return (
<button
key={option}
role="radio"
aria-checked={isSelected}
onPointerEnter={(e) => setHover({ index: i, side: nearestSide(e) })}
onPointerLeave={(e) => leave(nearestSide(e))}
onClick={() => {
setInternal(option)
onChange?.(option)
}}
className={cn(
"relative h-20 overflow-hidden rounded-2xl p-3 text-left text-sm font-medium outline-none transition-colors duration-200 focus-visible:ring-2 focus-visible:ring-neutral-400/60",
isSelected
? "bg-neutral-900 text-white dark:bg-white dark:text-neutral-900"
: "bg-neutral-100 text-neutral-800 dark:bg-neutral-800 dark:text-neutral-100"
)}
>
<AnimatePresence custom={exitSide} initial={false}>
{side && !isSelected && (
<motion.span
key="fill"
aria-hidden
className="absolute inset-0 bg-neutral-200 dark:bg-neutral-700"
custom={exitSide}
variants={{ exit: (s: Side) => ({ ...offsets[s] }) }}
initial={offsets[side]}
animate={{ x: "0%", y: "0%" }}
exit="exit"
transition={{ type: "spring", stiffness: 380, damping: 36 }}
/>
)}
</AnimatePresence>
<span className="relative">{option}</span>
<AnimatePresence initial={false}>
{isSelected && (
<motion.span
key="check"
className="absolute bottom-3 right-3 grid size-5 place-items-center rounded-full bg-white text-neutral-900 dark:bg-neutral-900 dark:text-white"
initial={{ scale: 0, opacity: 0 }}
animate={{ scale: 1, opacity: 1 }}
exit={{ scale: 0, opacity: 0 }}
transition={{ type: "spring", stiffness: 500, damping: 28 }}
>
<Check className="size-3" strokeWidth={3} />
</motion.span>
)}
</AnimatePresence>
</button>
)
})}
</div>
)
}