Skip to content

Commit 12cf60c

Browse files
committed
refactor: improve external style handling in buildStyleArray
- Extract recursive style flattening logic into pushExternalStyle helper - Add StyleValueLike type for better type safety with nested arrays - Simplify external style processing with consistent expansion - Maintain same functionality with cleaner, more maintainable code
1 parent 6dcaa5a commit 12cf60c

1 file changed

Lines changed: 19 additions & 16 deletions

File tree

packages/kstyled/src/utils/style-merger.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import type { StyleMetadata, StyleArray, StyleValue, StyleObject, PropsWithTheme
22
import type { CompiledStyles } from '../types';
33
import { normalizeStyleValue } from '../css-runtime-parser';
44

5+
type StyleValueLike = StyleValue | ReadonlyArray<StyleValueLike>;
6+
57
/**
68
* Expand shorthand padding/margin properties to longhand
79
* This is necessary because base styles use longhand properties (paddingTop, paddingRight, etc.)
@@ -92,24 +94,25 @@ export function buildStyleArray(
9294

9395
// Add external styles last (highest priority)
9496
// IMPORTANT: Expand shorthand properties to match base style specificity
95-
if (externalStyle) {
96-
if (Array.isArray(externalStyle)) {
97-
// Handle nested arrays from StyleProp
98-
for (const style of externalStyle) {
99-
if (style) {
100-
// Expand shorthand properties for proper override
101-
const expanded = expandShorthandProperties(style);
102-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
103-
styles.push(expanded as any);
104-
}
97+
const pushExternalStyle = (styleValue: StyleValueLike): void => {
98+
if (!styleValue) {
99+
return;
100+
}
101+
102+
if (Array.isArray(styleValue)) {
103+
for (const nestedStyle of styleValue) {
104+
pushExternalStyle(nestedStyle);
105105
}
106-
} else {
107-
// Expand shorthand properties for proper override
108-
const expanded = expandShorthandProperties(externalStyle);
109-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
110-
styles.push(expanded as any);
106+
return;
111107
}
112-
}
108+
109+
// Expand shorthand properties for proper override
110+
const expanded = expandShorthandProperties(styleValue);
111+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
112+
styles.push(expanded as any);
113+
};
114+
115+
pushExternalStyle(externalStyle);
113116

114117
return styles;
115118
}

0 commit comments

Comments
 (0)