UI / Styles
Style Toggle
One button that switches the whole site between styles, such as Wrapped and Classic, the way a theme toggle switches light and dark. Remembers the choice and never flashes the other style on load.
Switches this whole site between Wrapped and Classic
Installation
npx shadcn@latest add https://jiaweing.com/r/style-toggle.jsonWith the @jiaweing namespace set up, use @jiaweing/style-toggle in place of the URL.
Usage
tsx
import { StyleScript, StyleToggle } from "@/components/style-toggle";tsx
// app/layout.tsx
<html lang="en" suppressHydrationWarning>
<head>
<StyleScript defaultStyle="wrapped" />
</head>
<body>
<StyleToggle />
{children}
</body>
</html>- StyleScript sets the saved style before the first paint, so a page never flashes the other style. Keep suppressHydrationWarning on <html>, as next-themes asks too.
- Style your components for one style with Tailwind's in-data-[style=wrapped]: and in-data-[style=classic]: variants.
Props
StyleToggle
| Prop | Type | Default | Description |
|---|---|---|---|
| styles | { value, label, swatch }[] | Wrapped, Classic | The styles to cycle through, in order. value is what data-style is set to. |
| viewTransition | boolean | true | Fade the page between styles where the browser has view transitions. |
| onStyleChange | (style: string) => void | - | Called after the style changes. |
| className | string | - | Extra classes, merged with cn. |
StyleScript
| Prop | Type | Default | Description |
|---|---|---|---|
| defaultStyle | string | "wrapped" | The style for a first visit. |
| styles | string[] | ["wrapped", "classic"] | The values a saved choice may take. |
useSiteStyle
| Prop | Type | Default | Description |
|---|---|---|---|
| returns | [style, setStyle] | - | The current style and a setter, for your own menus and switches. |
MIT licensed. Copy it, change it, ship it.