Skip to content

Commit 73a0596

Browse files
committed
new: colorVariable helper
1 parent cfa4865 commit 73a0596

File tree

7 files changed

+216
-9
lines changed

7 files changed

+216
-9
lines changed

README.md

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@
99
This plugin allows you to configure CSS variables in the `tailwind.config.js`
1010

1111

12-
Similar to the tailwindcss configurations you are used to. It is also possible to define a different variable group for Dark Mode and to integrate it with your packages through plugin API.
12+
Similar to the tailwindcss configurations you are used to. It is also possible to define a different variable group for Dark Mode and to integrate it with your packages through plugin API.
1313

1414

1515
## Highlights
1616

1717
- Variables are as easy as defining tailwindcss colors...
1818
- You can designate the variables to `:root` or custom CSS selectors.
19-
- Variables can be formed through using nested object notation.
19+
- Variables can be formed through using nested object notation.
2020
- Different variables can be composed for the Dark Mode.
2121
- Dark Mode variables are set automatically through the `class` or `media` modes on your configuration.
2222
- If `darkMode` config are set as `class`, custom dark selector can be defined.
23-
- It allows you to add custom themes while creating your own plugin via the plugin API.
23+
- It allows you to add custom themes while creating your own plugin via the plugin API.
2424
- Prefix can be defined for variables. (It is useful when using the plugin API)
25-
- You can configure your own needs such as multi-themes without needing an additional plugin!
25+
- You can configure your own needs such as multi-themes without needing an additional plugin!
2626

2727

2828
## Documentation
@@ -38,7 +38,7 @@ Similar to the tailwindcss configurations you are used to. It is also possible t
3838
npm install -D @mertasan/tailwindcss-variables
3939
```
4040

41-
## Usage
41+
## Usage
4242

4343
```javascript
4444
// tailwind.config.js
@@ -484,6 +484,66 @@ module.exports = {
484484
}
485485
```
486486

487+
## Helpers
488+
489+
### `colorVariable()`
490+
491+
You can use the `colorVariable` helper to add `text-opacity` or `bg-opacity` to the variables for which colors are defined.
492+
493+
494+
```javascript
495+
// tailwind.config.js
496+
497+
const colorVariable = require('@mertasan/tailwindcss-variables/colorVariable')
498+
499+
module.exports = {
500+
theme: {
501+
colors: {
502+
primary: colorVariable('--colors-primary'),
503+
secondary: colorVariable('var(--colors-secondary)'), // or
504+
gray: 'var(--colors-gray)', // or default usage
505+
},
506+
variables: {
507+
DEFAULT: {
508+
colors: {
509+
primary: '#ffffff',
510+
secondary: '#000000',
511+
gray: '#6B7280'
512+
},
513+
},
514+
},
515+
},
516+
},
517+
plugins: [
518+
require('@mertasan/tailwindcss-variables')
519+
]
520+
}
521+
```
522+
523+
**Output:**
524+
525+
```css
526+
:root {
527+
--colors-primary: #ffffff;
528+
--colors-secondary: #000000;
529+
--colors-gray: #6B7280
530+
}
531+
532+
.text-primary {
533+
--tw-text-opacity: 1;
534+
color: rgba(var(--colors-primary), var(--tw-text-opacity))
535+
}
536+
537+
.bg-secondary {
538+
--tw-bg-opacity: 1;
539+
background-color: rgba(var(--colors-secondary), var(--tw-bg-opacity))
540+
}
541+
542+
.bg-gray {
543+
background-color: var(--colors-gray)
544+
}
545+
```
546+
487547
## API example for your own plugins
488548

489549
- [Detailed Explanation](#gerçek-kullanım-örneği-detaylı)
@@ -760,13 +820,13 @@ module.exports = {
760820
}
761821
```
762822

763-
Based on these examples, it won't be necessary to publish extra .css flies for your plugin styles and also, it won't be necessary for the users to sort out your style files to compile your packages.
823+
Based on these examples, it won't be necessary to publish extra .css flies for your plugin styles and also, it won't be necessary for the users to sort out your style files to compile your packages.
764824

765825

766826

767827
## Examples and tests
768828

769-
I have prepared examples on both helping with the usage and for testing all of the features that's being offered to make sure it works just fine.
829+
I have prepared examples on both helping with the usage and for testing all of the features that's being offered to make sure it works just fine.
770830

771831

772832
| Source | State |
@@ -778,7 +838,7 @@ I have prepared examples on both helping with the usage and for testing all of t
778838

779839
> Documents on examples and tests are re-organized on pull-request, push, release and etc. events.
780840
> For this reason, file paths like `require(../index)` have been used on the example files. If you were to use the examples, you need to change the relevant lines as `require('@mertasan/tailwindcss-variables')`.
781-
>
841+
>
782842
783843

784844
## If You Need Help

README.tr.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,66 @@ module.exports = {
485485
}
486486
```
487487

488+
## Yardımcı Fonksiyonlar (Helpers)
489+
490+
### `colorVariable()`
491+
492+
Renk değişkenlerini `colorVariable` helper fonksiyonunu kullanarak tanımlamanız halinde, renkleri `text-opacity` ya da `bg-opacity` gibi
493+
ek classlar ile uyumlu hale getirmeniz mümkün.
494+
495+
```javascript
496+
// tailwind.config.js
497+
498+
const colorVariable = require('@mertasan/tailwindcss-variables/colorVariable')
499+
500+
module.exports = {
501+
theme: {
502+
colors: {
503+
primary: colorVariable('--colors-primary'),
504+
secondary: colorVariable('var(--colors-secondary)'), // veya
505+
gray: 'var(--colors-gray)', // varsayılan kullanım şekli
506+
},
507+
variables: {
508+
DEFAULT: {
509+
colors: {
510+
primary: '#ffffff',
511+
secondary: '#000000',
512+
gray: '#6B7280'
513+
},
514+
},
515+
},
516+
},
517+
},
518+
plugins: [
519+
require('@mertasan/tailwindcss-variables')
520+
]
521+
}
522+
```
523+
524+
**Output:**
525+
526+
```css
527+
:root {
528+
--colors-primary: #ffffff;
529+
--colors-secondary: #000000;
530+
--colors-gray: #6B7280
531+
}
532+
533+
.text-primary {
534+
--tw-text-opacity: 1;
535+
color: rgba(var(--colors-primary), var(--tw-text-opacity))
536+
}
537+
538+
.bg-secondary {
539+
--tw-bg-opacity: 1;
540+
background-color: rgba(var(--colors-secondary), var(--tw-bg-opacity))
541+
}
542+
543+
.bg-gray {
544+
background-color: var(--colors-gray)
545+
}
546+
```
547+
488548
## Kendi eklentileriniz için API örneği
489549

490550
- [Ayrıntılı açıklama](#gerçek-kullanım-örneği-detaylı)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div class="text-primary text-opacity-50"></div>
2+
<div class="bg-secondary bg-opacity-50"></div>
3+
<div class="bg-gray bg-opacity-50"></div>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
const tailwindcssVariables = require('../src/index')
2+
const utils = require('./util/_utils')(__filename)
3+
const colorVariable = require('../colorVariable')
4+
5+
test('colorVariable helper', async () => {
6+
expect(
7+
await utils.diffOnly({
8+
corePlugins: ['textColor', 'textOpacity', 'backgroundColor', 'backgroundOpacity'],
9+
purge: {
10+
enabled: true,
11+
content: [utils.content()],
12+
},
13+
darkMode: false,
14+
theme: {
15+
screens: false,
16+
colors: {
17+
primary: colorVariable('--colors-primary'),
18+
secondary: colorVariable('var(--colors-secondary)'),
19+
gray: 'var(--colors-gray)',
20+
},
21+
variables: {
22+
DEFAULT: {
23+
colors: {
24+
primary: '#ffffff',
25+
secondary: '#000000',
26+
gray: '#6B7280',
27+
},
28+
},
29+
},
30+
},
31+
plugins: [tailwindcssVariables],
32+
})
33+
).toMatchInlineSnapshot(`
34+
"
35+
36+
37+
+ :root {
38+
+ --colors-primary: #ffffff;
39+
+ --colors-secondary: #000000;
40+
+ --colors-gray: #6B7280
41+
+ }
42+
+
43+
+ .text-primary {
44+
+ --tw-text-opacity: 1;
45+
+ color: rgba(var(--colors-primary), var(--tw-text-opacity))
46+
+ }
47+
+
48+
+ .text-opacity-50 {
49+
+ --tw-text-opacity: 0.5
50+
+ }
51+
+
52+
+ .bg-secondary {
53+
+ --tw-bg-opacity: 1;
54+
+ background-color: rgba(var(--colors-secondary), var(--tw-bg-opacity))
55+
+ }
56+
+
57+
+ .bg-gray {
58+
+ background-color: var(--colors-gray)
59+
+ }
60+
+
61+
+ .bg-opacity-50 {
62+
+ --tw-bg-opacity: 0.5
63+
+ }
64+
65+
"
66+
`)
67+
})

colorVariable.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const helpers = require('./src/helpers')
2+
module.exports = helpers.colorVariable

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@mertasan/tailwindcss-variables",
3-
"version": "1.0.3",
3+
"version": "1.1.0",
44
"description": "Easily create css variables without the need for a css file!",
55
"main": "src/index.js",
66
"license": "GPL-3.0-or-later",

src/helpers.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const startsWith = require('lodash/startsWith')
2+
3+
const colorVariable = (variable) => {
4+
return function ({ opacityVariable, opacityValue }) {
5+
variable = startsWith(variable, 'var') ? variable : 'var(' + variable + ')'
6+
if (opacityValue !== undefined) {
7+
return `rgba(${variable}, ${opacityValue})`
8+
}
9+
if (opacityVariable !== undefined) {
10+
return `rgba(${variable}, var(${opacityVariable}, 1))`
11+
}
12+
return `rgb(${variable})`
13+
}
14+
}
15+
module.exports.colorVariable = colorVariable

0 commit comments

Comments
 (0)