Skip to content

Commit e5b2d95

Browse files
yume-changajus
authored andcommitted
feat: add custom attribute mapping feature (#155)
* feat: add custom attribute mapping feature * style: fix extra spaces caused by formatter * style: fix eslint errrors * style: fix extra spaces again
1 parent 3144855 commit e5b2d95

File tree

16 files changed

+7361
-75
lines changed

16 files changed

+7361
-75
lines changed

README.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ In contrast to [`react-css-modules`](https://github.com/gajus/react-css-modules)
2121
* [Named reference](#named-reference)
2222
* [Configuration](#configuration)
2323
* [Configurate syntax loaders](#configurate-syntax-loaders)
24+
* [Custom Attribute Mapping](#custom-attribute-mapping)
2425
* [Installation](#installation)
2526
* [React Native](#react-native)
2627
* [Demo](#demo)
@@ -195,6 +196,7 @@ Configure the options for the plugin within your `.babelrc` as follows:
195196
|`removeImport`|`boolean`|Remove the matching style import. This option is used to enable server-side rendering.|`false`|
196197
|`webpackHotModuleReloading`|`boolean`|Enables hot reloading of CSS in webpack|`false`|
197198
|`handleMissingStyleName`|`"throw"`, `"warn"`, `"ignore"`|Determines what should be done for undefined CSS modules (using a `styleName` for which there is no CSS module defined). Setting this option to `"ignore"` is equivalent to setting `errorWhenNotFound: false` in [react-css-modules](https://github.com/gajus/react-css-modules#errorwhennotfound). |`"throw"`|
199+
|`attributeNames`|`?AttributeNameMapType`|Refer to [Custom Attribute Mapping](#custom-attribute-mapping)|`{"styleName": "className"}`|
198200

199201
Missing a configuration? [Raise an issue](https://github.com/gajus/babel-plugin-react-css-modules/issues/new?title=New%20configuration:).
200202

@@ -217,6 +219,10 @@ type GenerateScopedNameType = (localName: string, resourcePath: string) => strin
217219

218220
type GenerateScopedNameConfigurationType = GenerateScopedNameType | string;
219221

222+
type AttributeNameMapType = {
223+
[key: string]: string
224+
};
225+
220226
```
221227

222228
### Configurate syntax loaders
@@ -250,6 +256,16 @@ To add support for different CSS syntaxes (e.g. SCSS), perform the following two
250256
}
251257
```
252258

259+
### Custom Attribute Mapping
260+
261+
You can set your own attribute mapping rules using the `attributeNames` option.
262+
263+
It's an object, where keys are source attribute names and values are destination attribute names.
264+
265+
For example, the [<NavLink>](https://github.com/ReactTraining/react-router/blob/master/packages/react-router-dom/docs/api/NavLink.md) component from [React Router](https://github.com/ReactTraining/react-router) has a `activeClassName` attribute to accept an additional class name. You can set `"attributeNames": { "activeStyleName": "activeClassName" }` to transform it.
266+
267+
The default `styleName` -> `className` transformation **will not** be affected by an `attributeNames` value without a `styleName` key. Of course you can use `{ "styleName": "somethingOther" }` to change it, or use `{ "styleName": null }` to disable it.
268+
253269
## Installation
254270

255271
When `babel-plugin-react-css-modules` cannot resolve CSS module at a compile time, it imports a helper function (read [Runtime `styleName` resolution](#runtime-stylename-resolution)). Therefore, you must install `babel-plugin-react-css-modules` as a direct dependency of the project.
@@ -271,7 +287,7 @@ module.exports = {
271287
}
272288
```
273289

274-
Remember, also, that the bundler caches things like plugins and presets. If you want to change your `.babelrc` (to add this plugin) then you'll want to add the `--reset-cache` flag to the end of the package command.
290+
Remember, also, that the bundler caches things like plugins and presets. If you want to change your `.babelrc` (to add this plugin) then you'll want to add the `--reset-cache` flag to the end of the package command.
275291

276292
### Demo
277293

0 commit comments

Comments
 (0)