forked from chakra-ui/chakra-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: component theming system (chakra-ui#6221)
* refactor: theming system and helpers * chore: update pkgs * docs: update changesets * chore: update changesets * chore: fix typo * chore: updates * chore: fix error * chore: update changeset * chore: update lockfile * refactor: batch 1 * refactor: anatomy * refactor: anatomy * refactor: anatomy * fix: errors * fix: errors * fix: patch default props for now * chore: update mdx extension * chore: update * chore: update package json
- Loading branch information
1 parent
a00afda
commit 872c0cc
Showing
67 changed files
with
1,286 additions
and
1,132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@chakra-ui/anatomy": patch | ||
--- | ||
|
||
Update package to use styled-system as dependency |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@chakra-ui/theme-tools": patch | ||
--- | ||
|
||
Refactored code to use from `styled-system` package |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@chakra-ui/skeleton": patch | ||
"@chakra-ui/theme": patch | ||
--- | ||
|
||
Refactor to use the new helpers from `styled-system` package |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
--- | ||
"@chakra-ui/styled-system": minor | ||
--- | ||
|
||
Add `defineStyle` and `defineStyleConfig` to improve the TypeScript authoring | ||
experience of style objects and single part component themes. | ||
|
||
```jsx live=false | ||
import { defineStyleConfig, defineStyle } from "@chakra-ui/styled-system" | ||
|
||
// authoring style objects | ||
const style = defineStyle({ | ||
marginTop: "20px", | ||
}) | ||
|
||
// authoring style configs for single part component | ||
const button = defineStyleConfig({ | ||
baseStyle: {}, | ||
variants: {}, | ||
defaultProps: {}, | ||
}) | ||
``` | ||
|
||
Add `createMultiStyleConfigHelpers` factory that provides functions that help | ||
improve the TypeScript authoring experience of multipart component themes. | ||
|
||
```jsx live=false | ||
import { createMultiStyleConfigHelpers } from "@chakra-ui/styled-sytem" | ||
|
||
// create scoped helpers for that defined parts | ||
const helpers = createMultiStyleConfigHelpers(["button", "label"]) | ||
const { definePartsStyle, defineMultiStyleConfig } = helpers | ||
|
||
// authoring styles for each part | ||
const outlineVariant = definePartsStyle({ | ||
button: {}, | ||
label: {}, | ||
}) | ||
|
||
// authoring styles for multipart component | ||
const accordion = defineMultiStyleConfig({ | ||
baseStyle: { | ||
button: {}, | ||
label: {}, | ||
}, | ||
variants: { | ||
outline: outlineVariant, | ||
}, | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { SystemStyleObject } from "./system.types" | ||
|
||
type Dict<T = any> = { [key: string]: T } | ||
|
||
// ------------------------------------------------------------------ // | ||
|
||
export type StyleFunctionProps = { | ||
colorScheme: string | ||
colorMode: "light" | "dark" | ||
orientation?: "horizontal" | "vertical" | ||
theme: Dict | ||
[key: string]: any | ||
} | ||
|
||
export type SystemStyleFunction = ( | ||
props: StyleFunctionProps, | ||
) => SystemStyleObject | ||
|
||
export type SystemStyleInterpolation = SystemStyleObject | SystemStyleFunction | ||
|
||
// ------------------------------------------------------------------ // | ||
|
||
export function defineStyle<T extends SystemStyleInterpolation>(styles: T) { | ||
return styles | ||
} | ||
|
||
// ------------------------------------------------------------------ // | ||
|
||
type DefaultProps = { | ||
size?: string | ||
variant?: string | ||
colorScheme?: string | ||
} | ||
|
||
export type StyleConfig = { | ||
baseStyle?: SystemStyleInterpolation | ||
sizes?: { [size: string]: SystemStyleInterpolation } | ||
variants?: { [variant: string]: SystemStyleInterpolation } | ||
defaultProps?: DefaultProps | ||
} | ||
|
||
/** | ||
* Defines the style config for a single-part component. | ||
*/ | ||
export function defineStyleConfig< | ||
BaseStyle extends SystemStyleInterpolation, | ||
Sizes extends Dict<SystemStyleInterpolation>, | ||
Variants extends Dict<SystemStyleInterpolation>, | ||
>(config: { | ||
baseStyle?: BaseStyle | ||
sizes?: Sizes | ||
variants?: Variants | ||
defaultProps?: { | ||
size?: keyof Sizes | ||
variant?: keyof Variants | ||
colorScheme?: string | ||
} | ||
}) { | ||
return config | ||
} | ||
|
||
// ------------------------------------------------------------------ // | ||
|
||
type Anatomy = { keys: string[] } | ||
|
||
export type PartsStyleObject<Parts extends Anatomy = Anatomy> = Partial< | ||
Record<Parts["keys"][number], SystemStyleObject> | ||
> | ||
|
||
export type PartsStyleFunction<Parts extends Anatomy = Anatomy> = ( | ||
props: StyleFunctionProps, | ||
) => PartsStyleObject<Parts> | ||
|
||
export type PartsStyleInterpolation<Parts extends Anatomy = Anatomy> = | ||
| PartsStyleObject<Parts> | ||
| PartsStyleFunction<Parts> | ||
|
||
export interface MultiStyleConfig<Parts extends Anatomy = Anatomy> { | ||
parts: Parts["keys"] | ||
baseStyle?: PartsStyleInterpolation<Parts> | ||
sizes?: { [size: string]: PartsStyleInterpolation<Parts> } | ||
variants?: { [variant: string]: PartsStyleInterpolation<Parts> } | ||
defaultProps?: DefaultProps | ||
} | ||
|
||
// ------------------------------------------------------------------ // | ||
|
||
/** | ||
* Returns an object of helpers that can be used to define | ||
* the style configuration for a multi-part component. | ||
*/ | ||
export function createMultiStyleConfigHelpers<Part extends string>( | ||
parts: Part[] | Readonly<Part[]>, | ||
) { | ||
return { | ||
definePartsStyle< | ||
PartStyles extends PartsStyleInterpolation<{ keys: Part[] }>, | ||
>(config: PartStyles) { | ||
return config | ||
}, | ||
defineMultiStyleConfig< | ||
BaseStyle extends PartsStyleInterpolation<{ keys: Part[] }>, | ||
Sizes extends Dict<PartsStyleInterpolation<{ keys: Part[] }>>, | ||
Variants extends Dict<PartsStyleInterpolation<{ keys: Part[] }>>, | ||
>(config: { | ||
baseStyle?: BaseStyle | ||
sizes?: Sizes | ||
variants?: Variants | ||
defaultProps?: { | ||
size?: keyof Sizes | ||
variant?: keyof Variants | ||
colorScheme?: string | ||
} | ||
}) { | ||
return { parts: parts as Part[], ...config } | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { omit } from "@chakra-ui/object-utils" | ||
import { ThemeTypings } from "./theme.types" | ||
import { ResponsiveValue } from "./utils" | ||
|
||
export interface ThemingProps<ThemeComponent extends string = any> { | ||
variant?: ResponsiveValue< | ||
ThemeComponent extends keyof ThemeTypings["components"] | ||
? ThemeTypings["components"][ThemeComponent]["variants"] | ||
: string | ||
> | ||
size?: ResponsiveValue< | ||
ThemeComponent extends keyof ThemeTypings["components"] | ||
? ThemeTypings["components"][ThemeComponent]["sizes"] | ||
: string | ||
> | ||
colorScheme?: ThemeTypings["colorSchemes"] | ||
orientation?: "vertical" | "horizontal" | ||
styleConfig?: Record<string, any> | ||
} | ||
|
||
export function omitThemingProps<T extends ThemingProps>(props: T) { | ||
return omit(props, ["styleConfig", "size", "variant", "colorScheme"]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.