@structyl/themes 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +152 -0
- package/dist/index.cjs +928 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +425 -0
- package/dist/index.d.ts +425 -0
- package/dist/index.mjs +895 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +61 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 your-lib contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
# @structyl/themes
|
|
2
|
+
|
|
3
|
+
> Runtime theming for React, powered by CSS variables.
|
|
4
|
+
|
|
5
|
+

|
|
6
|
+

|
|
7
|
+
|
|
8
|
+
The theming layer of [structyl](https://github.com/imirfanul/structyl). It provides a `ThemeProvider` that drives a CSS-variable design-token system, with light/dark/system color modes, an SSR-safe script to prevent the flash of unstyled content, and a runtime accent-color preset API. It powers the look of `@structyl/styled` and the DataTable, but works standalone in any React app — App Router or otherwise.
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
pnpm add @structyl/themes
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @structyl/themes
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
yarn add @structyl/themes
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
Wrap your app in `ThemeProvider` and read or update the theme with `useTheme`:
|
|
27
|
+
|
|
28
|
+
```tsx
|
|
29
|
+
import { ThemeProvider, useTheme } from '@structyl/themes';
|
|
30
|
+
|
|
31
|
+
function ModeToggle() {
|
|
32
|
+
const { mode, setMode, resolvedMode, theme, setTheme, themes } = useTheme();
|
|
33
|
+
|
|
34
|
+
return (
|
|
35
|
+
<div>
|
|
36
|
+
<button onClick={() => setMode(resolvedMode === 'dark' ? 'light' : 'dark')}>
|
|
37
|
+
Toggle ({mode} → {resolvedMode})
|
|
38
|
+
</button>
|
|
39
|
+
|
|
40
|
+
<select value={theme} onChange={(e) => setTheme(e.target.value)}>
|
|
41
|
+
{themes.map((t) => (
|
|
42
|
+
<option key={t} value={t}>{t}</option>
|
|
43
|
+
))}
|
|
44
|
+
</select>
|
|
45
|
+
</div>
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function App() {
|
|
50
|
+
return (
|
|
51
|
+
<ThemeProvider defaultTheme="slate" defaultMode="system">
|
|
52
|
+
<ModeToggle />
|
|
53
|
+
{/* your app */}
|
|
54
|
+
</ThemeProvider>
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Prevent the flash of unstyled content (SSR / Next.js)
|
|
60
|
+
|
|
61
|
+
Render `ThemeScript` in your document `<head>` so the correct theme is applied from `localStorage` before first paint:
|
|
62
|
+
|
|
63
|
+
```tsx
|
|
64
|
+
// app/layout.tsx
|
|
65
|
+
import { ThemeScript } from '@structyl/themes';
|
|
66
|
+
|
|
67
|
+
export default function RootLayout({ children }: { children: React.ReactNode }) {
|
|
68
|
+
return (
|
|
69
|
+
<html lang="en" suppressHydrationWarning>
|
|
70
|
+
<head>
|
|
71
|
+
<ThemeScript defaultTheme="slate" defaultMode="system" />
|
|
72
|
+
</head>
|
|
73
|
+
<body>
|
|
74
|
+
<ThemeProvider defaultTheme="slate" defaultMode="system">
|
|
75
|
+
{children}
|
|
76
|
+
</ThemeProvider>
|
|
77
|
+
</body>
|
|
78
|
+
</html>
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Runtime accent presets
|
|
84
|
+
|
|
85
|
+
Layer a custom accent color on top of the active theme. `useColorPreset` persists the choice and re-applies it across theme and mode changes:
|
|
86
|
+
|
|
87
|
+
```tsx
|
|
88
|
+
import { useColorPreset, createColorPreset } from '@structyl/themes';
|
|
89
|
+
|
|
90
|
+
function AccentPicker() {
|
|
91
|
+
const brand = createColorPreset('brand', 'Brand Blue', '#1a6cf0');
|
|
92
|
+
const { presets, activeId, setPreset, clearPreset } = useColorPreset({
|
|
93
|
+
extraPresets: [brand],
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
return (
|
|
97
|
+
<div>
|
|
98
|
+
{presets.map((p) => (
|
|
99
|
+
<button key={p.id} onClick={() => setPreset(p.id, p.hex)} style={{ background: p.hex }}>
|
|
100
|
+
{p.name}
|
|
101
|
+
</button>
|
|
102
|
+
))}
|
|
103
|
+
{activeId && <button onClick={clearPreset}>Reset</button>}
|
|
104
|
+
</div>
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
The theme tokens are exposed as CSS custom properties on `:root` (e.g. `--color-primary`, `--color-bg`, `--color-fg`), so you can consume them directly in CSS or via Tailwind.
|
|
110
|
+
|
|
111
|
+
## Features
|
|
112
|
+
|
|
113
|
+
- **ThemeProvider** — manages the active theme plus `light` / `dark` / `system` modes, injects token CSS variables onto `document.documentElement`, and persists the selection to `localStorage`.
|
|
114
|
+
- **System mode** — follows `prefers-color-scheme` and reacts live to OS changes.
|
|
115
|
+
- **No-flash SSR** — `ThemeScript` applies the stored theme before paint and sets `color-scheme`, eliminating the dark-mode flash.
|
|
116
|
+
- **CSS-variable tokens** — a broad `ThemeTokens` contract covering surfaces, semantic colors (primary, success, warning, info, destructive), state/shade rgba shades, text, borders, and table tokens.
|
|
117
|
+
- **Built-in themes** — ships `slate`, `zinc`, `rose`, and `structyl`; extend or override via the `themes` prop.
|
|
118
|
+
- **Accent presets** — 10 built-in presets plus `useColorPreset` / `applyColorPreset` for runtime accent switching, with custom presets supported.
|
|
119
|
+
- **SSR-safe & zero runtime deps** — guards all `window` / `document` access; React is the only peer dependency.
|
|
120
|
+
|
|
121
|
+
## API
|
|
122
|
+
|
|
123
|
+
| Export | Kind | Description |
|
|
124
|
+
| --- | --- | --- |
|
|
125
|
+
| `ThemeProvider` | Component | Root provider that applies tokens, tracks mode, and persists state. Props: `defaultTheme`, `defaultMode`, `storageKey`, `enableTransitions`, `themes`, `attribute`. |
|
|
126
|
+
| `useTheme()` | Hook | Returns `{ theme, setTheme, mode, setMode, resolvedMode, themes }`. Must be used inside a `ThemeProvider`. |
|
|
127
|
+
| `ThemeScript` | Component | Inline `<style>` + script for the document `<head>` that prevents the FOUC. |
|
|
128
|
+
| `useColorPreset(options?)` | Hook | Manages a runtime accent preset (`presets`, `activeId`, `activePreset`, `setPreset`, `clearPreset`). |
|
|
129
|
+
| `COLOR_PRESETS` | Const | The 10 built-in accent presets. |
|
|
130
|
+
| `createColorPreset(id, name, hex)` | Function | Build a typed custom `ColorPreset`. |
|
|
131
|
+
| `applyColorPreset(hex)` | Function | Imperatively override the primary-color CSS variables. |
|
|
132
|
+
| `clearColorPreset()` | Function | Remove preset overrides, restoring the theme's colors. |
|
|
133
|
+
| `defaultThemes` | Const | Map of built-in `ThemeConfig`s (`slate`, `zinc`, `rose`, `structyl`). |
|
|
134
|
+
| `staticPalette`, `generalColors` | Const | Raw palette references used to build themes. |
|
|
135
|
+
| `sharedSemanticLight`, `sharedSemanticDark` | Const | Shared semantic token sets for light / dark modes. |
|
|
136
|
+
|
|
137
|
+
**Types:** `Theme`, `ThemeMode`, `ThemeConfig`, `ThemeTokens`, `ColorPreset`, `ColorPresetId`, `UseColorPresetOptions`, `UseColorPresetReturn`, `PaletteColor`, `AlertPaletteColor`, `StructylScale`, `StaticPalette`.
|
|
138
|
+
|
|
139
|
+
### Stylesheets
|
|
140
|
+
|
|
141
|
+
```ts
|
|
142
|
+
import '@structyl/themes/styles.css'; // base token stylesheet
|
|
143
|
+
import '@structyl/themes/themes/slate.css'; // a specific theme's variables
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
## Part of structyl
|
|
147
|
+
|
|
148
|
+
This package is part of [structyl](https://github.com/imirfanul/structyl) — the React UI library with structure. See the full documentation at [structyl.dev](https://structyl.dev).
|
|
149
|
+
|
|
150
|
+
## License
|
|
151
|
+
|
|
152
|
+
[MIT](https://github.com/imirfanul/structyl/blob/main/LICENSE)
|