SkillPilotAI

UI libraries and animations

A modal that only looks right is not enough: it must close on Escape, trap focus and talk to screen readers. This note covers headless UI with Radix, ready components with shadcn/ui, and animations with Motion (including exit animations with AnimatePresence).

52.1 Problem: a handmade modal

  • The open state shows a div that looks like a modal; with a mouse everything works.
  • With the keyboard it breaks: Escape does not close it and Tab moves focus to the page behind it.
React (JSX)
import { useState } from "react";export function DeleteButton() {  const [open, setOpen] = useState(false);  return (    <>      <button onClick={() => setOpen(true)}>Delete</button>      {open && (        <div className="modal">          <h2>Delete item?</h2>          <button onClick={() => setOpen(false)}>Cancel</button>        </div>      )}    </>  );}
Output
[button] Delete# Delete item?[button] CancelEsc pressed: modal still openTab: focus went behind the modal

52.2 Why is it used?

  • An accessible modal must close on Escape, keep focus trapped inside, and return focus to the trigger button when it closes.
  • The screen reader must know it is a dialog (role dialog, a title).
  • Writing all of this by hand is tricky, so we use headless libraries: they give behaviour and accessibility and leave styling to you.
What an accessible modal needs
  1. 1.User clicks the trigger: dialog opens

  2. 2.Focus moves inside and is trapped there (Tab cannot leave)

  3. 3.Screen reader announces role dialog + the title

  4. 4.Escape or Close closes the dialog

  5. 5.Focus returns to the trigger button

Headless libraries like Radix do all of this for you

52.3 Fix: Radix Dialog

  • Radix is a headless library; the npm package is radix-ui.
  • Compound component pattern: Root manages the state, Trigger opens it, Portal renders the content at the end of the page.
  • Content holds Title, Description and a Close button; Overlay is the backdrop.
  • No useState and no keyboard handler written by us, yet Escape, focus trap, focus return and the screen reader title all work.
React (JSX)
import { Dialog } from "radix-ui"; export const DeleteDialog = () => (  <Dialog.Root>    <Dialog.Trigger>Delete</Dialog.Trigger>    <Dialog.Portal>      <Dialog.Overlay className="overlay" />      <Dialog.Content className="modal">        <Dialog.Title>Delete item?</Dialog.Title>        <Dialog.Description>Can't undo this.</Dialog.Description>        <Dialog.Close>Cancel</Dialog.Close>      </Dialog.Content>    </Dialog.Portal>  </Dialog.Root>);
Output
[button] Delete # Delete item?Can't undo this.[button] Cancel [button] Delete

52.4 shadcn/ui: a CLI, not a library

  • shadcn/ui is not an npm dependency; it is a CLI that copies component source code into your project.
  • init sets up components.json, the cn helper (src/lib/utils.ts) and your CSS. Newer versions ask for the component library (default Base UI), so the flags -b radix -p nova pick Radix and the nova style.
  • add copies button and dialog straight into src/components/ui.
Terminal
npx shadcn@latest init -b radix -p novanpx shadcn@latest add button dialog
Output
$ npx shadcn@latest init -b radix -p nova✔ Preflight checks.✔ Verifying framework. Found Vite.✔ Validating Tailwind CSS. Found v4.✔ Validating import alias.✔ Writing components.json.✔ Checking registry.✔ Installing dependencies.✔ Created 1 file:  - src/lib/utils.ts✔ Updating src/index.cssProject initialization completed.You may now add components.$ npx shadcn@latest add button dialog✔ Checking registry.✔ Created 2 files:  - src/components/ui/button.tsx  - src/components/ui/dialog.tsx

52.5 The code is yours: add a variant

  • The generated button uses cva (class-variance-authority) to define variants.
  • default, destructive and outline come from shadcn; we added our own upi variant (green button).
  • Anywhere in the app, <Button variant="upi"> now gets that style.
React (TSX)
// components/ui/button.tsx (generated, now yours)const buttonVariants = cva(  "inline-flex rounded-lg text-sm",  {    variants: {      variant: {        default: "bg-primary",        destructive: "bg-destructive",        outline: "border bg-background",        upi: "bg-green-600 text-white",      },    },    defaultVariants: { variant: "default" },  });

52.6 Using the shadcn Dialog

  • Import from your own @/components/ui folder, not from an npm package.
  • DialogTrigger gets asChild with a destructive Button inside.
  • DialogContent already includes the close cross button; under the hood it is the same Radix Dialog with Tailwind styling.
React (TSX)
import { Button } from "@/components/ui/button";import { Dialog, DialogContent, DialogTitle,  DialogDescription, DialogTrigger } from "@/components/ui/dialog"; export const DeleteDialog = () => (  <Dialog>    <DialogTrigger asChild>      <Button variant="destructive">Delete</Button>    </DialogTrigger>    <DialogContent>      <DialogTitle>Delete item?</DialogTitle>      <DialogDescription>Can't undo this.</DialogDescription>    </DialogContent>  </Dialog>);
Output
[button] Delete # Delete item?Can't undo this.[button] ✕
What is inside shadcn/ui
  1. 1.Radix: behaviour and accessibility

  2. 2.Tailwind + cn(): styling

  3. 3.cva: variants

  4. 4.One component file in components/ui, inside your repo

52.7 Motion: enter and hover animations

  • Motion (formerly Framer Motion), npm package motion, import from motion/react.
  • motion.div is a normal div that understands animation props.
  • initial = start values (opacity 0, a bit lower), animate = final values, transition = duration 0.3 s.
  • whileHover grows the card a little, whileTap shrinks it, no CSS keyframes needed.
React (JSX)
import { motion } from "motion/react";export function ProductCard({ name }) {  return (    <motion.div      initial={{ opacity: 0, y: 20 }}      animate={{ opacity: 1, y: 0 }}      transition={{ duration: 0.3 }}      whileHover={{ scale: 1.03 }}      whileTap={{ scale: 0.97 }}      className="card"    >      {name}    </motion.div>  );}

52.8 Exit animation: AnimatePresence

  • When React removes an element it leaves the DOM immediately, so an exit animation never gets time to run.
  • AnimatePresence keeps its child in the DOM until its exit animation finishes.
  • The direct child of AnimatePresence needs a unique key.
React (JSX)
import { motion, AnimatePresence } from "motion/react";export const Toast = ({ show }) => (  <AnimatePresence>    {show && (      <motion.p        key="toast"        initial={{ opacity: 0, y: 10 }}        animate={{ opacity: 1, y: 0 }}        exit={{ opacity: 0, y: 10 }}      >        Added to cart      </motion.p>    )}  </AnimatePresence>);
  • Checkpoint: motion.p has exit but no AnimatePresence. Will it fade out when show becomes false?
  • No: React removes it immediately and Motion never runs the exit. exit only works inside AnimatePresence.
React (JSX)
const Toast = ({ show }) =>  show && (    <motion.p exit={{ opacity: 0 }}>      Added to cart    </motion.p>  );
Exit animation with AnimatePresence
  1. 1.show becomes false

  2. 2.React wants to remove <motion.p key="toast">

  3. 3.AnimatePresence keeps it in the DOM

  4. 4.Motion runs exit (opacity 0, y 10)

  5. 5.After the animation the element is removed

52.9 Comparison: headless vs shadcn vs styled library

PointRadix (headless)shadcn/uiMUI (styled library)
What you getBehaviour + accessibility, no stylesRadix + Tailwind styles, code copied inReady styled components
Where the code livesnode_modulesYour repo (components/ui)node_modules
CustomisingFull control, you style itEdit the file freelyTheme overrides
Updatesnpm updateNot automatic (you own the code)npm update
Best forYour own design systemCustom design, fast start with TailwindFast start, Material look is fine

52.10 Common mistakes

  • Putting your own button inside Trigger without asChild: Trigger already renders a button, so you get a button inside a button (invalid HTML, confusing focus).
  • With asChild, Radix does not create its own element and merges the behaviour onto your button.
  • Using exit without AnimatePresence.
  • Thinking shadcn/ui is a dependency like MUI that npm update upgrades: the code is copied, you own it, updates do not arrive automatically.
React (JSX)
// Wrong: button inside a button<Dialog.Trigger>  <button className="btn">Delete</button></Dialog.Trigger>
React (JSX)
// Right: one button, behaviour merged<Dialog.Trigger asChild>  <button className="btn">Delete</button></Dialog.Trigger>

52.11 Best practices

  • Keep one UI approach in the project (for example shadcn + Tailwind).
  • Animate only transform and opacity, not width or top: they run smoothly and the layout is not recalculated.
  • Keep animations short, around 200-300 ms.
  • Respect the user's reduced motion setting: MotionConfig reducedMotion="user".

52.12 Interview tips

  • Headless UI library = behaviour and accessibility (focus trap, keyboard, ARIA) while styling stays in your control. Radix is an example.
  • shadcn/ui copies the source of Tailwind-styled components built on Radix into your project.
  • Styled libraries like MUI give a fast start; headless is more flexible for a custom design.
  • Exit animations need AnimatePresence because React removes elements immediately.
  • Animate transform/opacity only, 200-300 ms, and honour prefers-reduced-motion.