Gatsby: CSS Modules not working with bracket notation (eg. `style['class-name']`)

Created on 3 Mar 2019  路  5Comments  路  Source: gatsbyjs/gatsby

Currently writing CSS classes with dashes by using the bracket notation (eg. style['class-name']) doesn't seem to work in Gatsby. The CSS modules page says bracket notation should work https://github.com/css-modules/css-modules#naming.

Is it intended behavior in Gatsby to not support dashed classes in bracket notation?

The use case is: I am moving many components using SUIT classes to Gatsby. If dashed classes are not supported via bracket notation, I would have to rename most of my classes, since they are all the kind of:

.MyComponent {}
.MyComponent.is-animating {}
.MyComponent--modifier {}

.MyComponent-part {}
.MyComponent-anotherPart {}
question or discussion

Most helpful comment

Thanks for the info! Any chance this behavior can be set more easily through a simple flag in gatsby-config.js? This would help people like myself migrate existing codebases to Gatsby.
@DSchau @sidharthachatterjee

All 5 comments

Hey @gvocale

From the documentation at https://www.gatsbyjs.org/docs/component-css/

If you鈥檙e used to writing CSS with the BEM methodology, a quick thing to note is that CSS Modules camelizes class names that contain dashes.

For example: block--modifier turns into blockModifier.

@sidharthachatterjee exactly, yep.

@gvocale we specifically convert class names to camel case (e.g. my-class-name is available as styles.myClassName) for easier consumption in the JS files.

I'm going to close this as answered, but please feel free to continue the discussion if we can help further.

Thanks for using Gatsby! 馃挏

Note that you can change this behaviour by modifying Gatsby's webpack config - there's an example here: https://www.gatsbyjs.org/docs/migrating-from-v1-to-v2/#update-css-modules-class-names-that-use-dashes

Thanks for the info! Any chance this behavior can be set more easily through a simple flag in gatsby-config.js? This would help people like myself migrate existing codebases to Gatsby.
@DSchau @sidharthachatterjee

Was this page helpful?
0 / 5 - 0 ratings

Related issues

totsteps picture totsteps  路  3Comments

rossPatton picture rossPatton  路  3Comments

dustinhorton picture dustinhorton  路  3Comments

jimfilippou picture jimfilippou  路  3Comments

kalinchernev picture kalinchernev  路  3Comments