Skip to content

Commit f72b041

Browse files
committed
fix(theme): simplify theme type
1 parent 6428fe8 commit f72b041

12 files changed

+323
-326
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@
113113
"stylelint-prettier": "5.0.3",
114114
"ts-jest": "29.2.5",
115115
"ts-node": "10.9.2",
116-
"typescript": "5.7.3",
116+
"typescript": "5.8.3",
117117
"typescript-eslint": "8.23.0",
118118
"vite": "6.2.6",
119-
"vite-plugin-dts": "4.3.0"
119+
"vite-plugin-dts": "4.5.3"
120120
}
121121
}

pnpm-lock.yaml

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

src/lib/LocalTheme/LocalRoot.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type {HTMLAttributes, ReactNode} from 'react';
22
import {createElement, forwardRef, useEffect, useMemo} from 'react';
3-
import type {ThemeType} from 'css-vars-hook';
43

54
import {createStyleObject} from '@/lib/utils';
65
import type {DataAttributes, LibraryProps} from '@/lib/NativeProps';
6+
import type {Theme} from '@/lib/ThemeType.ts';
77

88
/**
99
* @public
@@ -15,9 +15,9 @@ export type LocalRootProps = DataAttributes &
1515
/** Choose which HTMLElement to render as a root. div is default. */
1616
as?: string;
1717
/** Apply initial theme. */
18-
theme?: ThemeType;
18+
theme?: Theme;
1919
/** Provide theme setter function. */
20-
setTheme?: (arg0: ThemeType) => void;
20+
setTheme?: (arg0: Theme) => void;
2121
};
2222

2323
export const LocalRoot = forwardRef<HTMLElement, LocalRootProps>((props, ref) => {

src/lib/LocalTheme/useLocalTheme.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import {useCallback, useRef, useMemo} from 'react';
2-
import type {ThemeType} from 'css-vars-hook';
32

43
import {setCSSVariable, setCSSTheme} from '@/lib/utils';
5-
import type {UnitType} from '@/lib/UnitType';
4+
import type {UnitType, Theme} from '@/lib/ThemeType.ts';
65

76
import type {LocalRootProps} from './LocalRoot';
87
import {LocalRoot} from './LocalRoot';
@@ -21,10 +20,10 @@ import {LocalRoot} from './LocalRoot';
2120
* return <LocalRoot theme={{foo: 'bar'}} className="demo-local">//...
2221
*/
2322
export const useLocalTheme = <TElement extends HTMLElement>() => {
24-
const themeRef = useRef<ThemeType>();
23+
const themeRef = useRef<Theme>();
2524
const elementRef = useRef<TElement>(null);
2625

27-
const setTheme = useCallback((nextTheme: ThemeType) => {
26+
const setTheme = useCallback((nextTheme: Theme) => {
2827
setCSSTheme(elementRef.current!)(nextTheme);
2928

3029
themeRef.current = nextTheme;

src/lib/RootTheme/HookInterfaceType.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
import type {ThemeType} from 'css-vars-hook';
2-
3-
import type {UnitType} from '@/lib/UnitType';
1+
import type {UnitType, Theme} from '@/lib/ThemeType.ts';
42

53
/**
64
* @public
75
* `useRootTheme` return type.
86
*/
97
export type HookInterface = {
10-
setTheme: (nextTheme: ThemeType) => void;
11-
getTheme: () => ThemeType;
8+
setTheme: (nextTheme: Theme) => void;
9+
getTheme: () => Theme;
1210
setVariable: (variableName: string, value: UnitType) => void;
1311
getVariable: (variableName: string) => string;
1412
removeVariable: (variableName: string) => void;

src/lib/RootTheme/RootThemeProvider.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type {FC, ReactNode} from 'react';
22
import {useMemo, useEffect, useId} from 'react';
3-
import type {ThemeType} from 'css-vars-hook';
3+
4+
import type {Theme} from '@/lib/ThemeType.ts';
45

56
import type {DataAttributes, LibraryProps} from '../NativeProps';
67
import {RootContext} from './RootContext';
@@ -10,7 +11,7 @@ import {useRootTheme} from './useRootTheme';
1011
* @public
1112
*/
1213
export type RootThemeProviderProps = DataAttributes &
13-
LibraryProps & {children: ReactNode; theme: ThemeType};
14+
LibraryProps & {children: ReactNode; theme: Theme};
1415

1516
/**
1617
* @public

src/lib/RootTheme/useRootTheme.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import {useCallback, useMemo, useRef} from 'react';
22
import type {CSSProperties} from 'react';
3-
import type {ThemeType} from 'css-vars-hook';
43

5-
import type {UnitType} from '@/lib/UnitType';
4+
import type {UnitType, Theme} from '@/lib/ThemeType.ts';
65

76
import {
87
createStyleObject,
@@ -18,13 +17,13 @@ import type {HookInterface} from './HookInterfaceType';
1817
* Logic for root theme handling such as updates and CSS style creation
1918
*/
2019
export const useRootTheme = (
21-
theme: ThemeType,
20+
theme: Theme,
2221
id: string
2322
): HookInterface & {style: CSSProperties} => {
2423
const themeRef = useRef(theme);
2524

2625
const setTheme = useCallback(
27-
(nextTheme: ThemeType) => {
26+
(nextTheme: Theme) => {
2827
setRootTheme(id)(nextTheme);
2928
themeRef.current = nextTheme;
3029
},

src/lib/ThemeType.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export type UnitType = string | number;
2+
3+
export type Theme = Record<string, UnitType>;

src/lib/UnitType.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/lib/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ export {useRootTheme, RootThemeProvider} from './RootTheme';
22
export type {RootThemeProviderProps} from './RootTheme';
33
export {useLocalTheme} from './LocalTheme';
44
export type {LocalRootProps} from './LocalTheme';
5-
export type {UnitType} from './UnitType';
5+
export type {UnitType} from './ThemeType.ts';
66
export type {LibraryProps, DataAttributes, DataAttributeKey} from './NativeProps';

src/lib/utils.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type {MutableRefObject, CSSProperties} from 'react';
2-
import type {ThemeType} from 'css-vars-hook';
32

4-
import type {UnitType} from '@/lib/UnitType';
3+
import type {UnitType, Theme} from '@/lib/ThemeType.ts';
54

65
const normalizeUnit = (unit: UnitType) => {
76
if (typeof unit === 'string') {
@@ -13,9 +12,9 @@ const normalizeUnit = (unit: UnitType) => {
1312
/**
1413
* Add `--` prefix to property names in theme object in order to make it applicable to DOM node
1514
*/
16-
export const createStyleObject = (theme: ThemeType): CSSProperties => {
15+
export const createStyleObject = (theme: Theme): CSSProperties => {
1716
const keys = Object.keys(theme);
18-
const result = {} as ThemeType;
17+
const result: Record<string, UnitType> = {};
1918
keys.forEach(key => {
2019
result[`--${key}`] = theme[key];
2120
});
@@ -25,7 +24,7 @@ export const createStyleObject = (theme: ThemeType): CSSProperties => {
2524
/**
2625
* Add `--` prefix to property names in theme object in order to make it applicable to DOM node
2726
*/
28-
export const createStyleString = (theme: ThemeType) => {
27+
export const createStyleString = (theme: Theme) => {
2928
const keys = Object.keys(theme);
3029
let result = '';
3130
keys.forEach(key => {
@@ -37,7 +36,7 @@ export const createStyleString = (theme: ThemeType) => {
3736
/**
3837
* Set CSS theme at the provided DOM node
3938
*/
40-
export const setCSSTheme = (element: HTMLElement) => (theme: ThemeType) => {
39+
export const setCSSTheme = (element: HTMLElement) => (theme: Theme) => {
4140
const style = createStyleString(theme);
4241
element?.setAttribute('style', style);
4342
};
@@ -71,7 +70,7 @@ export const getCSSVariable =
7170
/**
7271
* Set new theme be replacing `style` attribute content
7372
*/
74-
export const setRootTheme = (id: string) => (theme: ThemeType) => {
73+
export const setRootTheme = (id: string) => (theme: Theme) => {
7574
const root = document.getElementById(id)!;
7675
const style = createStyleString(theme);
7776
root?.setAttribute('style', style);

src/types.d.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@ declare module '*.module.css' {
33
export default classes;
44
}
55

6-
declare module 'css-vars-hook' {
7-
import type {UnitType} from '@/lib';
8-
/** The most common theme type */
9-
export type ThemeType = Record<string, UnitType>;
10-
}
11-
126
declare module '*.png';
137
declare module '*.svg';
148
declare module '*.jpeg';

0 commit comments

Comments
 (0)