Skip to content

Commit

Permalink
feat: share inverted state through theme provider
Browse files Browse the repository at this point in the history
  • Loading branch information
abelflopes committed Nov 6, 2023
1 parent d3635af commit 87cb022
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
10 changes: 8 additions & 2 deletions packages/components/all/src/text/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import styles from "./styles/index.module.scss";
import React, { type ReactHTML, useMemo } from "react";
// Utils
import classNames from "classnames";
import { useThemeContext } from "@rck/theme";

export type TextVariation = "small" | "bold" | "link" | "link_hidden";
export type TextVariation = "small" | "bold" | "link" | "link_hidden" | "inverted";

export interface TextProps extends React.HTMLAttributes<HTMLElement> {
margin?: boolean;
Expand All @@ -22,6 +23,8 @@ export const Text = ({
children,
...otherProps
}: Readonly<TextProps>): React.ReactElement => {
const { inverted } = useThemeContext();

const computedVariations = Array.isArray(variation) ? variation : variation && [variation];

const computedClassNames = useMemo(
Expand All @@ -32,7 +35,10 @@ export const Text = ({
{
[`${styles.margin}`]: margin,
},
computedVariations?.map((i) => styles[i]),
computedVariations?.filter((i) => i !== "inverted").map((i) => styles[i]),
{
[`${styles.inverted}`]: inverted && !computedVariations?.includes("inverted"),
},
className,
),
[className, computedVariations, margin, type],
Expand Down
4 changes: 4 additions & 0 deletions packages/components/all/src/text/styles/index.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,7 @@
text-decoration: underline;
}
}

.inverted {
@include define-css-var(text, color, #fff);
}
38 changes: 35 additions & 3 deletions packages/components/theme/src/context.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,39 @@
import React, { useContext } from "react";
import React, { useContext, useMemo } from "react";
import type { Theme } from "./types";
import { defaultTheme } from "./themes/default";

export const ThemeContext = React.createContext<Theme>(defaultTheme);
export interface ThemeContextProps {
inverted: boolean;
theme: Theme;
}

export const useThemeContext = (): Theme => useContext(ThemeContext);
export const themeContextDefaults: ThemeContextProps = {
inverted: false,
theme: defaultTheme,
};

export interface ThemeContextProviderProps {
value?: Partial<ThemeContextProps>;
children: React.ReactNode;
}

export const ThemeContext = React.createContext<ThemeContextProps>(themeContextDefaults);

export const useThemeContext = (): ThemeContextProps => useContext(ThemeContext);

export const ThemeContextProvider = ({
value,
children,
}: Readonly<ThemeContextProviderProps>): React.ReactElement => {
const parentContext = useThemeContext();

const computedValue = useMemo(
() => ({
...parentContext,
...value,
}),
[value, parentContext],
);

return <ThemeContext.Provider value={computedValue}>{children}</ThemeContext.Provider>;
};
9 changes: 6 additions & 3 deletions packages/components/theme/src/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "./styles/index.module.scss";
import React, { useMemo } from "react";
import type { Theme } from "./types";
import { defaultTheme } from "./themes/default";
import { ThemeContext } from "./context";
import { ThemeContextProvider } from "./context";

export interface ThemeProviderProps {
target?: HTMLElement;
Expand All @@ -28,8 +28,11 @@ export const ThemeProvider = ({
}, [theme]);

return (
<ThemeContext.Provider value={theme}>
<ThemeContextProvider
value={{
theme,
}}>
<div style={themeCssVars}>{children}</div>
</ThemeContext.Provider>
</ThemeContextProvider>
);
};

0 comments on commit 87cb022

Please sign in to comment.