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

[styles] Support augmenting CSS properties #16333

Merged
merged 4 commits into from
Jul 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 5 additions & 12 deletions docs/src/pages/guides/right-to-left/RtlOptOut.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import { makeStyles } from '@material-ui/core/styles';

const styles = theme => ({
const useStyles = makeStyles(theme => ({
root: {
width: '100%',
marginTop: theme.spacing(4),
Expand All @@ -15,10 +14,10 @@ const styles = theme => ({
flip: false,
textAlign: 'right',
},
});
}));

function RtlOptOut(props) {
const { classes } = props;
export default function RtlOptOut() {
const classes = useStyles();

return (
<div className={classes.root}>
Expand All @@ -27,9 +26,3 @@ function RtlOptOut(props) {
</div>
);
}

RtlOptOut.propTypes = {
classes: PropTypes.object.isRequired,
};

export default withStyles(styles)(RtlOptOut);
40 changes: 40 additions & 0 deletions docs/src/pages/guides/right-to-left/RtlOptOut.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React from 'react';
import { makeStyles, createStyles, Theme } from '@material-ui/core/styles';

declare module '@material-ui/core/styles/withStyles' {
merceyz marked this conversation as resolved.
Show resolved Hide resolved
// Augment the BaseCSSProperties so that we can control jss-rtl
interface BaseCSSProperties {
/**
* Used to control if the rule-set should be affected by rtl transformation
*/
flip?: boolean;
}
}

const useStyles = makeStyles((theme: Theme) =>
createStyles({
root: {
width: '100%',
marginTop: theme.spacing(4),
marginRight: theme.spacing(2),
},
affected: {
textAlign: 'right',
},
unaffected: {
flip: false,
textAlign: 'right',
},
}),
);

export default function RtlOptOut() {
const classes = useStyles();

return (
<div className={classes.root}>
<div className={classes.affected}>Affected</div>
<div className={classes.unaffected}>Unaffected</div>
</div>
);
}
13 changes: 8 additions & 5 deletions packages/material-ui-styles/src/withStyles/withStyles.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ import { PropInjector, CoerceEmptyInterface, IsEmptyInterface } from '@material-
import * as CSS from 'csstype';
import * as JSS from 'jss';

export interface CSSProperties extends CSS.Properties<number | string> {
/**
* Allows the user to augment the properties available
*/
export interface BaseCSSProperties extends CSS.Properties<number | string> {}

export interface CSSProperties extends BaseCSSProperties {
// Allow pseudo selectors and media queries
[k: string]: CSS.Properties<number | string>[keyof CSS.Properties] | CSSProperties;
[k: string]: BaseCSSProperties[keyof BaseCSSProperties] | CSSProperties;
}

export type BaseCreateCSSProperties<Props extends object = {}> = {
[P in keyof CSS.Properties<number | string>]:
| CSS.Properties<number | string>[P]
| ((props: Props) => CSS.Properties<number | string>[P])
[P in keyof BaseCSSProperties]: BaseCSSProperties[P] | ((props: Props) => BaseCSSProperties[P])
};

export interface CreateCSSProperties<Props extends object = {}>
Expand Down
2 changes: 2 additions & 0 deletions packages/material-ui/src/styles/withStyles.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
StyleRulesCallback,
Styles,
ClassKeyOfStyles,
BaseCSSProperties,
} from '@material-ui/styles/withStyles';

export {
Expand All @@ -20,6 +21,7 @@ export {
Styles,
WithStylesOptions,
StyleRulesCallback,
BaseCSSProperties,
};

/**
Expand Down