@unifycss/react
v1.1.0
Published
React integration for UnifyCSS featuring ThemeProvider, custom hooks & theme switcher
Maintainers
Readme
@unifycss/react
React integration for UnifyCSS — ThemeProvider, custom hooks, and theme switching system.
Table of Contents
- Installation
- Quick Start
- ThemeProvider
- Hooks API
- Custom Themes
- Dark / Light Toggle Pattern
- Persistent Theme (localStorage)
- With CSS Classes
- License
Installation
npm install @unifycss/react @unifycss/unifycss
# or
pnpm add @unifycss/react @unifycss/unifycss
# or
yarn add @unifycss/react @unifycss/unifycssQuick Start
// main.tsx or index.tsx
import React from 'react';
import ReactDOM from 'react-dom/client';
import { ThemeProvider } from '@unifycss/react';
import '@unifycss/unifycss/unifycss.css';
import App from './App';
ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<ThemeProvider>
<App />
</ThemeProvider>
</React.StrictMode>
);Then use any UnifyCSS classes anywhere in your components:
// App.tsx
export default function App() {
return (
<main className="min-h-screen bg-dark-base p-8">
<h1 className="heading-1 text-white">Hello UnifyCSS + React</h1>
<button className="btn-gold mt-4">Click me</button>
</main>
);
}ThemeProvider
ThemeProvider wraps your application and provides theme context to all child components.
import { ThemeProvider } from '@unifycss/react';
import { lightTheme, darkTheme } from '@unifycss/shared';
// Default: light theme
<ThemeProvider>
<App />
</ThemeProvider>
// Start in dark mode
<ThemeProvider initialTheme={darkTheme} initialThemeName="dark">
<App />
</ThemeProvider>Props
| Prop | Type | Default | Description |
|---|---|---|---|
| children | ReactNode | — | Required. Your app component tree |
| initialTheme | ThemeConfig | lightTheme | Starting theme configuration object |
| initialThemeName | string | "light" | Starting theme name string identifier |
Hooks API
useTheme()
Access full theme context: current theme object, theme name, and the apply function.
import { useTheme } from '@unifycss/react';
import { darkTheme } from '@unifycss/shared';
export function MyComponent() {
const { theme, currentThemeName, applyTheme } = useTheme();
return (
<div>
<p>Current theme: <strong>{currentThemeName}</strong></p>
<p>Primary color: {theme?.colors?.primary}</p>
<button onClick={() => applyTheme(darkTheme, 'dark')}>
Switch to Dark
</button>
</div>
);
}Returns:
{
theme: ThemeConfig | null; // The currently applied theme object
appliedThemeName: string | null; // The name identifier of the applied theme
currentThemeName: string | null; // Alias for appliedThemeName
applyTheme: (theme: ThemeConfig, themeName?: string) => void; // Applies a new theme
}useApplyTheme()
Get a memoized function to switch themes without re-subscribing to the full context.
import { useApplyTheme } from '@unifycss/react';
import { darkTheme, lightTheme } from '@unifycss/shared';
export function ThemeToggle() {
const applyTheme = useApplyTheme();
return (
<div className="flex gap-2">
<button className="btn-ghost" onClick={() => applyTheme(lightTheme, 'light')}>
☀️ Light
</button>
<button className="btn-ghost" onClick={() => applyTheme(darkTheme, 'dark')}>
🌙 Dark
</button>
</div>
);
}useCurrentThemeName()
Get only the current theme name string — ideal for conditional rendering.
import { useCurrentThemeName } from '@unifycss/react';
export function ThemeIndicator() {
const themeName = useCurrentThemeName();
return (
<span className={`badge ${themeName === 'dark' ? 'badge-info' : 'badge-warning'}`}>
{themeName === 'dark' ? '🌙 Dark Mode' : '☀️ Light Mode'}
</span>
);
}useCurrentTheme()
Get only the current ThemeConfig object.
import { useCurrentTheme } from '@unifycss/react';
export function ThemeDebugger() {
const theme = useCurrentTheme();
if (!theme) return null;
return (
<pre className="glass-panel p-4 font-mono text-xs">
{JSON.stringify(theme.colors, null, 2)}
</pre>
);
}Custom Themes
Create custom themes by extending or composing ThemeConfig:
import type { ThemeConfig } from '@unifycss/shared';
import { lightTheme } from '@unifycss/shared';
import { useApplyTheme } from '@unifycss/react';
// Brand-specific theme
const brandTheme: ThemeConfig = {
...lightTheme,
colors: {
...lightTheme.colors,
primary: '#7c3aed', // Purple brand
secondary: '#4ade80', // Emerald accent
background: '#f5f3ff', // Purple-tinted surface
text: '#1e1b4b', // Dark purple text
},
typography: {
...lightTheme.typography,
fontFamily: '"Outfit", system-ui, sans-serif',
fontSize: '16px',
},
};
export function BrandThemeButton() {
const applyTheme = useApplyTheme();
return (
<button
className="btn-primary"
onClick={() => applyTheme(brandTheme, 'brand')}
>
Apply Brand Theme
</button>
);
}Dark / Light Toggle Pattern
Complete dark/light mode switcher component:
import { useState, useCallback } from 'react';
import { useTheme } from '@unifycss/react';
import { lightTheme, darkTheme } from '@unifycss/shared';
export function DarkModeToggle() {
const { currentThemeName, applyTheme } = useTheme();
const isDark = currentThemeName === 'dark';
const toggle = useCallback(() => {
if (isDark) {
applyTheme(lightTheme, 'light');
document.documentElement.classList.remove('dark');
} else {
applyTheme(darkTheme, 'dark');
document.documentElement.classList.add('dark');
}
}, [isDark, applyTheme]);
return (
<button
className={isDark ? 'btn-gold-outline' : 'btn-outline-primary'}
onClick={toggle}
aria-label="Toggle dark mode"
>
{isDark ? '☀️ Light Mode' : '🌙 Dark Mode'}
</button>
);
}Persistent Theme (localStorage)
ThemeProvider automatically persists the last applied theme to localStorage. On page reload, the saved theme is restored:
// ThemeProvider handles localStorage automatically.
// You can read the saved theme manually with:
const savedTheme = localStorage.getItem('unifycss-theme-name');
// → 'dark', 'light', or your custom theme nameWith CSS Classes
Use @unifycss/react together with @unifycss/unifycss class names:
import { useCurrentThemeName } from '@unifycss/react';
export function ThemedCard({ title, children }: { title: string; children: React.ReactNode }) {
const themeName = useCurrentThemeName();
const isDark = themeName === 'dark';
return (
<div className={`card ${isDark ? 'card-dark' : 'card-bordered'}`}>
<div className="card-header">
<h3 className="heading-3 text-white">{title}</h3>
</div>
<div className="card-body">{children}</div>
</div>
);
}License
MIT © UnifyCSS Team
