-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[styles] Allow CSS properties to be functions (#15546)
* [styles] Update type definition to let CSS properties be functions * Restore AdaptingHook * [docs] Updated AdaptingHook * [docs] Added AdaptingStyledComponents typescript demo * Consistent Props base type * [styles] Add test case * docs:typescript:formatted * Fix types for core * Reuse definition from styles * Remove unused definition * Reuse styleRules * Rename base * Reuse StyleRulesCallback * Reuse Styles and ClassKeyOfStyles * [core] Cleanup withStyles * Name generic arguments * Make StyleRules from core backwards compatible Co-authored-by: Sebastian Silbermann <silbermann.sebastian@gmail.com>
- Loading branch information
Showing
13 changed files
with
374 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { makeStyles } from '@material-ui/styles'; | ||
import Button from '@material-ui/core/Button'; | ||
|
||
const useStyles = makeStyles({ | ||
root: { | ||
background: props => | ||
props.color === 'red' | ||
? 'linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)' | ||
: 'linear-gradient(45deg, #2196F3 30%, #21CBF3 90%)', | ||
border: 0, | ||
borderRadius: 3, | ||
boxShadow: props => | ||
props.color === 'red' | ||
? '0 3px 5px 2px rgba(255, 105, 135, .3)' | ||
: '0 3px 5px 2px rgba(33, 203, 243, .3)', | ||
color: 'white', | ||
height: 48, | ||
padding: '0 30px', | ||
margin: 8, | ||
}, | ||
}); | ||
|
||
function MyButton(props) { | ||
const { color, ...other } = props; | ||
const classes = useStyles(props); | ||
return <Button className={classes.root} {...other} />; | ||
} | ||
|
||
MyButton.propTypes = { | ||
color: PropTypes.oneOf(['red', 'blue']).isRequired, | ||
}; | ||
|
||
function AdaptingHook() { | ||
return ( | ||
<React.Fragment> | ||
<MyButton color="red">Red</MyButton> | ||
<MyButton color="blue">Blue</MyButton> | ||
</React.Fragment> | ||
); | ||
} | ||
|
||
export default AdaptingHook; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { makeStyles } from '@material-ui/styles'; | ||
import Button, { ButtonProps as MuiButtonProps } from '@material-ui/core/Button'; | ||
|
||
type Omit<T, K> = Pick<T, Exclude<keyof T, K>>; | ||
|
||
interface Props { | ||
color: 'red' | 'blue'; | ||
} | ||
|
||
type MyButtonProps = Props & Omit<MuiButtonProps, keyof Props>; | ||
|
||
const useStyles = makeStyles({ | ||
root: { | ||
background: (props: Props) => | ||
props.color === 'red' | ||
? 'linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)' | ||
: 'linear-gradient(45deg, #2196F3 30%, #21CBF3 90%)', | ||
border: 0, | ||
borderRadius: 3, | ||
boxShadow: (props: Props) => | ||
props.color === 'red' | ||
? '0 3px 5px 2px rgba(255, 105, 135, .3)' | ||
: '0 3px 5px 2px rgba(33, 203, 243, .3)', | ||
color: 'white', | ||
height: 48, | ||
padding: '0 30px', | ||
margin: 8, | ||
}, | ||
}); | ||
|
||
function MyButton(props: MyButtonProps) { | ||
const { color, ...other } = props; | ||
const classes = useStyles(props); | ||
return <Button className={classes.root} {...other} />; | ||
} | ||
|
||
(MyButton as any).propTypes = { | ||
color: PropTypes.oneOf(['red', 'blue']).isRequired, | ||
}; | ||
|
||
function AdaptingHook() { | ||
return ( | ||
<React.Fragment> | ||
<MyButton color="red">Red</MyButton> | ||
<MyButton color="blue">Blue</MyButton> | ||
</React.Fragment> | ||
); | ||
} | ||
|
||
export default AdaptingHook; |
31 changes: 31 additions & 0 deletions
31
docs/src/pages/css-in-js/basics/AdaptingStyledComponents.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import React from 'react'; | ||
import { styled } from '@material-ui/styles'; | ||
import Button from '@material-ui/core/Button'; | ||
|
||
const MyButton = styled(({ color, ...other }) => <Button {...other} />)({ | ||
background: props => | ||
props.color === 'red' | ||
? 'linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)' | ||
: 'linear-gradient(45deg, #2196F3 30%, #21CBF3 90%)', | ||
border: 0, | ||
borderRadius: 3, | ||
boxShadow: props => | ||
props.color === 'red' | ||
? '0 3px 5px 2px rgba(255, 105, 135, .3)' | ||
: '0 3px 5px 2px rgba(33, 203, 243, .3)', | ||
color: 'white', | ||
height: 48, | ||
padding: '0 30px', | ||
margin: 8, | ||
}); | ||
|
||
function AdaptingStyledComponents() { | ||
return ( | ||
<React.Fragment> | ||
<MyButton color="red">Red</MyButton> | ||
<MyButton color="blue">Blue</MyButton> | ||
</React.Fragment> | ||
); | ||
} | ||
|
||
export default AdaptingStyledComponents; |
39 changes: 39 additions & 0 deletions
39
docs/src/pages/css-in-js/basics/AdaptingStyledComponents.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import React from 'react'; | ||
import { styled } from '@material-ui/styles'; | ||
import Button, { ButtonProps as MuiButtonProps } from '@material-ui/core/Button'; | ||
|
||
type Omit<T, K> = Pick<T, Exclude<keyof T, K>>; | ||
|
||
interface Props { | ||
color: 'red' | 'blue'; | ||
} | ||
|
||
type MyButtonProps = Props & Omit<MuiButtonProps, keyof Props>; | ||
|
||
const MyButton = styled(({ color, ...other }: MyButtonProps) => <Button {...other} />)({ | ||
background: (props: Props) => | ||
props.color === 'red' | ||
? 'linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)' | ||
: 'linear-gradient(45deg, #2196F3 30%, #21CBF3 90%)', | ||
border: 0, | ||
borderRadius: 3, | ||
boxShadow: (props: Props) => | ||
props.color === 'red' | ||
? '0 3px 5px 2px rgba(255, 105, 135, .3)' | ||
: '0 3px 5px 2px rgba(33, 203, 243, .3)', | ||
color: 'white', | ||
height: 48, | ||
padding: '0 30px', | ||
margin: 8, | ||
}); | ||
|
||
function AdaptingStyledComponents() { | ||
return ( | ||
<React.Fragment> | ||
<MyButton color="red">Red</MyButton> | ||
<MyButton color="blue">Blue</MyButton> | ||
</React.Fragment> | ||
); | ||
} | ||
|
||
export default AdaptingStyledComponents; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,3 @@ | ||
import { StyleRules } from './withStyles'; | ||
import createStyles from '@material-ui/styles/createStyles'; | ||
|
||
/** | ||
* This function doesn't really "do anything" at runtime, it's just the identity | ||
* function. Its only purpose is to defeat TypeScript's type widening when providing | ||
* style rules to `withStyles` which are a function of the `Theme`. | ||
* | ||
* @param styles a set of style mappings | ||
* @returns the same styles that were passed in | ||
*/ | ||
export default function createStyles<C extends string>(styles: StyleRules<C>): StyleRules<C>; | ||
export default createStyles; |
Oops, something went wrong.