Skip to content

Commit 87cb022

Browse files
committed
feat: share inverted state through theme provider
1 parent d3635af commit 87cb022

File tree

4 files changed

+53
-8
lines changed

4 files changed

+53
-8
lines changed

packages/components/all/src/text/index.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import styles from "./styles/index.module.scss";
33
import React, { type ReactHTML, useMemo } from "react";
44
// Utils
55
import classNames from "classnames";
6+
import { useThemeContext } from "@rck/theme";
67

7-
export type TextVariation = "small" | "bold" | "link" | "link_hidden";
8+
export type TextVariation = "small" | "bold" | "link" | "link_hidden" | "inverted";
89

910
export interface TextProps extends React.HTMLAttributes<HTMLElement> {
1011
margin?: boolean;
@@ -22,6 +23,8 @@ export const Text = ({
2223
children,
2324
...otherProps
2425
}: Readonly<TextProps>): React.ReactElement => {
26+
const { inverted } = useThemeContext();
27+
2528
const computedVariations = Array.isArray(variation) ? variation : variation && [variation];
2629

2730
const computedClassNames = useMemo(
@@ -32,7 +35,10 @@ export const Text = ({
3235
{
3336
[`${styles.margin}`]: margin,
3437
},
35-
computedVariations?.map((i) => styles[i]),
38+
computedVariations?.filter((i) => i !== "inverted").map((i) => styles[i]),
39+
{
40+
[`${styles.inverted}`]: inverted && !computedVariations?.includes("inverted"),
41+
},
3642
className,
3743
),
3844
[className, computedVariations, margin, type],

packages/components/all/src/text/styles/index.module.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,7 @@
8989
text-decoration: underline;
9090
}
9191
}
92+
93+
.inverted {
94+
@include define-css-var(text, color, #fff);
95+
}
Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,39 @@
1-
import React, { useContext } from "react";
1+
import React, { useContext, useMemo } from "react";
22
import type { Theme } from "./types";
33
import { defaultTheme } from "./themes/default";
44

5-
export const ThemeContext = React.createContext<Theme>(defaultTheme);
5+
export interface ThemeContextProps {
6+
inverted: boolean;
7+
theme: Theme;
8+
}
69

7-
export const useThemeContext = (): Theme => useContext(ThemeContext);
10+
export const themeContextDefaults: ThemeContextProps = {
11+
inverted: false,
12+
theme: defaultTheme,
13+
};
14+
15+
export interface ThemeContextProviderProps {
16+
value?: Partial<ThemeContextProps>;
17+
children: React.ReactNode;
18+
}
19+
20+
export const ThemeContext = React.createContext<ThemeContextProps>(themeContextDefaults);
21+
22+
export const useThemeContext = (): ThemeContextProps => useContext(ThemeContext);
23+
24+
export const ThemeContextProvider = ({
25+
value,
26+
children,
27+
}: Readonly<ThemeContextProviderProps>): React.ReactElement => {
28+
const parentContext = useThemeContext();
29+
30+
const computedValue = useMemo(
31+
() => ({
32+
...parentContext,
33+
...value,
34+
}),
35+
[value, parentContext],
36+
);
37+
38+
return <ThemeContext.Provider value={computedValue}>{children}</ThemeContext.Provider>;
39+
};

packages/components/theme/src/provider.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import "./styles/index.module.scss";
22
import React, { useMemo } from "react";
33
import type { Theme } from "./types";
44
import { defaultTheme } from "./themes/default";
5-
import { ThemeContext } from "./context";
5+
import { ThemeContextProvider } from "./context";
66

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

3030
return (
31-
<ThemeContext.Provider value={theme}>
31+
<ThemeContextProvider
32+
value={{
33+
theme,
34+
}}>
3235
<div style={themeCssVars}>{children}</div>
33-
</ThemeContext.Provider>
36+
</ThemeContextProvider>
3437
);
3538
};

0 commit comments

Comments
 (0)