Skip to content

Commit 0ef9db6

Browse files
committed
Expose useTheme
1 parent d0d47ac commit 0ef9db6

File tree

5 files changed

+54
-2
lines changed

5 files changed

+54
-2
lines changed

src/bin/css_to_ts/colorOptions.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,14 @@ export function generateGetColorOptionsTsCode(colorOptions: ColorOption[]) {
432432
});
433433

434434
return [
435-
`export function getColorOptions(isDark: boolean) {`,
435+
`export function getColorOptions(`,
436+
` params: {`,
437+
` isDark: boolean;`,
438+
` }`,
439+
`) {`,
440+
``,
441+
` const { isDark } = params;`,
442+
``,
436443
` return {`,
437444
multiReplace({
438445
"input": JSON.stringify(obj, null, 2),

src/lib/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
export { $colorScheme, useIsDark } from "./useColorScheme";
2+
export { useTheme } from "./useTheme";
3+
export type { Theme } from "./useTheme";
24
export type { ColorScheme } from "./useColorScheme";
35
export * from "./start";

src/lib/useTheme/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./useTheme";

src/lib/useTheme/useTheme.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { useMemo } from "react";
2+
import { getColorOptions } from "./generated/getColorOptions";
3+
import type { ColorOptions } from "./generated/getColorOptions";
4+
import { getColorDecisions } from "./generated/getColorDecisions";
5+
import type { ColorDecisions } from "./generated/getColorDecisions";
6+
import { useIsDark } from "../useColorScheme";
7+
8+
export type Theme = {
9+
isDark: boolean;
10+
colors: {
11+
decisions: ColorDecisions;
12+
options: ColorOptions;
13+
};
14+
};
15+
16+
export function useTheme(): Theme {
17+
const { isDark } = useIsDark();
18+
19+
const colorOptions = useMemo(() => getColorOptions({ isDark }), [isDark]);
20+
21+
const colorDecisions = useMemo(() => getColorDecisions({ colorOptions }), [colorOptions]);
22+
23+
const theme = useMemo(
24+
(): Theme => ({
25+
isDark,
26+
colors: {
27+
"decisions": colorDecisions,
28+
"options": colorOptions
29+
}
30+
}),
31+
[isDark]
32+
);
33+
34+
return theme;
35+
}

src/test/bin/colorOptions/generateGetColorOptionsTsCode.test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,14 @@ const input: ColorOption[] = [
9393
];
9494

9595
const expected = `
96-
export function getColorOptions(isDark: boolean) {
96+
export function getColorOptions(
97+
params: {
98+
isDark: boolean;
99+
}
100+
) {
101+
102+
const { isDark } = params;
103+
97104
return {
98105
"name1Name2": {
99106
"_111": {

0 commit comments

Comments
 (0)