tailwind-clamp 2.1.0 → 2.2.1
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 +3 -3
- package/package.json +1 -1
- package/src/clamp.js +1 -1
- package/src/index.js +11 -2
- package/src/parse-value.js +6 -2
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@ 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
|
-
-
|
|
13
|
+
- Supports using values defined in the Tailwind CSS configuration file, arbitrary values or a combination.
|
|
14
14
|
- Helper function to create clamped values directly in your config file.
|
|
15
15
|
|
|
16
16
|
## Installation
|
|
@@ -80,7 +80,7 @@ clamp-[<property>,<start>,<end>,[minViewportWidth,maxViewportWidth]]
|
|
|
80
80
|
- `property`: Property that the value should be applied to. See a list of all supported properties below.
|
|
81
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`.
|
|
82
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`.
|
|
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 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`.
|
|
84
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`.
|
|
85
85
|
|
|
86
86
|
### Examples
|
|
@@ -127,7 +127,7 @@ export default {
|
|
|
127
127
|
| **`[options.maxViewportWidth]`** | `{number}` | Viewport size, where the clamp stops. | `1440` |
|
|
128
128
|
| **`[options.unit]`** | `{'px'\|'rem'\|'em'}` | Unit that should be used in the css value. | `rem` |
|
|
129
129
|
|
|
130
|
-
|
|
130
|
+
All values are expected in pixels and will be converted to `[options.unit]`.
|
|
131
131
|
|
|
132
132
|
## Supported properties
|
|
133
133
|
|
package/package.json
CHANGED
package/src/clamp.js
CHANGED
|
@@ -6,7 +6,7 @@ export const clamp = (
|
|
|
6
6
|
_minvw = { number: 375, unit: 'px' },
|
|
7
7
|
_maxvw = { number: 1440, unit: 'px' }
|
|
8
8
|
) => {
|
|
9
|
-
const unit = _start.unit;
|
|
9
|
+
const unit = _start.unit === 'zero' ? _end.unit : _start.unit;
|
|
10
10
|
const isPx = unit === 'px';
|
|
11
11
|
|
|
12
12
|
let start = _start.number;
|
package/src/index.js
CHANGED
|
@@ -72,8 +72,17 @@ export default plugin.withOptions(function (options = defaultOptions) {
|
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
// handle other properties
|
|
75
|
-
const
|
|
76
|
-
const
|
|
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;
|
|
77
86
|
|
|
78
87
|
if (!checkValues(start, end, value)) {
|
|
79
88
|
return null;
|
package/src/parse-value.js
CHANGED
|
@@ -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
|
);
|