Skip to content

Commit 8f9bc86

Browse files
authored
[N/A] Dynamic Fluid Font Sizes (#193)
* [N/A] Dynamic fluid font sizes * [N/A] Added a comment
1 parent 33d6a8c commit 8f9bc86

File tree

4 files changed

+85
-13
lines changed

4 files changed

+85
-13
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
:root {
2+
/* Viewport breakpoints (pulled from font-sizes config) */
3+
--fluid-bp-min: var(--cfg-fluid-bp-min);
4+
--fluid-bp-max: var(--cfg-fluid-bp-max);
5+
6+
/* Linear interpolation factor between 0–1 */
7+
/* Using clamp to ensure t stays between 0 and 1 */
8+
--fluid-t: clamp(0, calc((100vw - (var(--fluid-bp-min) * 1px)) / ((var(--fluid-bp-max) - var(--fluid-bp-min)) * 1px)), 1);
9+
10+
/* Computed fluid tokens */
11+
--fluid-text-sm: clamp(var(--fs-sm-min), calc(var(--fs-sm-min) + (var(--fs-sm-max) - var(--fs-sm-min)) * var(--fluid-t)), var(--fs-sm-max));
12+
--fluid-text-base: clamp(var(--fs-base-min), calc(var(--fs-base-min) + (var(--fs-base-max) - var(--fs-base-min)) * var(--fluid-t)), var(--fs-base-max));
13+
--fluid-text-md: clamp(var(--fs-md-min), calc(var(--fs-md-min) + (var(--fs-md-max) - var(--fs-md-min)) * var(--fluid-t)), var(--fs-md-max));
14+
--fluid-text-lg: clamp(var(--fs-lg-min), calc(var(--fs-lg-min) + (var(--fs-lg-max) - var(--fs-lg-min)) * var(--fluid-t)), var(--fs-lg-max));
15+
--fluid-text-xl: clamp(var(--fs-xl-min), calc(var(--fs-xl-min) + (var(--fs-xl-max) - var(--fs-xl-min)) * var(--fluid-t)), var(--fs-xl-max));
16+
--fluid-text-2xl: clamp(var(--fs-2xl-min), calc(var(--fs-2xl-min) + (var(--fs-2xl-max) - var(--fs-2xl-min)) * var(--fluid-t)), var(--fs-2xl-max));
17+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
:root {
2+
--cfg-font-body: var(--wp--preset--font-family--body);
3+
--cfg-font-heading: var(--wp--preset--font-family--heading);
4+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
:root {
2+
/* ===========================================================
3+
VIEWPORT BREAKPOINTS
4+
=========================================================== */
5+
/* Unitless numbers for math */
6+
--cfg-fluid-bp-min: 375; /* smallest viewport in px */
7+
8+
/* Fallback to 1200px if WP var isn't set */
9+
--cfg-fluid-bp-max: calc(var(--wp--style--global--content-size,1200px)/1px);
10+
11+
/* ===========================================================
12+
STRICT FONT SIZES
13+
These font sizes should also be mirrored in
14+
theme-json/settings/typography.js#fontSizes.
15+
=========================================================== */
16+
--cfg-text-zero: 0;
17+
--cfg-text-2xs: 0.5rem; /* 8px */
18+
--cfg-text-xs: 0.625rem; /* 10px */
19+
20+
/* ===========================================================
21+
FLUID FONT SIZE RANGES
22+
=========================================================== */
23+
/* sm: 12px / 14px */
24+
--fs-sm-min: 0.75rem;
25+
--fs-sm-max: 0.875rem;
26+
27+
/* base: 15px / 16px */
28+
--fs-base-min: 0.9375rem;
29+
--fs-base-max: 1rem;
30+
31+
/* md: 18px / 24px */
32+
--fs-md-min: 1.125rem;
33+
--fs-md-max: 1.5rem;
34+
35+
/* lg: 24px / 32px */
36+
--fs-lg-min: 1.5rem;
37+
--fs-lg-max: 2rem;
38+
39+
/* xl: 32px / 48px */
40+
--fs-xl-min: 2rem;
41+
--fs-xl-max: 3rem;
42+
43+
/* 2xl: 48px / 64px */
44+
--fs-2xl-min: 3rem;
45+
--fs-2xl-max: 4rem;
46+
}
Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1+
@import './inc/font-families.css';
2+
@import './inc/font-sizes.css';
3+
@import './inc/font-calculations.css';
4+
15
@theme {
26
/* Font Families */
3-
--font-sans: var(--wp--preset--font-family--body);
4-
--font-body: var(--wp--preset--font-family--body);
5-
--font-heading: var(--wp--preset--font-family--heading);
7+
--font-sans: var(--cfg-font-body);
8+
--font-body: var(--cfg-font-body);
9+
--font-heading: var(--cfg-font-heading);
610

7-
/* Fluid Font Sizes */
8-
--text-zero: 0;
9-
--text-2xs: 0.5rem; /* 8px */
10-
--text-xs: 0.625rem; /* 10px */
11-
--text-sm: clamp(0.75rem, 2vw + 0.1rem, 0.875rem); /* 12px / 14px */
12-
--text-base: clamp(0.9375rem, 2vw + 0.1rem, 1rem); /* 15px / 16px */
13-
--text-md: clamp(1.125rem, 3vw + 0.1rem, 1.5rem); /* 18px / 24px */
14-
--text-lg: clamp(1.5rem, 3vw + 0.5rem, 2rem); /* 24px / 32px */
15-
--text-xl: clamp(2rem, 3vw + 1rem, 3rem); /* 32px / 48px */
16-
--text-2xl: clamp(3rem, 3vw + 2rem, 4rem); /* 48px / 64px */
11+
/* Font Sizes */
12+
--text-zero: var(--cfg-text-zero);
13+
--text-2xs: var(--cfg-text-2xs);
14+
--text-xs: var(--cfg-text-xs);
15+
--text-sm: var(--fluid-text-sm);
16+
--text-base: var(--fluid-text-base);
17+
--text-md: var(--fluid-text-md);
18+
--text-lg: var(--fluid-text-lg);
19+
--text-xl: var(--fluid-text-xl);
20+
--text-2xl: var(--fluid-text-2xl);
21+
--text-huge: var(--fluid-text-huge);
1722
}

0 commit comments

Comments
 (0)