Skip to content

Instantly share code, notes, and snippets.

@Hotell
Last active March 25, 2022 16:07
Show Gist options
  • Save Hotell/01035a3ec202245d6b97937444140877 to your computer and use it in GitHub Desktop.
Save Hotell/01035a3ec202245d6b97937444140877 to your computer and use it in GitHub Desktop.
typed css-modules - CRA 2.0

CRA 2.x + TS setup:

This will give you complete intellisense and type safety within your app and CSS modules

typesafe-css-modules

🚨 NOTE

  • refactoring className from ts file wont update your css/scss className, to change class names you have to change it within your .module.scss file

πŸ“ Setup your app:

  1. npx create-react-app --typescript

  2. (recommended πŸ‘‰) rename all files to kebab-case

src
β”œβ”€β”€ app.module.scss
β”œβ”€β”€ app.test.tsx
β”œβ”€β”€ app.tsx
β”œβ”€β”€ index.scss
β”œβ”€β”€ index.tsx
β”œβ”€β”€ logo.svg
β”œβ”€β”€ react-app-env.d.ts
└── service-worker.ts
  1. install dependencies
yarn add -D node-sass typed-css-modules concurrently serve
  1. update package.scripts
{
  "scripts": {
    "ts:css": "tcm -s -c -p src/**/*.module.scss",
    "start": "concurrently \"npm:ts:css -- -w\" \"react-scripts start\"",
    "build": "npm run ts:css && react-scripts build",
    "test": "concurrently \"npm:ts:css -- -w\" \"react-scripts test\""
  }
}
  1. (recommended πŸ‘‰) move @types/* packages to devDependencies as they are not dependencies
{
  "dependencies": {
    "react": "^16.7.0",
    "react-dom": "^16.7.0",
    "react-scripts": "2.1.3",
    "typescript": "3.2.2"
  },
  "devDependencies": {
    "@types/jest": "23.3.12",
    "@types/node": "10.12.18",
    "@types/react": "16.7.18",
    "@types/react-dom": "16.0.11",
    "concurrently": "4.1.0",
    "node-sass": "4.11.0",
    "serve": "10.1.1",
    "typed-css-modules": "0.3.7"
  }
}
  1. use dash-case for css classnames for css-modules
/* app.moudule.scss */

.root {
  text-align: center;
}

.logo {
  animation: App-logo-spin infinite 20s linear;
  height: 40vmin;
}

.header {
  background-color: #282c34;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: calc(10px + 2vmin);
  color: white;
}

.link {
  color: #61dafb;
}

@keyframes App-logo-spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
  1. update app.tsx
import React, { Component } from 'react';
import logo from './logo.svg';
import styles from './app.module.scss';

class App extends Component {
  render() {
    return (
      <div className={styles.root}>
        <header className={styles.header}>
          <img src={logo} className={styles.logo} alt="logo" />
          <p>
            Edit <code>src/App.tsx</code> and save to reload.
          </p>
          <a
            className={styles.link}
            href="https://reactjs.org"
            target="_blank"
            rel="noopener noreferrer"
          >
            Learn React
          </a>
        </header>
      </div>
    );
  }
}

export default App;
  1. START CODING ! πŸ’ͺ
{
"name": "my-project",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.7.0",
"react-dom": "^16.7.0",
"react-scripts": "2.1.3",
"typescript": "3.2.2"
},
"scripts": {
"ts:css": "tcm -s -c -p src/**/*.module.scss",
"start": "concurrently \"npm:ts:css -w\" \"react-scripts start\"",
"build": "npm run ts:css && react-scripts build",
"test": "concurrently \"npm:ts:css -w\" \"react-scripts test\""
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
],
"devDependencies": {
"@types/jest": "23.3.12",
"@types/node": "10.12.18",
"@types/react": "16.7.18",
"@types/react-dom": "16.0.11",
"concurrently": "4.1.0",
"node-sass": "4.11.0",
"serve": "10.1.1",
"typed-css-modules": "0.3.7"
}
}
@hieptuanle
Copy link

Thanks alot!

@arniebradfo
Copy link

arniebradfo commented Apr 4, 2019

I was having trouble with the npm packge typed-css-modules not installing, so I used another version named @nice-labs/typed-css-modules

Also, the -c camelcase argument seemed to break the css module import.
tcm -s -c -p src/**/*.module.scss => typed-css-modules -p src/**/*.module.scss

updated gist

@holylander
Copy link

awesome :) thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment