|
| 1 | +# cssobj-plugin-selector-localize |
| 2 | + |
| 3 | +Localize class names for cssobj. |
| 4 | + |
| 5 | +## Install |
| 6 | + |
| 7 | +install from github directly: |
| 8 | + |
| 9 | +``` javascript |
| 10 | +npm install cssobj/cssobj-plugin-selector-localize |
| 11 | +``` |
| 12 | + |
| 13 | +## API |
| 14 | + |
| 15 | +``` javascript |
| 16 | +var localize = require('cssobj-plugin-selector-localize') |
| 17 | +var loc = localize(prefix, localNames) |
| 18 | +``` |
| 19 | + |
| 20 | +### prefix |
| 21 | + |
| 22 | +- Type: `String` |
| 23 | + |
| 24 | +- Default: Random String |
| 25 | + |
| 26 | +If pass empty string `''`, will use `''` (empty prefix) |
| 27 | + |
| 28 | +If pass other falsy value, will use default. |
| 29 | + |
| 30 | +### localNames |
| 31 | + |
| 32 | +- Type: `Object` |
| 33 | + |
| 34 | +- Default: `{}` |
| 35 | + |
| 36 | +**key/val** pair to define how class name map into local name. |
| 37 | + |
| 38 | +Key is original class name. |
| 39 | + |
| 40 | +Val is localized name. |
| 41 | + |
| 42 | +## Usage |
| 43 | + |
| 44 | +### Localize |
| 45 | + |
| 46 | +``` javascript |
| 47 | +var localize = require('cssobj-plugin-selector-localize') |
| 48 | +var ret = cssobj({'.item': {color: 'red'}}, { |
| 49 | + plugins:{ selector: localize() } |
| 50 | +}) |
| 51 | +// css is => ._1hisnf23_item {color: red;} |
| 52 | + |
| 53 | +// you can get the map using: |
| 54 | +ret.map('item') // === _1hisnf23_item |
| 55 | + |
| 56 | +``` |
| 57 | + |
| 58 | +### Global |
| 59 | + |
| 60 | +There's 2 way to make class **Global** |
| 61 | + |
| 62 | +#### 1. :global(classNames) |
| 63 | + |
| 64 | +Add **:global()** to wrap class names, to make them global. |
| 65 | + |
| 66 | +``` javascript |
| 67 | +var ret = cssobj({'body :global(.nav .item) .login': {color: 'red'}}, { |
| 68 | + plugins:{ selector: localize() } |
| 69 | +}) |
| 70 | +// css is => body .nav .item ._1hisnf23_login {color: red;} |
| 71 | +``` |
| 72 | + |
| 73 | +#### 2. .!className |
| 74 | + |
| 75 | +Just add **!** in front of class name, if you want it global. |
| 76 | + |
| 77 | +``` javascript |
| 78 | +var ret = cssobj({'body .!nav .!item .login': {color: 'red'}}, { |
| 79 | + plugins:{ selector: localize() } |
| 80 | +}) |
| 81 | +// css is => body .nav .item ._1hisnf23_login {color: red;} |
| 82 | +``` |
| 83 | + |
| 84 | +### Custom Prefix |
| 85 | + |
| 86 | +You can control the prefix: |
| 87 | + |
| 88 | +``` javascript |
| 89 | +var ret = cssobj({'body .nav .item .login': {color: 'red'}}, { |
| 90 | + plugins:{ selector: localize('_your_prefix_') } |
| 91 | +}) |
| 92 | +// css is => body ._your_prefix_nav ._your_prefix_item ._your_prefix_login {color: red;} |
| 93 | +``` |
| 94 | + |
| 95 | + |
| 96 | +### Custom Local Names |
| 97 | + |
| 98 | +You can control the map for each class name: |
| 99 | + |
| 100 | +``` javascript |
| 101 | +var ret = cssobj({'body .nav .!item .login': {color: 'red'}}, { |
| 102 | + plugins:{ selector: localize(null, {nav: '_abc_'}) } |
| 103 | +}) |
| 104 | +// css is => body ._abc_ .!item ._1hisnf23_login {color: red;} |
| 105 | +``` |
| 106 | + |
| 107 | +### Get the class map |
| 108 | + |
| 109 | +``` javascript |
| 110 | +var ret = cssobj({'body .nav .item .login': {color: 'red'}}, { |
| 111 | + plugins:{ selector: localize('_prefix_', {nav: '_abc_'}) } |
| 112 | +}) |
| 113 | + |
| 114 | +ret.map('nav') // === _abc_ |
| 115 | +ret.map('item') // === _prefix_item |
| 116 | +``` |
| 117 | + |
0 commit comments