UNPKG

@nex-ui/system

Version:

A lightweight and performant styling library based on Emotion, focusing on component architecture and developer experience.

63 lines (60 loc) 2.98 kB
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 & {})>; interface ArrayInterpolation extends ReadonlyArray<InterpolationPrimitive> { } type Interpolation = InterpolationPrimitive | ArrayInterpolation; type CSSOthersObject = { [propertiesName: string]: Interpolation; }; 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]; }>; type Conditions<T> = { [K in keyof Selectors | keyof Breakpoints as `_${K}`]?: T; } & { _dark?: T; _light?: T; }; type BreakpointArray = (string | number | null | undefined)[] | readonly (string | number | null | undefined)[]; type ExtraCSSPropertyValue<T> = { [K in keyof T as T[K] extends undefined ? never : K]?: T[K] | BreakpointArray | ({ _DEFAULT?: T[K]; } & Conditions<T[K]>); }; /** * Shorthand types for CSS properties based on aliases. */ 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; }; type CSSPropertiesWithMultiValues = ExtraCSSPropertyValue<OverriddenCSSProps> & ExtraCSSPropertyValue<CSSPropShorthands> & Conditions<InterpolationPrimitive>; type CSSPseudos = { [K in CSS.Pseudos]?: CSSObject; }; interface CSSObject extends CSSPropertiesWithMultiValues, CSSPseudos, CSSOthersObject { colorPalette?: OverriddenCSSProps['color']; } export type { Aliases, ArrayInterpolation, Breakpoints, CSSObject, CSSProperties, Dictionary, Interpolation, InterpolationPrimitive, Scales, Selectors, SemanticTokens, Tokens };