using css modules how do I define more than one style name
You can add multiple classes using css modules as follows:
className={`${styles.description} ${styles.yellow}`}
e.g.
function Footer( props) {
return (
<div className={styles.footer}>
<div className={`${styles.description} ${styles.yellow}`}>
<p>this site was created by me</p>
</div>
</div>
);
}
Using react-css-modules you can use normal class name syntax:
<div styleName='description yellow'>
and you specify allowMultiple: true
for multiple classes
You can use an array that will be joined with a space. i.e
<div className={[styles.App, styles.bold, styles['d-flex-c']].join(' ')}>
I prefer this to using template literals like @steven iseki suggested because it is easier to add and remove classes without having to wrap them in ${}
every single time.
But if you're for some reason adding a lot of classes to a lot of elements you can write a higher order function to make it easier
import React from 'react';
import styles from './Person.module.css';
console.log(styles);
// sample console output =>
// {
// App: 'App_App__3TjUG',
// 'd-flex-c': 'App_d-flex-c__xpDp1',
// }
// func below returns a function that takes a list of classes as an argument
// and turns it in an array with the spread operator and reduces it into a spaced string
const classLister = styleObject => (...classList) =>
classList.reduce((list, myClass) => {
let output = list;
if (styleObject[myClass]) {
if (list) output += ' '; // appends a space if list is not empty
output += styleObject[myClass];
//Above: append 'myClass' from styleObject to the list if it is defined
}
return output;
}, '');
const classes = classLister(styles);
// this creates a function called classes that takes class names as an argument
// and returns a spaced string of matching classes found in 'styles'
Usage
<div className={classes('App', 'bold', 'd-flex-c')}>
Looks very neat and readable.
When rendered to the DOM it becomes
<div class="App_App__3TjUG App_d-flex-c__xpDp1">
/* Note: the class 'bold' is automatically left out because
in this example it is not defined in styles.module.css
as you can be observe in console.log(styles) */
As expected
And it can be used with conditionals by putting the conditionally generated classes in an array that is used as an argument for classes via ... spread operator
In fact while answering this I decided to publish an npm module because why not.
Get it with
npm install css-module-class-lister
I highly recommend using the classnames package. It's incredibly lightweight (600 bytes minified) and has no dependencies:
import classnames from 'classnames';
Function footer(props) {
...
<div className={classnames(styles.description, styles.yellow)}>
}
It even has the added benefit of being able to conditionally add class names (for example, to append a dark theme class), without having to concatenate strings which can accidentally add an undefined
or false
class:
<div className={classnames(styles.description, {styles.darkTheme: props.darkTheme })}>
You should add square brackets to make the classNames an array, and to remove ',' add join().
function Footer( props) {
const { route } = props;
return (
<div className={styles.footer}>
<div className={ [styles.description, styles.yellow].join(' ') }>
<p>this site was created by me</p>
</div>
<div className={styles.description}>
<p>copyright nz</p>
</div>
</div>
);
}
As an addition to Yuan-Hao Chiang's answer, the following function makes it even easier to work with:
const classes = (classNames: Array<string> | string): string => classnames((Array.isArray(classNames) ? classNames : classNames.split(' ')).map(x => styles[x]));
What this does is take either an array or a string (which is then split into an array of strings), and returns a final class name (scoped to the current module since it uses the imported styles
object of course).
You use it like this:
<div className={classes(['description', 'dark-theme', 'many', 'more', 'class-names'])}>
Or if you prefer, specify a single string (handy in case of using many classes when e.g. using TailwindCSS):
<div className={classes('description dark-theme many more class-names')}>