Skip to content

Commit 11febcd

Browse files
committed
updates and cleanup
1 parent 73ff25b commit 11febcd

File tree

5 files changed

+61
-21
lines changed

5 files changed

+61
-21
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.vscode
2-
dist
2+
dist
3+
node_modules

src/defaults.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { ThemesConfig } from "./types.ts";
2+
3+
export function themes(colors?: any): ThemesConfig {
4+
return {
5+
light: {
6+
"background-primary": colors?.zinc[100],
7+
"background-secondary": colors?.zinc[200],
8+
"content-primary": colors?.zinc[900],
9+
"content-secondary": colors?.zinc[700],
10+
accent: colors?.indigo[600],
11+
},
12+
dark: {
13+
"background-primary": colors?.zinc[900],
14+
"background-secondary": colors?.zinc[800],
15+
"content-primary": colors?.zinc[100],
16+
"content-secondary": colors?.zinc[300],
17+
accent: colors?.indigo[400],
18+
},
19+
};
20+
}
21+
22+
export const prefix = "theme--";

src/index.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,41 @@
11
import plugin from "https://esm.sh/[email protected]/plugin";
2-
import { ThemeConfig } from "https://esm.sh/[email protected]/types/config.d.ts";
2+
3+
import * as defaults from "./defaults.ts";
4+
import { ThemesConfig } from "./types.ts";
35

46
import {
57
getTailwindCssVariables,
68
getThemeTokens,
79
getRgbThemeConfig,
810
} from "./utils.ts";
911

12+
interface PluginOptions {
13+
prefix?: string;
14+
themes?: (colors?: any) => ThemesConfig;
15+
}
16+
1017
export default plugin.withOptions(
11-
({
12-
prefix,
13-
themes,
14-
}: {
15-
prefix?: string;
16-
themes: (
17-
colors?: Partial<ThemeConfig["colors"]>
18-
) => Record<string | number | symbol, never>;
19-
}) =>
18+
(options: PluginOptions) =>
2019
({ addComponents, theme }) => {
2120
const colors = theme("colors");
22-
const rgbThemeConfig = getRgbThemeConfig(themes(colors), prefix);
21+
22+
const rgbThemeConfig = getRgbThemeConfig(
23+
options.themes ? options.themes(colors) : defaults.themes(colors),
24+
options.prefix || defaults.prefix
25+
);
2326

2427
addComponents({
2528
...rgbThemeConfig,
2629
});
2730
},
2831
({ themes }) => {
29-
const tokens = getThemeTokens(themes());
32+
let tokens: string[];
33+
34+
if (themes) {
35+
tokens = getThemeTokens(themes());
36+
} else {
37+
tokens = getThemeTokens(defaults.themes());
38+
}
3039

3140
const cssVariables = getTailwindCssVariables(tokens);
3241

src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface ThemesConfig {
2+
[themeName: string]: Record<string, string>;
3+
}

src/utils.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ThemeConfig } from "https://esm.sh/[email protected]/types/config.d.ts";
22

33
import hexToRgb from "./hex-rgb.ts";
4+
import { ThemesConfig } from "./types.ts";
45

56
export function getThemeTokens(themes?: Partial<ThemeConfig["colors"]>) {
67
const tokens: string[] = [];
@@ -13,12 +14,10 @@ export function getThemeTokens(themes?: Partial<ThemeConfig["colors"]>) {
1314
}
1415
}
1516

16-
console.log(Array.from(new Set(tokens.flat())));
17-
1817
return Array.from(new Set(tokens.flat()));
1918
}
2019

21-
export function getTailwindCssVariables(tokens) {
20+
export function getTailwindCssVariables(tokens: string[]) {
2221
return tokens.reduce((acc, token) => {
2322
return {
2423
...acc,
@@ -27,20 +26,26 @@ export function getTailwindCssVariables(tokens) {
2726
}, {});
2827
}
2928

30-
export function getRgbThemeConfig(config, prefix) {
31-
let rgbThemeConfig = {};
29+
export function getRgbThemeConfig(
30+
config: ThemesConfig,
31+
prefix: string
32+
): ThemesConfig {
33+
const classString = `.`;
34+
const rgbThemeConfig: ThemesConfig = {};
3235

3336
// Loop through user themes to apply theme prefix and convert hex to rgb
3437
for (const [userThemeName, userThemeValues] of Object.entries(config)) {
35-
let updatedThemeConfig = [];
38+
const updatedThemeConfig: string[][] = [];
3639

3740
// Loop through each value of theme to convert hex to rgb
3841
for (const [token, hex] of Object.entries(userThemeValues)) {
39-
updatedThemeConfig.push([`--${token}`, hexToRgb(hex)]);
42+
if (hex) {
43+
updatedThemeConfig.push([`--${token}`, hexToRgb(hex)]);
44+
}
4045
}
4146

4247
const updatedTheme = {
43-
[`.${prefix.concat(userThemeName)}`]: Object.fromEntries(
48+
[classString.concat(prefix, userThemeName)]: Object.fromEntries(
4449
new Map(updatedThemeConfig)
4550
),
4651
};

0 commit comments

Comments
 (0)