|
1 | 1 | # react-classnaming |
2 | 2 |
|
3 | | -Easy applied function to use optionally with [CSS modules](https://github.com/css-modules/css-modules) and do not get lost in CSS classes |
| 3 | +Easy applied function to use optionally with [CSS modules](https://github.com/css-modules/css-modules) and do not get lost in CSS classes by powerful TypeScript notation |
4 | 4 |
|
5 | | -     |
| 5 | + [](https://www.npmjs.com/package/react-classnaming)  [](https://codeclimate.com/github/askirmas/react-classnaming/maintainability)    |
6 | 6 |
|
7 | 7 | ## Installation |
8 | 8 |
|
9 | 9 | ```bash |
10 | 10 | npm install --save react-classnaming |
11 | 11 | ``` |
12 | 12 |
|
13 | | -## Examples |
| 13 | +## Examples of usage |
| 14 | + |
| 15 | +Check the tests with detailed usage: [src/basic.spec.tsx](./src/basic.spec.tsx) and [src/ctx.spec.tsx](./src/ctx.spec.tsx) |
| 16 | + |
| 17 | +With create-react-application: [./\_\_recipes\_\_/cra/src/App.tsx](./__recipes__/cra/src/App.tsx) |
| 18 | + |
| 19 | +### Basic |
14 | 20 |
|
15 | 21 | ```tsx |
16 | | -import classNaming from "react-classnaming" |
| 22 | +import {classNamingBasic} from "react-classnaming" |
| 23 | +import classNamingBasic from "react-classnaming/basic" |
17 | 24 |
|
18 | | -<div {...classNaming("class1", {class2, class3})}> |
19 | | -<div className={`${classNaming({class4})}`}> |
20 | | -<div {...classNaming(classNames)({class1: bool1}, bool2 && class2)}> |
| 25 | +const {className, classNames: {class2, class3, class4, class5}} = props |
| 26 | + |
| 27 | +<div {...classNamingBasic("class1", {class2, class3})}> |
| 28 | +<div className={`${classNamingBasic({class4})}`}> |
| 29 | +<div className={classNamingBasic<string>({class5})}> |
21 | 30 | ``` |
22 | 31 |
|
23 | | -Check the tests with detailed usage: [./src/index.test.ts](./src/index.test.ts) |
| 32 | +### From css-module or simulation |
24 | 33 |
|
25 | | -With create-react-application: [./__recipes__/cra/src/App.tsx](./__recipes__/cra/src/App.tsx) |
| 34 | +```tsx |
| 35 | +import {classNamingCtx} from "react-classnaming" |
| 36 | +import classNamingCtx from "react-classnaming/ctx" |
| 37 | +import css from "./some.css" // {"class1": "hash1", "class2": "hash2"} |
| 38 | + |
| 39 | +const classNaming = classNamingCtx({className: "App", classNames: css}) |
| 40 | + |
| 41 | +<div {...classes(true, {class1: true, class2: false})}/> // className="App hash1" |
| 42 | +<div {...classes("class2")}/> // className="hash2" |
| 43 | +``` |
26 | 44 |
|
27 | | -## TS Generic for props |
| 45 | +### TS generic for props |
28 | 46 | ```ts |
29 | 47 | import type { ClassNames } from "react-classnaming" |
30 | 48 |
|
31 | | -ClassNames<true> |
| 49 | +ClassNames<true> // requires `className` |
32 | 50 |
|
33 | | -ClassNames<"class1"|"class2"> |
| 51 | +ClassNames<"class1"|"class2"> // requires to supply `.class1` and `.class2` |
34 | 52 |
|
35 | | -ClassNames<Props1, Props2> |
| 53 | +ClassNames<Props1, Props2> // requires to supply `classNames` for `Props1` and `Props2` |
36 | 54 |
|
37 | | -ClassNames<true, "class1", Props, typeof Component1, typeof FunctionalComponent> |
| 55 | +ClassNames<true, "class1", Props, typeof Component, typeof FunctionalComponent> //requires `className` and to supply `.class1`, `classNames` from `Props`, class component `Component` and function component `FunctionalComponent` |
38 | 56 | ``` |
39 | 57 |
|
40 | | -## Root apply |
| 58 | +### Root apply |
41 | 59 | ```tsx |
42 | 60 | import {classNamesCheck} from "react-classnaming" |
43 | 61 |
|
44 | 62 | ReactDOM.render( <Root classNames={classNamesCheck()}/> ) |
45 | 63 | ReactDOM.render( <Root classNames={classNamesCheck<"class1"|"class2">()}/> ) |
46 | 64 |
|
47 | | -import classNames from "./modules.scss" |
| 65 | +import classNames from "./modules.css" |
48 | 66 |
|
49 | | -ReactDOM.render( <Root classNames={names(classNames))}/> ) |
| 67 | +ReactDOM.render( <Root classNames={classNamesCheck(classNames))}/> ) |
50 | 68 | ``` |
51 | 69 |
|
52 | 70 | ## vs [classnames](https://github.com/JedWatson/classnames#readme) |
|
0 commit comments