Skip to content

Commit

Permalink
feat: Add support for component style overrides via themes.
Browse files Browse the repository at this point in the history
  • Loading branch information
kuakman committed Jan 3, 2025
1 parent 0ae8bd2 commit 1b9ccf8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/clean-wolves-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@suid/system": patch
---

Add support for component style overrides via themes
29 changes: 25 additions & 4 deletions packages/system/src/createStyled.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,22 @@ export const skipProps: (keyof ComponentProps<any, any>)[] = [
"as",
];

function resolveStyles<T extends Theme<any>, P, O>(
function getStyleOverrides<N>(name: N, theme: Theme<any>): any | undefined {
return theme.components?.[name]?.styleOverrides;
}

function resolveStyles<T extends Theme<any>, P, O, N extends string = string>(
useTheme: () => T,
className: string,
styles: Style<T, P, O>[],
inProps: ComponentProps<T, O>
inProps: ComponentProps<T, O>,
options: StyledOptions<N> = {}
) {
return createMemo(() => {
const theme = useTheme();
const styleOverrides = getStyleOverrides(options.name, theme);
const ownerState = inProps.ownerState;
return styles.reduce((result, style) => {
const baseStyles = styles.reduce((result, style) => {
let styledProps: StyledProps | false | undefined;
if (typeof style === "function") {
styledProps = style({
Expand All @@ -94,6 +100,20 @@ function resolveStyles<T extends Theme<any>, P, O>(
);
return result;
}, [] as StyledProps[]);
let resolvedStyleOverrides: any[] = [];
if (
options.overridesResolver &&
typeof options.overridesResolver === "function" &&
styleOverrides
) {
resolvedStyleOverrides = options
.overridesResolver(inProps, styleOverrides)
.map((styleProp) =>
styleProp ? resolveStyledProps(styleProp as StyledProps, {}) : null
)
.filter(Boolean);
}
return baseStyles.concat(resolvedStyleOverrides);
});
}

Expand Down Expand Up @@ -185,7 +205,8 @@ function createStyled<
$useTheme,
cssClassName,
styles,
inProps
inProps,
options
);

const inSx = () => {
Expand Down

0 comments on commit 1b9ccf8

Please sign in to comment.