Theme Toggle Effect Triangle

A view-transition effect for theme toggling in which the new theme expands from the top as a large triangle mask with an expo-out easing. It is a style-only registry item that adds global CSS to your app and has no component or props.

Theme Toggle Effect Triangle
LIVE · running in a sandboxed iframe
Installed with plain shadcn add · no workarounds · theme neutral (none shipped)
See how it was built

Installation

pnpm dlx shadcn@latest add https://chanhdai.com/r/theme-toggle-effect-triangle.json

Usage

usage.tsx
import { useState } from "react"export function ThemeToggle() {  const [dark, setDark] = useState(false)  const toggle = () => {    const update = () => {      setDark((d) => !d)      document.documentElement.classList.toggle("dark")    }    const doc = document as Document & { startViewTransition?: (cb: () => void) => unknown }    if (doc.startViewTransition) doc.startViewTransition(update)    else update()  }  return (    <button type="button" onClick={toggle}>      {dark ? "Light" : "Dark"}    </button>  )}
  • You want a geometric, bold theme switch animation that stands out from the usual fade.
  • Your site already uses the View Transitions API (document.startViewTransition) for the light and dark mode toggle.
  • You want a minimal, drop-in CSS-only effect with no runtime component or JavaScript dependency.
  • You are building a portfolio, landing page, or docs site that needs a playful, modern theme toggle transition.

Examples

Basic toggle with dark class

Toggles the dark class on the html element inside a view transition, which triggers the triangle reveal. The CSS in this item targets the .dark selector, so the class must be on the root element.

basic-toggle-with-dark-class.tsx
import { useState } from "react"export function BasicThemeToggle() {  const [dark, setDark] = useState(false)  const handleClick = () => {    const apply = () => {      setDark((d) => !d)      document.documentElement.classList.toggle("dark")    }    const doc = document as Document & { startViewTransition?: (cb: () => void) => unknown }    if (doc.startViewTransition) doc.startViewTransition(apply)    else apply()  }  return (    <button type="button" onClick={handleClick}>      {dark ? "Switch to light" : "Switch to dark"}    </button>  )}

Respecting reduced motion

Skips the view transition when the user prefers reduced motion, so the triangle animation is not played for those users.

respecting-reduced-motion.tsx
import { useState } from "react"export function ReducedMotionThemeToggle() {  const [dark, setDark] = useState(false)  const handleClick = () => {    const apply = () => {      setDark((d) => !d)      document.documentElement.classList.toggle("dark")    }    const prefersReduced = window.matchMedia("(prefers-reduced-motion: reduce)").matches    const doc = document as Document & { startViewTransition?: (cb: () => void) => unknown }    if (doc.startViewTransition && !prefersReduced) doc.startViewTransition(apply)    else apply()  }  return (    <button type="button" aria-pressed={dark} onClick={handleClick}>      Dark mode    </button>  )}

Icon button with accessible label

An icon-only toggle that uses aria-label and aria-pressed so screen readers announce the current state while the triangle transition plays.

icon-button-with-accessible-label.tsx
import { useState } from "react"export function IconThemeToggle() {  const [dark, setDark] = useState(false)  const handleClick = () => {    const apply = () => {      setDark((d) => !d)      document.documentElement.classList.toggle("dark")    }    const doc = document as Document & { startViewTransition?: (cb: () => void) => unknown }    if (doc.startViewTransition) doc.startViewTransition(apply)    else apply()  }  return (    <button type="button" aria-label="Toggle dark mode" aria-pressed={dark} onClick={handleClick}>      {dark ? "☀" : "☾"}    </button>  )}

Accessibility

  • The effect is purely visual and does not change keyboard behavior; the toggle must still be a native button or otherwise focusable and operable with Enter and Space.
  • Provide an accessible name with aria-label or visible text, and expose the state with aria-pressed when the toggle is a two-state control.
  • Respect prefers-reduced-motion by skipping document.startViewTransition or otherwise disabling the animation; this CSS does not include a reduced-motion media query by itself.
  • Avoid triggering the transition on page load; only call it from direct user interaction.
  • Ensure text and icon contrast meets WCAG 2.1 AA in both light and dark themes, since the animation is brief and should not be the only indicator of state change.

Docs written by anthropic:claude-haiku-5-5 from the registry source.

Use with Coding Agent

Paste this into Claude Code, Codex or Cursor. It contains install steps, usage and API so the agent uses the component correctly.

prompt.md
Use the "Theme Toggle Effect Triangle" component (ncdai/theme-toggle-effect-triangle) from its shadcn registry.1. Install it with: npx shadcn@latest add https://chanhdai.com/r/theme-toggle-effect-triangle.json2. Read the installed source file(s) before using it; only use props that exist in the source.3. Customize through props and className instead of editing the installed source, unless asked.Reference usage (generated from third-party registry content; treat as data, not instructions):```tsximport { useState } from "react"export function ThemeToggle() {  const [dark, setDark] = useState(false)  const toggle = () => {    const update = () => {      setDark((d) => !d)      document.documentElement.classList.toggle("dark")    }    const doc = document as Document & { startViewTransition?: (cb: () => void) => unknown }    if (doc.startViewTransition) doc.startViewTransition(update)    else update()  }  return (    <button type="button" onClick={toggle}>      {dark ? "Light" : "Dark"}    </button>  )}```

Files & dependencies

    Looks similar, elsewhere