✨(theme) add themes management
This is made in order to be able to handle natively multiple themes for the future light/dark themes and also allow consumers to create their own.
This commit is contained in:
@@ -68,6 +68,8 @@ describe("<Button/>", () => {
|
||||
it("uses custom token", async () => {
|
||||
await buildTheme();
|
||||
const tokens = await loadTokens();
|
||||
expect(tokens.components.button["border-radius"]).toBeDefined();
|
||||
expect(
|
||||
tokens.themes.default.components.button["border-radius"],
|
||||
).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -153,4 +153,37 @@ describe("<CunninghamProvider />", () => {
|
||||
};
|
||||
render(<Wrapped />, { wrapper: Wrapper });
|
||||
});
|
||||
|
||||
it("should change theme by updating :root classes", async () => {
|
||||
const Wrapper = (props: PropsWithChildren) => {
|
||||
const [theme, setTheme] = useState("default");
|
||||
return (
|
||||
<CunninghamProvider theme={theme}>
|
||||
<Button onClick={() => setTheme("default")}>Default</Button>
|
||||
<Button onClick={() => setTheme("dark")}>Dark</Button>
|
||||
<Button onClick={() => setTheme("custom")}>Custom</Button>
|
||||
{props.children}
|
||||
</CunninghamProvider>
|
||||
);
|
||||
};
|
||||
render(<Wrapper />);
|
||||
|
||||
expect(document.querySelector(":root")).toHaveClass(
|
||||
"cunningham-theme--default",
|
||||
);
|
||||
|
||||
const user = userEvent.setup();
|
||||
const darkButton = screen.getByRole("button", { name: "Dark" });
|
||||
await user.click(darkButton);
|
||||
|
||||
expect(document.querySelector(":root")).toHaveClass(
|
||||
"cunningham-theme--dark",
|
||||
);
|
||||
const customButton = screen.getByRole("button", { name: "Custom" });
|
||||
await user.click(customButton);
|
||||
|
||||
expect(Array.from(document.querySelector(":root")!.classList)).toEqual([
|
||||
"cunningham-theme--custom",
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,6 +2,7 @@ import React, {
|
||||
createContext,
|
||||
PropsWithChildren,
|
||||
useContext,
|
||||
useEffect,
|
||||
useMemo,
|
||||
} from "react";
|
||||
import * as enUS from ":/locales/en-US.json";
|
||||
@@ -30,10 +31,13 @@ export const useCunningham = () => {
|
||||
interface Props extends PropsWithChildren {
|
||||
customLocales?: Record<string, TranslationSet>;
|
||||
currentLocale?: string;
|
||||
theme?: string;
|
||||
}
|
||||
|
||||
export const DEFAULT_LOCALE = Locales.enUS;
|
||||
export const DEFAULT_THEME = "default";
|
||||
export const SUPPORTED_LOCALES = Object.values(Locales);
|
||||
const THEME_CLASSNAME_PREFIX = "cunningham-theme--";
|
||||
|
||||
const findTranslation = (
|
||||
key: string,
|
||||
@@ -46,6 +50,7 @@ const findTranslation = (
|
||||
export const CunninghamProvider = ({
|
||||
currentLocale = DEFAULT_LOCALE,
|
||||
customLocales,
|
||||
theme = DEFAULT_THEME,
|
||||
children,
|
||||
}: Props) => {
|
||||
const locales: Record<string, TranslationSet> = useMemo(
|
||||
@@ -83,6 +88,16 @@ export const CunninghamProvider = ({
|
||||
[currentLocale, locales],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
const root = document.querySelector(":root")!;
|
||||
root.classList.forEach((className) => {
|
||||
if (className.startsWith(THEME_CLASSNAME_PREFIX)) {
|
||||
root.classList.remove(className);
|
||||
}
|
||||
});
|
||||
root.classList.add(THEME_CLASSNAME_PREFIX + theme);
|
||||
}, [theme]);
|
||||
|
||||
return (
|
||||
<CunninghamContext.Provider value={context}>
|
||||
{children}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import "./index.scss";
|
||||
import { PartialNested } from ":/types";
|
||||
import { PartialExtendableNested, PartialNested } from ":/types";
|
||||
import { tokens } from "./cunningham-tokens";
|
||||
|
||||
export * from "./components/Button";
|
||||
@@ -19,5 +19,8 @@ export * from "./components/Pagination";
|
||||
export * from "./components/Popover";
|
||||
export * from "./components/Provider";
|
||||
|
||||
export type DefaultTokens = PartialNested<typeof tokens>;
|
||||
export const defaultTokens = tokens;
|
||||
export type DefaultTokens = PartialNested<typeof tokens.themes.default>;
|
||||
export const defaultTokens = tokens.themes.default;
|
||||
export type Configuration = {
|
||||
themes: Record<string, PartialExtendableNested<typeof tokens.themes.default>>;
|
||||
};
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
export type PartialNested<T> = {
|
||||
[K in keyof T]?: T extends object ? PartialNested<T[K]> : T[K];
|
||||
};
|
||||
|
||||
export type PartialExtendableNested<T> = {
|
||||
[K in keyof T]?: T[K] extends object ? PartialExtendableNested<T[K]> : T[K];
|
||||
} & Record<PropertyKey, any>;
|
||||
|
||||
Reference in New Issue
Block a user