çŸåšãå°éå ·ãšããŒããªããžã§ã¯ãã®äž¡æ¹ãå¿ èŠãšããã³ã³ããŒãã³ããéçºããŠããŸãã
æåã¯ãããŒããªããžã§ã¯ãã§ããŸãæ©èœããŸã
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ãäœæããããšã§åé¡ã«å¯ŸåŠã§ããã¯ãã§ãïŒ //github.com/cssinjs/theming/blob/master/src/channel.js#L1ã
ãŸããïŒ7633ãã芧ãã ãã
ç§ã¯ãreact-jssã®çžäºéçšæ§ã®äŸãåããPRã®æºåãã§ããŠããŸãã ãããããã¥ã¡ã³ãã«è¿œå ããŸãã cc @kof
@oliviertassinariããã®è§£æ±ºã¯ãã¹ã¿ã€ã«å®çŸ©å ã®å°éå ·ã«ã¢ã¯ã»ã¹ã§ããããã«ãªãã¯ãã§ããããšãæå³ããŸããïŒ ã©ã®ããã«ç§ã«ã¯æããã§ã¯ãããŸãã...
@pelotomããããwithStylesã¯ããããã£ã«ã¢ã¯ã»ã¹ã§ããŸããã ããããã©ãã ãã®äººããã®æ©èœãæ±ããŠããããèãããšã ãã°ä¿®æ£åŸãããã¯ç§ãåªå
ã§ãããã®ã§ãã injectSheet HOCã䜿çšã§ããŸãããã¡ã¢ãªãªãŒã¯ãããããªããŒãã®å€±æã classes
æ§æã®æ¬ åŠãå
éšåç
§ã¢ã¯ã»ã¹ã®æ¬ åŠãããŒãã®ãã¹ãåŠçã®å€±æãªã©ãè€æ°ã®åé¡ãžã®æãéãããŸãã å°ãªããšããããã¯ç§ãéå»ã«çŽé¢ããç§ã®æžãçŽãã®åæ©ãšãªã£ãåé¡ã®ããã€ãã§ãã ãããã®åé¡ã¯æ®µéçã«è§£æ±ºããããšæããŸãã
@oliviertassinariãããåªå ããããšãæ€èšãããšèããŠéåžžã«å®å¿ã§ãïŒ ããã«ãããã³ã³ããŒãã³ãã®ã«ã¹ã¿ãã€ãºãã¯ããã«ç°¡åã«ãªããŸãã ããšãã°ãèšå®å¯èœãªãµã€ãºïŒãã¯ã»ã«åäœã®å¹ ãšé«ãïŒã®ãã§ãã¯ããã¯ã¹ãå¿ èŠã§ãã
<CustomCheckbox size={16} />
styles
props
ã«ã¢ã¯ã»ã¹ã§ããã°ãããã¯éåžžã«ç°¡åã§ãã
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
ã䜿çšããŠãããéæã§ããããšãæãã§ããŸããããã€ã³ã©ã€ã³ã¹ã¿ã€ã«ã¯ããŸãæ©èœããŸãã ãããŠãããªãããã+ããã¥ã¡ã³ãã§ãããæšå¥šããŠãããšããäºå®ã¯ãç§ããã®æ±ºå®ã«éåžžã«èªä¿¡ãæã£ãŠãããšæããããŸãã å床ãæè¬ããŸãïŒ
èæ¯ç»åã®ã¹ã¿ã€ã«ã«å°éå ·ïŒç»å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 ïŒ
ïŒç§ã¯é©ããŠããŸã^ 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ãreact-jssã«ãããŸããããªãmaterial-uiã§äœ¿çšã§ããªãã£ãã®ã§ããããã ãŸããã€ã³ã©ã€ã³style
å°éå
·ã¯åçãªå€ã«æé©ã§ãããšããªããèšã£ãŠããç¹ãç解ããŠããŸããããã¹ãŠã®ã¹ã¿ã€ã«ã®å®çŸ©ãåãå Žæã«é
眮ããæ¹ãããã§ãããã ããå¹ççãªåçã¹ã¿ã€ã«ã®ããã«className
ãšstyle
ãåŠçããhttps://github.com/airbnb/react-with-stylesããããŸã
ç§ã¯åãåé¡ã«çŽé¢ããŠããŸã誰ããç§ãå©ããããšãã§ããŸã
`import React from'react ';
'prop-types'ããPropTypesãã€ã³ããŒãããŸãã
import {withStyles} from'material-ui / styles ';
'material-ui / Drawer'ããããã¯ãŒãã€ã³ããŒãããŸãã
'material-ui / AppBar'ããAppBarãã€ã³ããŒãããŸãã
'material-ui / Toolbar'ããããŒã«ããŒãã€ã³ããŒãããŸãã
'material-ui / List'ãããªã¹ããã€ã³ããŒãããŸãã
'material-ui / Typography'ããã¿ã€ãã°ã©ãã£ãã€ã³ããŒãããŸãã
'material-ui / IconButton'ããIconButtonãã€ã³ããŒãããŸãã
'material-ui / Hidden'ããé衚瀺ãã€ã³ããŒãããŸãã
'material-ui / Divider'ããDividerãã€ã³ããŒãããŸãã
'material-ui-icons / Menu'ããMenuIconãã€ã³ããŒãããŸãã
import {mailFolderListItemsãotherMailFolderListItems} from './ tileData';
constdrawerWidth = 240;
const styles = theme =>ïŒ{
æ ¹ïŒ {
å¹
ïŒã100ïŒ
ãã
é«ãïŒ430ã
marginTopïŒtheme.spacing.unit * 3ã
zIndexïŒ1ã
ãªãŒããŒãããŒïŒ 'é衚瀺'ã
}ã
appFrameïŒ{
äœçœ®ïŒ 'çžå¯Ÿ'ã
è¡šç€ºïŒ 'ãã¬ãã¯ã¹'ã
å¹
ïŒã100ïŒ
ãã
é«ãïŒã100ïŒ
ãã
}ã
appBarïŒ{
äœçœ®ïŒ '絶察'ã
marginLeftïŒdrawerWidthã
[theme.breakpoints.upïŒ 'md'ïŒ]ïŒ{
å¹
ïŒ calc(100% - ${drawerWidth}px)
ã
}ã
}ã
navIconHideïŒ{
[theme.breakpoints.upïŒ 'md'ïŒ]ïŒ{
è¡šç€ºïŒ 'ãªã'ã
}ã
}ã
åŒãåºãããããŒïŒtheme.mixins.toolbarã
åŒãåºãçŽïŒ{
å¹
ïŒ250ã
[theme.breakpoints.upïŒ 'md'ïŒ]ïŒ{
å¹
ïŒdrawerWidthã
äœçœ®ïŒ 'çžå¯Ÿ'ã
é«ãïŒã100ïŒ
ãã
}ã
}ã
ã³ã³ãã³ãïŒ {
backgroundColorïŒtheme.palette.background.defaultã
å¹
ïŒã100ïŒ
ãã
ããã£ã³ã°ïŒtheme.spacing.unit * 3ã
é«ãïŒ 'calcïŒ100ïŒ
-56pxïŒ'ã
marginTopïŒ56ã
[theme.breakpoints.upïŒ 'sm'ïŒ]ïŒ{
é«ãïŒ 'calcïŒ100ïŒ
-64pxïŒ'ã
marginTopïŒ64ã
}ã
}ã
}ïŒ;
ãšã¯ã¹ããŒãã¯ã©ã¹ResponsiveDrawerã¯React.Componentãæ¡åŒµããŸã{
ç¶æ
= {
mobileOpenïŒfalseã
};
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ã¯+
ç§ã¯ããã奜ãã§ããããã¹ããŒãã¬ã¹ã³ã³ããŒãã³ãã䜿çšãããšã 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åäœããŸãããããªãèŠçã§ããå€ã§å°éå
·ãæž¡ãããšãã§ããhttps://github.com/cssinjs/react-jssãããçŽæ¥çã«äœ¿çšããããã«ã withStyles
ãå€æŽããããšããŸãã
@SrikanthChebroluãããã¯ã§ã¯ãªãã®ã§ãã¡ãã»ãŒãžãå¥ã®åé¡ã«ç§»åã§ããŸããïŒ
ããã®ã¹ããŒã¿ã¹ãã©ããªã£ãŠããã®ãç¥ãããã§ããïŒ ç§ã¯ãã®åé¡ãJSSããã¥ã¡ã³ããmaterial-uiããã¥ã¡ã³ããèªãã§ããŸãããã€ã³ã©ã€ã³ã¹ã¿ã€ã«ã䜿çšããå¿ èŠã®ãªãMui + Jss + TypeScriptã®ãœãªã¥ãŒã·ã§ã³ããŸã èŠã€ããŠããŸããã ããã€ãã®ã€ã³ã©ã€ã³ã¹ã¿ã€ã«ãé 眮ããããšã¯é¿ããããªãå ŽåããããŸãããç§ã®å Žåãå€ãã®ç°ãªãç¶æ ãæã€è€æ°ã®ã¹ã¿ã€ã«ãããããã¹ãŠããŒããšå°éå ·ã«äŸåããŠããŸãïŒå€±æïŒ
@chazsoloãããã£ãºãå®éã«ã¯mui
ã®withStyles
代ããã«ã react-jss
injectSheet
ã䜿çšã§ããŸãã ãã®ããã«ããŠã 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
ãªããžã§ã¯ãå
ã®é¢æ°åŒã³åºãå
ã®å°éå
·ãèŠãããšãã§ããŸãããããç§ã¯ãšã©ãŒãåãåãç¶ããŸãã IAmç§ã¯ã¿ã€ãã足ããªãã ãã§ããïŒ å°éå
·ã®ã€ã³ã¿ãŒãã§ãŒã¹ã§WithStyles
ãæ¡åŒµããŠããã®ã§ãå°éå
የ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ã¡ãã£ãšãã£ãºã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以åã¯ããããŠããã®ã§ãããããããšã¯ãå§ãã§ããŸãããã¢ããªãããã«æé·ããéããããã©ãŒãã³ã¹ãäœäžããå¯èœæ§ããããŸãã æ°ããã€ã³ã¹ã¿ã³ã¹ã®äœæcomponent
æ°ããJSSãšstyle
ããããªããžã§ã¯ãã¯ãååã³ãŒãã£ã³ã°ã®çšèªã§ã¯è¯ããããŸããstyle
äœåºŠãäœåºŠããå®å
šã«åæç»ãããªãããªããžã§ã¯ãããã¹ãŠã®props
å€æŽãããã®æéã 䜿çšããŠinjectSheet
ããreact-jss
ããè¯ãéžæã§ãã injectSheet
ã調ã¹ããšã style
ãªããžã§ã¯ãã2ã€ã®éšåïŒ static
ãšdynamic
ïŒã«åå²ãããŠããã®ã§ã dynamic
ã ãã§ããããšãããããŸãã props
å€æŽããããšã dynamic
ãåã¬ã³ããªã³ã°ãããŸãã
jssãªã©ã®ãã©ã°ã€ã³ã䜿çšããæ¹æ³-injectSheetã§ãã¹ããããŠããŸããïŒ
injectSheetã䜿çšãããšããïŒïŒ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
é¢æ°ã䜿çšãã2çªç®ã®ãªãã·ã§ã³ãååŸããã®ã«èŠåŽããŠããŸãã makeStyles
ãšããã¯ã§ã®ã¿æ©èœããŸããïŒ
@Guardiannw @material-ui/styles
ã®APIã®ããããã§åäœããŠããŸãã
@oliviertassinariã¯æå¹ãªæ§æã®ããã«èŠããŸã
ããããŸãããããã¯ç§ããããè©Šãããã®ã§ãã åè©Šè¡ããå¿ èŠããããããããŸããã
@oliviertassinari @ materia-ui / stylesã®äœ¿çšã«ã€ããŠè³ªåããããŸãããæ¬çªç°å¢ã§äœ¿çšã§ããŸããïŒããã¥ã¡ã³ãã§ã¯ãå®å®ããŒãžã§ã³ã§ã¯æ©èœããªãããšã瀺ãããŠããŸããã 3.9.1 "ãããªããæ瀺ããäŸhttps://github.com/mui-org/material-ui/issues/8726#issuecomment -452047345ã«ã¯ãç§ãå¿ èŠãšãã匷åã§äŸ¿å©ãªæ©èœããããŸãã ãããã®åé¡ã§ã¯ãããŸããŸãªèŠ³ç¹ããå€ãã®ã³ã¡ã³ããããã@ caubã®ãœãªã¥ãŒã·ã§ã³https://github.com/mui-org/material-ui/issues/8726#issuecomment -363546636ãæ°ã«å ¥ã£ãŠããŸããã圌ã«ã€ããŠã®ã³ã¡ã³ãã¯è§£æ±ºçã¯è¯ãã§ãã
@ contrerasjf0 @material-ui/styles
ã¯ãã¢ã«ãã¡ãªãªãŒã¹ãšããŠã®ã¿å©çšå¯èœã§ãã ã¢ã«ãã¡çã¯ãreactãšã³ã·ã¹ãã ã®ã»ãšãã©ã®ããã±ãŒãžãšåãããã«æ±ããŸãã æ¬çªç°å¢ã§ã¯ã¢ã«ãã¡ããã±ãŒãžã䜿çšããªãããšããå§ãããŸãã ããããå ŽåããªãªãŒã¹éã§ãã°ãé倧ãªå€æŽãçºçããå¯èœæ§ããããŸããã€ãŸãããã£ãŒã³ã¢ã«ãã¡ããŒãžã§ã³ã®è¿œå ãåŠçã§ããã¯ãã§ãã
ç§ãæãã§ããã®ã¯ã人ã ããããã®ããŒãžã§ã³ã趣å³ã®ãããžã§ã¯ãã§äœ¿çšããããæ¬çªç°å¢ã«ãããã€ãããŠããªããæ¬çªç°å¢ã®ãã©ã³ããšåãããã«ãã¹ããããŠããå¥ã®ãã©ã³ãã§äœ¿çšããããšã§ãã ãããã®ã¢ã«ãã¡çã䜿çšãããã£ãŒãããã¯ãæäŸããŠãã ãã£ãçæ§ã«æè¬ããããŸãã
@ 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
åŸã®2çªç®ã®åŒæ°ãšããŠãããããã¬ãã«ã§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
ã¯react-hookçšã§ãwithStyles
HOCã¯åãæ§æãåãå
¥ããŸãã©ã¡ããäžæ¹ã䜿çšããŸãããã®äŸã§ã¯ã makeStyles
åé€ããã ãã§ãã
const styles = { message: {boxSizing: 'content-box', background: props => props.bg} };
export const ComponentWithStyles = withStyles(styles)(MyComponent);
Gdayã®äººã ã¯ãäžèšã®è°è«ãåç §ããŠãidãç§ã®çŸåšã®è§£æ±ºçãå ±æããŠãããšèããŸãããããŸãããã°ã誰ããŸãã¯èª°ããç§ã®çŸåšã®è§£æ±ºçã«ã€ããŠããè¯ãã¢ããã€ã¹ãæäŸã§ããããã«ãªããŸãã ç§ã®ãµã€ã³ã€ã³ããŒãžIDã¯ãã©ã³ãã ãªèæ¯ç»åã®ããã§ãããIDã¯äŸç¶ãšããŠãããªã¢ã«UIAPIã®ãã¯ãŒãç¶æããããšèããŠããŸãã AuthPageã¯ãåã ã®èªèšŒã³ã³ããŒãã³ãïŒãµã€ã³ã€ã³ãããã¯ããã¹ã¯ãŒããå¿ããããã¹ã¯ãŒãã®ãªã»ãããªã©ïŒãåãšããŠåãåã芪ãã¬ãŒã³ããŒã·ã§ã³å±€ã«ãããŸããã åããŒãžãæŽæ°ãããã³ã«ãAuthPageContainerããããå ã®æ°ããããã¯ã°ã©ãŠã³ãããŒããšåŒ·ãåä»ããããããããã確èªã§ããŸã
// AuthPage.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} />);
// AuthPage.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}
`}
/>
ãŸãã¯ã代ããã«clsxhttps ïŒ //www.npmjs.com/package/clsxã䜿çšããŠ
@caubãœãªã¥ãŒã·ã§ã³ã¯ãjssã§çæãããã¯ã©ã¹åã®é åºãå°ç¡ãã«ããŸãã
ããã«æžãããŠããããã«ïŒ https ïŒ
ç§ã¯ããªãã®ãœãªã¥ãŒã·ã§ã³ã䜿ãããšããŸããããHOCã³ã³ããŒãã³ãïŒwithStylesPropsïŒãœãªã¥ãŒã·ã§ã³ã¯ã©ãããããŠãããããwithStylesã®åŒã³åºããé
ãããŸãããã®ãããclassNamesã§åŒã³åºããšcssããªãŒããŒã©ã€ããããŸããã
ãããïŒ https ïŒ index.js
èæ¯ã¯ïŒ0000000ãè²ã¯éã§ããå¿ èŠããããŸã
æãåèã«ãªãã³ã¡ã³ã
@oliviertassinariãããåªå ããããšãæ€èšãããšèããŠéåžžã«å®å¿ã§ãïŒ ããã«ãããã³ã³ããŒãã³ãã®ã«ã¹ã¿ãã€ãºãã¯ããã«ç°¡åã«ãªããŸãã ããšãã°ãèšå®å¯èœãªãµã€ãºïŒãã¯ã»ã«åäœã®å¹ ãšé«ãïŒã®ãã§ãã¯ããã¯ã¹ãå¿ èŠã§ãã
styles
props
ã«ã¢ã¯ã»ã¹ã§ããã°ãããã¯éåžžã«ç°¡åã§ãããŸã
ãã®åŸïŒ
ä»ã®ãšããããããã®ã¿ã€ãã®ãŠãŒã¹ã±ãŒã¹ã«ã©ã®ããã«åãçµãã¹ããã«ã€ããŠã®æšå¥šäºé ã¯ãããŸããïŒ ãŸãã¯ãwithStylesã䜿çšããŠãããšãã«å°éå ·ã«ã¢ã¯ã»ã¹ããããã®ãµããŒãããã€è¿œå ã§ãããã«ã€ããŠã®èŠç©ããã¯ãããŸããïŒ