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

[Slider] Add sx prop in SliderStyled #23205

Merged
merged 89 commits into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from 77 commits
Commits
Show all changes
89 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
85856bc
init
mnajdova Oct 22, 2020
6791fa7
Merge branch 'next' into feat/slider-add-sx-prop
mnajdova Oct 22, 2020
8457392
added tests, updated tests & prettier
mnajdova Oct 22, 2020
5f8c013
lint
mnajdova Oct 22, 2020
4881d08
Update packages/material-ui-lab/src/SliderUnstyled/SliderUnstyled.d.ts
mnajdova Oct 22, 2020
c382d44
refactored and added TODO
mnajdova Oct 22, 2020
cac4bc3
Merge branch 'feat/slider-add-sx-prop' of https://github.com/mnajdova…
mnajdova Oct 22, 2020
3b36beb
Fixed tests
mnajdova Oct 22, 2020
509fb99
fixed this undefined
mnajdova Oct 22, 2020
d7da88b
fixed test
mnajdova Oct 22, 2020
99b994d
prettier
mnajdova Oct 22, 2020
61ec4ec
Update packages/material-ui/src/styles/experimentalStyled.test.js
mnajdova Oct 22, 2020
ec116cd
reverted sx examples
mnajdova Oct 22, 2020
3001e90
refactored tests to use container.firstChild
mnajdova Oct 23, 2020
af120ff
prettier
mnajdova Oct 23, 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
1 change: 1 addition & 0 deletions docs/pages/api-docs/slider-styled.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ You can learn more about the difference by [reading this guide](/guides/minimizi
| <span class="prop-name">orientation</span> | <span class="prop-type">'horizontal'<br>&#124;&nbsp;'vertical'</span> | | The slider orientation. |
| <span class="prop-name">scale</span> | <span class="prop-type">func</span> | | A transformation function, to change the scale of the slider. |
| <span class="prop-name">step</span> | <span class="prop-type">number</span> | | The granularity with which the slider can step through values. (A "discrete" slider.) The `min` prop serves as the origin for the valid values. We recommend (max - min) to be evenly divisible by the step.<br>When step is `null`, the thumb can only be slid onto marks provided with the `marks` prop. |
| <span class="prop-name">sx</span> | <span class="prop-type">object</span> | | The system prop that allows defining system overrides as well as additional CSS styles. |
| <span class="prop-name">track</span> | <span class="prop-type">'inverted'<br>&#124;&nbsp;'normal'<br>&#124;&nbsp;false</span> | | The track presentation:<br>- `normal` the track will render a bar representing the slider value. - `inverted` the track will render a bar representing the remaining slider value. - `false` the track will render without a bar. |
| <span class="prop-name">value</span> | <span class="prop-type">Array&lt;number&gt;<br>&#124;&nbsp;number</span> | | The value of the slider. For ranged sliders, provide an array with two values. |
| <span class="prop-name">valueLabelDisplay</span> | <span class="prop-type">'auto'<br>&#124;&nbsp;'off'<br>&#124;&nbsp;'on'</span> | | Controls when the value label is displayed:<br>- `auto` the value label will display when the thumb is hovered or focused. - `on` will display persistently. - `off` will never display. |
Expand Down
9 changes: 9 additions & 0 deletions docs/src/pages/components/slider-styled/ContinuousSlider.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ export default function ContinuousSlider() {
Disabled slider
</Typography>
<Slider disabled defaultValue={30} aria-labelledby="disabled-slider" />
<Slider
mnajdova marked this conversation as resolved.
Show resolved Hide resolved
defaultValue={30}
aria-labelledby="disabled-slider"
sx={{
m: 2,
color: 'secondary.main',
bgcolor: 'primary.light',
}}
/>
</Box>
);
}
9 changes: 9 additions & 0 deletions docs/src/pages/components/slider-styled/ContinuousSlider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ export default function ContinuousSlider() {
Disabled slider
</Typography>
<Slider disabled defaultValue={30} aria-labelledby="disabled-slider" />
<Slider
oliviertassinari marked this conversation as resolved.
Show resolved Hide resolved
defaultValue={30}
aria-labelledby="disabled-slider"
sx={{
m: 2,
color: 'secondary.main',
bgcolor: 'primary.light',
}}
/>
</Box>
);
}
36 changes: 23 additions & 13 deletions packages/material-ui-lab/src/SliderStyled/SliderStyled.d.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
import * as React from 'react';
import { OverridableComponent, OverrideProps } from '@material-ui/core/OverridableComponent';
import { SliderTypeMap } from '../SliderUnstyled';
import { SxProps } from '@material-ui/core/Box';
import { ExtendSliderUnstyledTypeMap, ExtendSliderUnstyled } from '../SliderUnstyled';

export type SliderProps<
D extends React.ElementType = SliderTypeMap['defaultComponent'],
export type SliderStyledTypeMap<
D extends React.ElementType = 'span',
P = {}
> = OverrideProps<SliderTypeMap<P, D>, D>;
> = ExtendSliderUnstyledTypeMap<{
props: P & {
/**
* The system prop that allows defining system overrides as well as additional CSS styles.
*/
sx?: SxProps;
};
defaultComponent: D;
}>;

type SliderRootProps = NonNullable<SliderProps['componentsProps']>['root'];
type SliderMarkProps = NonNullable<SliderProps['componentsProps']>['mark'];
type SliderMarkLabelProps = NonNullable<SliderProps['componentsProps']>['markLabel'];
type SliderRailProps = NonNullable<SliderProps['componentsProps']>['rail'];
type SliderTrackProps = NonNullable<SliderProps['componentsProps']>['track'];
type SliderThumbProps = NonNullable<SliderProps['componentsProps']>['thumb'];
type SliderValueLabel = NonNullable<SliderProps['componentsProps']>['valueLabel'];
type SliderRootProps = NonNullable<SliderStyledTypeMap['props']['componentsProps']>['root'];
type SliderMarkProps = NonNullable<SliderStyledTypeMap['props']['componentsProps']>['mark'];
type SliderMarkLabelProps = NonNullable<
SliderStyledTypeMap['props']['componentsProps']
>['markLabel'];
type SliderRailProps = NonNullable<SliderStyledTypeMap['props']['componentsProps']>['rail'];
type SliderTrackProps = NonNullable<SliderStyledTypeMap['props']['componentsProps']>['track'];
type SliderThumbProps = NonNullable<SliderStyledTypeMap['props']['componentsProps']>['thumb'];
type SliderValueLabel = NonNullable<SliderStyledTypeMap['props']['componentsProps']>['valueLabel'];

export const SliderRoot: React.FC<SliderRootProps>;
export const SliderMark: React.FC<SliderMarkProps>;
Expand All @@ -33,6 +43,6 @@ export const SliderValueLabel: React.FC<SliderValueLabel>;
*
* - [SliderStyled API](https://material-ui.com/api/slider-styled/)
*/
declare const Slider: OverridableComponent<SliderTypeMap>;
declare const Slider: ExtendSliderUnstyled<SliderStyledTypeMap>;

export default Slider;
4 changes: 4 additions & 0 deletions packages/material-ui-lab/src/SliderStyled/SliderStyled.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,10 @@ Slider.propTypes = {
* @default 1
*/
step: PropTypes.number,
/**
* The system prop that allows defining system overrides as well as additional CSS styles.
*/
sx: PropTypes.object,
/**
* The track presentation:
*
Expand Down
14 changes: 13 additions & 1 deletion packages/material-ui-lab/src/SliderUnstyled/SliderUnstyled.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OverridableComponent } from '@material-ui/core/OverridableComponent';
import { OverridableComponent, OverridableTypeMap } from '@material-ui/core/OverridableComponent';

export interface Mark {
value: number;
Expand Down Expand Up @@ -195,6 +195,18 @@ export interface SliderTypeMap<P = {}, D extends React.ElementType = 'span'> {
defaultComponent: D;
}

/**
* utility to create component types that inherit props from SliderUnstyled.
mnajdova marked this conversation as resolved.
Show resolved Hide resolved
*/
export interface ExtendSliderUnstyledTypeMap<M extends OverridableTypeMap> {
props: M['props'] & Omit<SliderTypeMap['props'], 'classes'>;
defaultComponent: M['defaultComponent'];
}

export type ExtendSliderUnstyled<M extends OverridableTypeMap> = OverridableComponent<
ExtendSliderUnstyledTypeMap<M>
>;

/**
*
* Demos:
Expand Down
8 changes: 6 additions & 2 deletions packages/material-ui-styled-engine-sc/src/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import scStyled from 'styled-components';

export default function styled(tag, options) {
let scStyledPrepared = scStyled(tag);

if (options) {
return scStyled(tag).withConfig({
scStyledPrepared = scStyled(tag).withConfig({
mnajdova marked this conversation as resolved.
Show resolved Hide resolved
displayName: options.label,
shouldForwardProp: options.shouldForwardProp,
});
}

return scStyled(tag);
return (styles) => {
oliviertassinari marked this conversation as resolved.
Show resolved Hide resolved
return scStyledPrepared(...styles);
};
}

export { ThemeContext } from 'styled-components';
3 changes: 2 additions & 1 deletion packages/material-ui/src/Box/Box.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export type BoxStyleFunction = ComposedStyleFunction<
type SystemProps = PropsFor<BoxStyleFunction>;
type ElementProps = Omit<React.HTMLAttributes<HTMLElement>, keyof SystemProps>;
type SxPropsValue = Omit<CSSObject, keyof SystemProps> & SystemProps;
type SxProps = {

export type SxProps = {
[Name in keyof SxPropsValue]?:
| SxPropsValue[Name]
| ((theme: Theme) => CSSObject | SxPropsValue[Name])
Expand Down
2 changes: 1 addition & 1 deletion packages/material-ui/src/Box/styleFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const getThemeValue = (prop, value, theme) => {
return { [prop]: value };
};

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

if (typeof styles === 'function') {
Expand Down
10 changes: 8 additions & 2 deletions packages/material-ui/src/styles/experimentalStyled.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import styled from '@material-ui/styled-engine';
import { propsToClassKey } from '@material-ui/styles';
import { styleFunctionSx } from '../Box/styleFunction';
import defaultTheme from './defaultTheme';

function isEmpty(obj) {
Expand Down Expand Up @@ -58,7 +59,7 @@ const variantsResolver = (props, styles, theme, name) => {
return variantsStyles;
};

const shouldForwardProp = (prop) => prop !== 'styleProps' && prop !== 'theme';
const shouldForwardProp = (prop) => prop !== 'styleProps' && prop !== 'theme' && prop !== 'sx';

const experimentalStyled = (tag, options, muiOptions = {}) => {
const name = muiOptions.muiName;
Expand All @@ -85,7 +86,12 @@ const experimentalStyled = (tag, options, muiOptions = {}) => {
});
}

return defaultStyledResolver(...stylesWithDefaultTheme);
stylesWithDefaultTheme.push((props) => {
const theme = isEmpty(props.theme) ? defaultTheme : props.theme;
return styleFunctionSx(props.sx, theme);
});

return defaultStyledResolver(stylesWithDefaultTheme);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Necessary for fixing You have illegal escape sequence in your template literal Spreading is dropped. This will be revisit separately after the system is implemented, but this should help at least with eliminating the warning.

};
return muiStyledResolver;
};
Expand Down
32 changes: 19 additions & 13 deletions packages/material-ui/src/styles/experimentalStyled.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,14 @@ describe('experimentalStyled', () => {
});

// FIXME: Should not error in DEV
expect(() => {
Test = styled(
'div',
{ shouldForwardProp: (prop) => prop !== 'variant' && prop !== 'size' },
{ muiName: 'MuiTest', overridesResolver: testOverridesResolver },
)`
width: 200px;
height: 300px;
`;
}).toErrorDev([
'You have illegal escape sequence in your template literal',
'You have illegal escape sequence in your template literal',
]);
Test = styled(
'div',
{ shouldForwardProp: (prop) => prop !== 'variant' && prop !== 'size' },
{ muiName: 'MuiTest', overridesResolver: testOverridesResolver },
)`
width: 200px;
height: 300px;
`;
});

it('should work with specified muiOptions', () => {
Expand Down Expand Up @@ -178,5 +173,16 @@ describe('experimentalStyled', () => {
height: '400px',
});
});

it('should resolve the sx prop', () => {
render(
<Test data-testid="component" sx={{ m: 1 }}>
Test
</Test>,
),
expect(screen.getByTestId('component')).toHaveComputedStyle({
margin: '8px',
});
});
});
});