@@ -158,18 +158,33 @@ This implementation is inspired by [Tailwind CSS’s `css-functions.js`](https:/
158
158
You can also implement [ Fluid Typography] ( https://www.smashingmagazine.com/2022/01/modern-fluid-typography-css-clamp/ ) as a custom function.
159
159
160
160
``` js
161
+ const inputPattern = / ^ ([+-] ? [0-9 ] * \. ? [0-9 ] + )(px| rem)$ / ;
162
+
163
+ function parseAsRem (input ) {
164
+ const match = input .match (inputPattern);
165
+
166
+ if (! match) {
167
+ throw new Error (` ${ input} is invalid input.` );
168
+ }
169
+
170
+ const [, value , unit ] = match;
171
+ const divider = unit === ' px' ? 16 : 1 ;
172
+
173
+ return Number (value) / divider;
174
+ }
175
+
161
176
function round (n ) {
162
177
return Math .round ((n + Number .EPSILON ) * 10000 ) / 10000 ;
163
178
}
164
179
165
180
function fluid (
166
- min ,
167
- max ,
168
- minBreakpoint = ' 640 ' ,
169
- maxBreakpoint = ' 1536 ' ,
181
+ minSize ,
182
+ maxSize ,
183
+ minBreakpoint = ' 40rem ' ,
184
+ maxBreakpoint = ' 96rem ' ,
170
185
... rest
171
186
) {
172
- if (! min || ! max ) {
187
+ if (! minSize || ! maxSize ) {
173
188
throw new Error (
174
189
' The --fluid(…) function requires at least 2 arguments, but received insufficient arguments.' ,
175
190
);
@@ -183,21 +198,18 @@ function fluid(
183
198
);
184
199
}
185
200
186
- min = Number (min );
187
- max = Number (max );
188
- minBreakpoint = Number (minBreakpoint);
189
- maxBreakpoint = Number (maxBreakpoint);
201
+ minSize = parseAsRem (minSize );
202
+ maxSize = parseAsRem (maxSize );
203
+ minBreakpoint = parseAsRem (minBreakpoint);
204
+ maxBreakpoint = parseAsRem (maxBreakpoint);
190
205
191
- const divider = 16 ;
192
- const slope =
193
- (max / divider - min / divider) /
194
- (maxBreakpoint / divider - minBreakpoint / divider);
195
- const intersection = - 1 * (minBreakpoint / divider) * slope + min / divider;
206
+ const slope = (maxSize - minSize) / (maxBreakpoint - minBreakpoint);
207
+ const intersection = - 1 * minBreakpoint * slope + minSize;
196
208
197
209
return ` clamp(${ [
198
- ` ${ (min > max ? max : min) / divider } rem` ,
210
+ ` ${ minSize > maxSize ? maxSize : minSize } rem` ,
199
211
` ${ round (intersection)} rem + ${ round (slope * 100 )} svw` ,
200
- ` ${ (min > max ? min : max) / divider } rem` ,
212
+ ` ${ minSize > maxSize ? minSize : maxSize } rem` ,
201
213
].join (' , ' )} )` ;
202
214
}
203
215
@@ -238,13 +250,13 @@ function toPx(length) {
238
250
}
239
251
240
252
function fluid (
241
- min ,
242
- max ,
253
+ minSize ,
254
+ maxSize ,
243
255
minBreakpoint = ' var(--breakpoint-sm)' ,
244
256
maxBreakpoint = ' var(--breakpoint-2xl)' ,
245
257
... rest
246
258
) {
247
- if (! min || ! max ) {
259
+ if (! minSize || ! maxSize ) {
248
260
throw new Error (
249
261
' The --fluid(…) function requires at least 2 arguments, but received insufficient arguments.' ,
250
262
);
@@ -258,14 +270,14 @@ function fluid(
258
270
);
259
271
}
260
272
261
- const t = ` ( ${ toPx ( ' 100svw ' ) } - ${ toPx (minBreakpoint) } ) / ( ${ toPx (
262
- maxBreakpoint,
263
- )} - ${ toPx (minBreakpoint)} )` ;
273
+ const t =
274
+ ` ( ${ toPx ( ' 100svw ' ) } - ${ toPx (minBreakpoint) } ) ` +
275
+ ` / ( ${ toPx (maxBreakpoint )} - ${ toPx (minBreakpoint)} )` ;
264
276
265
277
return ` clamp(${ [
266
- ` min(${ min } , ${ max } )` ,
267
- ` ${ min } + (${ max } - ${ min } ) * ${ t} ` ,
268
- ` max(${ min } , ${ max } )` ,
278
+ ` min(${ minSize } , ${ maxSize } )` ,
279
+ ` ${ minSize } + (${ maxSize } - ${ minSize } ) * ${ t} ` ,
280
+ ` max(${ minSize } , ${ maxSize } )` ,
269
281
].join (' , ' )} )` ;
270
282
}
271
283
0 commit comments