|
| 1 | +# String to Tailwind Color |
| 2 | + |
| 3 | +> Generates a random but deterministic Tailwind CSS color and shade from a string |
| 4 | +
|
| 5 | +[](https://github.com/jamieweavis/string-to-tailwind-color/actions) |
| 6 | +[](https://npmjs.com/package/string-to-tailwind-color) |
| 7 | +[](https://github.com/jamieweavis/string-to-tailwind-color/releases) |
| 8 | +[](https://github.com/jamieweavis/string-to-tailwind-color/blob/main/LICENSE) |
| 9 | + |
| 10 | +## Install |
| 11 | + |
| 12 | +```sh |
| 13 | +npm install string-to-tailwind-color |
| 14 | +``` |
| 15 | + |
| 16 | +## Usage |
| 17 | + |
| 18 | +Generate a random but deterministic Tailwind CSS color and shade from a string: |
| 19 | + |
| 20 | +```javascript |
| 21 | +import { stringToTailwindColor } from 'string-to-tailwind-color'; |
| 22 | + |
| 23 | +const foo = stringToTailwindColor('foo'); |
| 24 | +console.log(foo); // 'teal-500' |
| 25 | + |
| 26 | +const bar = stringToTailwindColor('bar'); |
| 27 | +console.log(bar); // 'emerald-100' |
| 28 | +``` |
| 29 | + |
| 30 | +By default all colors and shades are used - to use a subset of colors and/or shades, you can pass an options object as the second argument: |
| 31 | + |
| 32 | +```javascript |
| 33 | +import { stringToTailwindColor } from 'string-to-tailwind-color'; |
| 34 | + |
| 35 | +const foo = stringToTailwindColor('foo', { |
| 36 | + colors: ['red', 'green', 'blue'], |
| 37 | + shades: [300, 500, 700], |
| 38 | +}); |
| 39 | + |
| 40 | +console.log(foo); // 'red-300' |
| 41 | +``` |
| 42 | + |
| 43 | +> [!NOTE] |
| 44 | +> For a full list of colors and shades see [https://tailwindcss.com/docs/colors](https://tailwindcss.com/docs/colors) |
| 45 | +
|
| 46 | + |
| 47 | +If you don't like the color generated for a particular string, you can use the `hashOffset` option to rotate the color (incrememnting by 1 will give you a shade higher, decrementing by 1 will give you a shade lower, colors will also rotate): |
| 48 | + |
| 49 | +```javascript |
| 50 | +import { stringToTailwindColor } from 'string-to-tailwind-color'; |
| 51 | + |
| 52 | +const a = stringToTailwindColor('foo'); |
| 53 | +console.log(a); // 'teal-500' |
| 54 | + |
| 55 | +const b = stringToTailwindColor('foo', { hashOffset: 1 }); |
| 56 | +console.log(b); // 'teal-600' |
| 57 | + |
| 58 | +const c = stringToTailwindColor('foo', { hashOffset: 11 }); |
| 59 | +console.log(c); // 'cyan-500' |
| 60 | +``` |
| 61 | + |
| 62 | +Use in combination with background color (`bg-`), text color (`text-`), border color (`border-`), etc: |
| 63 | + |
| 64 | +```javascript |
| 65 | +import { stringToTailwindColor } from 'string-to-tailwind-color'; |
| 66 | + |
| 67 | +const SomeComponent = ({ text }) => { |
| 68 | + const color = stringToTailwindColor(text); |
| 69 | + return ( |
| 70 | + <p className={`bg-${color} text-${color} border-${color}`}> |
| 71 | + {text} |
| 72 | + </p> |
| 73 | + ); |
| 74 | +}; |
| 75 | +``` |
| 76 | + |
| 77 | +> [!NOTE] |
| 78 | +> For a full list of classes compatible with color see [https://tailwindcss.com/docs/colors#using-color-utilities](https://tailwindcss.com/docs/colors#using-color-utilities) |
| 79 | +
|
| 80 | +## Use Cases |
| 81 | + |
| 82 | +Generate a color for a tag component based on it's contents: |
| 83 | + |
| 84 | +```javascript |
| 85 | +import { stringToTailwindColor } from 'string-to-tailwind-color'; |
| 86 | + |
| 87 | +const Tag = ({ name }) => { |
| 88 | + const color = stringToTailwindColor(name, { |
| 89 | + colors: ['red', 'green', 'blue', 'yellow', 'purple', 'pink'], |
| 90 | + shades: [300, 400, 500, 600, 700], |
| 91 | + }); |
| 92 | + return ( |
| 93 | + <span className={`bg-${color} text-white px-2 py-1 rounded`}> |
| 94 | + {name} |
| 95 | + </span> |
| 96 | + ); |
| 97 | +}; |
| 98 | +``` |
| 99 | + |
| 100 | +## Built with |
| 101 | + |
| 102 | +- [Node.js](https://github.com/nodejs/node) |
| 103 | +- [TypeScript](https://github.com/microsoft/TypeScript) |
| 104 | +- [Parcel](https://github.com/parcel-bundler/parcel) |
| 105 | + |
| 106 | +## License |
| 107 | + |
| 108 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
0 commit comments