tailwind-clamp 2.0.2 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -10,7 +10,8 @@ The plugin is based on the formula presented in this [article](https://chriskirk
10
10
  - Possibility to use small to large, large to small, negative to positive, positive to negative and negative to negative values. (Negative values only work on properties that allow them, e.g. `margin`)
11
11
  - Supports `px`, `rem` and `em` units.
12
12
  - Support `text` values with multiple properties (`fontSize`, `lineHeight`, `letterSpacing`).
13
- - Support using defined in the Tailwind CSS configuration file, arbitrary values or a combination.
13
+ - Supports using values defined in the Tailwind CSS configuration file, arbitrary values or a combination.
14
+ - Helper function to create clamped values directly in your config file.
14
15
 
15
16
  ## Installation
16
17
 
@@ -24,12 +25,14 @@ Add the plugin in your Tailwind CSS configuration file:
24
25
 
25
26
  ```js
26
27
  // tailwind.config.js
27
- module.exports = {
28
+ import tailwindClamp from 'tailwind-clamp';
29
+
30
+ export default {
28
31
  theme: {
29
32
  // ...
30
33
  },
31
34
  plugins: [
32
- require('tailwind-clamp'),
35
+ tailwindClamp,
33
36
  // ...
34
37
  ],
35
38
  };
@@ -39,19 +42,23 @@ module.exports = {
39
42
 
40
43
  The plugin allows two configuration options:
41
44
 
42
- | Name | Description | Default value |
43
- | ------------------ | ------------------------------------ | ------------- |
44
- | `minViewportWidth` | Viewport size where the clamp starts | `375` |
45
- | `maxViewportWidth` | Viewport size where the clamp end | `1440` |
45
+ | Name | Type | Description | Default value |
46
+ | ---------------------- | ------------------ | ------------------------------------- | ------------- |
47
+ | **`minViewportWidth`** | `{number\|string}` | Viewport size where the clamp starts. | `375` |
48
+ | **`maxViewportWidth`** | `{number\|string}` | Viewport size where the clamp end. | `1440` |
49
+
50
+ Value should be a css value (`px`, `rem`, `em`) or a number (unit will be `px`). The unit for both options need to match.
46
51
 
47
52
  ```js
48
53
  // tailwind.config.js
49
- module.exports = {
54
+ import tailwindClamp from 'tailwind-clamp';
55
+
56
+ export default {
50
57
  theme: {
51
58
  // ...
52
59
  },
53
60
  plugins: [
54
- require('tailwind-clamp')({
61
+ tailwindClamp({
55
62
  minViewportWidth: 375,
56
63
  maxViewportWidth: 1440,
57
64
  }),
@@ -73,7 +80,7 @@ clamp-[<property>,<start>,<end>,[minViewportWidth,maxViewportWidth]]
73
80
  - `property`: Property that the value should be applied to. See a list of all supported properties below.
74
81
  - `start`: Value at `minViewportWidth` viewport size. It can be a key from your Tailwind CSS config file, a css value (`px`, `rem`, `em`) or a number (unit will be `px`), the unit will need to match `end`.
75
82
  - `end`: Value at `maxViewportWidth` viewport size. It can be a key from your Tailwind CSS config file, a css value (`px`, `rem`, `em`) or a number (unit will be `px`), the unit will need to match `start`.
76
- - `[minViewportWidth=375]`: Viewport size, where the clamp starts, defaults to `375`. Can be a key from `screens` a css value (`px`, `rem`, `em`) or a number (unit will be `px`), the unit will need to match `maxViewportWidth`. Value needs be smaller than `maxViewportWidth`.
83
+ - `[minViewportWidth=375]`: Viewport size, where the clamp starts, defaults to `375`. Can be a key from `screens` a css value (`px`, `rem`, `em`) or a number (unit will be `px`), the unit will need to match `maxViewportWidth`. Value needs to be smaller than `maxViewportWidth`.
77
84
  - `[maxViewportWidth=1440]`: Viewport size, where the clamp stops, defaults to `1440`. Can be a key from `screens` a css value (`px`, `rem`, `em`) or a number (unit will be `px`), the unit will need to match `minViewportWidth`. Value needs to be larger than `minViewportWidth`.
78
85
 
79
86
  ### Examples
@@ -84,7 +91,45 @@ clamp-[<property>,<start>,<end>,[minViewportWidth,maxViewportWidth]]
84
91
  </div>
85
92
  ```
86
93
 
87
- ##### Supported properties
94
+ ## Clamped values in config
95
+
96
+ The plugin includes a utility function to create clamped values directly in your config file.
97
+
98
+ ```js
99
+ // tailwind.config.js
100
+ import tailwindClamp, { clampValue } from 'tailwind-clamp';
101
+
102
+ export default {
103
+ theme: {
104
+ // ...
105
+ padding: {
106
+ 'my-claped-value': clampValue(20, 40),
107
+ 'my-other-claped-value': clampValue(30, 60, {
108
+ unit: 'px',
109
+ maxViewportWidth: 1960,
110
+ }),
111
+ },
112
+ },
113
+ plugins: [
114
+ tailwindClamp,
115
+ // ...
116
+ ],
117
+ };
118
+ ```
119
+
120
+ ### Arguments
121
+
122
+ | Name | Type | Description | Default value |
123
+ | -------------------------------- | --------------------- | ------------------------------------------ | ------------- |
124
+ | **`start`** | `{number}` | Value at `minViewportWidth` viewport size. | |
125
+ | **`end`** | `{number}` | Value at `maxViewportWidth` viewport size. | |
126
+ | **`[options.minViewportWidth]`** | `{number}` | Viewport size, where the clamp starts. | `375` |
127
+ | **`[options.maxViewportWidth]`** | `{number}` | Viewport size, where the clamp stops. | `1440` |
128
+ | **`[options.unit]`** | `{'px'\|'rem'\|'em'}` | Unit that should be used in the css value. | `rem` |
129
+
130
+ Al values are expected in pixels and will be converted to `[options.unit]`.
131
+
132
+ ## Supported properties
88
133
 
89
134
  - `p` including `pt`, `pb`, `pl`, `pr`, `px`, `py`.
90
135
  - `m` including `mt`, `mb`, `ml`, `mr`, `mx`, `my`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tailwind-clamp",
3
- "version": "2.0.2",
3
+ "version": "2.2.0",
4
4
  "description": "Tailwind CSS plugin to use CSS clamp in your projects",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
package/src/clamp.js CHANGED
@@ -55,3 +55,30 @@ export const clamp = (
55
55
 
56
56
  return negative ? `calc(${value} * -1)` : value;
57
57
  };
58
+
59
+ const defaultOptions = {
60
+ unit: 'rem',
61
+ minViewportWidth: 375,
62
+ maxViewportWidth: 1440,
63
+ };
64
+
65
+ export const clampValue = (
66
+ start,
67
+ end,
68
+ options = {
69
+ unit: 'rem',
70
+ minViewportWidth: 375,
71
+ maxViewportWidth: 1440,
72
+ }
73
+ ) => {
74
+ const o = { ...defaultOptions, ...options };
75
+
76
+ const convert = (val) => (o.unit === 'rem' ? val / 16 : val);
77
+
78
+ return clamp(
79
+ { number: convert(start), unit: o.unit },
80
+ { number: convert(end), unit: o.unit },
81
+ { number: convert(o.minViewportWidth), unit: o.unit },
82
+ { number: convert(o.maxViewportWidth), unit: o.unit }
83
+ );
84
+ };
package/src/index.js CHANGED
@@ -2,13 +2,15 @@ import plugin from 'tailwindcss/plugin';
2
2
  import { resolveProperty } from './resolve-property.js';
3
3
  import { log } from './log.js';
4
4
  import { parseValue, parseFontSizeValue, checkValues } from './parse-value.js';
5
- import { clamp } from './clamp.js';
5
+ import { clamp, clampValue } from './clamp.js';
6
6
 
7
7
  const defaultOptions = {
8
8
  minViewportWidth: 375,
9
9
  maxViewportWidth: 1440,
10
10
  };
11
11
 
12
+ export const clampValue = clampValue;
13
+
12
14
  export default plugin.withOptions(function (options = defaultOptions) {
13
15
  return function ({ matchUtilities, theme, config }) {
14
16
  matchUtilities(
@@ -70,8 +72,17 @@ export default plugin.withOptions(function (options = defaultOptions) {
70
72
  }
71
73
 
72
74
  // handle other properties
73
- const start = parseValue(config().theme[key][args[1]] || args[1]);
74
- const end = parseValue(config().theme[key][args[2]] || args[2]);
75
+ const startNegative = args[1].startsWith('-');
76
+ const endNegative = args[2].startsWith('-');
77
+
78
+ const startVal = startNegative ? args[1].slice(1) : args[1];
79
+ const endVal = endNegative ? args[2].slice(1) : args[2];
80
+
81
+ const start = parseValue(config().theme[key][startVal] || startVal);
82
+ const end = parseValue(config().theme[key][endVal] || endVal);
83
+
84
+ if (startNegative) start.number = start.number * -1;
85
+ if (endNegative) end.number = end.number * -1;
75
86
 
76
87
  if (!checkValues(start, end, value)) {
77
88
  return null;
@@ -5,7 +5,7 @@ export const parseValue = (v) => {
5
5
  const number = parseFloat(value);
6
6
  let unit = 'unsupported';
7
7
 
8
- if (/^\d+$/.test(value)) {
8
+ if (/^\d+$/.test(value) && number !== 0) {
9
9
  unit = 'px';
10
10
  }
11
11
 
@@ -14,6 +14,10 @@ export const parseValue = (v) => {
14
14
  unit = match[0];
15
15
  }
16
16
 
17
+ if (number === 0) {
18
+ unit = 'zero';
19
+ }
20
+
17
21
  return { number, unit };
18
22
  };
19
23
 
@@ -47,7 +51,7 @@ export const checkValues = (start, end, value, prop = null) => {
47
51
  return null;
48
52
  }
49
53
 
50
- if (start.unit !== end.unit) {
54
+ if (start.unit !== end.unit && start.unit !== 'zero' && end.unit !== 'zero') {
51
55
  log.error(
52
56
  `Units need to match${prop ? ` (${prop})` : ''}: "clamp-[${value}]".`
53
57
  );