16

If I have a react component and I want to pass in a className, how do I do this with CSS Modules. It currently just gives the className but not the hash generated css module name which I would get for <div className={styles.tile + ' ' + styles.blue}>

Here is my Tile.js component

import React, { Component } from 'react';

import styles from './Tile.css';

class Tile extends Component {

  render() {

    return (
      <div className={styles.tile + ' ' + this.props.color}>
        {this.props.children}
      </div>
    );
  }
};

export default Tile;

Tile.css

@value colors: "../../styles/colors.css";
@value blue, black, red from colors;

.tile {
    position: relative;
    width: 100%;
    padding-bottom: 100%;
}

.black {
  background-color: black;
}

.blue {
  background-color: blue;
}

.red {
  background-color: red;
}

So as you can see I initialize this Tile wrapper component as follows in my Author Tile, but I want to pass a color prop into the component:

AuthorTile.js

return (
  <Tile orientation='blue'>
   <p>{this.props.title}</p>
   <img src={this.props.image} />
  </Tile>
);

1 Answer 1

26

From the docs:

Avoid using multiple CSS Modules to describe a single element. https://github.com/gajus/react-css-modules#multiple-css-modules

@value colors: "../../styles/colors.css";
@value blue, black, red from colors;

.tile {
    position: relative;
    width: 100%;
    padding-bottom: 100%;
}

.black {
    composes: tile;
    background-color: black;
}

.blue {
    composes: tile;
    background-color: blue;
}

.red {
    composes: tile;
    background-color: red;
}

Then <div className={styles[this.props.color]} should do the job, e.g:

render: function(){
  // ES2015
  const className = styles[this.props.color];

  // ES5
  var className = '';

  if (this.props.color === 'black') {
    className = styles.black;
  }

  return (
    <div className={className}>
      {this.props.children}
    </div>
}
Sign up to request clarification or add additional context in comments.

6 Comments

How do you propose having multiple color backgrounds for this Tile component, which can be defined when you initialize it?
What you have done is not a bad approach to using composition with css modules, but still my main concern is how do I use the passes in color for the Tile, e.g. <Tile color='black'> How does this translate in the Tile? <div className={this.props.color}> is not going to work because it expects {styles.blue} not blue which would become something like components_Tile__blue__abc5436
If you're using ES2015 <div className={styles[this.props.color]} should do the job. If not, just check for this.props.color value e.g var className = ''; if (this.props.color === 'black') { className = styles.black; }.
<div className={styles[this.props.color]} did it. You are awesome, if you answer with this I can mark it as accepted. I actually thought about doing this but messed the syntax up first time around. Thanks
if I am using ES5 or 6 and I have e.g. 10 classnames, do I have to check through all of them to create the right styles.x ? Isn't there any better way to solve this problem?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.