νμ¬ propsμ theme κ°μ²΄κ° λͺ¨λ νμν κ΅¬μ± μμλ₯Ό κ°λ° μ€μ λλ€.
μ²μμλ ν λ§ κ°μ²΄μ μ μλν©λλ€.
const styles = theme => ({
title: {
...theme.typography.headline,
textAlign: 'center',
padding: '8px 16px',
margin: 0,
color: theme.palette.common.white,
backgroundColor: theme.palette.primary[500],
},
withStyles(styles, { withTheme: true })(Component);
....
νμ§λ§ μ€νμΌ κ°μ²΄μ μνμλ μ‘μΈμ€ν΄μΌ ν©λλ€.
μμ λ₯Ό μλνμ§λ§ μλνμ§ μμ΅λλ€.
{
....
display: (props) => props.display
}
κ²°κ΅ react-jss
μ withTheme
λ₯Ό κ²°ν©νμ¬ κ·Έλ κ² ν©λλ€.
import { withTheme } from 'material-ui/styles';
import injectSheet from 'react-jss';
function withStyles(styles, Component) {
return withTheme()(injectSheet(styles)(Component));
}
export default withStyles;
....
const styles = {
title: {
display: (props) => props.display,
textAlign: 'center',
padding: '8px 16px',
margin: 0,
color: ({ theme }) => theme.palette.common.white,
backgroundColor: ({ theme }) => theme.palette.primary[500],
},
μλνμ§λ§ μ λ§ κ·Έλ¦¬μ
title: {
...theme.typography.headline,
react-jssμ λμΌν 컨ν
μ€νΈ ν€λ₯Ό μ¬μ©νμ¬ Material-UIλ₯Ό λ§λ€μ΄ λ¬Έμ λ₯Ό ν΄κ²°ν μ μμ κ²μ
λλ€. https://github.com/cssinjs/theming/blob/master/src/channel.js#L1.
λν #7633μ μ΄ν΄λ³΄μμμ€.
react-jss μνΈ μ΄μ©μ± μμ λ‘ PRμ μ€λΉνμ΅λλ€. λ¬Έμμ μΆκ°νκ² μ΅λλ€. cc @kof
@oliviertassinari μ΄ λ¬Έμ μ ν΄κ²°μ μ΄μ μ€νμΌ μ μ λ΄μμ μνμ μ‘μΈμ€ν μ μμ΄μΌ νλ€λ κ²μ μλ―Έν©λκΉ? λ°©λ²μ΄ λͺ ννμ§ μμ΅λλ€...
@pelotom μλμ, withStylesλ μμ±μ μ‘μΈμ€ν μ μμ΅λλ€. κ·Έλ¬λ μΌλ§λ λ§μ μ¬λλ€μ΄ μ΄ κΈ°λ₯μ μꡬνλμ§ κ°μν λ. λ²κ·Έ μμ ν μ°μ μμλ₯Ό μ ν μ μμ΅λλ€. injectionSheet HOCλ₯Ό μ¬μ©ν μ μμ§λ§ λ©λͺ¨λ¦¬ λμ, ν« λ¦¬λ‘λ© μ€λ¨, classes
κ΅¬μ± μμ, λ΄λΆ μ°Έμ‘° μ‘μΈμ€ μμ, ν
λ§ μ€μ²© μ²λ¦¬ μ€λ₯ λ± μ¬λ¬ λ¬Έμ κ° λ°μν μ μμ΅λλ€. μ μ΄λ κ·Έκ²μ λ΄κ° κ³Όκ±°μ μ§λ©΄νλ λ¬Έμ μ€ μΌλΆμ΄λ©° λ€μ μμ±νλλ‘ λκΈ°λ₯Ό λΆμ¬νμ΅λλ€. κ·Έλ° λ¬Έμ λ€μ΄ μ°¨κ·Όμ°¨κ·Ό ν΄κ²°λ κ²μ΄λΌκ³ μκ°ν©λλ€.
@oliviertassinari μ΄κ²μ μ°μ μμλ‘ κ³ λ €ν κ²μ΄λΌλ μμμ λ£κ² λμ΄ λ§€μ° μμ¬μ΄ λ©λλ€! μ΄λ κ² νλ©΄ κ΅¬μ± μμλ₯Ό ν¨μ¬ μ½κ² μ¬μ©μ μ§μ ν μ μμ΅λλ€. μλ₯Ό λ€μ΄, κ΅¬μ± κ°λ₯ν ν¬κΈ°(μ: λλΉ λ° λμ΄(ν½μ ))κ° μλ νμΈλμ κ°κ³ μΆμ΅λλ€.
<CustomCheckbox size={16} />
μ°λ¦¬κ° μ‘μΈμ€ ν μμλ κ²½μ° props
μμ styles
μ΄ λ§€μ° κ°λ¨ ν κ²μ
λλ€ :
const styles = {
root: {
width: props => props.size,
height: props => props.size
}
}
λλ
const styles = props => ({
root: {
width: props.size,
height: props.size
}
})
κ·Έλ¦¬κ³ :
const CustomCheckbox = ({size, classes}) => <Checkbox className={classes.root} />;
export default withStyles(styles)(CustomCheckbox);
νμ¬λ‘μλ μ΄λ¬ν μ νμ μ¬μ© μ¬λ‘μ μ κ·Όνλ λ°©λ²μ λν κΆμ₯ μ¬νμ΄ μμ΅λκΉ? λλ withStylesλ₯Ό μ¬μ©ν λ μν μ‘μΈμ€ μ§μμ μΆκ°ν μ μμ κ²μΌλ‘ μμλλ μκΈ°κ° μμ΅λκΉ?
@nmchaves μ¬μ© μ¬λ‘λ μΈλΌμΈ μ€νμΌ μ κ·Ό λ°©μμ μλ²½νκ² λΆν©νλ κ² κ°μ΅λλ€. μ€λͺ
μμμ μ΄μ λν΄ μ‘°κΈ μ°Ύμ μ μμ΅λλ€. μμ£Όνλ μ§λ¬Έ
https://github.com/callemall/material-ui/blob/75a30061e76eae93c711ec202a2c7e4238a4f19a/docs/src/pages/style/SvgIcons.js#L38 -L44
@oliviertassinari κ°μ¬ withStyles
μ¬μ©νμ¬ μ΄ μμ
μ μνν μ μκΈ°λ₯Ό λ°λμ§λ§ μΈλΌμΈ μ€νμΌμ΄ μ μλν κ²μ
λλ€. κ·Έλ¦¬κ³ λ¬Έμμμ μ¬κΈ° +λ₯Ό κΆμ₯νλ€λ μ¬μ€μ μ΄ κ²°μ μ λν΄ λ§€μ° νμ μ κ°κ² ν©λλ€. λ€μ νλ² κ°μ¬ν©λλ€!
backgroundImageμ μ€νμΌμ μν(μ΄λ―Έμ§ src)μ μ λ¬ν μ μμΌλ©΄ μ’μ κ²μ λλ€.
withStyle
ν¬μ₯νκ² μ΅λλ€
const withStylesProps = styles =>
Component =>
props => {
console.log(props);
const Comp = withStyles(styles(props))(Component);
// return <div>lol</div>;
return <Comp {...props} />;
};
const styles = props => ({
foo: {
height: `${props.y || 50}px`,
}
});
export default withStylesProps(styles)(
props => (
<div className={props.classes.foo} style={{ ...props.style, background: 'yellow' }}>
<h1>Hello!</h1>
</div>
)
);
λ°λͺ¨: https://codesandbox.io/s/k2y01rj3w7
(λλμ΅λλ€ ^ ThemeProvider
λ° JssProvider
μ€μ μμ΄ https://codesandbox.io/s/q6v7krx6, μ μ΄κΈ°νν©λλ€)
@caub μλ μ€μ΄μ§λ§ μ΄ ν¨ν΄μ μ£Όμν΄μΌ ν©λλ€. μ½μ λ CSSλ κ΅¬μ± μμμ μΈμ€ν΄μ€ μμ ν¨κ» 컀μ§λλ€. #7633μ 볡μ νμ λλ€. λλ μ£Όμ λ₯Ό νν€μΉμ§ μμλ€. κ·Έλ¬λ @kof λ²μ μ μΌλΆ μ±λ₯ μ΅μ νλ₯Ό μ¬μ©νλ€κ³ μκ°ν©λλ€.
@caub 곡μ
@oliviertassinari https://github.com/cssinjs/react-jss/blob/master/readme.md#dynamic -values ββin react-jssκ° μμ΅λλ€. μ material-uiμμ μ¬μ©ν μ μλμ§ κΆκΈν©λλ€. λν μΈλΌμΈ style
propμ΄ λμ κ°μ μ ν©νμ§λ§ λͺ¨λ μ€νμΌ μ μλ₯Ό κ°μ μμΉμ λλ κ²μ΄ λ λ«λ€κ³ λ§μνμ μμ μ μ΄ν΄ν©λλ€. λ³΄λ€ ν¨μ¨μ μΈ λμ μ€νμΌμ μν΄ className
λ° style
λ₯Ό μ²λ¦¬νλ https://github.com/airbnb/react-with-styles λ
λλ κ°μ λ¬Έμ μ μ§λ©΄ ν΄μλ€. λκ΅°κ° λλ₯Ό λμΈ μ μμ΅λκΉ?
`'react'μμ React κ°μ Έμ€κΈ°;
'prop-types'μμ PropTypeμ κ°μ Έμ΅λλ€.
'material-ui/styles'μμ { withStyles } κ°μ Έμ€κΈ°;
'material-ui/Drawer'μμ μλ κ°μ Έμ€κΈ°;
'material-ui/AppBar'μμ AppBar κ°μ Έμ€κΈ°;
'material-ui/Toolbar'μμ λꡬ λͺ¨μ κ°μ Έμ€κΈ°;
'material-ui/List'μμ λͺ©λ‘ κ°μ Έμ€κΈ°;
'material-ui/Typography'μμ νμ΄ν¬κ·ΈλνΌ κ°μ Έμ€κΈ°;
'material-ui/IconButton'μμ IconButton κ°μ Έμ€κΈ°;
import Hidden from 'material-ui/Hidden';
'material-ui/Divider'μμ λλ°μ΄λ κ°μ Έμ€κΈ°;
'material-ui-icons/Menu'μμ MenuIcon κ°μ Έμ€κΈ°;
'./tileData'μμ { mailFolderListItems, otherMailFolderListItems } κ°μ Έμ€κΈ°;
const μλ λλΉ = 240;
const μ€νμΌ = ν
λ§ => ({
루νΈ: {
λλΉ: '100%',
ν€: 430,
marginTop: theme.spacing.unit * 3,
zμΈλ±μ€: 1,
μ€λ²νλ‘: 'μ¨κΉ',
},
μ±νλ μ: {
μμΉ: 'μλμ ',
λμ€νλ μ΄: 'νλ μ€',
λλΉ: '100%',
λμ΄: '100%',
},
μ±λ°: {
μμΉ: 'μ λ',
marginLeft: μλ λλΉ,
[theme.breakpoints.up('md')]: {
λλΉ: calc(100% - ${drawerWidth}px)
,
},
},
navIconHide: {
[theme.breakpoints.up('md')]: {
νμ: 'μμ',
},
},
μλ ν€λ: theme.mixins.toolbar,
μλμ©μ§: {
λλΉ: 250,
[theme.breakpoints.up('md')]: {
λλΉ: μλ λλΉ,
μμΉ: 'μλμ ',
λμ΄: '100%',
},
},
μ½ν
μΈ : {
backgroundColor: theme.palette.background.default,
λλΉ: '100%',
ν¨λ©: theme.spacing.unit * 3,
λμ΄: 'κ³μ°(100% - 56px)',
μ¬λ°±μλ¨: 56,
[theme.breakpoints.up('sm')]: {
λμ΄: 'κ³μ°(100% - 64px)',
μ¬λ°±μλ¨: 64,
},
},
});
λ΄λ³΄λ΄κΈ° ν΄λμ€ ResponsiveDrawerλ React.Component {λ₯Ό νμ₯ν©λλ€.
μν = {
mobileOpen: κ±°μ§,
};
handleDrawerToggle = () => {
this.setState({ mobileOpen: !this.state.mobileOpen });
};
λ λλ§() {
const { ν΄λμ€, ν
λ§ } = this.props;
const drawer = (
<div>
<div className={classes.drawerHeader} />
<Divider />
<List>{mailFolderListItems}</List>
<Divider />
<List>{otherMailFolderListItems}</List>
</div>
);
return (
<div className={classes.root}>
<div className={classes.appFrame}>
<AppBar className={classes.appBar}>
<Toolbar>
<IconButton
color="inherit"
aria-label="open drawer"
onClick={this.handleDrawerToggle}
className={classes.navIconHide}
>
<MenuIcon />
</IconButton>
<Typography variant="title" color="inherit" noWrap>
Responsive drawer
</Typography>
</Toolbar>
</AppBar>
<Hidden mdUp>
<Drawer
variant="temporary"
anchor={theme.direction === 'rtl' ? 'right' : 'left'}
open={this.state.mobileOpen}
classes={{
paper: classes.drawerPaper,
}}
onClose={this.handleDrawerToggle}
ModalProps={{
keepMounted: true, // Better open performance on mobile.
}}
>
{drawer}
</Drawer>
</Hidden>
<Hidden smDown implementation="css">
<Drawer
variant="permanent"
open
classes={{
paper: classes.drawerPaper,
}}
>
{drawer}
</Drawer>
</Hidden>
<main className={classes.content}>
<Typography noWrap>{'You think water moves fast? You should see ice.'}</Typography>
</main>
</div>
</div>
);
}
}
ResponsiveDrawer.propTypes = {
ν΄λμ€: PropTypes.object.isRequired,
ν
λ§: PropTypes.object.isRequired,
};
κΈ°λ³Έ λ΄λ³΄λ΄κΈ° withStyles(styles)(ResponsiveDrawer);
`
μ½μ λ CSSλ κ΅¬μ± μμμ μΈμ€ν΄μ€ μμ ν¨κ» 컀μ§λλ€.
@oliviertassinari μ£Όμ CSSλ +- htmlμ΄ μΈλΌμΈ μ€νμΌλ‘ μ±μ₯νλ κ²κ³Ό κ°μ λ°©μμΌλ‘ μ±μ₯ν κ²μ λλ€. μ μ μ€νμΌμ λ³λμ μνΈμ λ λλ§λκ³ λͺ¨λ κ΅¬μ± μμ μΈμ€ν΄μ€μμ μ¬μ¬μ©λ©λλ€.
λΉ μ μ₯ κ΅¬μ± μμκ° λ€μ λ λλ§νμ§λ§ λλμ΄ μ’μνλ€ withStyle
μμ render
μ render
, μ°λ¦¬λ μ 체 μμν κ΅¬μ± μμλ₯Ό μ¬μ©νμ¬ νΌν μ μμ΅λλ€.
import React from 'react';
import {
withStyles,
Grid,
CircularProgress
} from 'material-ui';
const PreloadComponent = props => {
const { classes,size } = props;
return (
<Grid className={classes.container} container justify={'center'} alignItems={'center'}>
<CircularProgress size={size}/>
</Grid>
)
};
const StyleWithThemeProps = (props) => {
return withStyles(theme => ({
container: {
paddingTop: props.size*2 || 50,
paddingBottom: props.size*2 || 50,
}
}),{withTheme: true})(PreloadComponent)
};
const Preload = props => {
const { size } = props;
const WithStylesPreloadComponent = StyleWithThemeProps(props);
return (
<WithStylesPreloadComponent {...props}/>
)
};
Preload.defaultProps = {
size: 20
};
export default Preload;
μ λ°μ΄νΈλ₯Ό νΌνκΈ° μν΄ μμ ν μμ κ΅¬μ± μμλ₯Ό μ¬μ©ν μ μμ΅λλ€.
const PreloadComponent = props => {
const { classes,size } = props;
return (
<Grid className={classes.container} container justify={'center'} alignItems={'center'}>
<CircularProgress size={size}/>
</Grid>
)
};
const StyleWithThemeProps = (props) => {
return withStyles(theme => ({
container: {
paddingTop: props.size*2 || 50,
paddingBottom: props.size*2 || 50,
}
}),{withTheme: true})(PreloadComponent)
};
class PreloadFull extends React.PureComponent {
constructor(props,context) {
super(props);
}
componentWillMount() {
this.StyledPreloadFull = StyleWithThemeProps(this.props);
}
componentWillUpdate(nextProps) {
this.StyledPreloadFull = StyleWithThemeProps(nextProps);
}
render() {
const { StyledPreloadFull,props } = this;
return (
<StyledPreloadFull {...props}/>
);
}
}
PreloadFull.defaultProps = {
size: 20
};
export default PreloadFull;
@up209d μλνμ§λ§ κ½€ κ³ ν΅μ€λ½μ΅λλ€. withStyles
λ₯Ό μμ νμ¬ κ°μ μνμ μ λ¬ν μ μλ https://github.com/cssinjs/react-jss λ₯Ό λ³΄λ€ μ§μ μ μΌλ‘ μ¬μ©νλ €κ³ ν©λλ€.
@SrikanthChebrolu μ£Όμ κ° μλλ― λ‘ λ©μμ§λ₯Ό λ€λ₯Έ λ¬Έμ λ‘ μ΄λν μ μμ΅λκΉ?
μ΄ μνκ° μ΄λ€μ§ κΆκΈνμΈμ? μ΄ λ¬Έμ , JSS λ¬Έμ, material-ui λ¬Έμλ₯Ό μ½μμ§λ§ μμ§ μΈλΌμΈ μ€νμΌμ μ¬μ©ν νμκ° μλ Mui+Jss+TypeScriptμ λν μ루μ μ μ°Ύμ§ λͺ»νμ΅λλ€. λͺ κ°μ§ μΈλΌμΈ μ€νμΌμ λ£λ κ²μ΄ λλ‘λ λΆκ°νΌνμ§λ§ μ κ²½μ°μλ ν λ§μ μνμ λͺ¨λ μμ‘΄νλ λ€μν μνλ₯Ό κ°μ§ μ¬λ¬ μ€νμΌμ΄ μμ΅λλ€.
@chazsolo Hey Chaz, μ€μ λ‘ withStyles
from mui
λμ injectSheet
from react-jss
λ₯Ό μ¬μ©ν μ μμ΅λλ€. κ·Έλ° μμΌλ‘ props
λ° theme
λ λ€ κ°μ§ μ μμ΅λλ€.
import injectSheet from 'react-jss';
const styles = theme => ({
container: {
color: props => theme.palette[props.color || 'primary'].main
}
});
...
export default injectSheet(styles)(AnyComponent);
import { JssProvider, jss, createGenerateClassName } from 'react-jss/lib';
import { MuiThemeProvider } from 'material-ui';
const generateClassName = createGenerateClassName();
...
<JssProvider jss={jss} generateClassName={generateClassName}>
<MuiThemeProvider theme={props.theme} sheetsManager={new Map()}>
<App/>
</MuiThemeProvider>
</JssProvider>
@chazsolo μ΄ λ¬Έμ λ₯Ό νλ‘μ°νκ³ μΆλ€κ³ μκ°ν©λλ€ https://github.com/cssinjs/jss/issues/682
@kof μ @up209d μκ² κ°μ¬λ립λλ€. ꡬλ νκ³ up209dμ μλ₯Ό ν λ² λ³΄μ¬μ£ΌμΈμ.
@up209d
λΆννλ λλ κ·Έκ²μ΄ λμκ² ν¨κ³Όμ μ΄μ§ μμ κ²μ΄λΌκ³ μκ°ν©λλ€. κ·νκ° μ μν κ²μ ꡬννμΌλ©° styles
κ°μ²΄ λ΄λΆμ ν¨μ νΈμΆ λ΄μμ μνμ λ³Ό μ μμ§λ§ κ³μ μ€λ₯κ° λ°μν©λλ€. λ΄κ° μ νμ λμΉκ³ μμ΅λκΉ? props Interfacesμμ WithStyles
λ₯Ό νμ₯νκ³ μμΌλ―λ‘ propsμ classes
κ°μ²΄μ μ‘μΈμ€ν μ μμ΅λλ€(μ΄μ https://github.com/mui-μμ μ°Έμ‘°ν λ¬Έμ μΈμ§ κΆκΈν©λλ€. org/material-ui/issues/8726#issuecomment-337482040)
TS2344: Type '(theme: ITheme) => { arc: { stroke: string; strokeWidth: (props: any) => string | number; }; arcM...' does not satisfy the constraint 'string | Record<string, CSSProperties> | StyleRulesCallback<string>'.
Type '(theme: ITheme) => { arc: { stroke: string; strokeWidth: (props: any) => string | number; }; arcM...' is not assignable to type 'StyleRulesCallback<string>'.
Type '{ arc: { stroke: string; strokeWidth: (props: any) => string | number; }; arcMovement: { strokeDa...' is not assignable to type 'Record<string, CSSProperties>'.
Property 'arc' is incompatible with index signature.
Type '{ stroke: string; strokeWidth: (props: any) => string | number; }' is not assignable to type 'CSSProperties'.
Types of property 'strokeWidth' are incompatible.
Type '(props: any) => string | number' is not assignable to type 'string | number | undefined'.
Type '(props: any) => string | number' is not assignable to type 'number'.
λ΄ ν λ§λ λ€μκ³Ό κ°μ΅λλ€.
import { ITheme } from '...';
export default (theme: ITheme) => ({
arc: {
// ...
strokeWidth: (props: any): number | string => {
// this logs the correct data I'm expecting
console.log(props.data[0].properties.name)
return 1.5
}
},
arcMovement: {
// ...
},
})
ν₯λ―Έλ‘μ΄ μ μ λ΄ κ΅¬μ± μμ λ΄μμ classes
κ°μ²΄λ₯Ό μ¬μ©ν λ arc
λ° arcMovement
κ° μ ν¨ν μμ±μ΄λΌλ κ²μ
λλ€.
// from Chrome console
{
arc: "Arcs-arc-0-2-1 Arcs-arc-0-2-3",
arcMovement: "Arcs-arcMovement-0-2-2"
}
μ λ°μ΄νΈ
μ΄ μμ
μ μνν μ μμμ§λ§ μμ μ£Όμμμ μΈκΈνλ―μ΄ WithStyles
, withStyles
λν λͺ¨λ μ°Έμ‘°λ₯Ό μ κ±°ν΄μΌ νμΌλ©° classes
κ΅¬μ± λ° ν
λ§λ₯Ό μμ΄λ²λ Έμ΅λλ€. μ€μ²©. λλ μ΄μ μ¬κ³ μ€λ λλ₯Ό μ£Όμν κ²μ
λλ€. λͺ¨λ λμμ κ°μ¬λ립λλ€!
@chazsolo Hey Chaz, νμ€νμ§ μμ§λ§ style
κ°μ²΄μ props
λ΄λΆμ μλ classes
μ μ‘μΈμ€νλ €λ κ²μ
λλ€. κ·Έλ λ€λ©΄, λλ κ·Έκ²μ΄ λΆκ°λ₯νλ€κ³ μκ°λΆν° classes
λ§ μ¬μ©ν μ ν jss
μ²λ¦¬ style
κ°μ²΄ μ΄λ»κ² μ‘μΈμ€ ν μ classes
κ²°μ μ κ³Όμ μ μ classes
λ μ€νλμ§ μμμ΅λκΉ?
@caubκ° μ΄λ―Έ μ루μ μ μ 곡νλ€κ³ μκ°ν©λλ€. μ½κ°μ λΉνκΈ°λ‘ μ루μ μ λ€μ κ²μνμμμ€. μΆκ° λΌμ΄λΈλ¬λ¦¬κ° νμνμ§ μμ΅λλ€.
μμ λ§μ λνΌ withStylesProps
λΉλν©λλ€.
import { withStyles } from 'material-ui/styles';
const styles = ( theme, props ) => ({
exampleStyle: {
color: 'red' // <-- or try theme.palette.primary[600]
}
})
const withStylesProps = ( styles ) =>
Component =>
props => {
const Comp = withStyles(theme => styles(theme, props))(Component);
return <Comp {...props} />;
};
const YourComponent = ({ classes }) =>
<Typography type="display4" className={classes.exampleStyle}>{type}</Typography>
export default withStylesProps(styles)(YourComponent);
λͺ¨λ κ΅¬μ± μμμ λν΄ withStylesProps
λ§λ€κΈ°κ° λ§μμ λ€μ§ μμΌλ©΄ λ³λμ νμΌμ μΆκ°νκ³ μνλ μμΉμ κ°μ Έμ΅λλ€.
@iamthuypham ν κ°μ¬ν©λλ€. κ·Έλ¬λ withStylesProps
λ‘ κ΅¬μ± μμλ₯Ό λννλ©΄ λνλ κ΅¬μ± μμ λ΄λΆμ μ΄λκ°μμ μ¬μ©νκ³ μλ μ ν κ΅¬μ± μμ <Collapse
μ μ λλ©μ΄μ
μ΄ μλμ λ©μΆ₯λλ€.
@jdolinski1 μ½λ μμ λ₯Ό 볡μ¬/λΆμ¬λ£κΈ° ν μ μμ΅λκΉ?
@iamthuypham κ·νμ μ루μ
μλ κ΅¬μ± μμκ° μμ±λ λλ§λ€ μλ‘μ΄ <style>
νκ·Έλ₯Ό μμ±νλ λ¨μ μ΄ μμ΅λλ€. λν defaultPropsλ₯Ό μ¬μ©ν λ μ£Όμνκ³ κΈ°λ³Έ κ΅¬μ± μμκ° μλ HOC κ΅¬μ± μμμ μΆκ°ν μ μμ΅λλ€.
μ΄ λͺ¨λ κ²μ΄ react-jss
μμ μ§μλλ©°, κΈ°λ³Έμ μΌλ‘ material-ui
μ§μλμ§ μμ΅λκΉ?
λν @jdolinski1 μ λ¬Έμ λ μ½λκ° λνλ κ΅¬μ± μμμ μμ μ μλ children
μ ννμ§ μλλ€λ κ²μ
λλ€.
@iamthuypham μμ μ κ·Έλ¬λ κ²μ²λΌ κ·Έλ κ² νλ κ²μ κΆμ₯νμ§ μλ κ² κ°κ³ , μ±μ΄ 빨리 μ±μ₯νλ ν μ±λ₯μ΄ μ νλ μ μμ΅λλ€. μλ‘μ΄ jss style
κ°μ²΄λ‘ component
μ μ μΈμ€ν΄μ€λ₯Ό μμ±νλ κ²μ style
κ°μ²΄κ° μμ ν λ€μ λ λλ§λμ΄μΌ νκΈ° λλ¬Έμ μ½λ© μμΉ μΈ‘λ©΄μμ μ’μ§ μμ΅λλ€. props
λ³κ²½λΉ μκ°. injectSheet
μμ react-jss
κ²μ΄ λ λμ μ νμ
λλ€. injectSheet
λ₯Ό μ΄ν΄λ³΄λ©΄ style
κ°μ²΄λ₯Ό 2κ°μ μ‘°κ°( static
& dynamic
)μΌλ‘ λλλ―λ‘ dynamic
props
λ³κ²½λλ©΄ dynamic
κ° λ€μ λ λλ§λ©λλ€.
InjectSheetμ ν¨κ» jss-nestedμ κ°μ νλ¬κ·ΈμΈμ μ¬μ©νλ λ°©λ²μ 무μμ λκΉ?
μ£Όμ
μνΈλ₯Ό μ¬μ©νλ©΄ '&:hover'λ¬Έμ΄ μλνμ§ μμ΅λλ€.
withStylesλ₯Ό μ¬μ©νλ©΄ μνμ μ‘μΈμ€ν μ μμ΅λλ€...
@koutsenko λ€μμ μμ λλ€.
import React from "react";
import { makeStyles } from "@material-ui/styles";
import Button from "@material-ui/core/Button";
const useStyles = makeStyles({
root: {
background: props => props.color,
"&:hover": {
background: props => props.hover
},
border: 0,
borderRadius: 3,
color: "white",
height: 48,
padding: "0 30px"
}
});
export default function Hook() {
const classes = useStyles({
color: "red",
hover: "blue"
});
return <Button className={classes.root}>Hook</Button>;
}
https://codesandbox.io/s/pw32vw2j3m
λμμ΄ λκΈ°λ₯Ό λ°λλλ€.
μ, ~1λ λμ μ°λ¦¬κ° μ΄λ£©ν λ°μ μ΄ λλμ΅λλ€π.
μ΄μ μ΄λ»κ² νμ΄νν©λκΉ?
@stunaz μ’μ μ§λ¬Έμ
λλ€. λͺ¨λ₯΄κ² μ΄μ. λλ κ·Έκ²μ μ‘°μ¬νμ§ μμλ€. @eps1lon μ λͺ¨λμ TypeScript μ μλ₯Ό μλ£νμ΅λλ€. μμμ μΌλ‘ μ¬μ©ν μ μμ΅λλ€.
https://github.com/mui-org/material-ui/blob/f4281a77d15b0d6eec9d33cdc358cfb89844996d/packages/material-ui-styles/src/index.d.ts#L72
@koutsenko λ€μμ μμ λλ€.
@oliviertassinari κ°μ¬ν©λλ€. " react@next "λ₯Ό μ¬μ©νλ©΄ μ΄μ μλν©λλ€.
@koutsenko jss-nested
μλνμ§ μμΌλ©΄ μ½λ©μ μ΄λκ°μ κ΅¬μ± λ¬Έμ κ° μμ΄μΌ ν©λλ€. jss-nested
κ° jss-default-preset
μ ν¬ν¨λμ΄ μμΌλ―λ‘ oob
@oliviertassinari
μνμ μ¬μ©νμ¬ μ£Όμ΄μ§ μ νκΈ°μ μ 체 μ€νμΌ κ°μ²΄λ₯Ό μ€μ ν μλ μμ΅λκΉ? 쑰건λΆλ‘ μμ±μ μ μ©ν μ μλ κ³³μ μ΄λμΈκ°μ?
μλ₯Ό λ€μ΄ μ΄μ κ°μ΄
withStyles({
root: {
'& > path': (props) => {
if(props.color)
return {
fill: props.color
};
return {};
}
}
})
κ·Έλμ μνμ΄ μ‘΄μ¬νμ§ μμΌλ©΄ λ΄κ° μ€μ ν΄μΌ νλ λ€λ₯Έ κ°μ΄ μλ μ΄μ μ±μ°κΈ° κ°μ μ¬μ©ν©λκΉ? μλ₯Ό λ€μ΄, μΌλ°μ μΌλ‘ μ±μ°κΈ°μ μ μ©λλ λ€λ₯Έ κ·μΉμ΄ μμ§λ§ color
μνμ΄ μ€μ λ κ²½μ°μλ§ μ΄ μ μ±μ°κΈ° μμ±μ μ€μ νκ³ μΆμ΅λλ€.
κ°μ¬ ν΄μ!
@Guardiannw μ΄λ€ μ΄μ λ‘ κ·νμ λ³νμ΄ μλνμ§ μμ΅λλ€. @kof κ° μ΄μ π‘μ λν
// π
const useStyles = makeStyles({
root: {
"& > span": {
backgroundColor: props => props.color || null,
}
}
});
// or
const useStyles = makeStyles({
root: props => ({
"& > span": {
backgroundColor: props.color || null
}
})
});
@oliviertassinari withStyles
κΈ°λ₯μ μ¬μ©ν μ μλ λ λ²μ§Έ μ΅μ
μ μ»λ λ° μ΄λ €μμ κ²ͺκ³ μμ΅λλ€. makeStyles
λ° νν¬μμλ§ μλν©λκΉ?
@Guardiannw @material-ui/styles
μ API μ€ νλμ μλν©λλ€.
@oliviertassinari λ μ ν¨ν ꡬ문μ²λΌ 보μ λλ€. fn κ°μ΄ v10μ μΆκ°λμμΌλ―λ‘ v9κ° μ¬μ©λμκ±°λ μ½λ λ° μμ μ¬μμ΄ νμν©λλ€.
μ’μ, κ·Έκ² λ΄κ° κ·Έκ²μ μλν κ²μ λλ€. λ€μ μλν΄μΌ ν μλ μμ΅λλ€.
@oliviertassinari @materia-ui/styles μ¬μ©μ λν΄ μ§λ¬Έμ΄ μμ΅λλ€. νλ‘λμ νκ²½μμ μ¬μ©ν μ μκ³ μ¬μ©ν μ μμ΅λκΉ? μ€λͺ μμ μμ μ μΈ λ²μ μμλ μλνμ§ μλλ€κ³ λμ μμ΅λλ€. μ λ " 3.9.1", κ·νκ° μ μνλ https://github.com/mui-org/material-ui/issues/8726#issuecomment -452047345 μμλ λ΄κ° νμλ‘ νλ κ°λ ₯νκ³ μ μ©ν κΈ°λ₯μ΄ μμ΅λλ€. μ΄λ¬ν λ¬Έμ μ, λλ λ€λ₯Έ κ΄μ μμ λ§μ μ견μλ³΄κ³ λ λλ ν΄κ²°μ± μ’μ https://github.com/mui-org/material-ui/issues/8726#issuecomment -363546636 @caubμ, κ·Έλ¬λμ λν μ견μ μμ μ μ루μ μ΄ μ’μ΅λλ€.
@contrerasjf0 @material-ui/styles
λ μν 릴리μ€λ‘λ§ μ¬μ©ν μ μμ΅λλ€. μ°λ¦¬λ μν λ²μ μ λ°μ μνκ³μ λλΆλΆμ ν¨ν€μ§μ²λΌ μ·¨κΈν©λλ€. νλ‘λμ
νκ²½μμλ μν ν¨ν€μ§λ₯Ό μ¬μ©νμ§ μλ κ²μ΄ μ’μ΅λλ€. κ·Έλ κ² νλ©΄ λͺ¨λ λ¦΄λ¦¬μ€ κ°μ λ²κ·Έμ μ£Όμ λ³κ²½ μ¬νμ μμν΄μΌ ν©λλ€. μ¦, λ³λ μν λ²μ μΆκ°λ₯Ό μ²λ¦¬ν μ μμ΄μΌ ν©λλ€.
λ΄κ° λ°λΌλ κ²μ μ¬λλ€μ΄ μ·¨λ―Έ νλ‘μ νΈμμ μ΄λ¬ν λ²μ μ μ¬μ©νκ±°λ νλ‘λμ μ λ°°ν¬λμ§ μμμ§λ§ νλ‘λμ λΆκΈ°μ²λΌ μ¬μ ν ν μ€νΈλλ λ³λμ λΆκΈ°μμ μ¬μ©νλ κ²μ λλ€. μν λ²μ μ μ¬μ©νκ³ νΌλλ°±μ μ 곡νλ λͺ¨λ μ¬λμκ² κ°μ¬ν©λλ€.
@up209d μ, μ루μ
μ΄ μλνμ§λ§
styles = { name: { cssprop: props => {} }
νκΈ°λ², μλ
styles = props => ({ name: { cssprop: {} })
λν JssProviderλ νμνμ§ μμ΅λλ€.
@koutsenko
// at value level:
styles = { name: { cssprop: props => value }
styles = theme => ({ name: { cssprop: props => value })
// at class name level
styles = { name: props => ({ cssprop: value }) }
styles = theme => ({ name: props => ({ cssprop: value }) })
theme
λ€μμ λ λ²μ§Έ μΈμλ‘λ μ΅μμ μμ€μμ props
μ μ‘μΈμ€ν μ μμ΅λλ€.
λ°©λ²μ μ°Ύμμ΄μ
// MyComponent.tsx
import React, { PureComponent } from 'react';
import { myComponentWithStyles } from './myComponentWithStyles';
export interface MyComponentProps {
copy: string;
size?: number;
}
export class Twemoji extends PureComponent<myComponentWithStyles> {
public render() {
const { copy, classes } = this.props;
return (
<div className={classes.message}>
{copy}
<img src="https://via.placeholder.com/150" />
</div>
);
}
}
// myComponentWithStyles.tsx
import React from 'react';
import { withStyles, WithStyles, Theme } from '@material-ui/core';
import { MyComponent, MyComponentProps } from './my-component';
const styles = (props: Theme & MyComponentProps) => ({
message: {
fontSize: props.typography.caption.fontSize,
'box-sizing': 'content-box',
'& img': {
width: `${props.size || 24}px`,
height: `${props.size || 24}px`,
padding: '0 4px',
verticalAlign: 'middle',
},
},
});
export type myComponentWithStyles = WithStyles<any>;
export const Component = (props: MyComponentProps) => {
const StyledComponent = withStyles((theme: Theme) => styles({ ...props, ...theme }))(
MyComponent
);
return <StyledComponent {...props} />;
};
md5-d0e1b51e375682cf2aad9c4d66b6c73a
<Component size={12} />
@andreasonny83 μ΄ ν¨ν΄μ νΌνμΈμ. v4μμ κΈ°λ³Έ APIλ₯Ό μ 곡νκ³ μμ΅λλ€.
@oliviertassinari μ λ°μ΄νΈμ κ°μ¬λ립λλ€. κ·Έ ν¨ν΄μ μ΄λ―Έ μ¬μ© κ°λ₯νκ°μ? μ¬μ© κ°λ₯ν λ¬Έμκ° μμ΅λκΉ?
https://next.material-ui.com/css-in-js/basics/#adapting -based-on-props
λ§μ§λ§ μ§λ¬Έ @oliviertassinari . makeStyles
λ₯Ό withStyles
μ ν¨κ» μ¬μ©ν μ μμ΅λκΉ?
κ·Έμ λν λ¬Έμλ₯Ό μ°Ύμ μ μμ΅λλ€. λ΄κ°νλ €κ³ νλ κ²μ μ΄κ²μ λλ€ :
const useStyles = makeStyles({
message: {
boxSizing: 'content-box'
}
});
export const ComponentWithStyles = withStyles(useStyles())(MyComponent);
@andreasonny83
makeStyles
λ λ°μ νν¬μ©μ
λλ€.withStyles
HOCλ λμΌν ꡬ문μ νμ©ν©λλ€.κ·νμ μμμ makeStyles
μ κ±°νμμμ€.
const styles = { message: {boxSizing: 'content-box', background: props => props.bg} };
export const ComponentWithStyles = withStyles(styles)(MyComponent);
Gday μ¬λλ€μ μμ ν λ‘ μ μ°Έμ‘°νμ¬ νμ¬ μ루μ μ 곡μ νλ€κ³ μκ°νμ΅λλ€. λκ΅°κ° λλ λκ΅°κ°κ° νμ¬ μ루μ μ λν΄ λ λμ μ‘°μΈμ μ 곡ν μ μκΈ°λ₯Ό λ°λλλ€. μμμ λ°°κ²½ μ΄λ―Έμ§μ κ°μ λ΄ λ‘κ·ΈμΈ νμ΄μ§ IDμ κ²½μ° IDλ μ¬μ ν μ¬λ£ UI APIμ νμ μ μ§νκ³ μΆμ΅λλ€. AuthPageλ κ°λ³ μΈμ¦ κ΅¬μ± μμ(λ‘κ·ΈμΈ, μ κΈ, λΉλ°λ²νΈ λΆμ€, λΉλ°λ²νΈ μ¬μ€μ λ±)λ₯Ό μμμΌλ‘ μ¬μ©νλ λΆλͺ¨ νλ μ ν μ΄μ λ μ΄μ΄μΌ λΏμ λλ€. κ° νμ΄μ§λ₯Ό μλ‘ κ³ μΉ λλ§λ€ μλ‘μ΄ λ°±κ·ΈλΌμ΄λ λ‘λμ AuthPageContainer prop λ΄μ κ°λ ₯ν νμμ propsλ₯Ό νμΈν μ μμ΅λλ€.
// μΈμ¦νμ΄μ§.styles.tsx
import { Container } from "@material-ui/core";
import { ContainerProps } from "@material-ui/core/Container";
import { withStyles } from "@material-ui/core/styles";
import React from "react";
interface IAuthContainerProps extends ContainerProps {
background: string;
}
export const AuthContainer = withStyles({
root: props => ({
alignItems: "center",
backgroundImage: `url(${props.background})`,
backgroundPosition: "50% 50%",
backgroundRepeat: "no-repeat",
backgroundSize: "cover",
display: "flex",
height: "100vh",
justifyContent: "center",
margin: 0,
padding: 0,
width: "100%"
})
})((props: IAuthContainerProps) => <Container maxWidth={false} {...props} />);
// μΈμ¦νμ΄μ§.tsx
import React from "react";
import forest from "../../assets/backgrounds/forest.jpg";
import sky from "../../assets/backgrounds/sky.jpg";
import uluru from "../../assets/backgrounds/uluru.jpg";
import { AuthContainer } from "./AuthPage.styles";
const AuthPage = ({ children }) => {
const generateBackground = () => {
const backgrounds = [forest, sky, uluru];
const index = Math.floor(Math.random() * backgrounds.length);
return backgrounds[index];
};
return (
<AuthContainer background={generateBackground()}>{children}</AuthContainer>
);
};
export default AuthPage;
κ°λ¨ν λ€μκ³Ό κ°μ΄ νμμμ€.
// styles.js
export default theme => ({
root: props => ({
// some styles
}),
...
});
//container.js
export default withStyles(styles)(MyComponent);
λν μνλ₯Ό μ λ¬νλ κ²μ μ΄λ»μ΅λκΉ?
@luky1984
λΉμ μ ν μ μμ΅λλ€. λμ λ€μμ μνν μ μμ΅λλ€.
// Component.js
<Button
className={`
${classes.button}
${this.state.isEnable
? classes.enable
: classes.disable}
`}
/>
λλ λμ clsx https://www.npmjs.com/package/clsxλ₯Ό μ¬μ©νμμμ€.
@caub κ·νμ μ루μ
μ jss μμ± ν΄λμ€ μ΄λ¦ μμλ₯Ό
μ¬κΈ°μ μμ±λ κ²μ²λΌ: https://github.com/mui-org/material-ui/issues/8726#issuecomment -363546636
κ·νμ μ루μ
μ μ¬μ©νλ €κ³ μλνμ§λ§ κ·νμ HOC κ΅¬μ± μμ(withStylesProps) μ루μ
μ λνλμ΄ μκΈ° λλ¬Έμ withStylesμ νΈμΆμ μ§μ°μν€λ―λ‘ classNamesλ‘ νΈμΆν΄λ CSSκ° μ¬μ μλμ§ μμ΅λλ€.
μ’μμ: https://codesandbox.io/s/hocs-8uhw1?file=/index.js
λ°°κ²½μ #0000000μ΄κ³ μμμ νλμμ΄μ΄μΌ ν©λλ€.
κ°μ₯ μ μ©ν λκΈ
@oliviertassinari μ΄κ²μ μ°μ μμλ‘ κ³ λ €ν κ²μ΄λΌλ μμμ λ£κ² λμ΄ λ§€μ° μμ¬μ΄ λ©λλ€! μ΄λ κ² νλ©΄ κ΅¬μ± μμλ₯Ό ν¨μ¬ μ½κ² μ¬μ©μ μ§μ ν μ μμ΅λλ€. μλ₯Ό λ€μ΄, κ΅¬μ± κ°λ₯ν ν¬κΈ°(μ: λλΉ λ° λμ΄(ν½μ ))κ° μλ νμΈλμ κ°κ³ μΆμ΅λλ€.
μ°λ¦¬κ° μ‘μΈμ€ ν μμλ κ²½μ°
props
μμstyles
μ΄ λ§€μ° κ°λ¨ ν κ²μ λλ€ :λλ
κ·Έλ¦¬κ³ :
νμ¬λ‘μλ μ΄λ¬ν μ νμ μ¬μ© μ¬λ‘μ μ κ·Όνλ λ°©λ²μ λν κΆμ₯ μ¬νμ΄ μμ΅λκΉ? λλ withStylesλ₯Ό μ¬μ©ν λ μν μ‘μΈμ€ μ§μμ μΆκ°ν μ μμ κ²μΌλ‘ μμλλ μκΈ°κ° μμ΅λκΉ?