Skip to content

Commit eb2e37c

Browse files
author
Petter Walbø Johnsgård
authored
Support both config object or argument (#1)
1 parent 6f6ddec commit eb2e37c

File tree

3 files changed

+78
-9
lines changed

3 files changed

+78
-9
lines changed

Diff for: README.md

+3
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,21 @@ clamp('1rem', '2rem');
3939
*/
4040

4141
// 'clamp(0.5rem, 0.3239rem + 0.5634vw, 1rem)'
42+
clamp(16, 32, 400, 1000);
4243
clamp(16, 32, {
4344
minWidth: 400,
4445
maxWidth: 1000,
4546
});
4647

4748
// 'clamp(0.5rem, 0.3239rem + 0.5634vw, 1rem)'
49+
clamp('16px', '32px', '400px', '1000px');
4850
clamp('16px', '32px', {
4951
minWidth: '400px',
5052
maxWidth: '1000px',
5153
});
5254

5355
// 'clamp(0.5rem, 0.3239rem + 0.5634vw, 1rem)'
56+
clamp('1rem', '2rem', '25rem', '62.5rem');
5457
clamp('1rem', '2rem', {
5558
minWidth: '25rem',
5659
maxWidth: '62.5rem',

Diff for: __tests__/index.test.ts

+33-2
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,53 @@ describe('clamp', () => {
3737
});
3838

3939
it('should handle different widths', () => {
40+
const expected = 'clamp(0.5rem, 0rem + 8vw, 1rem)';
41+
42+
// With config object
4043
expect(
4144
clamp(8, 16, {
4245
minWidth: 100,
4346
maxWidth: 200,
4447
}),
45-
).toBe('clamp(0.5rem, 0rem + 8vw, 1rem)');
48+
).toBe(expected);
49+
50+
// With minWidth and maxWidth
51+
expect(clamp(8, 16, 100, 200)).toBe(expected);
4652
});
4753

4854
it('should handle different root', () => {
55+
const expected = 'clamp(0.4rem, 0rem + 8vw, 0.8rem)';
56+
57+
// With config object
4958
expect(
5059
clamp(8, 16, {
5160
minWidth: 100,
5261
maxWidth: 200,
5362
root: 20,
5463
}),
55-
).toBe('clamp(0.4rem, 0rem + 8vw, 0.8rem)');
64+
).toBe(expected);
65+
66+
// With minWidth and maxWidth
67+
expect(clamp(8, 16, 100, 200, 20)).toBe(expected);
68+
});
69+
70+
it('should ignore 4th and 5th arguments if 3rd is an object', () => {
71+
const expected = 'clamp(0.5rem, 0rem + 8vw, 1rem)';
72+
73+
// With config object
74+
expect(
75+
clamp(
76+
8,
77+
16,
78+
// @ts-expect-error Testing invalid arguments
79+
{
80+
minWidth: 100,
81+
maxWidth: 200,
82+
},
83+
900,
84+
24,
85+
),
86+
).toBe(expected);
5687
});
5788

5889
it('should throw a warning if a unsupported unit is passed', () => {

Diff for: src/index.ts

+42-7
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,65 @@ const defaultConfig: Config = {
1818
maxWidth: 1920,
1919
};
2020

21+
// Function Overloads
22+
function clamp(
23+
minSize: string | number,
24+
maxSize: string | number,
25+
config?: Partial<Config>,
26+
): string;
27+
28+
function clamp(
29+
minSize: string | number,
30+
maxSize: string | number,
31+
minWidth?: string | number,
32+
maxWidth?: string | number,
33+
root?: string | number,
34+
): string;
35+
2136
/**
2237
* Creates a CSS clamp() function that is supported by all modern browsers.
2338
*/
2439
function clamp(
2540
minSize: string | number,
2641
maxSize: string | number,
27-
config: Partial<Config> = {},
42+
configOrMinWidth?: Partial<Config> | string | number,
43+
maxWidth?: string | number,
44+
root?: string | number,
2845
): string {
2946
if (!minSize || !maxSize) {
3047
return '';
3148
}
3249

50+
let userConfig: Partial<Config> = {};
51+
52+
if (typeof configOrMinWidth === 'object') {
53+
userConfig = configOrMinWidth;
54+
} else {
55+
if (!!configOrMinWidth) {
56+
userConfig.minWidth = configOrMinWidth;
57+
}
58+
59+
if (!!maxWidth) {
60+
userConfig.maxWidth = maxWidth;
61+
}
62+
63+
if (!!root) {
64+
userConfig.root = root;
65+
}
66+
}
67+
3368
// Merge the global and local config
3469
const mergedConfig = {
3570
...getConfig(),
36-
...config,
71+
...userConfig,
3772
};
3873

39-
const root = parseFloat(String(mergedConfig.root));
74+
const rootSize = parseFloat(String(mergedConfig.root));
4075

41-
const minSizeRem = convertToRem(minSize, root);
42-
const maxSizeRem = convertToRem(maxSize, root);
43-
const minWidthRem = convertToRem(mergedConfig.minWidth, root);
44-
const maxWidthRem = convertToRem(mergedConfig.maxWidth, root);
76+
const minSizeRem = convertToRem(minSize, rootSize);
77+
const maxSizeRem = convertToRem(maxSize, rootSize);
78+
const minWidthRem = convertToRem(mergedConfig.minWidth, rootSize);
79+
const maxWidthRem = convertToRem(mergedConfig.maxWidth, rootSize);
4580

4681
if (
4782
[minSizeRem, maxSizeRem, minWidthRem, maxWidthRem].some((v) => isNaN(v))

0 commit comments

Comments
 (0)