Skip to content

Commit 76ed20a

Browse files
committed
fix: fix & optimize style sorting
1 parent c8f1707 commit 76ed20a

File tree

4 files changed

+25
-22
lines changed

4 files changed

+25
-22
lines changed

packages/system/src/style.ts

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -245,22 +245,18 @@ function indexGeneratorsByProp(styles: StyleGenerator[]) {
245245
return index
246246
}
247247

248-
function sortStyles(styles: Record<string | number, unknown>, props: IProps) {
249-
const breakpoints = getBreakpoints(props)
250-
251-
const breakpointsStyles: Record<string | number, unknown> = {}
252-
253-
for (const key in breakpoints) {
254-
const min = getBreakpointMin(breakpoints, key)
255-
const mediaMin = mediaMinWidth(min)
256-
if (!mediaMin) continue
257-
258-
const style = styles[mediaMin]
259-
if (!style) continue
260-
breakpointsStyles[mediaMin] = style
248+
function sortStyles(
249+
styles: Record<string, unknown>,
250+
medias: Record<string, string>,
251+
) {
252+
for (const key in medias) {
253+
const mediaValue = medias[key]
254+
const s = styles[mediaValue]
255+
if (!s) continue
256+
delete styles[mediaValue]
257+
styles[mediaValue] = s
261258
}
262-
263-
return assign(styles, breakpointsStyles)
259+
return styles
264260
}
265261

266262
export function compose(...generators: StyleGenerator[]): StyleGenerator {
@@ -277,17 +273,24 @@ export function compose(...generators: StyleGenerator[]): StyleGenerator {
277273

278274
const generatorsByProp = indexGeneratorsByProp(flatGenerators)
279275

280-
function getStyle(props: IProps) {
276+
function getStyle(props: IProps, sort = true) {
281277
const styles: IStyles = {}
278+
282279
for (const key in props) {
283280
const generator = generatorsByProp[key]
284281
if (generator) {
285-
const style = generator.meta.getStyle(props)
282+
const style = generator.meta.getStyle(props, false)
286283
merge(styles, style)
287284
}
288285
}
289286

290-
return sortStyles(styles, props)
287+
if (!sort) return styles
288+
289+
const medias = getCachedMedias(
290+
props,
291+
getCacheNamespace(props.theme, '__medias'),
292+
)
293+
return sortStyles(styles, medias)
291294
}
292295

293296
const props = flatGenerators.reduce(

packages/system/src/styles/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ describe('#system', () => {
1818
const keys = Object.keys(res)
1919
expect(keys).toEqual([
2020
'display',
21+
'& > :not([hidden]) ~ :not([hidden])',
2122
'@media (min-width: 640px)',
22-
'@media (min-width: 1024px)',
2323
'@media (min-width: 768px)',
24-
'& > :not([hidden]) ~ :not([hidden])',
24+
'@media (min-width: 1024px)',
2525
])
2626
})
2727
})

packages/system/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export type SystemProp<TType, TTheme extends ITheme> =
5959
| BreakpointsProps<TType, TTheme>
6060

6161
export interface StyleGenerator {
62-
(props: IProps): any
62+
(props: IProps, sort?: boolean): any
6363
meta: {
6464
props: string[]
6565
getStyle: StyleGenerator

packages/util/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export const merge = <TObject, TSource>(
102102
// @ts-ignore
103103
if (obj(a[key])) {
104104
// @ts-ignore
105-
a[key] = merge(assign({}, a[key]), b[key])
105+
a[key] = merge(a[key], b[key])
106106
} else {
107107
// @ts-ignore
108108
a[key] = b[key]

0 commit comments

Comments
 (0)