Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/mui-system/src/createStyled.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ function defaultOverridesResolver(slot) {

function processStyleArg(callableStyle, { ownerState, ...props }) {
const resolvedStylesArg =
typeof callableStyle === 'function' ? callableStyle({ ownerState, ...props }) : callableStyle;
typeof callableStyle === 'function'
? callableStyle({ ownerState, ...props, ...ownerState })
: callableStyle;

if (Array.isArray(resolvedStylesArg)) {
return resolvedStylesArg.flatMap((resolvedStyle) =>
Expand All @@ -66,7 +68,7 @@ function processStyleArg(callableStyle, { ownerState, ...props }) {
variants.forEach((variant) => {
let isMatch = true;
if (typeof variant.props === 'function') {
isMatch = variant.props({ ownerState, ...props });
isMatch = variant.props({ ownerState, ...props, ...ownerState });
} else {
Object.keys(variant.props).forEach((key) => {
if (ownerState?.[key] !== variant.props[key] && props[key] !== variant.props[key]) {
Expand All @@ -80,7 +82,7 @@ function processStyleArg(callableStyle, { ownerState, ...props }) {
}
result.push(
typeof variant.style === 'function'
? variant.style({ ownerState, ...props })
? variant.style({ ownerState, ...props, ...ownerState })
: variant.style,
);
}
Expand Down
32 changes: 32 additions & 0 deletions test/integration/mui-system/createStyled.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,38 @@ describe('createStyled', () => {
expect(getByTestId('red')).toHaveComputedStyle({ backgroundColor: 'rgb(255, 0, 0)' });
});

it('should merge props and ownerState in props callback', () => {
const styled = createStyled({
defaultTheme: {
colors: { blue: 'rgb(0, 0, 255)', red: 'rgb(255, 0, 0)', green: 'rgb(0, 255, 0)' },
},
});

const Test = styled('div')(({ theme, color }) => ({
variants: [
{
props: (props) => props.color === 'green' || props.color === 'red',
style: {
backgroundColor: theme.colors[color],
},
},
],
}));

const { getByTestId } = render(
<React.Fragment>
<Test data-testid="red" ownerState={{ color: 'red' }}>
Red
</Test>
<Test data-testid="green" ownerState={{ color: 'green' }}>
Green
</Test>
</React.Fragment>,
);
expect(getByTestId('green')).toHaveComputedStyle({ backgroundColor: 'rgb(0, 255, 0)' });
expect(getByTestId('red')).toHaveComputedStyle({ backgroundColor: 'rgb(255, 0, 0)' });
});

it('should accept variants in arrays', () => {
const styled = createStyled({ defaultTheme: { colors: { blue: 'rgb(0, 0, 255)' } } });

Expand Down