cyhip-dynamic-themes 0.2.0 → 0.2.2
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/README.md +70 -27
- package/dist/package.json +1 -1
- package/dist/src/theme-provider.js +1 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -7,9 +7,9 @@ Implement dynamic color themes in your React apps with Tailwind CSS in a simple
|
|
|
7
7
|
|
|
8
8
|
## Examples
|
|
9
9
|
|
|
10
|
-
-
|
|
10
|
+
- **Vite + React-ts: [cyhip-dynamic-themes](https://cyhip-dynamic-themes.vercel.app/)**
|
|
11
11
|
|
|
12
|
-
-
|
|
12
|
+
- **Nextjs + React-ts: [cyhip-dynamic-themes-nextjs-example](https://cyhip-dynamic-themes-nextjs-example.vercel.app/)**
|
|
13
13
|
|
|
14
14
|
## Features
|
|
15
15
|
|
|
@@ -22,10 +22,10 @@ Inspired by the excellent [article](https://evilmartians.com/chronicles/better-d
|
|
|
22
22
|
|
|
23
23
|
`cyhip-dynamic-themes` simplifies theme setup with a unique approach:
|
|
24
24
|
|
|
25
|
-
-
|
|
26
|
-
-
|
|
25
|
+
- **Single Hue Input**: Define just the **Hue** value, and the package automatically generates all color variants across the spectrum.
|
|
26
|
+
- **Automatic Color Variants**: Unlike traditional methods, there’s no need to set up each shade manually—simply select a hue, and the package takes care of the rest.
|
|
27
27
|
|
|
28
|
-
-
|
|
28
|
+
- **Custom Hook for Dynamic Theme Switching**: Allow your users to switch themes dynamically with the `useColorTheme` hook.
|
|
29
29
|
|
|
30
30
|
## Installation
|
|
31
31
|
|
|
@@ -55,7 +55,7 @@ This command generates the following files in the `/themes/` folder:
|
|
|
55
55
|
|
|
56
56
|
```bash
|
|
57
57
|
/themes/
|
|
58
|
-
├──
|
|
58
|
+
├── theme.config.ts # To set your available hue-based colors.
|
|
59
59
|
├── root.css # Main CSS file for styling.
|
|
60
60
|
├── theme-colors.ts # Includes color definitions for Tailwind.
|
|
61
61
|
└── theme-switcher.tsx # Example component for theme switching.
|
|
@@ -67,12 +67,12 @@ To enable dynamic colors and dark mode, modify your `tailwind.config.ts` as foll
|
|
|
67
67
|
|
|
68
68
|
```ts
|
|
69
69
|
// tailwind.config.ts
|
|
70
|
-
import type { Config } from
|
|
71
|
-
import { themeColors } from
|
|
70
|
+
import type { Config } from 'tailwindcss';
|
|
71
|
+
import { themeColors } from './src/themes/theme-colors';
|
|
72
72
|
|
|
73
73
|
export default {
|
|
74
|
-
content: [
|
|
75
|
-
darkMode:
|
|
74
|
+
content: ['./index.html', './src/**/*.{js,ts,jsx,tsx}'],
|
|
75
|
+
darkMode: 'class',
|
|
76
76
|
theme: {
|
|
77
77
|
extend: {
|
|
78
78
|
colors: themeColors,
|
|
@@ -90,27 +90,55 @@ To apply CSS styles linked to the defined themes, add `/themes/root.css` to your
|
|
|
90
90
|
|
|
91
91
|
```tsx
|
|
92
92
|
// src/main.tsx
|
|
93
|
-
import { StrictMode } from
|
|
94
|
-
import { createRoot } from
|
|
95
|
-
import App from
|
|
93
|
+
import { StrictMode } from 'react';
|
|
94
|
+
import { createRoot } from 'react-dom/client';
|
|
95
|
+
import App from './App.tsx';
|
|
96
96
|
|
|
97
97
|
// Import CSS
|
|
98
|
-
import
|
|
98
|
+
import './themes/root.css';
|
|
99
99
|
|
|
100
|
-
createRoot(document.getElementById(
|
|
100
|
+
createRoot(document.getElementById('root')!).render(
|
|
101
101
|
<StrictMode>
|
|
102
102
|
<App />
|
|
103
103
|
</StrictMode>
|
|
104
104
|
);
|
|
105
105
|
```
|
|
106
106
|
|
|
107
|
+
### Theme Provider
|
|
108
|
+
|
|
109
|
+
Use the `ThemeProvider` to initialize a default theme.
|
|
110
|
+
|
|
111
|
+
```tsx
|
|
112
|
+
// src/main.tsx
|
|
113
|
+
import { ThemeConfig, ThemeProvider } from 'cyhip-dynamic-themes';
|
|
114
|
+
import { chromaData, hueScheme } from './themes/theme.config.ts';
|
|
115
|
+
|
|
116
|
+
import './index.css';
|
|
117
|
+
|
|
118
|
+
createRoot(document.getElementById('root')!).render(
|
|
119
|
+
<StrictMode>
|
|
120
|
+
<ThemeProvider
|
|
121
|
+
themeConfig={
|
|
122
|
+
{
|
|
123
|
+
hue: hueScheme.default,
|
|
124
|
+
mode: 'light',
|
|
125
|
+
chromaData: chromaData,
|
|
126
|
+
} as ThemeConfig
|
|
127
|
+
}
|
|
128
|
+
>
|
|
129
|
+
<App />
|
|
130
|
+
</ThemeProvider>
|
|
131
|
+
</StrictMode>
|
|
132
|
+
);
|
|
133
|
+
```
|
|
134
|
+
|
|
107
135
|
### Switching Themes Dynamically
|
|
108
136
|
|
|
109
137
|
Switching the main color palette can be done using the `ThemeSwitcher` component. Here's a basic example to illustrate its use:
|
|
110
138
|
|
|
111
139
|
```tsx
|
|
112
140
|
// App.tsx
|
|
113
|
-
import { ThemeSwitcher } from
|
|
141
|
+
import { ThemeSwitcher } from './themes/theme-switcher';
|
|
114
142
|
function App() {
|
|
115
143
|
return (
|
|
116
144
|
<>
|
|
@@ -151,12 +179,12 @@ You can add or modify hue palettes by visiting [OKLCH Color Preview](https://okl
|
|
|
151
179
|
*/
|
|
152
180
|
|
|
153
181
|
const hueScheme: Record<string, string> = {
|
|
154
|
-
white:
|
|
155
|
-
blue:
|
|
156
|
-
green:
|
|
157
|
-
orange:
|
|
158
|
-
pink:
|
|
159
|
-
purple:
|
|
182
|
+
white: '-1',
|
|
183
|
+
blue: '250',
|
|
184
|
+
green: '150',
|
|
185
|
+
orange: '35',
|
|
186
|
+
pink: '0',
|
|
187
|
+
purple: '316',
|
|
160
188
|
};
|
|
161
189
|
|
|
162
190
|
export { hueScheme };
|
|
@@ -164,22 +192,37 @@ export { hueScheme };
|
|
|
164
192
|
|
|
165
193
|
## API
|
|
166
194
|
|
|
167
|
-
### `
|
|
195
|
+
### `Type ThemeConfig`
|
|
168
196
|
|
|
169
|
-
|
|
197
|
+
The `ThemeConfig` type represents the configuration settings for an application's theme, specifically controlling color and mode settings. This type provides key properties for determining color hues, dark or light mode, and chromatic adjustments based on a data record.
|
|
198
|
+
|
|
199
|
+
- **Properties:**
|
|
200
|
+
|
|
201
|
+
- `hue`: `number`
|
|
202
|
+
It determines the primary color base; if set to -1, the theme uses a white color based scheme.
|
|
203
|
+
|
|
204
|
+
- `mode`: `'light' | 'dark'`
|
|
205
|
+
Defines whether the theme is in "light" or "dark" mode. Acceptable
|
|
170
206
|
|
|
171
|
-
-
|
|
207
|
+
- `chromaData`: `Record<number,number>`
|
|
208
|
+
A record that maps specific numeric values to chroma levels. This data allows for dynamic chromatic adjustments, enabling fine-tuning of the theme's color saturation or intensity.
|
|
172
209
|
|
|
173
|
-
### `
|
|
210
|
+
### `useColorTheme(theme: ThemeConfig)`
|
|
211
|
+
|
|
212
|
+
A custom hook that manages the application of color themes based on the provided HUE value and dark mode setting.
|
|
213
|
+
|
|
214
|
+
### `getThemeProperties(hue: number, darkMode: boolean, chromaData: Record<number,number>)`
|
|
174
215
|
|
|
175
216
|
Defines CSS class and style properties based on the provided HUE value and dark mode setting.
|
|
176
217
|
|
|
177
218
|
- **Parameters:**
|
|
178
219
|
|
|
179
|
-
- `hue
|
|
220
|
+
- `hue`
|
|
180
221
|
|
|
181
222
|
- `darkMode`: A boolean indicating if dark mode is active.
|
|
182
223
|
|
|
224
|
+
- `chromaData`
|
|
225
|
+
|
|
183
226
|
- **Returns:**
|
|
184
227
|
|
|
185
228
|
- An object containing:
|
package/dist/package.json
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
+
'use client'; // enable on Nextjs
|
|
1
2
|
import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
// 'use client'; // enable on Nextjs
|
|
3
3
|
import { updateTheme, useTheme } from './theme-hook';
|
|
4
4
|
import { useEffect, useState } from 'react';
|
|
5
5
|
var ThemeProvider = function (_a) {
|
|
6
6
|
var children = _a.children, themeConfig = _a.themeConfig;
|
|
7
|
-
/**@TODO Get this values from user cookie */
|
|
8
7
|
var theme = useTheme().theme;
|
|
9
8
|
var _b = useState(false), isInitialized = _b[0], setIsInitialized = _b[1];
|
|
10
9
|
useEffect(function () {
|