Skip to content

Commit

Permalink
refactor: restore deep path generation for colors
Browse files Browse the repository at this point in the history
  • Loading branch information
quantizor committed Mar 16, 2023
1 parent 9955f61 commit 63daead
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
7 changes: 2 additions & 5 deletions packages/system/src/styles/colors.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import * as CSS from 'csstype'
import { themeGetter } from '../style'
import { ThemeNamespaceValue, ITheme, Theme } from '../types'
import { SynthesizedPath, ITheme, Theme } from '../types'

export type ThemeColor<T extends ITheme = Theme> = ThemeNamespaceValue<
'colors',
T
>
export type ThemeColor<T extends ITheme = Theme> = SynthesizedPath<T['colors']>
export type Color<T extends ITheme = Theme> = ThemeColor<T> | CSS.Property.Color
export const getColor = themeGetter<ThemeColor>({
name: 'color',
Expand Down
23 changes: 11 additions & 12 deletions packages/system/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,17 @@ export interface TransformValue {
): CSSScalar
}

// declare type SynthesizedPath<T> = {
// [P in keyof T]:
// | (T[P] extends { [key: string]: any; [key: number]: any }
// ? `${string & P}` | `${string & P}.${SynthesizedPath<T[P]>}`
// : `${string & P}`)
// | (number & P)
// }[T extends any[] ? number & keyof T : keyof T]

// export type ThemeNamespaceValue<
// K extends string,
// T extends ITheme,
// > = SynthesizedPath<T[K]>
/**
* Recursively explores a given object and creates a union of the deep paths
* leading to primitive values (non-objects.)
*/
export type SynthesizedPath<T extends Record<string, unknown>> = {
[P in keyof T]:
| (T[P] extends Record<string, unknown>
? `${string & P}.${SynthesizedPath<T[P]>}`
: `${string & P}`)
| number
}[T extends any[] ? number & keyof T : keyof T]

export type ThemeNamespaceValue<K extends string, T extends ITheme> =
| NamespaceType<T[K]>
Expand Down

0 comments on commit 63daead

Please sign in to comment.