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

[Box] Add sx prop #23053

Merged
merged 74 commits into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
c2d31cc
add emotion peer dependencies
mnajdova Sep 25, 2020
5ae933f
fixed types & tests
mnajdova Sep 25, 2020
18b0668
prettier
mnajdova Sep 25, 2020
f0ef95c
Merge branch 'next' of https://github.com/mui-org/material-ui into next
mnajdova Sep 28, 2020
c7bebb8
Merge branch 'next' of https://github.com/mui-org/material-ui into next
mnajdova Sep 28, 2020
92b2d6e
Merge branch 'next' of https://github.com/mui-org/material-ui into next
mnajdova Sep 28, 2020
13da531
Merge branch 'next' of https://github.com/mui-org/material-ui into next
mnajdova Sep 29, 2020
cf5d9a5
Merge branch 'next' of https://github.com/mui-org/material-ui into next
mnajdova Sep 29, 2020
b8d1291
Merge branch 'next' of https://github.com/mui-org/material-ui into next
mnajdova Sep 30, 2020
a9d8690
Merge branch 'next' of https://github.com/mui-org/material-ui into next
mnajdova Oct 2, 2020
74dc451
wip
mnajdova Oct 2, 2020
688d13a
fixed typo
mnajdova Oct 2, 2020
497830a
Merge branch 'next' of https://github.com/mui-org/material-ui into next
mnajdova Oct 5, 2020
d50ea1e
Merge branch 'next' of https://github.com/mui-org/material-ui into next
mnajdova Oct 5, 2020
a1da207
added cross-env
mnajdova Oct 5, 2020
6ff31f9
yarn lock
mnajdova Oct 5, 2020
9fd7f7b
version
mnajdova Oct 5, 2020
e561135
Merge branch 'feat/benchmark-window-fix' into feat/update-box-to-use-…
mnajdova Oct 5, 2020
2a71a6e
fixes
mnajdova Oct 5, 2020
77d6017
prettier
mnajdova Oct 5, 2020
a3beed3
removed cross-env dependency
mnajdova Oct 5, 2020
0b2d503
removed unused theme
mnajdova Oct 5, 2020
eddfa14
reversed style fn
mnajdova Oct 7, 2020
83e0ea2
sx wip
mnajdova Oct 8, 2020
ff2d4c3
Updated box filterProps
mnajdova Oct 8, 2020
f7a74e9
prettier
mnajdova Oct 8, 2020
bc55cdb
Merge remote-tracking branch 'origin/feat/update-box-to-use-emotion' …
mnajdova Oct 14, 2020
72893a9
merge conflicts
mnajdova Oct 14, 2020
d596058
cleanup
mnajdova Oct 14, 2020
94c905d
Update docs/src/pages/components/slider-styled/ContinuousSlider.js
mnajdova Oct 14, 2020
f179fb9
Update docs/src/pages/components/slider-styled/ContinuousSlider.tsx
mnajdova Oct 14, 2020
0373182
updated scenarios to be in the correct benchmark
mnajdova Oct 14, 2020
6021478
cleaned up benchmark scenarios
mnajdova Oct 14, 2020
5a70cf2
Merge branch 'feat/box-sx-prop' of https://github.com/mnajdova/materi…
mnajdova Oct 14, 2020
92847d7
Merge branch 'next' into feat/box-sx-prop
mnajdova Oct 15, 2020
d438191
fixed typings, updated box page
mnajdova Oct 15, 2020
7bab4bd
moved styleFunction to Box
mnajdova Oct 15, 2020
d966402
Update docs/src/pages/components/box/box.md
mnajdova Oct 15, 2020
76c0f7e
Update packages/material-ui-system/src/palette.js
mnajdova Oct 15, 2020
448bc82
Update packages/material-ui/src/Box/Box.js
mnajdova Oct 15, 2020
a7b690d
Update docs/src/pages/components/box/box.md
mnajdova Oct 15, 2020
59461b3
added support for functions for the values
mnajdova Oct 15, 2020
f6faf73
prettier
mnajdova Oct 15, 2020
c53c4bd
fixed sizing filterProps
mnajdova Oct 15, 2020
ed02098
support sx as function
mnajdova Oct 15, 2020
f1ae038
prettier
mnajdova Oct 15, 2020
d2756c7
implemented breakpoints, fixed types, removed theme function callback
mnajdova Oct 19, 2020
0f64e53
added theme-ui benchmark + sx prop benchmark for the box
mnajdova Oct 19, 2020
b32dd5a
fixed custom breakpoints support
mnajdova Oct 19, 2020
79af939
Update docs/src/pages/components/box/box.md
mnajdova Oct 20, 2020
64174a6
added theme-ui div scenario benchmark
mnajdova Oct 20, 2020
70c1d51
Merge branch 'feat/box-sx-prop' of https://github.com/mnajdova/materi…
mnajdova Oct 20, 2020
502733e
improved examples, added chakra-ui box & added results in readme
mnajdova Oct 20, 2020
49641ac
added tests, fixes
mnajdova Oct 20, 2020
15b118b
breakpoints more tests and fixes
mnajdova Oct 20, 2020
b386b29
improved breakpoints checks
mnajdova Oct 20, 2020
e200326
added more examples
mnajdova Oct 20, 2020
e12c35f
Added Box examples
mnajdova Oct 20, 2020
d32da87
temporarly removed dependency
mnajdova Oct 20, 2020
2d4c5c9
Merge branch 'next' into feat/box-sx-prop
mnajdova Oct 20, 2020
80682d1
reverted yarn.lock changes
mnajdova Oct 20, 2020
6533c27
reverted benchmark changes
mnajdova Oct 20, 2020
5df2f8c
Merge branch 'next' into feat/box-sx-prop
mnajdova Oct 20, 2020
3767add
Update benchmark/browser/README.md
mnajdova Oct 20, 2020
dce99bc
reverted some changes
mnajdova Oct 20, 2020
9671da1
Merge branch 'feat/box-sx-prop' of https://github.com/mnajdova/materi…
mnajdova Oct 20, 2020
b0cc366
fixed lint & tests
mnajdova Oct 20, 2020
0b1ecd5
Merge branch 'next' into feat/box-sx-prop
mnajdova Oct 21, 2020
9e3ad18
fixed breakpoints merging
mnajdova Oct 21, 2020
2e557d4
prettier
mnajdova Oct 21, 2020
f00fd05
lint
mnajdova Oct 21, 2020
43b629d
Update benchmark/browser/scenarios/sx-prop-box-material-ui/index.js
mnajdova Oct 21, 2020
0d57b2d
Update benchmark/browser/scripts/benchmark.js
mnajdova Oct 21, 2020
861d4f6
removed render function code snippet in favor of example
mnajdova Oct 21, 2020
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
40 changes: 39 additions & 1 deletion docs/src/pages/components/slider-styled/ContinuousSlider.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function ContinuousSlider() {
};

return (
<Box width={200}>
<Box width={300}>
mnajdova marked this conversation as resolved.
Show resolved Hide resolved
<Typography id="continuous-slider" gutterBottom>
Volume
</Typography>
Expand All @@ -37,6 +37,44 @@ export default function ContinuousSlider() {
Disabled slider
</Typography>
<Slider disabled defaultValue={30} aria-labelledby="disabled-slider" />
<Typography gutterBottom>Regular Box</Typography>
<Box
component="span"
sx={{
p: 1,
bgcolor: 'primary.main',
color: 'green',
':hover': {
backgroundColor: 'secondary.main',
border: '1px solid #232323',
},
}}
>
<Slider
value={value}
onChange={handleChange}
aria-labelledby="continuous-slider"
/>
</Box>
<Typography gutterBottom>Cloned Box</Typography>
<Box component="span" color="secondary.main" clone>
<Slider
value={value}
onChange={handleChange}
aria-labelledby="continuous-slider"
/>
</Box>
<Typography gutterBottom>Children as fn Box</Typography>
<Box color="secondary.main">
{({ className }) => (
<Slider
value={value}
onChange={handleChange}
aria-labelledby="continuous-slider"
className={className}
/>
)}
</Box>
</Box>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default function ContinuousSlider() {
};

return (
<Box width={200}>
<Box width={300}>
mnajdova marked this conversation as resolved.
Show resolved Hide resolved
<Typography id="continuous-slider" gutterBottom>
Volume
</Typography>
Expand Down
13 changes: 6 additions & 7 deletions packages/material-ui-benchmark/src/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import ReactDOMServer from 'react-dom/server';
import styledEmotion from '@emotion/styled';
import { ThemeProvider as EmotionTheme } from 'emotion-theming';
import { space, color, fontFamily, fontSize, compose as compose2 } from 'styled-system';
import { spacing, palette, typography, compose } from '@material-ui/system';
import { spacing, palette, typography, compose, styleFunctionInversed } from '@material-ui/system';
import { createMuiTheme } from '@material-ui/core/styles';
import { styleFunction } from '@material-ui/core/Box';
import { styled, ThemeProvider as StylesThemeProvider } from '@material-ui/styles';
import { ThemeProvider as StylesThemeProvider } from '@material-ui/styles';
import BoxStyles from '@material-ui/core/Box';
import styledComponents, {
ThemeProvider as StyledComponentsThemeProvider,
} from 'styled-components';
Expand All @@ -23,10 +23,9 @@ Benchmark.options.minSamples = 100;
const materialSystem = compose(palette, spacing, typography);
const styledSystem = compose2(color, space, fontFamily, fontSize);

const BoxStyles = styled('div')(styleFunction);
const BoxStyleComponents = styledComponents('div')(styleFunction);
const BoxStyleComponents = styledComponents('div')(styleFunctionInversed);
const NakedStyleComponents = styledComponents('div')(spacing);
const BoxEmotion = styledEmotion('div')(styleFunction);
const BoxEmotion = styledEmotion('div')(styleFunctionInversed);

const BoxMaterialSystem = styledComponents('div')(materialSystem);
const BoxStyledSystem = styledComponents('div')(styledSystem);
Expand Down Expand Up @@ -89,7 +88,7 @@ suite
})
// ---
.add('@material-ui/core all-inclusive', () => {
styleFunction({
styleFunctionInversed({
theme: materialSystemTheme,
color: 'primary.main',
bgcolor: 'background.paper',
Expand Down
2 changes: 2 additions & 0 deletions packages/material-ui-system/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,5 @@ export const visuallyHidden: React.CSSProperties;

// utils
type Omit<T, K> = Pick<T, Exclude<keyof T, K>>;

export const styleFunctionInversed: (props: any) => object;
1 change: 1 addition & 0 deletions packages/material-ui-system/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ export { default as style } from './style';
export { default as typography } from './typography';
export * from './typography';
export { default as visuallyHidden } from './visuallyHidden';
export { default as styleFunctionInversed } from './styleFunctionInversed';
8 changes: 7 additions & 1 deletion packages/material-ui-system/src/palette.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ export const bgcolor = style({
themeKey: 'palette',
});

const palette = compose(color, bgcolor);
export const backgroundColor = style({
prop: 'backgroundColor',
cssProperty: 'backgroundColor',
mnajdova marked this conversation as resolved.
Show resolved Hide resolved
themeKey: 'palette',
});

const palette = compose(color, bgcolor, backgroundColor);

export default palette;
118 changes: 118 additions & 0 deletions packages/material-ui-system/src/styleFunctionInversed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import borders from './borders';
import display from './display';
import flexbox from './flexbox';
import grid from './grid';
import positions from './positions';
import palette from './palette';
import shadows from './shadows';
import sizing from './sizing';
import spacing from './spacing';
import typography from './typography';

const filterProps = [
...borders.filterProps,
...display.filterProps,
...flexbox.filterProps,
...grid.filterProps,
...positions.filterProps,
...palette.filterProps,
...shadows.filterProps,
...spacing.filterProps,
...typography.filterProps,
'sx',
];

const getThemeValue = (prop, value, theme) => {
const inputProps = {
[prop]: value,
theme,
};

if (borders.filterProps.indexOf(prop) !== -1) {
return borders(inputProps);
}
if (display.filterProps.indexOf(prop) !== -1) {
return display(inputProps);
}
if (flexbox.filterProps.indexOf(prop) !== -1) {
return flexbox(inputProps);
}
if (grid.filterProps.indexOf(prop) !== -1) {
return grid(inputProps);
}
if (positions.filterProps.indexOf(prop) !== -1) {
return positions(inputProps);
}
if (palette.filterProps.indexOf(prop) !== -1) {
return palette(inputProps);
}
if (shadows.filterProps.indexOf(prop) !== -1) {
return shadows(inputProps);
}
if (sizing.filterProps.indexOf(prop) !== -1) {
return sizing(inputProps);
}
if (spacing.filterProps.indexOf(prop) !== -1) {
return spacing(inputProps);
}
if (typography.filterProps.indexOf(prop) !== -1) {
return typography(inputProps);
}
return { [prop]: value };
};

const traverseSx = (styles, theme) => {
if (!styles) return null;

if (typeof styles !== 'object') {
// value
return styles;
}

let css = {};

Object.keys(styles).forEach((styleKey) => {
if (typeof styles[styleKey] === 'object') {
if (filterProps.indexOf(styleKey) !== -1) {
css = {
...css,
...getThemeValue(styleKey, value, theme),
};
} else {
const transformedValue = traverseSx(styles[styleKey], theme);
css[styleKey] = transformedValue;
}
} else {
const value = styles[styleKey];
css = {
...css,
...getThemeValue(styleKey, value, theme),
};
}
});

return css;
};

const styleFunctionInversed = (props) => {
let result = {};
Object.keys(props).forEach((prop) => {
if (filterProps.indexOf(prop) !== -1) {
result = {
...result,
...getThemeValue(prop, props[prop], props.theme),
};
}
});

let sxValue = traverseSx(props.sx, props.theme);

return {
...result,
...sxValue,
};
};

styleFunctionInversed.filterProps = filterProps;

export default styleFunctionInversed;
38 changes: 6 additions & 32 deletions packages/material-ui/src/Box/Box.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,10 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import {
borders,
compose,
display,
flexbox,
grid,
palette,
positions,
shadows,
sizing,
spacing,
typography,
css,
} from '@material-ui/system';
import React from 'react';
mnajdova marked this conversation as resolved.
Show resolved Hide resolved
import * as PropTypes from 'prop-types';
import clsx from 'clsx';
import { styleFunctionInversed } from '@material-ui/system';
import styled from '../styles/experimentalStyled';

export const styleFunction = css(
compose(
borders,
display,
flexbox,
grid,
positions,
palette,
shadows,
sizing,
spacing,
typography,
),
);
export const styleFunction = styleFunctionInversed;

function omit(input, fields) {
const output = {};
Expand Down Expand Up @@ -70,15 +44,15 @@ const BoxRoot = React.forwardRef(function StyledComponent(props, ref) {
);
});

const shouldForwardProp = (prop) => styleFunction.filterProps.indexOf(prop) === -1;

BoxRoot.propTypes = {
children: PropTypes.node,
className: PropTypes.string,
clone: PropTypes.bool,
component: PropTypes.elementType,
};

const shouldForwardProp = (prop) => styleFunction.filterProps.indexOf(prop) === -1;

/**
* @ignore - do not document.
*/
Expand Down