Skip to content

Commit 29ec3ba

Browse files
committed
chore: initial commit
0 parents  commit 29ec3ba

File tree

14 files changed

+17285
-0
lines changed

14 files changed

+17285
-0
lines changed

.github/workflows/ci.yml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
name: ci
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
permissions:
10+
contents: read
11+
12+
13+
jobs:
14+
build:
15+
runs-on: ubuntu-latest
16+
strategy:
17+
matrix:
18+
node-version: [18.x, 20.x, 22.x]
19+
steps:
20+
- uses: actions/checkout@v4
21+
- uses: actions/setup-node@v4
22+
with:
23+
node-version: ${{ matrix.node-version }}
24+
- run: npm clean-install
25+
- run: npm run lint
26+
- run: npm run build
27+
28+
test:
29+
needs: [build]
30+
runs-on: ubuntu-latest
31+
strategy:
32+
matrix:
33+
node-version: [18.x, 20.x, 22.x]
34+
steps:
35+
- uses: actions/checkout@v4
36+
- uses: actions/setup-node@v4
37+
with:
38+
node-version: ${{ matrix.node-version }}
39+
- run: npm clean-install
40+
- run: npm test
41+
42+
# release:
43+
# needs: [test]
44+
# if: github.event_name == 'push' && github.ref == 'refs/heads/main'
45+
# runs-on: ubuntu-latest
46+
# permissions:
47+
# contents: write # to be able to publish a GitHub release
48+
# issues: write # to be able to comment on released issues
49+
# pull-requests: write # to be able to comment on released pull requests
50+
# id-token: write # to enable use of OIDC for npm provenance
51+
# steps:
52+
# - uses: actions/checkout@v4
53+
# - uses: actions/setup-node@v4
54+
# with:
55+
# node-version: 22.x
56+
# - run: npm clean-install
57+
# - run: npm audit signatures
58+
# - run: npm run build
59+
# - run: npx semantic-release
60+
# env:
61+
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
62+
# NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.parcel-cache/
2+
node_modules/
3+
coverage/
4+
lib/

.husky/commit-msg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
npx --no -- commitlint --edit "$1"

.husky/pre-commit

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
npx --no -- lint-staged

.husky/pre-push

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
npm run lint && npm run build && npm run test

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025-present Jamie Weavis
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# String to Tailwind Color
2+
3+
> Generates a random but deterministic Tailwind CSS color and shade from a string
4+
5+
[![ci](https://github.com/jamieweavis/string-to-tailwind-color/workflows/ci/badge.svg)](https://github.com/jamieweavis/string-to-tailwind-color/actions)
6+
[![downloads](https://img.shields.io/npm/dt/string-to-tailwind-color.svg)](https://npmjs.com/package/string-to-tailwind-color)
7+
[![version](https://img.shields.io/npm/v/string-to-tailwind-color.svg)](https://github.com/jamieweavis/string-to-tailwind-color/releases)
8+
[![license](https://img.shields.io/badge/license-MIT-blue.svg)](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.

biome.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
3+
"vcs": {
4+
"enabled": false,
5+
"clientKind": "git",
6+
"useIgnoreFile": false
7+
},
8+
"files": {
9+
"ignoreUnknown": false,
10+
"ignore": []
11+
},
12+
"formatter": {
13+
"enabled": true,
14+
"indentStyle": "space",
15+
"indentWidth": 2
16+
},
17+
"organizeImports": {
18+
"enabled": true
19+
},
20+
"linter": {
21+
"enabled": true,
22+
"rules": {
23+
"recommended": true
24+
}
25+
},
26+
"javascript": {
27+
"formatter": {
28+
"quoteStyle": "single"
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)