Skip to content

Commit

Permalink
fix(theme): improve theme type
Browse files Browse the repository at this point in the history
  • Loading branch information
morewings committed May 27, 2024
1 parent 3625e91 commit c9205a6
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
11 changes: 4 additions & 7 deletions src/lib/LocalTheme/LocalRoot.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type {HTMLAttributes, ReactNode} from 'react';
import {createElement, forwardRef, useEffect, useMemo} from 'react';
import type {ThemeType} from 'css-vars-hook';

import {createStyleObject} from '../utils';
import type {DataAttributes, LibraryProps} from '../NativeProps';
import type {UnitType} from '../UnitType';

/**
* @public
Expand All @@ -15,20 +15,17 @@ export type LocalRootProps = DataAttributes &
/** Choose which HTMLElement to render as a root. div is default. */
as?: string;
/** Apply initial theme. */
theme?: Record<string, UnitType>;
theme?: ThemeType;
/** Provide theme setter function. */
setTheme?: (arg0: Record<string, UnitType>) => void;
setTheme?: (arg0: ThemeType) => void;
};

export const LocalRoot = forwardRef<HTMLElement, LocalRootProps>((props, ref) => {
// This is needed to fix an error introduced in version 0.6.14.
// Props were not transported to returned HTMLElement.
const {children, as = 'div', theme = {}, setTheme = () => {}, ...restProps} = props;

// const initialStyle = useRef(createStyleObject(theme));

/* eslint-disable-next-line react-hooks/exhaustive-deps */
const initialStyle = useMemo(() => createStyleObject(theme), []);
const initialStyle = useMemo(() => createStyleObject(theme), [theme]);

useEffect(() => {
setTheme(theme);
Expand Down
5 changes: 3 additions & 2 deletions src/lib/LocalTheme/useLocalTheme.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {useCallback, useRef, useMemo} from 'react';
import type {ThemeType} from 'css-vars-hook';

import {setCSSVariable} from '../utils';
import type {LocalRootProps} from './LocalRoot';
Expand All @@ -19,10 +20,10 @@ import type {UnitType} from '../UnitType';
* return <LocalRoot theme={{foo: 'bar'}} className="demo-local">//...
*/
export const useLocalTheme = <TElement extends HTMLElement>() => {
const themeRef = useRef<Record<string, UnitType>>();
const themeRef = useRef<ThemeType>();
const elementRef = useRef<TElement>(null);

const setTheme = useCallback((nextTheme: Record<string, UnitType>) => {
const setTheme = useCallback((nextTheme: ThemeType) => {
Object.keys(nextTheme).forEach((key: string) => {
setCSSVariable(elementRef.current!)(key, nextTheme[key]);
});
Expand Down
5 changes: 3 additions & 2 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {MutableRefObject, CSSProperties} from 'react';
import type {ThemeType} from 'css-vars-hook';

import {ROOT_ID} from './config';
import type {UnitType} from './UnitType';
Expand Down Expand Up @@ -43,9 +44,9 @@ export const getCSSVariable =
* @name createStyleObject
* @description Add `--` prefix to property names in theme object in order to make it applicable to DOM node
*/
export const createStyleObject = (theme: Record<string, UnitType>): CSSProperties => {
export const createStyleObject = (theme: ThemeType): CSSProperties => {
const keys = Object.keys(theme);
const result = {} as Record<string, UnitType>;
const result = {} as ThemeType;
keys.forEach(key => {
result[`--${key}`] = theme[key];
});
Expand Down
3 changes: 2 additions & 1 deletion src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ declare module '*.module.css' {
}

declare module 'css-vars-hook' {
import type {UnitType} from '@/lib';
/** The most common theme type */
export type ThemeType = Record<string, string | number>;
export type ThemeType = Record<string, UnitType>;
}

declare module '*.png';
Expand Down

0 comments on commit c9205a6

Please sign in to comment.