Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow variant configuration overrides #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/Theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { camelToKebab } from "./utils/camelToKebab";
*
* TODO: consider exposing a user-facing API for configuring this themePropertiesConfig; users could create their own ThemeProperty sub-classes (like ColorProperty) to write their own value conversion logic.. would make tailwind-easy-theme incredibly flexible -- like more of a low-level theming framework
*/
const themePropertiesConfig: InternalThemePropertiesConfig = {
const themePropertiesConfig: InternalThemePropertiesConfig<ThemeProps> = {
colors: {
prefix: "",
type: ColorProperty,
Expand Down Expand Up @@ -85,7 +85,7 @@ const themePropertiesConfig: InternalThemePropertiesConfig = {
prefix: "stroke",
type: ColorProperty,
},
};
} as const;

export class Theme<T extends ThemeProps = ThemeProps> {
private userPrefix: string | undefined;
Expand Down Expand Up @@ -115,9 +115,12 @@ export class Theme<T extends ThemeProps = ThemeProps> {
let Property = ThemeProperty;

if (propertyKey in themePropertiesConfig) {
const config = themePropertiesConfig[propertyKey];
prefix = config.prefix ?? prefix;
Property = config.type ?? Property;
const config =
themePropertiesConfig[
propertyKey as keyof typeof themePropertiesConfig
];
prefix = config?.prefix ?? prefix;
Property = config?.type ?? Property;
}

prefix = userPrefix ? `${userPrefix}-${prefix}` : prefix;
Expand Down
34 changes: 29 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,34 @@ export type ThemePropertyConfig = Record<
string,
FlatThemePropertyConfig | string
>;
export type PartialThemePropertyConfig<
PrimaryTheme extends ThemePropertyConfig

type HexCode = string | undefined;

type RecursiveOptionalKeyValuePair<
K extends string | number | symbol,
V extends string | object
> = {
[K in keyof PrimaryTheme]?: Partial<PrimaryTheme[K]>;
[Key in K]?: V extends string
? HexCode
: V extends object
? V[keyof V] extends string | object
? RecursiveOptionalKeyValuePair<keyof V, V[keyof V]>
: never
: never;
};

export type PartialThemePropertyConfig<PrimaryTheme extends ThemeProps> = {
[K in keyof PrimaryTheme]?: PrimaryTheme[K] extends infer PrimaryThemeKey
? {
[Key in keyof PrimaryThemeKey]?: PrimaryThemeKey[Key] extends infer Value
? Value extends object
? Value[keyof Value] extends string | object
? RecursiveOptionalKeyValuePair<keyof Value, Value[keyof Value]>
: never
: HexCode
: never;
}
: never;
Comment on lines +58 to +69
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could use the LiteralToPrimitiveDeep from type-fest instead.

};

export type ThemePropertyOptions = {
Expand All @@ -52,8 +76,8 @@ export type ThemePropertyOptions = {
// Constructor signature for classes extending ThemeProperty
export type ThemePropertyConstructor = new (...args: any[]) => ThemeProperty;

export type InternalThemePropertiesConfig = {
[P: keyof ThemeConfig]: {
export type InternalThemePropertiesConfig<T extends ThemeProps> = {
[P in keyof T]?: {
prefix: string;
type: ThemePropertyConstructor;
};
Expand Down