Skip to content

Commit

Permalink
feat: resolve the theme context on access
Browse files Browse the repository at this point in the history
  • Loading branch information
juanrgm committed Sep 17, 2024
1 parent 0272aa9 commit 6406d78
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changeset/nine-buckets-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@suid/material": minor
"@suid/system": minor
---

Resolve the theme context on access
4 changes: 2 additions & 2 deletions packages/material/src/styles/ThemeProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Theme } from ".";
import ThemeContext from "@suid/system/ThemeContext";
import { JSXElement } from "solid-js";
import { Accessor, JSXElement } from "solid-js";

function ThemeProvider<T extends string>(props: {
theme: Theme<T>;
theme: Theme<T> | Accessor<Theme<T>>;
children: JSXElement;
}) {
return (
Expand Down
7 changes: 5 additions & 2 deletions packages/system/src/useTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ function useTheme(
Context: typeof ThemeContext = ThemeContext
) {
const theme = useContext(Context);
if (isEmptyObject(theme) && defaultTheme) {
if (typeof theme === "function") {
return new Proxy({}, { get: (_, p) => theme()[p] });
} else if (isEmptyObject(theme) && defaultTheme) {
if (typeof defaultTheme === "function") return defaultTheme();
return defaultTheme;
} else if (!theme) {
throw new Error("Theme is not defined");
}
if (!theme) throw new Error("Theme is not defined");
return theme as Theme;
}

Expand Down

0 comments on commit 6406d78

Please sign in to comment.