Skip to content

Commit cd8083c

Browse files
committed
FEAT: improve useTheming hook
1 parent e323591 commit cd8083c

File tree

8 files changed

+48
-34
lines changed

8 files changed

+48
-34
lines changed

.npmrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# don't install with ^ in version string when using `npm install`
2+
save-exact=true

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 0.6.0
4+
5+
- change `useTheming` hook, now gives theme values based on a supplied schema
6+
37
## 0.5.11
48

59
- properly cascade colors to TextInput and clean-up options to allow custom components to do the same

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "style-composer",
3-
"version": "0.5.10",
3+
"version": "0.6.0-alpha1",
44
"main": "./cjs/index.js",
55
"types": "./esm/index.d.ts",
66
"module": "./esm/index.js",
@@ -23,16 +23,16 @@
2323
"build:watch": "npm-run-all -p \"build:esm -- -w\" \"build:cjs -- -w\""
2424
},
2525
"devDependencies": {
26-
"@typescript-eslint/eslint-plugin": "^4.5.0",
27-
"@typescript-eslint/parser": "^4.5.0",
28-
"eslint": "^7.11.0",
29-
"npm-run-all": "^4.1.5",
30-
"rimraf": "^3.0.2",
31-
"typescript": "^4.0.3",
32-
"@types/node": "^14.14.0",
33-
"@types/react": "^16.9.56",
34-
"@types/react-dom": "^16.9.8",
35-
"@types/react-native": "^0.63.33"
26+
"@typescript-eslint/eslint-plugin": "4.5.0",
27+
"@typescript-eslint/parser": "4.5.0",
28+
"eslint": "7.11.0",
29+
"npm-run-all": "4.1.5",
30+
"rimraf": "3.0.2",
31+
"typescript": "4.0.3",
32+
"@types/node": "14.14.0",
33+
"@types/react": "16.9.56",
34+
"@types/react-dom": "16.9.8",
35+
"@types/react-native": "0.63.2"
3636
},
3737
"peerDependencies": {
3838
"react": "^16.8.0",

src/StyleHooks.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ import {
3737
isFontLoaded,
3838
isStyleComposerFont,
3939
removeFontLoadListener,
40-
} from "./font/FontFamily";
41-
import child, {ChildQuery} from "./selector/ChildSelector";
42-
import {setupFontPreProcessor} from "./font/FontPreProcessor";
43-
import StyleEnvironment from "./StyleEnvironment";
44-
import {fixStylePropTypes} from "./StyleSheetPropTypeFixer";
40+
} from "./font/FontFamily";
41+
import child, {ChildQuery} from "./selector/ChildSelector";
42+
import {setupFontPreProcessor} from "./font/FontPreProcessor";
43+
import StyleEnvironment from "./StyleEnvironment";
44+
import {fixStylePropTypes} from "./StyleSheetPropTypeFixer";
45+
import {ThemingContext} from "./theme/Theming";
4546

4647
export const useForceUpdate = (): [number, () => void] => {
4748
const [state, setState] = useState(0);
@@ -233,7 +234,7 @@ export const useStylingInternals = (classes: Classes | undefined, session: Styli
233234
}, [classes]);
234235

235236
const [forceUpdateKey, forceUpdate] = useSelectorsEffect(uidRef.current, classArray, session, classId);
236-
const theming = useTheming();
237+
const theming = useContext(ThemingContext);
237238

238239
useEffect(() => {
239240
if (hasDynamicUnit) {

src/theme/Theming.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ export const finishThemeSession = (): boolean => {
2121
return themedSession.called;
2222
};
2323

24-
let currentTheme: any = {};
25-
2624
export type Theme = Record<string, string | number>;
2725

28-
interface ThemeContextState {
29-
theme: Theme;
26+
export interface ThemingContextState<T extends Theme> {
27+
theme: T;
3028
key: string | null;
3129
}
3230

33-
const ThemeContext = React.createContext<ThemeContextState>({
31+
let currentTheme: any = {};
32+
33+
export const ThemingContext = React.createContext<ThemingContextState<any>>({
3434
theme: currentTheme,
3535
key : null,
3636
});
@@ -43,11 +43,12 @@ export interface ThemeProviderProps<T extends Theme> {
4343

4444
export function ThemeProvider<T extends Theme>(props: ThemeProviderProps<T>): JSX.Element {
4545
const {children, value} = props;
46-
const currentValue = useMemo<ThemeContextState>(() => ({
46+
const currentValue = useMemo<ThemingContextState<T>>(() => ({
4747
theme: value as any,
4848
key : Date.now().toString(),
4949
}), [value]);
50-
return React.createElement(ThemeContext.Provider, {value: currentValue}, children);
50+
currentTheme = currentValue.theme;
51+
return React.createElement(ThemingContext.Provider, {value: currentValue}, children);
5152
}
5253

5354
export interface ThemeProperty<T = any> {
@@ -58,12 +59,6 @@ export interface ThemeProperty<T = any> {
5859
toString: () => string;
5960
}
6061

61-
export const useTheming = (): ThemeContextState => {
62-
const state = useContext(ThemeContext);
63-
currentTheme = state.theme;
64-
return state;
65-
};
66-
6762
export type ThemeFor<T extends ThemeSchema<any>> = T extends ThemeSchema<infer P> ? Partial<P> : never;
6863

6964
export type ThemeSchema<T extends Theme> = {

src/theme/ThemingHooks.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {useContext, useMemo} from "react";
2+
import {Theme, ThemeSchema, ThemingContext, ThemingContextState} from "./Theming";
3+
4+
export const useTheming = <T extends Theme>(schema: ThemeSchema<T>): ThemingContextState<T> => {
5+
const themingContext = useContext(ThemingContext);
6+
7+
const defaultValues = useMemo(() => Object.keys(schema).reduce((defaultValues, key) => Object.assign(defaultValues, {[key]: schema[key].defaultValue}), {}), []);
8+
9+
const theme = useMemo(() => Object.assign({}, defaultValues, themingContext.theme), []);
10+
11+
return {theme, key: themingContext.key};
12+
};

src/theme/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ export {
77
ThemeFor,
88
ThemeProvider,
99
ThemeProviderProps,
10-
useTheming,
1110
} from "./Theming";
11+
export * from "./ThemingHooks";

0 commit comments

Comments
 (0)