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.
- Loading branch information
Showing
7 changed files
with
109 additions
and
215 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/styled-system": patch | ||
--- | ||
|
||
Revert #6335 |
109 changes: 0 additions & 109 deletions
109
packages/components/styled-system/benchmarks/benchmark.js
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
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
62 changes: 62 additions & 0 deletions
62
packages/components/styled-system/src/utils/expand-responsive.ts
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,62 @@ | ||
import { isObject, runIfFn } from "@chakra-ui/shared-utils" | ||
|
||
/** | ||
* Expands an array or object syntax responsive style. | ||
* | ||
* @example | ||
* expandResponsive({ mx: [1, 2] }) | ||
* // or | ||
* expandResponsive({ mx: { base: 1, sm: 2 } }) | ||
* | ||
* // => { mx: 1, "@media(min-width:<sm>)": { mx: 2 } } | ||
*/ | ||
export const expandResponsive = | ||
(styles: Record<string, any>) => (theme: Record<string, any>) => { | ||
/** | ||
* Before any style can be processed, the user needs to call `toCSSVar` | ||
* which analyzes the theme's breakpoint and appends a `__breakpoints` property | ||
* to the theme with more details of the breakpoints. | ||
* | ||
* To learn more, go here: packages/utils/src/responsive.ts #analyzeBreakpoints | ||
*/ | ||
if (!theme.__breakpoints) return styles | ||
const { isResponsive, toArrayValue, media: medias } = theme.__breakpoints | ||
|
||
const computedStyles: Record<string, any> = {} | ||
|
||
for (const key in styles) { | ||
let value = runIfFn(styles[key], theme) | ||
|
||
if (value == null) continue | ||
|
||
// converts the object responsive syntax to array syntax | ||
value = | ||
isObject(value) && isResponsive(value) ? toArrayValue(value) : value | ||
|
||
if (!Array.isArray(value)) { | ||
computedStyles[key] = value | ||
continue | ||
} | ||
|
||
const queries = value.slice(0, medias.length).length | ||
|
||
for (let index = 0; index < queries; index += 1) { | ||
const media = medias?.[index] | ||
|
||
if (!media) { | ||
computedStyles[key] = value[index] | ||
continue | ||
} | ||
|
||
computedStyles[media] = computedStyles[media] || {} | ||
|
||
if (value[index] == null) { | ||
continue | ||
} | ||
|
||
computedStyles[media][key] = value[index] | ||
} | ||
} | ||
|
||
return computedStyles | ||
} |
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.