@nex-ui/system
Version:
A lightweight and performant styling library based on Emotion, focusing on component architecture and developer experience.
90 lines (87 loc) • 4.24 kB
TypeScript
import { Keyframes, SerializedStyles, ComponentSelector } from '@emotion/react';
import * as CSS from 'csstype';
import { TokenCategory } from './tokens/types.js';
interface Breakpoints {
}
interface Selectors {
}
interface Aliases {
}
interface Scales {
}
interface Tokens {
}
interface SemanticTokens {
}
type Overwrite<K, T> = Omit<K, keyof T> & T;
type Dictionary<T = any> = Record<string, T>;
type InterpolationPrimitive = null | undefined | boolean | number | string | CSSObject | Keyframes | SerializedStyles | ComponentSelector;
type CSSProperties = CSS.Properties<number | (string & {})>;
type ArrayInterpolation = ReadonlyArray<Interpolation>;
type Interpolation = InterpolationPrimitive | ArrayInterpolation;
type CSSOthersObject = {
[propertiesName: string]: Interpolation;
};
type CSSPropertiesWithMultiValues = ExtraCSSPropertyValue<OverriddenCSSProps> & ExtraCSSPropertyValue<CSSPropShorthands> & Conditions<Interpolation>;
type CSSPseudos = {
[K in CSS.Pseudos]?: CSSObject;
};
interface CSSObject extends CSSPropertiesWithMultiValues, CSSPseudos, CSSOthersObject {
colorPalette?: OverriddenCSSProps['color'];
}
type ConvertToVirtualColor<T> = T extends `${string}.${infer U}` ? `colorPalette.${U}` : 'colorPalette';
type VirtualColor = (Tokens extends infer T ? 'colors' extends keyof T ? ConvertToVirtualColor<T['colors']> : never : never) | (SemanticTokens extends infer T ? 'colors' extends keyof T ? ConvertToVirtualColor<T['colors']> : never : never);
type TypeValueByKey<T, K> = K extends keyof T ? T[K] : never;
/**
* Add the corresponding token values according to the scales.
*/
type OverriddenCSSProps = Overwrite<CSSProperties, {
[K in keyof Scales]?: Exclude<Scales[K], undefined> extends TokenCategory ? CSSProperties[K] | TypeValueByKey<Tokens, Scales[K]> | TypeValueByKey<SemanticTokens, Scales[K]> | (Exclude<Scales[K], undefined> extends 'colors' ? VirtualColor : never) : CSSProperties[K];
}>;
/**
* The Conditions type uses template literal types to prefix keys from Selectors and Breakpoints with an underscore (e.g., _hover, _sm).
* This enables conditional styling based on selector or breakpoint context, allowing for easy extension and type safety.
* Additional keys like _dark and _light are included for theme-based conditions.
*/
type Conditions<T> = {
[K in keyof Selectors | keyof Breakpoints as `_${K}`]?: T;
} & {
_dark?: T;
_light?: T;
};
type BreakpointArray = (string | number | null | undefined)[];
type NestedConditions<T> = {
_DEFAULT?: T;
} & {
[K in keyof Conditions<T>]?: NestedConditions<T> | T;
};
/**
* Defines the possible values for each CSS property, supporting direct values, breakpoint arrays, and deeply nested conditional objects.
* This type enables flexible assignment of CSS property values, including responsive and state-based variations.
*/
type ExtraCSSPropertyValue<T> = {
[K in keyof T as T[K] extends undefined ? never : K]?: T[K] | BreakpointArray
/**
* bg: {
* _DEFAULT: 'colorPalette.100',
* _hover: 'colorPalette.50',
* _dark: {
* _DEFAULT: 'colorPalette.800/30',
* _hover: 'colorPalette.900/30',
* },
* },
*/
| NestedConditions<T[K]>;
};
/**
* CSSPropShorthands maps each key in Aliases to its corresponding CSS property value type.
*
* If an alias maps to a string, it checks if that string is a key in OverriddenCSSProps and uses its type.
* If an alias maps to an array of strings, it uses the type of the first string as a key in OverriddenCSSProps.
*
* This enables shorthand properties (like 'bg' for 'backgroundColor') to inherit the correct type from OverriddenCSSProps.
*/
type CSSPropShorthands = {
[K in keyof Aliases]?: Aliases[K] extends infer CSSProps ? CSSProps extends string ? CSSProps extends keyof OverriddenCSSProps ? OverriddenCSSProps[CSSProps] : never : CSSProps extends [infer CSSProp, ...unknown[]] ? CSSProp extends keyof OverriddenCSSProps ? OverriddenCSSProps[CSSProp] : never : never : never;
};
export type { Aliases, ArrayInterpolation, Breakpoints, CSSObject, CSSProperties, Dictionary, Interpolation, InterpolationPrimitive, Scales, Selectors, SemanticTokens, Tokens };