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

Update PropertyValue and ScaleValue types to support config #829

Merged
merged 1 commit into from
Oct 5, 2021
Merged
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
23 changes: 21 additions & 2 deletions packages/core/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,29 @@ export type CSS<
export type ComponentProps<Component> = Component extends ((...args: any[]) => any) ? Parameters<Component>[0] : never

/** Returns a type that expects a value to be a kind of CSS property value. */
export type PropertyValue<K extends keyof CSSUtil.CSSProperties> = { readonly [CSSUtil.$$PropertyValue]: K }
export type PropertyValue<K extends keyof CSSUtil.CSSProperties, C = null> = (
C extends null
? { readonly [CSSUtil.$$PropertyValue]: K }
: C extends { [K: string]: any }
? CSSUtil.CSS<
C['media'],
C['theme'],
C['themeMap'],
C['utils']
>[K]
: never
)

/** Returns a type that expects a value to be a kind of theme scale value. */
export type ScaleValue<K> = { readonly [CSSUtil.$$ScaleValue]: K }
export type ScaleValue<K, C = null> = (
C extends null
? { readonly [CSSUtil.$$ScaleValue]: K }
: C extends { [K: string]: any }
? K extends keyof C['theme']
? `$${string & keyof C['theme'][K]}`
: never
: never
)

/** Returns a type that suggests variants from a component as possible prop values. */
export type VariantProps<Component extends {[key: symbol | string]: any}> = StyledComponent.TransformProps<Component[StyledComponent.$$StyledComponentProps], Component[StyledComponent.$$StyledComponentMedia]>
Expand Down
23 changes: 21 additions & 2 deletions packages/react/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,29 @@ export type CSS<
export type ComponentProps<Component> = Component extends ((...args: any[]) => any) ? Parameters<Component>[0] : never

/** Returns a type that expects a value to be a kind of CSS property value. */
export type PropertyValue<K extends keyof CSSUtil.CSSProperties> = { readonly [CSSUtil.$$PropertyValue]: K }
export type PropertyValue<K extends keyof CSSUtil.CSSProperties, C = null> = (
C extends null
? { readonly [CSSUtil.$$PropertyValue]: K }
: C extends { [K: string]: any }
? CSSUtil.CSS<
C['media'],
C['theme'],
C['themeMap'],
C['utils']
>[K]
: never
)

/** Returns a type that expects a value to be a kind of theme scale value. */
export type ScaleValue<K> = { readonly [CSSUtil.$$ScaleValue]: K }
export type ScaleValue<K, C = null> = (
C extends null
? { readonly [CSSUtil.$$ScaleValue]: K }
: C extends { [K: string]: any }
? K extends keyof C['theme']
? `$${string & keyof C['theme'][K]}`
: never
: never
)

/** Returns a type that suggests variants from a component as possible prop values. */
export type VariantProps<Component extends {[key: symbol | string]: any}> = StyledComponent.TransformProps<Component[StyledComponent.$$StyledComponentProps], Component[StyledComponent.$$StyledComponentMedia]>
Expand Down
36 changes: 36 additions & 0 deletions packages/test/Issue-813-core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// https://github.com/modulz/stitches/issues/813
import { createStitches } from '@stitches/core'
import * as Stitches from '@stitches/core'

const { config, css } = createStitches({
theme: {
colors: {
primary: 'transparent'
}
},
utils: {
bg: (value: Stitches.PropertyValue<'backgroundColor'>) => ({
color: value
}),
c: (value: Stitches.ScaleValue<'colors'>) => ({
color: value
})
}
})

// ensure `PropertyValue` accepts a valid CSS Color
export const colorValue1: Stitches.PropertyValue<'backgroundColor', typeof config> = "RebeccaPurple"
// ensure `PropertyValue` accepts a valid token
export const colorValue2: Stitches.PropertyValue<'backgroundColor', typeof config> = "$primary"

// ensure `ScaleValue` accepts a valid token
export const colorToken: Stitches.ScaleValue<'colors', typeof config> = '$primary'

export const box = css({
// ensure `bg` accepts a valid CSS Color
bg: 'RebeccaPurple',
// ensure `bg` accepts a valid token
'&': { bg: '$primary' },
// ensure `c` accepts a valid token
c: '$primary',
})
36 changes: 36 additions & 0 deletions packages/test/Issue-813-react.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// https://github.com/modulz/stitches/issues/813
import { createStitches } from '@stitches/react'
import * as Stitches from '@stitches/react'

const { config, styled } = createStitches({
theme: {
colors: {
primary: 'transparent'
}
},
utils: {
bg: (value: Stitches.PropertyValue<'backgroundColor'>) => ({
color: value
}),
c: (value: Stitches.ScaleValue<'colors'>) => ({
color: value
})
}
})

// ensure `PropertyValue` accepts a valid CSS Color
export const colorValue1: Stitches.PropertyValue<'backgroundColor', typeof config> = "RebeccaPurple"
// ensure `PropertyValue` accepts a valid token
export const colorValue2: Stitches.PropertyValue<'backgroundColor', typeof config> = "$primary"

// ensure `ScaleValue` accepts a valid token
export const colorToken: Stitches.ScaleValue<'colors', typeof config> = '$primary'

export const Box = styled('div', {
// ensure `bg` accepts a valid CSS Color
bg: 'RebeccaPurple',
// ensure `bg` accepts a valid token
'&': { bg: '$primary' },
// ensure `c` accepts a valid token
c: '$primary',
})