Skip to content

Commit ed2ade1

Browse files
merceyzeps1lon
authored andcommitted
[styles] Support augmenting CSS properties (#16333)
* [styles] Add flip to CSS properties * guides/right-to-left/RtlOptOut * Remove flip, augment it instead * Update RtlOptOut.tsx
1 parent dc981b6 commit ed2ade1

File tree

4 files changed

+55
-17
lines changed

4 files changed

+55
-17
lines changed
Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import React from 'react';
2-
import PropTypes from 'prop-types';
3-
import { withStyles } from '@material-ui/core/styles';
2+
import { makeStyles } from '@material-ui/core/styles';
43

5-
const styles = theme => ({
4+
const useStyles = makeStyles(theme => ({
65
root: {
76
width: '100%',
87
marginTop: theme.spacing(4),
@@ -15,10 +14,10 @@ const styles = theme => ({
1514
flip: false,
1615
textAlign: 'right',
1716
},
18-
});
17+
}));
1918

20-
function RtlOptOut(props) {
21-
const { classes } = props;
19+
export default function RtlOptOut() {
20+
const classes = useStyles();
2221

2322
return (
2423
<div className={classes.root}>
@@ -27,9 +26,3 @@ function RtlOptOut(props) {
2726
</div>
2827
);
2928
}
30-
31-
RtlOptOut.propTypes = {
32-
classes: PropTypes.object.isRequired,
33-
};
34-
35-
export default withStyles(styles)(RtlOptOut);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import React from 'react';
2+
import { makeStyles, createStyles, Theme } from '@material-ui/core/styles';
3+
4+
declare module '@material-ui/core/styles/withStyles' {
5+
// Augment the BaseCSSProperties so that we can control jss-rtl
6+
interface BaseCSSProperties {
7+
/**
8+
* Used to control if the rule-set should be affected by rtl transformation
9+
*/
10+
flip?: boolean;
11+
}
12+
}
13+
14+
const useStyles = makeStyles((theme: Theme) =>
15+
createStyles({
16+
root: {
17+
width: '100%',
18+
marginTop: theme.spacing(4),
19+
marginRight: theme.spacing(2),
20+
},
21+
affected: {
22+
textAlign: 'right',
23+
},
24+
unaffected: {
25+
flip: false,
26+
textAlign: 'right',
27+
},
28+
}),
29+
);
30+
31+
export default function RtlOptOut() {
32+
const classes = useStyles();
33+
34+
return (
35+
<div className={classes.root}>
36+
<div className={classes.affected}>Affected</div>
37+
<div className={classes.unaffected}>Unaffected</div>
38+
</div>
39+
);
40+
}

packages/material-ui-styles/src/withStyles/withStyles.d.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@ import { PropInjector, CoerceEmptyInterface, IsEmptyInterface } from '@material-
33
import * as CSS from 'csstype';
44
import * as JSS from 'jss';
55

6-
export interface CSSProperties extends CSS.Properties<number | string> {
6+
/**
7+
* Allows the user to augment the properties available
8+
*/
9+
export interface BaseCSSProperties extends CSS.Properties<number | string> {}
10+
11+
export interface CSSProperties extends BaseCSSProperties {
712
// Allow pseudo selectors and media queries
8-
[k: string]: CSS.Properties<number | string>[keyof CSS.Properties] | CSSProperties;
13+
[k: string]: BaseCSSProperties[keyof BaseCSSProperties] | CSSProperties;
914
}
1015

1116
export type BaseCreateCSSProperties<Props extends object = {}> = {
12-
[P in keyof CSS.Properties<number | string>]:
13-
| CSS.Properties<number | string>[P]
14-
| ((props: Props) => CSS.Properties<number | string>[P])
17+
[P in keyof BaseCSSProperties]: BaseCSSProperties[P] | ((props: Props) => BaseCSSProperties[P])
1518
};
1619

1720
export interface CreateCSSProperties<Props extends object = {}>

packages/material-ui/src/styles/withStyles.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
StyleRulesCallback,
1111
Styles,
1212
ClassKeyOfStyles,
13+
BaseCSSProperties,
1314
} from '@material-ui/styles/withStyles';
1415

1516
export {
@@ -20,6 +21,7 @@ export {
2021
Styles,
2122
WithStylesOptions,
2223
StyleRulesCallback,
24+
BaseCSSProperties,
2325
};
2426

2527
/**

0 commit comments

Comments
 (0)