Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[system] Fix duplicated styles in Box #33774

Merged
merged 2 commits into from Aug 8, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/mui-system/src/createBox.js
Expand Up @@ -11,7 +11,10 @@ export default function createBox(options = {}) {
generateClassName,
styleFunctionSx = defaultStyleFunctionSx,
} = options;
const BoxRoot = styled('div')(styleFunctionSx);
const BoxRoot = styled('div', {
shouldForwardProp: (prop) =>
prop !== 'ownerState' && prop !== 'theme' && prop !== 'sx' && prop !== 'as',
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved
})(styleFunctionSx);

const Box = React.forwardRef(function Box(inProps, ref) {
const theme = useTheme(defaultTheme);
Expand Down
25 changes: 25 additions & 0 deletions packages/mui-system/src/createBox.test.js
@@ -1,5 +1,6 @@
import * as React from 'react';
import { expect } from 'chai';
import { spy } from 'sinon';
import { createRenderer } from 'test/utils';
import { createBox, ThemeProvider } from '@mui/system';

Expand Down Expand Up @@ -54,4 +55,28 @@ describe('createBox', () => {
const { container } = render(<Box />);
expect(container.firstChild).to.have.class('Box');
});

it('should accept sx prop', () => {
const Box = createBox();
const { container } = render(<Box sx={{ color: 'rgb(255, 0, 0)' }}>Content</Box>);
expect(container.firstChild).toHaveComputedStyle({ color: 'rgb(255, 0, 0)' });
});

it('should call styleFunctionSx once', () => {
const Box = createBox();
const sypSx = spy();
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved
render(<Box sx={sypSx}>Content</Box>);
expect(sypSx.callCount).to.equal(2); // React 18 renders twice in strict mode.
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved
});

it('should still call styleFunctionSx once', () => {
const Box = createBox();
const sypSx = spy();
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved
render(
<Box component={Box} sx={sypSx}>
Content
</Box>,
);
expect(sypSx.callCount).to.equal(2); // React 18 renders twice in strict mode.
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved
});
});