|
| 1 | +# CSS Modules |
| 2 | + |
| 3 | +To make this library work with CSS Modules, we need to rewrite the class names in |
| 4 | +our meta object based on the styles object we get from CSS Modules on import. |
| 5 | + |
| 6 | +## Parcel |
| 7 | + |
| 8 | +With parcel, the setup is quite simple, as it supports PostCSS configuration |
| 9 | +files. |
| 10 | + |
| 11 | +The following shows a usual PostCSS setup, with container queries and CSS modules: |
| 12 | + |
| 13 | +```sh |
| 14 | +yarn add postcss-nested \ |
| 15 | + postcss-media-minmax \ |
| 16 | + @zeecoder/postcss-container-query \ |
| 17 | + postcss-modules --dev |
| 18 | + |
| 19 | +# or with NPM: |
| 20 | + |
| 21 | +npm install postcss-nested \ |
| 22 | + postcss-media-minmax \ |
| 23 | + @zeecoder/postcss-container-query \ |
| 24 | + postcss-modules --save-dev |
| 25 | +``` |
| 26 | + |
| 27 | +Then put the following `.postcssrc` file in your root directory: |
| 28 | + |
| 29 | +```json |
| 30 | +{ |
| 31 | + "modules": true, |
| 32 | + "plugins": { |
| 33 | + "postcss-nested": { "bubble": ["container"] }, |
| 34 | + "postcss-media-minmax": {}, |
| 35 | + "@zeecoder/postcss-container-query": {}, |
| 36 | + "postcss-modules": {} |
| 37 | + } |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +Now you can do the following: |
| 42 | + |
| 43 | +```js |
| 44 | +import React, { Component } from "react"; |
| 45 | +import styles, { meta as rawMeta } from "./App.css"; |
| 46 | +import { ContainerQuery } from "@zeecoder/react-container-query"; |
| 47 | +import { remapMetaSelectors } from "@zeecoder/container-query-meta-builder"; |
| 48 | + |
| 49 | +// Since the container query postcss plugin comes before css-modules - and which |
| 50 | +// order we cannot change for technical reasons -, we need to remap class names |
| 51 | +// to the hashed class names CSS modules generates in the styles object. |
| 52 | +const meta = remapMetaSelectors(rawMeta, styles); |
| 53 | + |
| 54 | +class App extends Component { |
| 55 | + render() { |
| 56 | + return ( |
| 57 | + <ContainerQuery className={styles.App} meta={meta}> |
| 58 | + My app. |
| 59 | + </ContainerQuery> |
| 60 | + ); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +export default App; |
| 65 | +``` |
| 66 | + |
| 67 | +This would work the same way without react React, or without any component |
| 68 | +library for that matter. |
| 69 | + |
| 70 | +## With webpack |
| 71 | + |
| 72 | +Usage is the same with webpack, just add something like the following to your config: |
| 73 | + |
| 74 | +```js |
| 75 | +{ |
| 76 | + // ... |
| 77 | + test: /\.css$/, |
| 78 | + use: [ |
| 79 | + "style-loader", |
| 80 | + { |
| 81 | + loader: "css-loader", |
| 82 | + options: { |
| 83 | + modules: true |
| 84 | + } |
| 85 | + }, |
| 86 | + { |
| 87 | + loader: "postcss-loader", |
| 88 | + options: { |
| 89 | + plugins: [ |
| 90 | + require("postcss-nested")({ bubble: ["container"] }), |
| 91 | + require("postcss-media-minmax")(), |
| 92 | + require("autoprefixer")(), |
| 93 | + require("../packages/postcss-container-query/dist")() |
| 94 | + ] |
| 95 | + } |
| 96 | + } |
| 97 | + ] |
| 98 | + // ... |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +You can of course have the PostCSS setup in a config file for webpack as well. |
0 commit comments