diff --git a/packages/react/types/create-css.d.ts b/packages/react/types/create-css.d.ts index 5bc79a4c..86285660 100644 --- a/packages/react/types/create-css.d.ts +++ b/packages/react/types/create-css.d.ts @@ -1,23 +1,19 @@ import type * as CSSUtil from './css-util' import type * as Default from './default' -import type * as Stitches from './stitches' - -export type PropertyValue = { readonly [CSSUtil.$$PropertyValue]: K } - -export type ScaleValue = { readonly [CSSUtil.$$ScaleValue]: K } +import type Sheet from './sheet' /* Interfaces */ /* ========================================================================== */ /** Returns a shaped theme object from the given media object. */ -export type InterfaceOfMedia< +type InterfaceOfMedia< Media extends {} > = { [K in keyof Media]: string } /** Returns a shaped theme object from the given theme object. */ -export type InterfaceOfTheme< +type InterfaceOfTheme< Theme extends {} > = ( // shape of left-hand scale names @@ -39,7 +35,7 @@ export type InterfaceOfTheme< ) /** Returns a shaped themeMap object from the given themeMap object and theme object. */ -export type InterfaceOfThemeMap< +type InterfaceOfThemeMap< ThemeMap extends {} = {}, Theme extends {} = {} > = ( @@ -61,7 +57,7 @@ export type InterfaceOfThemeMap< } ) -export type InterfaceOfUtils< +type InterfaceOfUtils< Utils extends {} = {} > = ( // shape of right-hand values @@ -75,7 +71,7 @@ export type InterfaceOfUtils< ) /** Returns a function used to create a new Stitches interface. */ -export type CreateCss = { +type CreateCss = { < Prefix extends string, Media extends InterfaceOfMedia, @@ -90,7 +86,7 @@ export type CreateCss = { themeMap?: ThemeMap utils?: Utils } - ): Stitches.Stitches< + ): Sheet< // post-process prefix string extends Prefix ? Default.Prefix : Prefix, // post-process media @@ -104,4 +100,4 @@ export type CreateCss = { > } -export declare var createCss: CreateCss +export default CreateCss diff --git a/packages/react/types/css-util.d.ts b/packages/react/types/css-util.d.ts index e829af09..b59b169b 100644 --- a/packages/react/types/css-util.d.ts +++ b/packages/react/types/css-util.d.ts @@ -14,7 +14,7 @@ type TokenByPropertyName = PropertyName extends k type TokenByScaleName = ScaleName extends keyof Theme ? Util.Prefixed<'$', keyof Theme[ScaleName]> : never /** Returns a Style interface, leveraging the given media and style map. */ -export type Style< +export type CSS< Media = Default.Media, Theme = {}, ThemeMap = Default.ThemeMap, @@ -22,7 +22,7 @@ export type Style< > = ( // nested at-rule css styles & { - [K in Util.Prefixed<'@', keyof Media>]?: Style + [K in Util.Prefixed<'@', keyof Media>]?: CSS } // known property styles & { @@ -88,7 +88,7 @@ export type Style< : K extends keyof { variants: unknown; defaultVariants: unknown; compoundVariants: unknown } ? unknown : ( - | Style + | CSS | CSS.Globals | Util.Index | any[] @@ -97,6 +97,70 @@ export type Style< } ) + +/** Returns a Style interface, leveraging the given media and style map. */ +export type KnownCSS< + Media = Default.Media, + Theme = {}, + ThemeMap = Default.ThemeMap, + Utils = {} +> = ( + // nested at-rule css styles + & { + [K in Util.Prefixed<'@', keyof Media>]?: KnownCSS + } + // known property styles + & { + [K in keyof CSSProperties]?: ( + | ValueByPropertyName + | TokenByPropertyName + | CSS.Globals + | Util.Index + ) + } + // known utility styles + & { + [K in keyof Utils]?: ( + K extends keyof CSSProperties + ? unknown + : ( + | ( + Utils[K] extends (arg: infer P) => any + ? $$PropertyValue extends keyof P + ? ( + | ValueByPropertyName + | TokenByPropertyName + | CSS.Globals + | Util.Index + ) + : $$ScaleValue extends keyof P + ? ( + | TokenByScaleName + | Util.Index + ) + : never + : never + ) + ) + ) + } + // known theme styles + & { + [K in keyof ThemeMap]?: ( + K extends keyof CSSProperties + ? unknown + : K extends keyof CSSProperties + ? unknown + : K extends keyof Utils + ? unknown + : ( + | CSS.Globals + | Util.Index + ) + ) + } +) + /** Unique symbol used to reference a property value. */ export declare const $$PropertyValue: unique symbol @@ -112,18 +176,3 @@ export type $$ScaleValue = typeof $$ScaleValue export declare const $$ThemeValue: unique symbol export type $$ThemeValue = typeof $$ThemeValue - -/** Returns a Style interface from a configuration, leveraging the given media and style map. */ -export type CSS< - Config extends { - media?: {} - theme?: {} - themeMap?: {} - utils?: {} - } -> = Style< - Config['media'], - Config['theme'], - Config['themeMap'], - Config['utils'] -> diff --git a/packages/react/types/index.d.ts b/packages/react/types/index.d.ts index 64f3ca42..e8d2aafa 100644 --- a/packages/react/types/index.d.ts +++ b/packages/react/types/index.d.ts @@ -1,9 +1,33 @@ -import type { CreateCss, PropertyValue, ScaleValue } from './create-css' -import type { CSS, Style } from './css-util' -import type { Stitches } from './stitches' -import type { ThemeMap as DefaultThemeMap } from './default' +import type CreateCss from './create-css' +import type Sheet from './sheet' -export type { CSS, CreateCss, DefaultThemeMap, PropertyValue, ScaleValue, Stitches, Style } +import type * as CSSUtil from './css-util' +import type * as Default from './default' + +export type { CreateCss, Sheet } + +export type DefaultThemeMap = Default.ThemeMap + +/** Returns a Style interface from a configuration, leveraging the given media and style map. */ +export type CSS< + Config extends { + media?: {} + theme?: {} + themeMap?: {} + utils?: {} + } +> = CSSUtil.CSS< + Config['media'], + Config['theme'], + Config['themeMap'], + Config['utils'] +> + +/** Returns a type that expects a value to be a kind of CSS property value. */ +export type PropertyValue = { readonly [CSSUtil.$$PropertyValue]: K } + +/** Returns a type that expects a value to be a kind of theme scale value. */ +export type ScaleValue = { readonly [CSSUtil.$$ScaleValue]: K } /** Returns a library used to create styles. */ export declare const createCss: CreateCss @@ -12,13 +36,13 @@ export declare const createCss: CreateCss export declare const defaultThemeMap: DefaultThemeMap /** Returns a function that applies global styles. */ -export declare const global: Stitches['global'] +export declare const global: Sheet['global'] /** Returns an object that applies `@keyframes` styles. */ -export declare const keyframes: Stitches['keyframes'] +export declare const keyframes: Sheet['keyframes'] /** Returns a function that applies styles and variants for a specific class. */ -export declare const css: Stitches['css'] +export declare const css: Sheet['css'] /** Returns a function that applies styles and variants for a specific class. */ -export declare const styled: Stitches['styled'] +export declare const styled: Sheet['styled'] diff --git a/packages/react/types/stitches.d.ts b/packages/react/types/sheet.d.ts similarity index 72% rename from packages/react/types/stitches.d.ts rename to packages/react/types/sheet.d.ts index 107e268c..a101fef2 100644 --- a/packages/react/types/stitches.d.ts +++ b/packages/react/types/sheet.d.ts @@ -3,13 +3,15 @@ import type * as Default from './default' import type * as StyledComponent from './styled-component' import type * as Util from './util' -/** Stitches interface. */ -export interface Stitches< +/** Sheet interface. */ +export default interface Sheet< Prefix = Default.Prefix, Media = Default.Media, Theme = {}, ThemeMap = Default.ThemeMap, - Utils = {} + Utils = {}, + CSS extends { [prelude: string]: unknown } = CSSUtil.CSS, + KnownCSS extends { [prelude: string]: unknown } = CSSUtil.KnownCSS > { config: { prefix: Prefix @@ -21,14 +23,14 @@ export interface Stitches< prefix: Prefix global: { (style: { - [prelude: string]: CSSUtil.Style + [prelude: string]: CSS }): { (): string } }, keyframes: { (style: { - [offset: string]: CSSUtil.Style + [offset: string]: CSS }): { (): string name: string @@ -98,7 +100,7 @@ export interface Stitches< : Composers[K] extends Function ? Composers[K] : Composers[K] extends { - [K2 in keyof Composers[K]]: Composers[K][K2] + [Prelude in keyof Composers[K]]: Composers[K][Prelude] } ? ( & { @@ -106,12 +108,12 @@ export interface Stitches< K2 extends 'variants' ? { [name: string]: { - [pair in number | string]: CSSUtil.Style + [pair in number | string]: CSS } } : unknown } - & CSSUtil.Style + & KnownCSS ) : never ) @@ -149,46 +151,41 @@ export interface Stitches< [K2 in keyof Composers[K]]: Composers[K][K2] } ? ( + & KnownCSS & { - [K2 in keyof Composers[K]]: ( - | ( - K2 extends 'variants' - ? { - [name: string]: { - [pair in number | string]: CSSUtil.Style - } - } - : unknown - ) - ) - } - & { - variants?: unknown + variants?: { + [name: string]: { + [pair in number | string]: CSS + } + } compoundVariants?: ( & ( 'variants' extends keyof Composers[K] ? { - [Name in keyof Composers[K]['variants']]?: Util.Widen - } - : { - [name: string]: boolean | number | string | undefined - } + [Name in keyof Composers[K]['variants']]?: Util.Widen | Util.String + } & Util.WideObject + : Util.WideObject ) & { - css: CSSUtil.Style + css: CSS } )[] defaultVariants?: ( 'variants' extends keyof Composers[K] ? { - [Name in keyof Composers[K]['variants']]?: Util.Widen + [Name in keyof Composers[K]['variants']]?: Util.Widen | Util.String } - : { - [name: string]: boolean | number | string | undefined - } + : Util.WideObject ) } - & CSSUtil.Style + & { + [Prelude in keyof Composers[K]]: + Prelude extends keyof KnownCSS | 'compoundVariants' | 'defaultVariants' | 'variants' + ? unknown + : Composers[K][Prelude] extends {} + ? CSS + : boolean | number | string + } ) : never ) @@ -203,15 +200,3 @@ export interface Stitches< > } } - -/* ========================================================================== */ - -export interface GlobalComponent { - (): string -} - -export interface GlobalExpressor { - toString(): '' -} - -export interface ThemedExpressor {} diff --git a/packages/react/types/styled-component.d.ts b/packages/react/types/styled-component.d.ts index de529d6e..aee85c4d 100644 --- a/packages/react/types/styled-component.d.ts +++ b/packages/react/types/styled-component.d.ts @@ -1,13 +1,7 @@ +import type * as CSSUtil from './css-util' import type * as Default from './default' import type * as React from 'react' import type * as Util from './util' -import type * as CSSUtil from './css-util' - -type TransformProps = { - [K in keyof Props]: Props[K] | { - [KMedia in Util.Prefixed<'@', 'initial' | keyof Media>]?: Props[K] - } -} /** Returns a new Styled Component. */ export interface StyledComponent< @@ -17,7 +11,8 @@ export interface StyledComponent< Theme = {}, ThemeMap = Default.ThemeMap, Utils = {}, - TransformedProps = TransformProps + TransformedProps = TransformProps, + CSS = CSSUtil.CSS > extends ForwardRefExoticComponent { < As extends @@ -26,9 +21,9 @@ export interface StyledComponent< >( props: ( As extends keyof JSX.IntrinsicElements - ? Util.Assign & { as?: As, css?: CSSUtil.Style }> + ? Util.Assign & { as?: As, css?: CSS }> : As extends React.ComponentType - ? Util.Assign & { as?: As, css?: CSSUtil.Style }> + ? Util.Assign & { as?: As, css?: CSS }> : never ) ): ( @@ -47,13 +42,14 @@ export interface CssComponent< Theme = {}, ThemeMap = Default.ThemeMap, Utils = {}, - TransformedProps = TransformProps + TransformedProps = TransformProps, + CSS = CSSUtil.CSS > { ( props: ( & Partial & { - css?: CSSUtil.Style + css?: CSS } & { [name in number | string]: any @@ -72,6 +68,12 @@ export interface CssComponent< [$$StyledComponentProps]: Props } +type TransformProps = { + [K in keyof Props]: Props[K] | { + [KMedia in Util.Prefixed<'@', 'initial' | keyof Media>]?: Props[K] + } +} + /** Unique symbol used to reference the type of a Styled Component. */ export declare const $$StyledComponentType: unique symbol