Extend JSS style class instead of overwriting it












0















In my React app, I use React JSS for styling. Suppose I have these two files (skipping imports and another non interesting stuff).



This is App.js:



const styles = {
root: {
backgroundColor: '#ffffff',
},
header: {
backgroundColor: '#ff0000',
}
};

class App extends Component {
render() {
const { classes } = this.props;
return (
<div className={classes.root}>
<Header classes={{ root: classes.header }}/>
</div>
);
}
}

export default withStyles(styles)(App);


and this is Header.js:



const styles = theme => ({
root: {
backgroundColor: '#0000ff',
padding: '1em',
},
});

class Header extends Component {
render() {
const { classes } = this.props;
return (
<header className={classes.root}>
Hello header
</header>
);
}
}

export default withStyles(styles)(Header);


What I would like to have is "overriding" the style of the root component of Header without overwriting it completely. I can do either of two things:




  1. use <Header className={classes.header}/>, which results in the header element having the class App-root-0-1-2, which means the background is blue with the padding;


  2. use <Header classes={{ root: classes.header }}/> (as above), which results in the header element having the class App-header-0-1-2, which means the background is read without padding.



It seems I can only have either the style defined by the component OR the one that the parent defines to override it. However, I would like to extend the internal style with the one passed by the parent - of course, with the parent taking precedence in conflicts. In this case, I wish to have the red background with the padding.



How can I achieve that? Is it impossible - do I need to pass the editable style as a property?










share|improve this question





























    0















    In my React app, I use React JSS for styling. Suppose I have these two files (skipping imports and another non interesting stuff).



    This is App.js:



    const styles = {
    root: {
    backgroundColor: '#ffffff',
    },
    header: {
    backgroundColor: '#ff0000',
    }
    };

    class App extends Component {
    render() {
    const { classes } = this.props;
    return (
    <div className={classes.root}>
    <Header classes={{ root: classes.header }}/>
    </div>
    );
    }
    }

    export default withStyles(styles)(App);


    and this is Header.js:



    const styles = theme => ({
    root: {
    backgroundColor: '#0000ff',
    padding: '1em',
    },
    });

    class Header extends Component {
    render() {
    const { classes } = this.props;
    return (
    <header className={classes.root}>
    Hello header
    </header>
    );
    }
    }

    export default withStyles(styles)(Header);


    What I would like to have is "overriding" the style of the root component of Header without overwriting it completely. I can do either of two things:




    1. use <Header className={classes.header}/>, which results in the header element having the class App-root-0-1-2, which means the background is blue with the padding;


    2. use <Header classes={{ root: classes.header }}/> (as above), which results in the header element having the class App-header-0-1-2, which means the background is read without padding.



    It seems I can only have either the style defined by the component OR the one that the parent defines to override it. However, I would like to extend the internal style with the one passed by the parent - of course, with the parent taking precedence in conflicts. In this case, I wish to have the red background with the padding.



    How can I achieve that? Is it impossible - do I need to pass the editable style as a property?










    share|improve this question



























      0












      0








      0








      In my React app, I use React JSS for styling. Suppose I have these two files (skipping imports and another non interesting stuff).



      This is App.js:



      const styles = {
      root: {
      backgroundColor: '#ffffff',
      },
      header: {
      backgroundColor: '#ff0000',
      }
      };

      class App extends Component {
      render() {
      const { classes } = this.props;
      return (
      <div className={classes.root}>
      <Header classes={{ root: classes.header }}/>
      </div>
      );
      }
      }

      export default withStyles(styles)(App);


      and this is Header.js:



      const styles = theme => ({
      root: {
      backgroundColor: '#0000ff',
      padding: '1em',
      },
      });

      class Header extends Component {
      render() {
      const { classes } = this.props;
      return (
      <header className={classes.root}>
      Hello header
      </header>
      );
      }
      }

      export default withStyles(styles)(Header);


      What I would like to have is "overriding" the style of the root component of Header without overwriting it completely. I can do either of two things:




      1. use <Header className={classes.header}/>, which results in the header element having the class App-root-0-1-2, which means the background is blue with the padding;


      2. use <Header classes={{ root: classes.header }}/> (as above), which results in the header element having the class App-header-0-1-2, which means the background is read without padding.



      It seems I can only have either the style defined by the component OR the one that the parent defines to override it. However, I would like to extend the internal style with the one passed by the parent - of course, with the parent taking precedence in conflicts. In this case, I wish to have the red background with the padding.



      How can I achieve that? Is it impossible - do I need to pass the editable style as a property?










      share|improve this question
















      In my React app, I use React JSS for styling. Suppose I have these two files (skipping imports and another non interesting stuff).



      This is App.js:



      const styles = {
      root: {
      backgroundColor: '#ffffff',
      },
      header: {
      backgroundColor: '#ff0000',
      }
      };

      class App extends Component {
      render() {
      const { classes } = this.props;
      return (
      <div className={classes.root}>
      <Header classes={{ root: classes.header }}/>
      </div>
      );
      }
      }

      export default withStyles(styles)(App);


      and this is Header.js:



      const styles = theme => ({
      root: {
      backgroundColor: '#0000ff',
      padding: '1em',
      },
      });

      class Header extends Component {
      render() {
      const { classes } = this.props;
      return (
      <header className={classes.root}>
      Hello header
      </header>
      );
      }
      }

      export default withStyles(styles)(Header);


      What I would like to have is "overriding" the style of the root component of Header without overwriting it completely. I can do either of two things:




      1. use <Header className={classes.header}/>, which results in the header element having the class App-root-0-1-2, which means the background is blue with the padding;


      2. use <Header classes={{ root: classes.header }}/> (as above), which results in the header element having the class App-header-0-1-2, which means the background is read without padding.



      It seems I can only have either the style defined by the component OR the one that the parent defines to override it. However, I would like to extend the internal style with the one passed by the parent - of course, with the parent taking precedence in conflicts. In this case, I wish to have the red background with the padding.



      How can I achieve that? Is it impossible - do I need to pass the editable style as a property?







      javascript reactjs jss






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 11:31









      janhartmann

      9,1421259120




      9,1421259120










      asked Nov 14 '18 at 11:15









      SimoneSimone

      4661916




      4661916
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You can provide an external class name and use classnames (https://github.com/JedWatson/classnames) (or just inline them) to conditionally render this class name if present:



          import classNames from "classnames";

          const styles = theme => ({
          root: {
          backgroundColor: '#0000ff',
          padding: '1em',
          },
          });

          class Header extends Component {
          render() {
          const { classes, className } = this.props;
          return (
          <header
          className={classNames({
          [classes.root]: true,
          [className]: className
          })}>
          Hello header
          </header>
          );
          }
          }

          export default withStyles(styles)(Header);


          Then use it:



          <Header className={classes.myParentClass} />


          This will result in a class names, e.g. Header-root-0-1-2 App-myParentClass-0-4-3






          share|improve this answer
























          • Thanks @janhartmann. This works great, but in the end it seems that I need to specify which component can receive an override, rather than it being the client of the component (i.e., its parent). It seems to me it would be much cleaner the other way around.

            – Simone
            Nov 14 '18 at 11:41






          • 1





            Yes, and in my opinion that is a good thing, in control! ;-) If you put the class name on the root element of the component, you can always from a parent style it like myParentClass: { "& > span": { color: "yellow" } }

            – janhartmann
            Nov 14 '18 at 11:55











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53298931%2fextend-jss-style-class-instead-of-overwriting-it%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You can provide an external class name and use classnames (https://github.com/JedWatson/classnames) (or just inline them) to conditionally render this class name if present:



          import classNames from "classnames";

          const styles = theme => ({
          root: {
          backgroundColor: '#0000ff',
          padding: '1em',
          },
          });

          class Header extends Component {
          render() {
          const { classes, className } = this.props;
          return (
          <header
          className={classNames({
          [classes.root]: true,
          [className]: className
          })}>
          Hello header
          </header>
          );
          }
          }

          export default withStyles(styles)(Header);


          Then use it:



          <Header className={classes.myParentClass} />


          This will result in a class names, e.g. Header-root-0-1-2 App-myParentClass-0-4-3






          share|improve this answer
























          • Thanks @janhartmann. This works great, but in the end it seems that I need to specify which component can receive an override, rather than it being the client of the component (i.e., its parent). It seems to me it would be much cleaner the other way around.

            – Simone
            Nov 14 '18 at 11:41






          • 1





            Yes, and in my opinion that is a good thing, in control! ;-) If you put the class name on the root element of the component, you can always from a parent style it like myParentClass: { "& > span": { color: "yellow" } }

            – janhartmann
            Nov 14 '18 at 11:55
















          1














          You can provide an external class name and use classnames (https://github.com/JedWatson/classnames) (or just inline them) to conditionally render this class name if present:



          import classNames from "classnames";

          const styles = theme => ({
          root: {
          backgroundColor: '#0000ff',
          padding: '1em',
          },
          });

          class Header extends Component {
          render() {
          const { classes, className } = this.props;
          return (
          <header
          className={classNames({
          [classes.root]: true,
          [className]: className
          })}>
          Hello header
          </header>
          );
          }
          }

          export default withStyles(styles)(Header);


          Then use it:



          <Header className={classes.myParentClass} />


          This will result in a class names, e.g. Header-root-0-1-2 App-myParentClass-0-4-3






          share|improve this answer
























          • Thanks @janhartmann. This works great, but in the end it seems that I need to specify which component can receive an override, rather than it being the client of the component (i.e., its parent). It seems to me it would be much cleaner the other way around.

            – Simone
            Nov 14 '18 at 11:41






          • 1





            Yes, and in my opinion that is a good thing, in control! ;-) If you put the class name on the root element of the component, you can always from a parent style it like myParentClass: { "& > span": { color: "yellow" } }

            – janhartmann
            Nov 14 '18 at 11:55














          1












          1








          1







          You can provide an external class name and use classnames (https://github.com/JedWatson/classnames) (or just inline them) to conditionally render this class name if present:



          import classNames from "classnames";

          const styles = theme => ({
          root: {
          backgroundColor: '#0000ff',
          padding: '1em',
          },
          });

          class Header extends Component {
          render() {
          const { classes, className } = this.props;
          return (
          <header
          className={classNames({
          [classes.root]: true,
          [className]: className
          })}>
          Hello header
          </header>
          );
          }
          }

          export default withStyles(styles)(Header);


          Then use it:



          <Header className={classes.myParentClass} />


          This will result in a class names, e.g. Header-root-0-1-2 App-myParentClass-0-4-3






          share|improve this answer













          You can provide an external class name and use classnames (https://github.com/JedWatson/classnames) (or just inline them) to conditionally render this class name if present:



          import classNames from "classnames";

          const styles = theme => ({
          root: {
          backgroundColor: '#0000ff',
          padding: '1em',
          },
          });

          class Header extends Component {
          render() {
          const { classes, className } = this.props;
          return (
          <header
          className={classNames({
          [classes.root]: true,
          [className]: className
          })}>
          Hello header
          </header>
          );
          }
          }

          export default withStyles(styles)(Header);


          Then use it:



          <Header className={classes.myParentClass} />


          This will result in a class names, e.g. Header-root-0-1-2 App-myParentClass-0-4-3







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 11:29









          janhartmannjanhartmann

          9,1421259120




          9,1421259120













          • Thanks @janhartmann. This works great, but in the end it seems that I need to specify which component can receive an override, rather than it being the client of the component (i.e., its parent). It seems to me it would be much cleaner the other way around.

            – Simone
            Nov 14 '18 at 11:41






          • 1





            Yes, and in my opinion that is a good thing, in control! ;-) If you put the class name on the root element of the component, you can always from a parent style it like myParentClass: { "& > span": { color: "yellow" } }

            – janhartmann
            Nov 14 '18 at 11:55



















          • Thanks @janhartmann. This works great, but in the end it seems that I need to specify which component can receive an override, rather than it being the client of the component (i.e., its parent). It seems to me it would be much cleaner the other way around.

            – Simone
            Nov 14 '18 at 11:41






          • 1





            Yes, and in my opinion that is a good thing, in control! ;-) If you put the class name on the root element of the component, you can always from a parent style it like myParentClass: { "& > span": { color: "yellow" } }

            – janhartmann
            Nov 14 '18 at 11:55

















          Thanks @janhartmann. This works great, but in the end it seems that I need to specify which component can receive an override, rather than it being the client of the component (i.e., its parent). It seems to me it would be much cleaner the other way around.

          – Simone
          Nov 14 '18 at 11:41





          Thanks @janhartmann. This works great, but in the end it seems that I need to specify which component can receive an override, rather than it being the client of the component (i.e., its parent). It seems to me it would be much cleaner the other way around.

          – Simone
          Nov 14 '18 at 11:41




          1




          1





          Yes, and in my opinion that is a good thing, in control! ;-) If you put the class name on the root element of the component, you can always from a parent style it like myParentClass: { "& > span": { color: "yellow" } }

          – janhartmann
          Nov 14 '18 at 11:55





          Yes, and in my opinion that is a good thing, in control! ;-) If you put the class name on the root element of the component, you can always from a parent style it like myParentClass: { "& > span": { color: "yellow" } }

          – janhartmann
          Nov 14 '18 at 11:55


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53298931%2fextend-jss-style-class-instead-of-overwriting-it%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          List item for chat from Array inside array React Native

          Thiostrepton

          Caerphilly