Material-ui: [withStyles] μŠ€νƒ€μΌ 개체 λ‚΄μ—μ„œ ꡬ성 μš”μ†Œμ˜ μ†Œν’ˆμ„ κ°€μ Έμ˜€λŠ” κΈ°λŠ₯ μΆ”κ°€

에 λ§Œλ“  2017λ…„ 08μ›” 02일  Β·  39μ½”λ©˜νŠΈ  Β·  좜처: mui-org/material-ui

μ„€λͺ…

createStyleSheet λ₯Ό μ‚¬μš©ν•  λ•Œ ꡬ성 μš”μ†Œμ˜ μ†Œν’ˆμ— μ•‘μ„ΈμŠ€ν•  수 μžˆλŠ” 방법이 μ—†μŠ΅λ‹ˆλ‹€. 크기, URL 이미지 및 기타 μŠ€νƒ€μΌμ„ ꡬ성 μš”μ†Œμ— μ†Œν’ˆμœΌλ‘œ μ „λ‹¬ν•˜λ €λŠ” κ²½μš°κ°€ μžˆμœΌλ―€λ‘œ 이것이 μ€‘μš”ν•˜λ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€.

μ˜€λŠ˜λ‚  이것에 λŒ€ν•œ μœ μΌν•œ 해결책은 μ΄λŸ¬ν•œ 것듀을 인라인 μŠ€νƒ€μΌλ‘œ λΆ„λ¦¬ν•˜λŠ” κ²ƒμ΄μ§€λ§Œ λ‚΄κ°€ μ•„λŠ” ν•œ material-ui v1은 jss 및 react-jss λ₯Ό μ‚¬μš©ν•˜κ³  이 두 κ°€μ§€λŠ” 이미 κ°€λŠ₯성을 μ œκ³΅ν•©λ‹ˆλ‹€ propsλ₯Ό 받은 λ‹€μŒ μ›ν•˜λŠ” μŠ€νƒ€μΌμ„ λ°˜ν™˜ν•˜λŠ” ν•¨μˆ˜λ₯Ό 톡해 props에 μ•‘μ„ΈμŠ€ν•˜λŠ” 방법은 λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

const styles = {
  button: {
    background: props => props.color
  },
  root: {
    backgroundImage: props => `url(${props.backgroundImage})`
  }
}

// Reference: https://github.com/cssinjs/react-jss#example

material-uiμ—μ„œλ„ 이와 같은 것을 κ΅¬ν˜„ν•˜λŠ” 것에 λŒ€ν•΄ μ–΄λ–»κ²Œ μƒκ°ν•˜μ‹­λ‹ˆκΉŒ?

enhancement important

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

이 문제λ₯Ό v1.x λ§ˆμΌμŠ€ν†€ 에 μΆ”κ°€ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€. λ§Žμ€ 뢄듀이 μš”μ²­ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€. μš°λ¦¬λŠ” 곧 그것을 얻을 κ²ƒμž…λ‹ˆλ‹€.

capture d ecran 2018-07-29 a 13 12 03

λͺ¨λ“  39 λŒ“κΈ€

react-jssλŠ” ν•΄λ‹Ή κΈ°λŠ₯을 κ΅¬ν˜„ν•©λ‹ˆλ‹€. 우리의 μŠ€νƒ€μΌλ§ μ†”λ£¨μ…˜ μœ„μ— 항상 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€. μ—¬κΈ°μ—μ„œ ν•΄λ‹Ή κΈ°λŠ₯을 κ΅¬ν˜„ν•˜λŠ” μ§„μ •ν•œ λ™κΈ°λŠ” ꡬ성 μš”μ†Œμ˜ 더 κ°„λ‹¨ν•˜κ³  더 λ‚˜μ€ λ‚΄λΆ€ κ΅¬ν˜„μ— κ΄€ν•œ κ²ƒμž…λ‹ˆλ‹€.

react-jss injectStyles() 도 이 μž‘μ—…μ„ μˆ˜ν–‰ν•˜μ§€λ§Œ StyleRuleCallback 에 propsλ₯Ό μΆ”κ°€ν•˜λŠ” 것이 더 λ‚˜μ€ 것 κ°™μŠ΅λ‹ˆλ‹€.

const styles = (props, theme) => ({})
κ·Έλ ‡κ²Œ ν•˜λ©΄ μ†Œν’ˆμ— 따라 κ°’μ—λ§Œ μ œν•œλ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

@Shamplol ν”Όλ“œλ°±μ„ 톡해 μ‚¬μš©μžκ°€ 사전 μ •μ˜λœ ν•­λͺ© λŒ€μ‹  μ‚¬μš©μž μ •μ˜ color λ₯Ό ꡬ성 μš”μ†Œμ— μ œκ³΅ν•  수 μžˆλ„λ‘ 이 μƒˆλ‘œμš΄ κΈ°λŠ₯을 ν™œμš©ν•  수 μžˆλ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€. CSS 크기 영ν–₯은 λͺ…ν™•ν•˜μ§€ μ•Šμ§€λ§Œ ꡬ성 μš”μ†Œ 색상 μž¬μ •μ˜λ₯Ό 훨씬 더 κ°„λ‹¨ν•˜κ²Œ λ§Œλ“­λ‹ˆλ‹€. 이것은 쑰사할 μ‚¬ν•­μž…λ‹ˆλ‹€. 그런 λ‹€μŒ CSS λ³€μˆ˜ λΈŒλΌμš°μ € 지원이 μΆ©λΆ„νžˆ λ†’μœΌλ©΄(μ§€κΈˆλΆ€ν„° 2λ…„?), μš°λ¦¬λŠ” 그것에 μ˜μ‘΄ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

@oliviertassinari κ²½μš°μ— 따라 CSS 크기가 μ‹€μ œλ‘œ 쀄어듀지 μ•ŠμŠ΅λ‹ˆκΉŒ?
λ‚΄κ°€ μ•„λŠ” ν•œ, μš°λ¦¬λŠ” ν˜„μž¬ ...Primary 및 ...Accent λŒ€ν•œ λͺ¨λ“  클래슀λ₯Ό μ •μ˜ν•©λ‹ˆλ‹€. 이 변경은 ...Colorized λŒ€ν•œ 클래슀만 μœ μ§€ν•΄μ•Ό ν•œλ‹€λŠ” 것을 μ˜λ―Έν•˜μ§€ μ•ŠμŠ΅λ‹ˆκΉŒ? μ•„λ‹ˆλ©΄ μƒμ„±λœ CSSκ°€ κ±±μ •λ˜μ‹­λ‹ˆκΉŒ?

μ–΄λŠ μͺ½μ΄λ“ , 기본적으둜 https://github.com/callemall/material-ui/blob/v1-beta/src/Button/Button.js#L11 κ³Ό 같은 λ³΅μž‘ν•œ 클래슀λ₯Ό λ‹€μ‹œ κ΅¬ν˜„ν•΄μ•Ό ν•˜λ―€λ‘œ dxκ°€ 크게 ν–₯상될 것이라고 μƒκ°ν•©λ‹ˆλ‹€. λΉ„ νŒ”λ ˆνŠΈ 색상을 μ‚¬μš©ν•˜κ³  μ‹ΆμŠ΅λ‹ˆλ‹€.

μ–΄λ–€ κ²½μš°μ—λŠ” CSS 크기가 μ‹€μ œλ‘œ 쀄어듀지 μ•ŠμŠ΅λ‹ˆκΉŒ?

@sakulstra λ§ν•˜κΈ° μ–΄λ ΅μŠ΅λ‹ˆλ‹€. κ΅¬ν˜„μ— 따라 λ‹€λ¦…λ‹ˆλ‹€. μ•„λ§ˆλ„ :).

TypeScript 타이핑 κ΄€μ μ—μ„œ _ props 및 theme λͺ¨λ‘ μŠ€νƒ€μΌ 사양 λ‚΄μ—μ„œ λ‹€μŒκ³Ό 같이 μ•‘μ„ΈμŠ€ν•  수 있으면 쒋을 κ²ƒμž…λ‹ˆλ‹€.

const styles = {
  button: {
    background: ({ theme }) => theme.palette.primary[200]
  },
  root: {
    backgroundImage: ({ props }) => `url(${props.backgroundImage})`
  }
};

κ·Έ μ΄μœ λŠ” TypeScriptκ°€ ν•¨μˆ˜ 개체λ₯Ό 전달할 λ•Œ withStyles λŒ€ν•œ μ˜¬λ°”λ₯Έ μœ ν˜•μ„ μΆ”λ‘ ν•˜λŠ” 데 자주 μ‹€νŒ¨ν•˜κΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€. λ”°λΌμ„œ μž‘λ™ν•˜λ €λ©΄ μΆ”κ°€ μœ ν˜• 주석을 μ œκ³΅ν•΄μ•Ό ν•©λ‹ˆλ‹€.

withStyles<'button' | 'root'>(theme => ...)

μ†Œν’ˆλ„ μ „λ‹¬λ˜λ©΄ λ‹€μŒμ΄ λ©λ‹ˆλ‹€.

withStyles<'button' | 'root', Props>((theme, props) => ...)

ν˜„μž¬ μƒνƒœλŠ” μ–΄λ–»μŠ΅λ‹ˆκΉŒ? κ·Έ κΈ°λŠ₯이 있으면 정말 μ’‹κ² μ§€λ§Œ

@lucasmafra 이 κΈ°λŠ₯을 v1 릴리슀 μ΄μ •ν‘œ 이후에 μΆ”κ°€ν–ˆμŠ΅λ‹ˆλ‹€. v1을 빨리 μΆœμ‹œν• μˆ˜λ‘ μ’‹μŠ΅λ‹ˆλ‹€.

이 κΈ°λŠ₯은 μ†Œν’ˆ, λ―Έλ””μ–΄ 쿼리 및 λŒ€ν™”ν˜• μƒνƒœλ₯Ό κ²°ν•©ν•˜λŠ” ν‘œν˜„ μŠ€νƒ€μΌ κ·œμΉ™μ„ μž‘μ„±ν•  수 μžˆλŠ” ν•΅μ‹¬μž…λ‹ˆλ‹€. ν•΄λ‹Ή κΈ°λŠ₯을 인라인 μŠ€νƒ€μΌλ‘œ λ°”κΏ€ 수 μ—†μŠ΅λ‹ˆλ‹€. λΆˆν–‰νžˆλ„ withStyles λŠ” 이것이 좔가될 λ•ŒκΉŒμ§€ λ‚΄ ν”„λ‘œμ νŠΈμ—μ„œ μ‚¬μš©ν•  수 μ—†μŠ΅λ‹ˆλ‹€.

μ–Έμ œλ“ μ§€ μ‚¬μš©μž μ •μ˜ ν…Œλ§ˆλ₯Ό μ„€μ •ν•  수 있고 λ‹€μŒ κ·œμΉ™μ„ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€. μš°λ¦¬λŠ” μŠ€νƒ€μΌμ΄ μ§€μ •λœ ꡬ성 μš”μ†Œκ°€ λͺ¨λ‘ theme={theme}λ₯Ό ν˜ΈμΆœν•  λ•Œ MuiThemeProvider 내뢀에 ThemeProviderλ₯Ό μ€‘μ²©ν•˜μ—¬ 기본적으둜 props.theme에 μ•‘μ„ΈμŠ€ν•  수 μžˆλŠ” 방법을 정말 μ’‹μ•„ν•©λ‹ˆλ‹€. muiκ°€ λ…ΈμΆœν•˜λŠ” κΈ°λ³Έ ν…Œλ§ˆλ₯Ό ν™•μž₯ν•©λ‹ˆλ‹€.

//inline with HOC Method
 h1 style= {{ 'color: this.props.theme.palette.primary[500]' }}

//styled components ( no method necessary )
const Heading = styled.h1`
  color: ${p => p.theme.palette.primary['500']};
`

μ €λŠ” κ·Έλƒ₯ ν•¨μˆ˜ 값을 μ‚¬μš©ν•΄μ•Ό ν•΄μ„œ react-jss의 injectSheet() ν•¨μˆ˜λ₯Ό μ‚¬μš©ν–ˆμŠ΅λ‹ˆλ‹€. Mui의 withStyles() ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜λŠ”λ°, withStyle λŒ€μ‹  injectSheetλ₯Ό μ‚¬μš©ν•  λ•Œ 단점이 μžˆμŠ΅λ‹ˆκΉŒ?

@damien-monni MUI ν…Œλ§ˆμ— μ•‘μ„ΈμŠ€ν•  수 μ—†μŠ΅λ‹ˆλ‹€.

μ•ˆλ…•ν•˜μ„Έμš”, Material-UI styled-components-like API의 제 버전을 κ³΅μœ ν•˜κ³  μ‹Άμ—ˆμŠ΅λ‹ˆλ‹€. "많이" ν…ŒμŠ€νŠΈλ˜μ§€λŠ” μ•Šμ•˜μ§€λ§Œ 잘 μž‘λ™ν•˜κ³  λ‹€μŒμ„ μ œκ³΅ν•©λ‹ˆλ‹€.

  • props 및 theme λ₯Ό μŠ€νƒ€μΌ κΈ°λŠ₯으둜 λŒμ–΄ μ˜¬λ ΈμŠ΅λ‹ˆλ‹€.
  • λž˜ν•‘λœ ꡬ성 μš”μ†Œμ— μ‚¬μš©μž μ •μ˜ props 전달
  • React.forwardRef λ₯Ό 톡해 μ°Έμ‘° 전달
    (νŽΈμ§‘: μ°Έμ‘°λŠ” ν˜„μž¬ λŒ€λΆ€λΆ„μ˜ κΈ°λ³Έ Material-UI ꡬ성 μš”μ†Œκ°€ μ•„λ‹Œ html νƒœκ·Έμ—μ„œλ§Œ μž‘λ™ν•˜λ―€λ‘œ λ‚΄λΆ€ 지원이 ν•„μš”ν•©λ‹ˆλ‹€. μ‹œκ°„μ΄ μžˆμ„ λ•Œ PRμ—μ„œ μž‘λ™ν•  수 있으며 function λ˜λŠ” object 수락이 ν•„μš”ν•  κ²ƒμž…λ‹ˆλ‹€

κ΄€λ ¨ 문제: #10825, #7633

styled.js

import React, { Component } from 'react';
import classNames from 'classnames';
import PropTypes from 'prop-types';
import { withStyles } from 'material-ui/styles';

function styled(WrappedComponent, customProps) {
  return (style, options) => {
    class StyledComponent extends Component {
      render() {
        let {
          forwardRef,
          classes,
          className,
          ...passThroughProps
        } = this.props;
        return (
          <WrappedComponent
            ref={forwardRef}
            className={classNames(classes.root, className)}
            {...passThroughProps}
            {...customProps}
          />
        );
      }
    }

    StyledComponent.propTypes = {
      classes: PropTypes.object.isRequired,
      className: PropTypes.string,
    };

    let hoistedProps = {};

    const styles =
      typeof style === 'function'
        ? theme => {
            return { root: style({ ...theme, theme, props: hoistedProps }) };
          }
        : { root: style };

    const WithRef = withStyles(styles, options)(StyledComponent);

    return React.forwardRef((props, ref) => {
      hoistedProps = props;
      return <WithRef {...props} forwardRef={ref} />;
    });
  };
}

export default styled;

μ‚¬μš© 예

const MobileNav = styled('nav')(({ theme, props }) => ({
  '&.pos-fixed': {
    top: 0,
    position: props.fixed ? 'fixed' : 'absolute',
    zIndex: 99,
    animation: 'fadeInDown 0.3s ease-out forwards',
    backgroundColor: theme.palette.common.white,
  },
}));

μ»€μŠ€ν…€ μ†Œν’ˆμœΌλ‘œ

const StyledButton = styled(Button, { component: Link })(
  ({ theme, props }) => ({
    color: props.disabled ? 'gray' : 'blue',
    [theme.breakpoints.down('sm')]: {
      color: 'red',
    },
  })
);

@oliviertassinari 이 문제λ₯Ό ν•΄κ²°ν•˜κΈ° μœ„ν•΄ μ•„λž˜ μ†”λ£¨μ…˜μ„ μ‚¬μš©ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

    componentWillMount() {
        const {color} = this.props;
        jss.setup(jssPreset());
        const stylesheet = jss.createStyleSheet({
            underline: {
              "&:after": {
                backgroundColor: `${color} !important`
              }
            }
        }).attach();
        this.underlineClass = stylesheet.classes.underline;
    }

잘 μž‘λ™ν•˜μ§€λ§Œ λ‚΄κ°€ 보지 λͺ»ν•˜λŠ” 잠재적인 λ¬Έμ œκ°€ μžˆμŠ΅λ‹ˆκΉŒ? 예λ₯Ό λ“€μ–΄ jss.setup을 두 번 ν˜ΈμΆœν•΄μ•Ό ν•˜λŠ” 것이 λ§ˆμŒμ— 듀지 μ•ŠμŠ΅λ‹ˆλ‹€. jss 수λͺ… μ£ΌκΈ°λ₯Ό μ΄ν•΄ν•˜κ³  μžˆλŠ”μ§€ 잘 λͺ¨λ₯΄κ² μŠ΅λ‹ˆλ‹€. μ—¬κΈ°μ„œ setup() λ₯Ό ν˜ΈμΆœν•΄μ•Ό ν•œλ‹€λŠ” 사싀에 λ†€λžμŠ΅λ‹ˆλ‹€.

잘 μž‘λ™ν•˜μ§€λ§Œ λ‚΄κ°€ 보지 λͺ»ν•˜λŠ” 잠재적인 λ¬Έμ œκ°€ μžˆμŠ΅λ‹ˆκΉŒ?

@wcandillon λ‚΄κ°€ λ³Ό 수 μžˆλŠ” λͺ‡ 가지 잠재적인 문제: 1. ꡬ성 μš”μ†Œμ˜ μΈμŠ€ν„΄μŠ€κ°€ 마운트될 λ•Œλ§ˆλ‹€ μƒˆ 클래슀 이름을 μ‚½μž…ν•©λ‹ˆλ‹€. 2. μ„œλ²„ μΈ‘μ—μ„œ ꡬ성 μš”μ†Œλ₯Ό λ Œλ”λ§ν•  수 μ—†μŠ΅λ‹ˆλ‹€. 3. μ˜¬λ°”λ₯Έ CSS μž¬μ •μ˜ μ£Όμž… μˆœμ„œλ₯Ό 얻지 λͺ»ν•  κ²ƒμž…λ‹ˆλ‹€.

μŠ€νƒ€μΌμ΄ μžˆλŠ” μ†Œν’ˆμ„ 갖도둝 이 라이브러리λ₯Ό κ°œλ°œν–ˆμŠ΅λ‹ˆλ‹€.

https://github.com/JacquesBonet/jss-material-ui

ν”„λ‘œμ νŠΈμ—μ„œ μ„±κ³΅μ μœΌλ‘œ ν…ŒμŠ€νŠΈλ˜μ—ˆμŠ΅λ‹ˆλ‹€.

μ²˜μŒμ—λŠ” danielmahon μ†”λ£¨μ…˜μ„ μ‚¬μš©ν•˜μ§€λ§Œ μŠ€νƒ€μΌ 상속 λ¬Έμ œκ°€ λ°œμƒν•©λ‹ˆλ‹€.

@oliviertassinari ν˜„μž¬ 동적 CSS/μ• λ‹ˆλ©”μ΄μ…˜μ„ λ§Œλ“€κΈ° μœ„ν•œ λŒ€μ•ˆμ΄ μžˆμŠ΅λ‹ˆκΉŒ? 감사 ν•΄μš” :)

@HunderlineK λͺ‡ 가지 λŒ€μ•ˆ: https://material-ui.com/customization/overrides/#2 -dynamic-variation-for-a-one-time-situation.

@danielmahon λ‹Ήμ‹ μ˜ μ ‘κ·Ό 방식은 λ°”λ‘œ μ§€κΈˆ λ‚΄ 문제λ₯Ό ν•΄κ²°ν•˜κΈ° μœ„ν•΄ μ°Ύκ³  μžˆλŠ” κ²ƒμž…λ‹ˆλ‹€. ν•˜μ§€λ§Œ "μŠ€νƒ€μΌ ꡬ성 μš”μ†Œ"λŠ” μƒˆ μ†Œν’ˆμ„ 받을 λ•Œ λ‹€μ‹œ λ Œλ”λ§λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. λ‹€λ₯Έ 것을 μ‹œλ„ν•΄ λ³΄μ…¨μŠ΅λ‹ˆκΉŒ?

λ‚˜λŠ” λ‹€λ₯Έ 것을 생각할 것이고, λ‚΄κ°€ μƒκ°λ‚˜λŠ” 것이 있으면 λ‹Ήμ‹ μ—κ²Œ μ•Œλ €μ€„ 것이닀.

πŸ’° 방금 $50 ν˜„μƒκΈˆμ„ κ±Έμ—ˆμŠ΅λ‹ˆλ‹€ :)

https://www.bountysource.com/issues/47838203-withstyles-add-the-ability-to-get-the-component-s-props-from-within-the-style-object

@lewisdiamondκ°€ λ§ν–ˆλ“―μ΄ const styles = (props, theme) => ({}) λŠ” 정말 κΉ”λ”ν•©λ‹ˆλ‹€.

λ˜λŠ” const styles = (theme, props) => ({}) 이 아닐 μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.

이 문제λ₯Ό v1.x λ§ˆμΌμŠ€ν†€ 에 μΆ”κ°€ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€. λ§Žμ€ 뢄듀이 μš”μ²­ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€. μš°λ¦¬λŠ” 곧 그것을 얻을 κ²ƒμž…λ‹ˆλ‹€.

capture d ecran 2018-07-29 a 13 12 03

@oliviertassinari 이것은 μ•„λ§ˆλ„ withStyles μž…λ ₯에 μ€‘μš”ν•œ μ˜λ―Έκ°€ μžˆμ„ κ²ƒμž…λ‹ˆλ‹€. μ œκ°€ λ„μ™€λ“œλ¦΄ 수 μžˆλŠ”μ§€ μ•Œλ €μ£Όμ„Έμš”.

@pelotom κ°μ‚¬ν•©λ‹ˆλ‹€, μ•Œλ €λ“œλ¦¬κ² μŠ΅λ‹ˆλ‹€. 이번 달에 문제λ₯Ό μ‚΄νŽ΄λ³Ό 수 있기λ₯Ό λ°”λžλ‹ˆλ‹€.

이에 λŒ€ν•œ μž‘μ—…μ΄ 진행 μ€‘μž…λ‹ˆκΉŒ? IMO의 핡심 κΈ°λŠ₯μž…λ‹ˆλ‹€. μ œκ°€ λ„μ™€λ“œλ¦΄ 수 μžˆμŠ΅λ‹ˆλ‹€.

νŽΈμ§‘: λ‚˜λŠ” 그것에 λŒ€ν•΄ μž‘μ—…ν•˜κΈ° μ‹œμž‘ν–ˆμŠ΅λ‹ˆλ‹€. withStyles ν—ˆμš©ν•˜λŠ” styles ν•¨μˆ˜μ— propsλ₯Ό μ „λ‹¬ν–ˆμ§€λ§Œ λ¬Έμ œλŠ” propsκ°€ 변경될 λ•Œ μŠ€νƒ€μΌμ΄ μ—…λ°μ΄νŠΈλ˜μ§€ μ•ŠλŠ”λ‹€λŠ” κ²ƒμž…λ‹ˆλ‹€. ν•΄κ²°λ˜λ©΄ PR을 μž‘μ„±ν•©λ‹ˆλ‹€.

μ•ˆλ…•ν•˜μ„Έμš” μ €λŠ” 아바타 ꡬ성 μš”μ†Œμ˜ 색상을 μ‚¬μš©μž μ •μ˜ν•˜κΈ° μœ„ν•΄ 이것이 ν•„μš”ν•œ μ‚¬μš© 사둀λ₯Ό λ°œκ²¬ν–ˆμœΌλ©° 이것 μ΄μ™Έμ˜ ꡬ성 μš”μ†Œμ˜ λͺ¨λ“  λ³€ν˜•μ— λŒ€ν•œ μŠ€νƒ€μΌμ„ μ œμ–΄ν•  수 μžˆλŠ” λ‹€λ₯Έ 방법이 μ—†μŠ΅λ‹ˆλ‹€.

const styles = theme => ({
  chip:{
  },
  separator: {
    marginRight: theme.spacing.unit,
  },
  fright: {
    float: 'right',
  },
  fleft: {
    float: 'left',
  },
  outlinedPrimary:{
    color: props =>  stringToColor( props.username),
    border: props => `1px solid ${fade(stringToColor(props.username), 0.5)}`,
    '$clickable&:hover, $clickable&:focus, $deletable&:focus': props => ({
      backgroundColor: fade(stringToColor(props.username), theme.palette.action.hoverOpacity),
      border: `1px solid ${stringToColor(props.username)}`,
      }),
  },
  outlined: {
    backgroundColor: 'transparent',
    border: props => `1px solid ${
      theme.palette.type === 'light' ? stringToColor(props.username) : fade(stringToColor(props.username))
    }`,
    '$clickable&:hover, $clickable&:focus, $deletable&:focus': props => ({
      backgroundColor: fade(stringToColor(props.username), theme.palette.action.hoverOpacity),
      }),
    },
});

λ‚΄ μ†”λ£¨μ…˜ Japrogramerλ₯Ό 확인할 수 μžˆμŠ΅λ‹ˆλ‹€. https://github.com/JacquesBonet/jss-material-ui

κ°μ‚¬ν•©λ‹ˆλ‹€. 잘 μ‚΄νŽ΄λ³΄κ² μŠ΅λ‹ˆλ‹€.

였늘 일찍 이 κΈ°λŠ₯이 ν•„μš”ν–ˆκΈ° λ•Œλ¬Έμ— HOCλ₯Ό μž‘μ„±ν–ˆμŠ΅λ‹ˆλ‹€. withStyles λŠ” 자체적으둜 캐싱을 μˆ˜ν–‰ν•˜λ―€λ‘œ 이것이 μ–Όλ§ˆλ‚˜ 영ν–₯을 λ―ΈμΉ˜λŠ”μ§€ 말할 μˆ˜λŠ” μ—†μ§€λ§Œ μ—¬κ°€ μ‹œκ°„μ— withStyles 의 캐싱 κ΅¬ν˜„μ„ μ‚΄νŽ΄λ³΄κ² μŠ΅λ‹ˆλ‹€. μ†Œν’ˆκ³Ό ν…Œλ§ˆλ₯Ό λΉ λ₯΄κ²Œ μ‚¬μš©ν•˜μ—¬ λ©‹μ§€κ²Œ ν”Œλ ˆμ΄ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

κ²½κ³ 

이 ꡬ성 μš”μ†ŒλŠ” propsκ°€ withStyles HOCμ—μ„œ λ³€κ²½λ˜λŠ” μŠ€νƒ€μΌμ‹œνŠΈ 클래슀의 인덱슀 번호 λ˜λŠ” 무언가λ₯Ό λ³€κ²½ν•˜λŠ” 경우 전체 λ‹€μ‹œ 마운트λ₯Ό μˆ˜ν–‰ν•©λ‹ˆλ‹€.

import React from 'react';
import { withStyles } from '@material-ui/core/styles';

const { createElement, forwardRef } = React

const withPropsStyles = ( style ) => {

    const withPropsStyles = ( component ) => {

        return forwardRef( (props, ref) => {

            const proxy = (theme) => style(props, theme)

            const hoc = withStyles(proxy)(component)

            return props.children ? 
                createElement(hoc, { ...props, ref}, props.children) :
                createElement(hoc, {...props, ref}) 
        }) 
    }

    return withPropsStyles
}

export default withPropsStyles

μ‚¬μš© 예

const styles = (props, theme) => ({
    root:{
        backgroundColor: props.light ? theme.palette.primary.light : theme.palette.primary.main 
    },
})

const SomeComponent = ({classes}) => <div className={classes.root}/>

export default withPropsStyles(styles)(SomeComponent)

κ²°λ‘ 

κ°„λ‹¨ν•˜κ³  μž‘λ™ν•©λ‹ˆλ‹€(κ·ΈλŸ¬λ‚˜ 전체 λ‹€μ‹œ 마운트 λΉ„μš©λ„ rem)

이 λ³€κ²½μœΌλ‘œ 100% JSSλ₯Ό μœ„ν•΄ λΌμ΄λΈŒλŸ¬λ¦¬μ—μ„œ 인라인 μŠ€νƒ€μΌ μ‚¬μš©μ„ μ œκ±°ν•  수 μžˆμŠ΅λ‹ˆκΉŒ? λ‚΄ 앱은 인라인 μŠ€νƒ€μΌμ—μ„œ μž‘λ™ν•˜μ§€ μ•ŠμœΌλ©° νŒŒκΈ‰ 효과λ₯Ό JSS둜 κ΅μ²΄ν•˜λ €κ³  ν–ˆμ„ λ•Œ 이 κΈ°λŠ₯이 ν•„μš”ν•˜λ‹€λŠ” 것을 κΉ¨λ‹¬μ•˜μŠ΅λ‹ˆλ‹€. μ•½κ°„μ˜ μ„±λŠ₯ μ €ν•˜κ°€ μžˆμ„ 수 μžˆμ§€λ§Œ 더 깨끗해 λ³΄μž…λ‹ˆλ‹€.

@koshea λŠ” 인라인 μŠ€νƒ€μΌμ„ withStyles λ₯Ό λŒ€μ²΄ν•˜λŠ” λ“œλ‘­μΈμœΌλ‘œ 잘 μž‘λ™ν•΄μ•Ό ν•©λ‹ˆλ‹€.

인라인 μŠ€νƒ€μΌμ„ μ‚¬μš©ν•˜λ©΄ κ°•λ ₯ν•œ μ½˜ν…μΈ  λ³΄μ•ˆ 정책을 μ‚¬μš©ν•  수 μ—†λ‹€λŠ” 점도 μ–ΈκΈ‰ν•˜κ³  μ‹Άμ—ˆμŠ΅λ‹ˆλ‹€.
μ•ˆμ „ν•˜μ§€ μ•Šμ€ μŠ€νƒ€μΌ μ§€μ‹œλ¬Έμ— unsafe-inline ν”Œλž˜κ·Έλ₯Ό μΆ”κ°€ν•΄μ•Ό ν•©λ‹ˆλ‹€.

μ†Œν’ˆ 지원이 μžˆλŠ” 동적 μŠ€νƒ€μΌμ€ 이 문제λ₯Ό ν•΄κ²°ν•΄μ•Ό ν•©λ‹ˆλ‹€.

μ•ˆλ…•ν•˜μ„Έμš” μ—¬λŸ¬λΆ„, "이런 μ‹μœΌλ‘œ" 토둠에 λ›°μ–΄λ“€μ–΄μ„œ μ£„μ†‘ν•©λ‹ˆλ‹€. λ‚˜λŠ” μ΅œκ·Όμ— Mui(Typescript 포함)λ₯Ό μ‚¬μš©ν•˜κΈ° μ‹œμž‘ν–ˆμœΌλ©° 맀우 κ°•λ ₯ν•œ 라이브러리라고 μƒκ°ν•˜μ§€λ§Œ ν™•μ‹€νžˆ λ³΅μž‘ν•©λ‹ˆλ‹€.

μœ„μ˜ 일뢀 λŒ“κΈ€μ—μ„œ 이 κΈ°λŠ₯이 (props, theme) => {} λ˜λŠ” (theme, props) => {} μ—¬μ•Ό ν•˜λŠ”μ§€μ— λŒ€ν•œ μ•½κ°„μ˜ 토둠이 μžˆμŒμ„ μ•Œμ•˜μŠ΅λ‹ˆλ‹€. @pelotom이 props 및 theme λͺ…λͺ…λœ 속성을 λͺ¨λ‘ λ§Œλ“œλŠ” 것에 λŒ€ν•΄ 그의 의견 을 κ°•ν™”ν•˜κ³  μ‹ΆμŠ΅λ‹ˆλ‹€. κ·Έλ ‡κ²Œ ν•˜λ©΄ 이 λ³€κ²½ 사항이 적용되면 μŠ€νƒ€μΌ μ •μ˜λ₯Ό λ¦¬νŒ©ν† λ§ν•˜κΈ°κ°€ 더 μ‰¬μ›Œμ§ˆ κ²ƒμž…λ‹ˆλ‹€. 건배 πŸ™‚

κΈ°λ‹€λ €μ£Όμ‹  λͺ¨λ“  λΆ„λ“€κ»˜ κ°μ‚¬λ“œλ¦½λ‹ˆλ‹€! 이 λ¬Έμ œλŠ” #13503μ—μ„œ 처리되고 μžˆμŠ΅λ‹ˆλ‹€. κ΅¬ν˜„ν•˜λ €λŠ” ꡬ성 μš”μ†Œ λ„μš°λ―Έμ˜ μš”κ΅¬ μ‚¬ν•­μž…λ‹ˆλ‹€. μš°λ¦¬λŠ” λ˜ν•œ 후크 API μ‹€ν—˜μ„ μ‹œμž‘ν–ˆμŠ΅λ‹ˆλ‹€: https://twitter.com/olivtassinari/status/1058805751404261376.

이거 4.0으둜 λ‚˜μ™”λ‚˜μš”? makeStyles μ½œλ°±μ— props λ§€κ°œλ³€μˆ˜κ°€ μ—†λŠ” 것 κ°™μŠ΅λ‹ˆλ‹€.

@city41 const classes = useStyles(props);

μ•Œ κ² μ–΄μš”. κ·Έλž˜μ„œ 그런 것 κ°™λ‹€.

const useStyles = makeStyles(theme => {
    return {
        foo: {
            color: theme.props.danger ? '#ff0000' : '#00ff00'
        }
    };
});

function MyComponent(props) {
    const classes = useStyles(props);

    return <div className={classes.foo}>...</div>;
}

μ›Ή μ‚¬μ΄νŠΈμ˜ μŠ€νƒ€μΌ API μ„Ήμ…˜μ— 이것이 λ¬Έμ„œν™”λ˜μ–΄ μžˆλŠ” 것을 λ³Ό 수 μ—†μŠ΅λ‹ˆλ‹€. λ‚΄κ°€ PR을 보낼 수 μžˆλŠ”μ§€ 보자.

@city41 https://material-ui.com/styles/basics/#adapting -based -on-props에 λ¬Έμ„œ μ‹œμž‘μ΄ μžˆμŠ΅λ‹ˆλ‹€.

멋지고, λ¬Έμ„œκ°€ μ—…λ°μ΄νŠΈλ˜λŠ” 것을 보게 λ˜μ–΄ κΈ°μ©λ‹ˆλ‹€. 이 문제λ₯Ό λ‹€λ£¨λŠ” λ‹€λ₯Έ μ‚¬λžŒμ„ μœ„ν•΄ λ‹€μŒμ€ ꡬ성 μš”μ†Œμ˜ μŠ€νƒ€μΌμ„ μ§€μ •ν•˜κΈ° μœ„ν•΄ ν…Œλ§ˆμ™€ μ†Œν’ˆμ„ κ²°ν•©ν•œ λ°©λ²•μž…λ‹ˆλ‹€.

import React from 'react';
import { Button, Theme, makeStyles } from '@material-ui/core';

interface ButtonProps {
    destructive: boolean;
}

const useButtonStyles = makeStyles<Theme, ButtonProps>(theme => {
    return {
        root: props => ({
            backgroundColor: props.destructive ? theme.palette.error.main : theme.palette.primary.main
        })
    };
});

export const PrimaryButton: React.FunctionComponent<ButtonProps> = props => {
    const classes = useButtonStyles(props);

    return <Button {...props} className={classes.root} variant="contained" />;
};

μ™ΈλΆ€ μŠ€νƒ€μΌ json νŒŒμΌμ—μ„œ prop을 μ–΄λ–»κ²Œ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆκΉŒ?

예λ₯Ό λ“€μ–΄ 이것은 μ™ΈλΆ€ νŒŒμΌμž…λ‹ˆλ‹€.

const typographyStyle = {
  title2: {
    fontFamily:"Montserrat",
    fontStyle:"normal",
    fontWeight:"800",
    fontSize:"72px",
    lineHeight:"88px",
    letterSpacing:"-0.02em",
    // color:"#304C82"
    color : props => {
      console.log(props,'c1');
      return props.color
    }

  }
};

export default typographyStyle;

이 νŒŒμΌμ„ κ°€μ Έμ™€μ„œ 개체λ₯Ό νΌλœ¨λ¦½λ‹ˆλ‹€.

import typographyStyle from "../assets/jss/material-kit-pro-react/views/componentsSections/typographyStyle";


const styles = theme =>  ({
    ...typographyStyle,
    homeSearch:{
        width: '100%',
        '& div':{
            '& input':{
                "color":"#304C82",
                height: 65,
                fontFamily: 'Open Sans',
                fontStyle: 'normal',
                fontWeight: 800,
                fontSize: '48px',
                lineHeight: '65px',
                letterSpacing: '-0.03em',
                '&::placeholder':{
                    fontFamily: 'Open Sans',
                    fontStyle: 'normal',
                    fontWeight: 800,
                    fontSize: '48px',
                    lineHeight: '65px',
                    letterSpacing: '-0.03em',
                    color: '#EAEAEA'
                }
            }
        }
    },

  });

이제 색상 κΈ°λŠ₯μ—μ„œ props = {} λ₯Ό μ–»μŠ΅λ‹ˆλ‹€.

λˆ„κ΅°κ°€κ°€ 이와 κ΄€λ ¨ν•˜μ—¬ λ‚˜λ₯Ό λ„μšΈ 수 μžˆμŠ΅λ‹ˆκΉŒ?

μ—…λ°μ΄νŠΈ:

λ‚΄ κΈ°λ³Έ styles.js νŒŒμΌμ—μ„œλ„ 빈 κ°œμ²΄κ°€ λ°œμƒν•˜κΈ° λ•Œλ¬Έμ— λ­”κ°€ 잘λͺ»λœ 일을 ν•˜κ³  μžˆλŠ” 것 κ°™μŠ΅λ‹ˆλ‹€.

    homeSearch: props => {
        console.log(props);
        return {
        width: '100%',
        border:  `1px solid ${props.color}`
        ,
        '& div':{
            '& input':{
                "color":"#304C82",
                height: 65,
                fontFamily: 'Open Sans',
                fontStyle: 'normal',
                fontWeight: 800,
                fontSize: '48px',
                lineHeight: '65px',
                letterSpacing: '-0.03em',
                '&::placeholder':{
                    fontFamily: 'Open Sans',
                    fontStyle: 'normal',
                    fontWeight: 800,
                    fontSize: '48px',
                    lineHeight: '65px',
                    letterSpacing: '-0.03em',
                    color: '#EAEAEA'
                }
            }
        }
    }
},
이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰