tailwind-clamp 2.0.1 → 2.1.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
@@ -11,6 +11,7 @@ The plugin is based on the formula presented in this [article](https://chriskirk
11
11
  - Supports `px`, `rem` and `em` units.
12
12
  - Support `text` values with multiple properties (`fontSize`, `lineHeight`, `letterSpacing`).
13
13
  - Support using 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
  }),
@@ -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.1",
3
+ "version": "2.1.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,14 +2,16 @@ 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';
6
-
7
- export default plugin.withOptions(function (
8
- options = {
9
- minViewportWidth: 375,
10
- maxViewportWidth: 1440,
11
- }
12
- ) {
5
+ import { clamp, clampValue } from './clamp.js';
6
+
7
+ const defaultOptions = {
8
+ minViewportWidth: 375,
9
+ maxViewportWidth: 1440,
10
+ };
11
+
12
+ export const clampValue = clampValue;
13
+
14
+ export default plugin.withOptions(function (options = defaultOptions) {
13
15
  return function ({ matchUtilities, theme, config }) {
14
16
  matchUtilities(
15
17
  {
@@ -19,13 +21,15 @@ export default plugin.withOptions(function (
19
21
  const minvw = parseValue(
20
22
  config().theme.screens[args[3]] ||
21
23
  args[3] ||
22
- `${options.minViewportWidth}`
24
+ options.minViewportWidth ||
25
+ defaultOptions.minViewportWidth
23
26
  );
24
27
 
25
28
  const maxvw = parseValue(
26
29
  config().theme.screens[args[4]] ||
27
30
  args[4] ||
28
- `${options.maxViewportWidth}`
31
+ options.maxViewportWidth ||
32
+ defaultOptions.maxViewportWidth
29
33
  );
30
34
 
31
35
  if (args.length < 3) {