Skip to content

Commit 78bf512

Browse files
committed
feat: improve the shades generating algorithm and add test cases
1 parent c640e7a commit 78bf512

22 files changed

Lines changed: 3559 additions & 178 deletions

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ jobs:
2424
- name: Install and Build 🔧
2525
run: |
2626
pnpm i
27+
pnpm test
2728
pnpm run build
2829
for d in examples/* ; do
2930
(cd "$d" && pnpm run build && cp -r dist/ ../../dist/"${d:9}" && cp -r dist/ ../../dist)

README.md

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
- [Override auto-generated shade values](#override-auto-generated-shade-values)
88
- [Customized palette keys](#customized-palette-keys)
99
- [Full configurations](#full-configurations)
10-
- [Reference](#reference)
10+
- [About shades generating algorithm](#about-shades-generating-algorithm)
11+
- [Credits](#credits)
1112

1213
Adds multiple themes support for Tailwind CSS and Windi CSS.
1314

14-
You can just develop your app with one theme and it will work with multiple themes color palettes, all you need is just to specify your default (for shade `500`) color values for your theme pallette. We will generate all shades from `50` to `900` for you, following the built-in shade name convention of the [default color values](https://tailwindcss.com/docs/customizing-colors).
15+
You can just develop your app with one theme and it will work with multiple themes color palettes, all you need is just to specify your default color values for your theme pallette. We will generate all shades from `50` to `900` for you, following the built-in shade name convention of the [default color values](https://tailwindcss.com/docs/customizing-colors).
1516

1617
| Dracula (default theme) | Material |
1718
| --------------------------- | ----------------------------- |
@@ -242,7 +243,7 @@ As the [tailwindcss docs says](https://tailwindcss.com/docs/customizing-colors#g
242243

243244
> Bad news, color is complicated and despite trying dozens of different tools, we’ve yet to find one that does a good job generating these sorts of color palettes. We picked all of Tailwind’s default colors by hand, meticulously balancing them by eye and testing them in real designs to make sure we were happy with them.
244245
245-
You can specify all (or just a part of) the shades of a color if you want, this will overwrite the auto-generated shade value. It will be super useful if you find the auto-generated not satisfying. Below is an example configuration, you can explore more on the [`override-shades`](examples/override-shades) example:
246+
Although our shade generating algorithm is okay in most cases, you may still find the auto-generated shades not satisfying. You can specify all (or just a part of) the shades of a color if you want, this will overwrite the auto-generated shade value. Below is an example configuration, you can explore more on the [`override-shades`](examples/override-shades) example:
246247

247248
```js
248249
themeable({
@@ -301,39 +302,42 @@ themeable({
301302
This is the type definition of this plugin, you can dive into the source code to see more, all the type definitions are well documented for your convenience. If you have any questions, please fell free to open an issue. And new theme contribution is welcome!
302303
303304
```ts
304-
interface ThemeableOptions {
305-
/**
306-
* Support a list of theme definitions, the user should define the colors of the theme follow the contribute of Dracula theme.
307-
* See https://draculatheme.com/contribute#color-palette
308-
* @default []
309-
*/
310-
themes?: Theme[];
311-
/**
312-
* Prefix of the class to enable a theme, for example the container with class `${classPrefix}-dracula` will enable dracula theme in its children elements
313-
* @default `themeable`
314-
*/
315-
classPrefix?: string;
316-
/** The lighten step for auto generated shades smaller than the default `500` color
317-
* For example, if you passed `#50FA7B` as the `green` theme key, and `shadeLightenStep` is 8,
318-
* then we will use this color as the `DEFAULT` and shade `500` to generate all other shades of `green`,
319-
* for shade smaller than `500`, we will add the lightness up `shadeLightenStep` in per 100 gap.
320-
* Color `#50FA7B` converted to HSL is [135, 94, 64], so the shade `400` will be computed to [135, 94, 72]
321-
* @default 8
322-
*/
323-
shadeLightenStep?: number;
324-
/** Similar with `shadeLightenStep` but for shades larger than `500`
325-
* @default 11
326-
*/
327-
shadeDarkenStep?: number;
328-
/** When not specify any theme in HTML, the `defaultTheme` will be used
329-
* @default `dracula`
330-
*/
331-
defaultTheme?: string;
305+
export interface ThemeableOptions {
306+
/**
307+
* Support a list of theme definitions, the user should define the colors of the theme follow the contribute of Dracula theme.
308+
* See https://draculatheme.com/contribute#color-palette
309+
* @default []
310+
*/
311+
themes?: Theme[]
312+
/**
313+
* Prefix of the class to enable a theme, for example the container with class `${classPrefix}-dracula` will enable dracula theme in its children elements
314+
* @default `themeable`
315+
*/
316+
classPrefix?: string
317+
/** When not specify any theme in HTML, the `defaultTheme` will be used
318+
* @default `dracula`
319+
*/
320+
defaultTheme?: string
321+
/**
322+
* This will allow you the change the difference in saturation between each shade of color. By default we use 1.771968374684816 because these are the averages that steps change in tailwind's default colors. Thanks to https://tw-shade-gen.netlify.app/
323+
*/
324+
saturationFactor?: number
325+
/**
326+
* This will allow you the change the difference in lightness between each shade of color. By default we use 7.3903743315508015 because these are the averages that steps change in tailwind's default colors. Thanks to https://tw-shade-gen.netlify.app/
327+
*/
328+
lightFactor?: number
332329
}
333330
```
334331
335-
## Reference
332+
## About shades generating algorithm
336333
337-
1. https://github.com/dracula/tailwind
338-
2. https://github.com/anheric/tailwindshades
339-
3. [Theming Tailwind with CSS Variables](https://www.youtube.com/watch?v=MAtaT8BZEAo)
334+
The shades generating algorithm will find a best shade number (`50`-`900`) for your color, and then generate darker and lighter shades with fixed saturation and lightness step (`saturationFactor` and `lightFactor`).
335+
336+
## Credits
337+
338+
1. Idea of shades generation
339+
1. https://github.com/dracula/tailwind
340+
2. https://github.com/nickgraffis/tailwind-color-generator
341+
3. https://github.com/anheric/tailwindshades
342+
2. Idea of theming
343+
1. [Theming Tailwind with CSS Variables](https://www.youtube.com/watch?v=MAtaT8BZEAo)

babel.config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// https://jestjs.io/docs/getting-started#using-typescript
2+
module.exports = {
3+
presets: [
4+
['@babel/preset-env', { targets: { node: 'current' } }],
5+
'@babel/preset-typescript'
6+
]
7+
}

examples/customized-palette-keys/vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ export default defineConfig({
55
plugins: [
66
WindiCSS()
77
],
8-
base: '/tailwindcss-themeable/customized-palette-keys/'
8+
base: './'
99
})

examples/override-shades/vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ export default defineConfig({
55
plugins: [
66
WindiCSS()
77
],
8-
base: '/tailwindcss-themeable/override-shades/'
8+
base: './'
99
})

examples/tailwind/index.html

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
top-0
2222
flex
2323
justify-between
24-
bg-themeable-selection
24+
bg-themeable-background
25+
border-themeable-selection
26+
shadow-lg shadow-themeable-foreground
2527
px-8
2628
py-4
2729
text-themeable-foreground
@@ -35,14 +37,15 @@
3537
<div class="dropdown block relative">
3638
<button
3739
class="
38-
bg-themeable-cyan
40+
bg-themeable-cyan-500
3941
text-themeable-background
4042
font-semibold
4143
py-2
4244
px-4
4345
rounded
4446
inline-flex
4547
items-center
48+
focus:outline-transparent
4649
"
4750
>
4851
<span class="mr-1">Theme</span>
@@ -110,8 +113,17 @@
110113
</ul>
111114
</nav>
112115

113-
<div class="m-8 space-y-4 mb-20 text-themeable-foreground">
114-
<figure class="md:flex rounded-xl p-8 md:p-0 bg-themeable-selection">
116+
<div class="m-16 space-y-16 mb-20 text-themeable-foreground">
117+
<figure
118+
class="
119+
md:flex
120+
rounded-xl
121+
p-8
122+
md:p-0
123+
bg-themeable-selection
124+
shadow-lg shadow-themeable-foreground
125+
"
126+
>
115127
<img
116128
class="
117129
w-32
@@ -129,12 +141,14 @@
129141
<div class="pt-6 md:p-8 text-center md:text-left space-y-4">
130142
<blockquote>
131143
<p class="text-lg font-semibold">
132-
Multiple themes support for tailwindcss. Developing with one theme
133-
and it works with multiple themes, all you need is just to define
134-
your default (for shade <code>500</code>) color values for your
135-
theme. We will generate all shades from <code>50</code> to
136-
<code>900</code> for you, following the built-in shade name
137-
convention of the
144+
Adds multiple themes support for Tailwind CSS and Windi CSS.
145+
</p>
146+
<p class="text-lg font-semibold">
147+
You can just develop your app with one theme and it will work with
148+
multiple themes color palettes, all you need is just to specify
149+
your default color values for your theme pallette. We will
150+
generate all shades from <code>50</code> to <code>900</code> for
151+
you, following the built-in shade name convention of the
138152
<a
139153
href="https://tailwindcss.com/docs/customizing-colors"
140154
class="text-link"
@@ -143,8 +157,8 @@
143157
</p>
144158
</blockquote>
145159
<figcaption class="font-medium">
146-
<div class="text-themeable-cyan-600">Yiming Li</div>
147-
<div class="text-themeable-foreground-600">GitHub ID: upupming</div>
160+
<div class="text-themeable-cyan-50">Author: Yiming Li</div>
161+
<div class="text-themeable-foreground">GitHub ID: upupming</div>
148162
</figcaption>
149163
</div>
150164
</figure>
@@ -153,8 +167,8 @@
153167
relative
154168
z-10
155169
rounded-xl
156-
shadow-lg
157-
divide-y-2 divide-themeable-orange
170+
shadow-lg shadow-themeable-foreground
171+
divide-y-2 divide-themeable-orange-500
158172
flex flex-col
159173
"
160174
>
@@ -163,12 +177,13 @@
163177
class="
164178
flex-none
165179
w-48
166-
bg-themeable-orange-200
180+
border-themeable-orange-500 border-t-2 border-l-2
181+
bg-themeable-orange-50
182+
text-themeable-orange-500
167183
rounded-tl-xl
168184
text-lg
169185
leading-6
170186
font-semibold
171-
text-themeable-orange
172187
px-4
173188
py-3
174189
p-8
@@ -193,10 +208,7 @@
193208
</dd>
194209
</div>
195210
<div class="space-y-1">
196-
<dd
197-
class="text-themeable-foreground-600 text-sm"
198-
style="opacity: 1"
199-
>
211+
<dd class="text-themeable-foreground text-sm" style="opacity: 1">
200212
We follow the
201213
<a href="https://draculatheme.com/contribute" class="text-link"
202214
>Dracula color palette definition</a
@@ -220,12 +232,13 @@
220232
class="
221233
flex-none
222234
w-48
223-
bg-themeable-orange-200
235+
bg-themeable-orange-50
236+
text-themeable-orange-500
237+
border-themeable-orange-500 border-b-2 border-l-2
224238
rounded-bl-xl
225239
text-lg
226240
leading-6
227241
font-semibold
228-
text-themeable-orange
229242
px-4
230243
py-3
231244
p-8
@@ -240,10 +253,20 @@
240253
bg-themeable-selection
241254
rounded-br-xl
242255
overflow-hidden
243-
text-themeable-foreground-600
256+
text-themeable-foreground
244257
"
245258
>
246-
<dl class="px-4 py-6 p-6 space-y-6">
259+
<dl
260+
class="
261+
px-4
262+
py-6
263+
p-6
264+
grid
265+
space-x-2 space-y-2
266+
lg:grid-cols-3
267+
grid-cols-1
268+
"
269+
>
247270
<div class="space-y-2">
248271
<dt class="font-mono text-xs">
249272
bg-themeable-background-{50-900}

examples/tailwind/style.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
}
1212

1313
.text-link {
14-
@apply text-themeable-cyan-700
15-
hover:text-themeable-cyan-800
14+
@apply text-themeable-orange-500
15+
hover:text-themeable-orange-600
1616
transition;
1717
}
1818

examples/tailwind/vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export default {
2-
base: '/tailwindcss-themeable/tailwind/'
2+
base: './'
33
}

0 commit comments

Comments
 (0)