Skip to content

Commit 041656e

Browse files
v1.4.0-alpha.2
1 parent 947deb6 commit 041656e

File tree

8 files changed

+85
-46
lines changed

8 files changed

+85
-46
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/app export-ignore

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ tempCodeRunnerFile.*
1313
.release-it.json
1414
.github
1515
.vscode
16+
.gitattributes

rollup.config.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ export default [
2525
{
2626
file: packageJson.main,
2727
format: "cjs",
28-
sourcemap: true,
28+
// sourcemap: true,
2929
},
3030
{
3131
file: packageJson.module,
3232
format: "esm",
33-
sourcemap: true,
33+
// sourcemap: true,
3434
},
3535

3636
],
@@ -61,4 +61,12 @@ export default [
6161
plugins: [dts.default()],
6262
external: [/\.css$/],
6363
},
64-
];
64+
];
65+
66+
// export default {
67+
// input: 'src/index.js',
68+
// output: 'dist/index.js',
69+
// format: 'esm',
70+
// exports: 'named', /** Disable warning for default imports */
71+
// sourcemap: true,
72+
// };

src/components/SideBarColors.tsx

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,19 @@
1-
import { getColors, ls, resetTheme, setColorsProperties } from "../lib/utils";
2-
import React, { useEffect, useState } from "react";
1+
import { getColors, ls, print } from "../lib/utils";
2+
import { useEffect, useState } from "react";
33
import { Item } from "./item";
4-
import { ThemeWithHSLColor } from "../lib/theme";
54
import { useTheme } from "next-themes";
65
import { useDebounceCallback } from "../hooks/useDebounceCallback";
7-
import z from "zod";
86
import { LOCAL_STORAGE_KEY } from "../lib/consts";
97
import { useEmittor } from "emittor";
108
import { themeEmittor } from "../lib/emittors";
11-
12-
function print(...props: any) {
13-
if (
14-
typeof window !== "undefined" &&
15-
(window as any).shadcnThemeEditorDebugMode
16-
) {
17-
console.log(...props);
18-
}
19-
}
20-
21-
const ZodTheme = z.array(
22-
z.object({
23-
title: z.string(),
24-
variable: z.string(),
25-
color: z.object({
26-
h: z.number(),
27-
s: z.number(),
28-
l: z.number(),
29-
}),
30-
})
31-
);
9+
import { setSavedTheme } from "../lib/set-saved-theme";
10+
import { useIsMount } from "../hooks/useIsMount";
3211

3312
function SideBarColors() {
34-
3513
const { resolvedTheme } = useTheme();
3614
const [currentTheme, setCurrentTheme] = useState<string | undefined>();
15+
const isMount = useIsMount();
16+
3717
useEffect(() => {
3818
setCurrentTheme(resolvedTheme);
3919
}, [resolvedTheme]);
@@ -45,27 +25,21 @@ function SideBarColors() {
4525
}, 2000);
4626

4727
useEffect(() => {
48-
// resetTheme();
49-
print("reading theme", LOCAL_STORAGE_KEY + ":" + currentTheme);
50-
let theme = ls.getLocalStorageItem<ThemeWithHSLColor[]>(
51-
LOCAL_STORAGE_KEY + ":" + currentTheme
52-
);
53-
if (theme) {
28+
let isSavedThemeApplied = false;
29+
if (typeof colors == "undefined" || isMount) {
30+
// If colors are not defined (i.e., they haven't been set by other functions yet, meaning it's the first time),
31+
// or if this is due to a re-render caused by dependency changes (e.g., when currentTheme is updated).
5432
try {
55-
const isValid = ZodTheme.parse(theme);
56-
print("theme is valid and appling", isValid);
57-
print("applied theme", theme);
58-
themeEmittor.applyTheme(theme);
33+
isSavedThemeApplied = setSavedTheme(currentTheme);
5934
return;
60-
} catch (error) {
61-
print("invalid theme found in localStorage");
62-
// localStorage.removeItem(LOCAL_STORAGE_KEY+":"+currentTheme); //* remove key
63-
}
35+
} catch (error) {}
36+
}
37+
38+
if (typeof colors == "undefined") {
39+
themeEmittor.setDefaultTheme();
6440
}
65-
print("theme not found in localStorage");
66-
print("Now theme: ", theme);
67-
themeEmittor.setDefaultTheme();
6841
}, [currentTheme]);
42+
6943
return (
7044
<>
7145
{colors?.map((color) => (

src/components/icons.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,13 @@ const ColorPalette = ({size, ...props}: SVGProps<SVGSVGElement> & { size: number
115115
<path d="M416 352c-12.6-.84-21-4-28-12-14-16-14-36 5.49-52.48l32.82-29.14c50.27-44.41 50.27-117.21 0-161.63C389.26 64.14 339.54 48 287.86 48c-60.34 0-123.39 22-172 65.11-90.46 80-90.46 210.92 0 290.87 45 39.76 105.63 59.59 165.64 60h1.84c60 0 119.07-19.5 161.2-56.77C464 390 464 385 444.62 355.56 440 348 431 353 416 352zM112 208a32 32 0 1 1 32 32 32 32 0 0 1-32-32zm40 135a32 32 0 1 1 32-32 32 32 0 0 1-32 32zm40-199a32 32 0 1 1 32 32 32 32 0 0 1-32-32zm64 271a48 48 0 1 1 48-48 48 48 0 0 1-48 48zm72-239a32 32 0 1 1 32-32 32 32 0 0 1-32 32z" />
116116
</svg>
117117
);
118+
export const Icons = {
119+
ResetIcon,
120+
CopyIcon,
121+
Dices,
122+
UnLock,
123+
Lock,
124+
X,
125+
ColorPalette,
126+
}
118127
export default ColorPalette;

src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
import "./tailwind.css";
22
import ShadcnThemeEditor from "./components/editor-open";
3+
import { setSavedTheme } from "./lib/set-saved-theme";
4+
export { Icons } from "./components/icons";
5+
6+
export { setSavedTheme };
37
export default ShadcnThemeEditor;

src/lib/set-saved-theme.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { LOCAL_STORAGE_KEY } from "./consts";
2+
import { themeEmittor } from "./emittors";
3+
import { type ThemeWithHSLColor } from "./theme";
4+
import { ls, ZodTheme } from "./utils";
5+
6+
export function setSavedTheme(theme: string | undefined){
7+
let lsTheme = ls.getLocalStorageItem<ThemeWithHSLColor[]>(
8+
LOCAL_STORAGE_KEY + ":" + theme
9+
);
10+
if (lsTheme) {
11+
try {
12+
const isValid = ZodTheme.parse(lsTheme);
13+
themeEmittor.applyTheme(lsTheme);
14+
return true;
15+
} catch (error) {
16+
// localStorage.removeItem(LOCAL_STORAGE_KEY+":"+currentTheme); //* remove key
17+
}
18+
}
19+
return false
20+
}

src/lib/utils.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
themeColors,
66
} from "./theme";
77
import { LOCAL_STORAGE_KEY } from "./consts";
8+
import { z } from "zod";
89

910
export function cn(...inputs: ClassValue[]) {
1011
return clsx(inputs);
@@ -124,3 +125,24 @@ export const ls = {
124125
export function saveTheme(key: string | undefined, theme: ThemeWithHSLColor[]){
125126
ls.setLocalStorage(LOCAL_STORAGE_KEY + ":" + key, theme);
126127
}
128+
129+
export function print(...props: any) {
130+
if (
131+
typeof window !== "undefined" &&
132+
(window as any).shadcnThemeEditorDebugMode
133+
) {
134+
console.log(...props);
135+
}
136+
}
137+
138+
export const ZodTheme = z.array(
139+
z.object({
140+
title: z.string(),
141+
variable: z.string(),
142+
color: z.object({
143+
h: z.number(),
144+
s: z.number(),
145+
l: z.number(),
146+
}),
147+
})
148+
);

0 commit comments

Comments
 (0)