An open source design system that's fully customizable and agent ready
TypeScript
feat(themes): pin type scale on touch + narrow viewports (#6699)
Xinzi Zhou8527f921e82f
diff --git a/.changeset/mobile-type-pin-touch-narrow.md b/.changeset/mobile-type-pin-touch-narrow.mdnew file mode 100644index 0000000000..46d7e7f10d--- /dev/null+++ b/.changeset/mobile-type-pin-touch-narrow.md@@ -0,0 +1,15 @@+---+'@astryxdesign/theme-butter': minor+'@astryxdesign/theme-chocolate': minor+'@astryxdesign/theme-neutral': minor+'@astryxdesign/theme-stone': minor+'@astryxdesign/cli': minor+---++[breaking] On touch devices narrower than the `lg` breakpoint (`(width < 1024px) and (pointer: coarse)`), the 14px-base first-party themes now floor their type base to 16px and re-derive the ratio so Display 1 keeps its desktop size: neutral and chocolate (base 14, ratio 1.2 → 1.1736) and butter and stone (base 14, ratio 1.25 → 1.2225). On phones, body, label and code text rise from 14px to 16px; supporting text, the headings and Display 2 and 3 grow by 1–2px; only Display 1 holds its desktop size. Desktop sizes are unchanged. gothic, matcha and y2k already use a 16px base and need no rule. The CLI's theme templates carry the same rule, so a newly scaffolded theme gets it too.++An app theme that extends one of these four themes and sets its own `typography.scale` or type-size tokens inherits this rule, and on touch + narrow screens the rule replaces those values (a base-18 child drops to a 16px body and a 42px Display 1). Append your own rule for the same condition with the values you want; a later rule for the same condition wins.++Migration: nothing to change to take the new phone scale. To keep the previous 14px phone scale in an app, extend the theme and append a later rule for the same condition: `defineTheme({name: 'app', extends: neutralTheme, adaptations: {rules: [{when: {pointer: 'coarse', width: {below: 'lg'}}, value: {typography: {scale: {base: 14, ratio: 1.2}}}}]}})`. For butter and stone use `ratio: 1.25` and also set `tokens: {'--text-supporting-size': '12px'}` in that rule's value, because those themes pin supporting text at 12px rather than the 11px the scale alone gives. This cannot be rewritten by a codemod: whether an app wants the floor is a product choice.++@imdreamrunnerdiff --git a/packages/cli/assets/templates/themes/butter/butterTheme.ts b/packages/cli/assets/templates/themes/butter/butterTheme.tsindex 641db25868..ddbf7ae752 100644--- a/packages/cli/assets/templates/themes/butter/butterTheme.ts+++ b/packages/cli/assets/templates/themes/butter/butterTheme.ts@@ -65,6 +65,18 @@ export const butterTheme = defineTheme({},},+ // Touch + narrow (Mobile Type, Pin): floor base to the 16px floor and+ // re-derive the ratio so Display 1 keeps its desktop size:+ // ratio' = 1.25 * (14/16)^(1/6) ~= 1.2225. Body 14 -> 16px, display-1 stays 53px.+ adaptations: {+ rules: [+ {+ when: {pointer: 'coarse', width: {below: 'lg'}},+ value: {typography: {scale: {base: 16, ratio: 1.2225}}},+ },+ ],+ },+motion: {fast: 125, medium: 300, slow: 700, ratio: 0.75},syntax: butterSyntax,diff --git a/packages/cli/assets/templates/themes/chocolate/chocolateTheme.ts b/packages/cli/assets/templates/themes/chocolate/chocolateTheme.tsindex 75ec917b9c..cc724c1ab4 100644--- a/packages/cli/assets/templates/themes/chocolate/chocolateTheme.ts+++ b/packages/cli/assets/templates/themes/chocolate/chocolateTheme.ts@@ -55,6 +55,18 @@ export const chocolateTheme = defineTheme({},},+ // Touch + narrow (Mobile Type, Pin): floor base to the 16px floor and+ // re-derive the ratio so Display 1 keeps its desktop size:+ // ratio' = 1.2 * (14/16)^(1/6) ~= 1.1736. Body 14 -> 16px, display-1 stays 42px.+ adaptations: {+ rules: [+ {+ when: {pointer: 'coarse', width: {below: 'lg'}},+ value: {typography: {scale: {base: 16, ratio: 1.1736}}},+ },+ ],+ },+motion: {fast: 125, medium: 300, slow: 700, ratio: 0.75},syntax: chocolateSyntax,diff --git a/packages/cli/assets/templates/themes/neutral/neutralTheme.ts b/packages/cli/assets/templates/themes/neutral/neutralTheme.tsindex af615504cd..57c7778bdd 100644--- a/packages/cli/assets/templates/themes/neutral/neutralTheme.ts+++ b/packages/cli/assets/templates/themes/neutral/neutralTheme.ts@@ -95,6 +95,18 @@ export const neutralTheme = defineTheme({},},+ // Touch + narrow (Mobile Type, Pin): floor base to the 16px floor and+ // re-derive the ratio so Display 1 keeps its desktop size:+ // ratio' = 1.2 * (14/16)^(1/6) ~= 1.1736. Body 14 -> 16px, display-1 stays 42px.+ adaptations: {+ rules: [+ {+ when: {pointer: 'coarse', width: {below: 'lg'}},+ value: {typography: {scale: {base: 16, ratio: 1.1736}}},+ },+ ],+ },+// Motion: snappier than default to match shadcn/Tailwind conventions.// Produces: fast-min=95ms, fast=125ms, fast-max=165ms,// medium-min=225ms, medium=300ms, medium-max=400ms.diff --git a/packages/cli/assets/templates/themes/stone/stoneTheme.ts b/packages/cli/assets/templates/themes/stone/stoneTheme.tsindex a1e5d2ef1d..3f741a23db 100644--- a/packages/cli/assets/templates/themes/stone/stoneTheme.ts+++ b/packages/cli/assets/templates/themes/stone/stoneTheme.ts@@ -81,6 +81,18 @@ export const stoneTheme = defineTheme({},},+ // Touch + narrow (Mobile Type, Pin): floor base to the 16px floor and+ // re-derive the ratio so Display 1 keeps its desktop size:+ // ratio' = 1.25 * (14/16)^(1/6) ~= 1.2225. Body 14 -> 16px, display-1 stays 53px.+ adaptations: {+ rules: [+ {+ when: {pointer: 'coarse', width: {below: 'lg'}},+ value: {typography: {scale: {base: 16, ratio: 1.2225}}},+ },+ ],+ },+motion: {fast: 125, medium: 300, slow: 700, ratio: 0.75},syntax: stoneSyntax,diff --git a/packages/themes/butter/src/butterTheme.test.ts b/packages/themes/butter/src/butterTheme.test.tsnew file mode 100644index 0000000000..0ed627d08c--- /dev/null+++ b/packages/themes/butter/src/butterTheme.test.ts@@ -0,0 +1,25 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++import {describe, expect, it} from 'vitest';+import {generateThemeCSS} from '@astryxdesign/core/theme';+import {butterTheme} from './butterTheme';++describe('butter touch + narrow type pin', () => {+ it('floors body to 16px and holds Display 1 at its desktop size', () => {+ const rule = butterTheme.__adaptationRules?.[0];++ expect(butterTheme.__adaptations.rules).toHaveLength(1);+ expect(rule?.when).toEqual({pointer: 'coarse', width: {below: 'lg'}});+ expect(rule?.query).toBe('(width < 1024px) and (pointer: coarse)');+ // Pinned ladder: base 16 (was 14), ratio 1.25 -> 1.2225, Display 1 pinned.+ expect(rule?.tokens['--font-size-base']).toBe('1rem');+ expect(rule?.tokens['--font-size-lg']).toBe('1.25rem');+ expect(rule?.tokens['--font-size-5xl']).toBe('3.3125rem');+ // Desktop scale is untouched.+ expect(butterTheme.tokens['--font-size-base']).toBe('0.875rem');+ expect(butterTheme.tokens['--font-size-5xl']).toBe('3.3125rem');+ expect(generateThemeCSS(butterTheme).component).toContain(+ '@media (width < 1024px) and (pointer: coarse)',+ );+ });+});diff --git a/packages/themes/butter/src/butterTheme.ts b/packages/themes/butter/src/butterTheme.tsindex 641db25868..ddbf7ae752 100644--- a/packages/themes/butter/src/butterTheme.ts+++ b/packages/themes/butter/src/butterTheme.ts@@ -65,6 +65,18 @@ export const butterTheme = defineTheme({},},+ // Touch + narrow (Mobile Type, Pin): floor base to the 16px floor and+ // re-derive the ratio so Display 1 keeps its desktop size:+ // ratio' = 1.25 * (14/16)^(1/6) ~= 1.2225. Body 14 -> 16px, display-1 stays 53px.+ adaptations: {+ rules: [+ {+ when: {pointer: 'coarse', width: {below: 'lg'}},+ value: {typography: {scale: {base: 16, ratio: 1.2225}}},+ },+ ],+ },+motion: {fast: 125, medium: 300, slow: 700, ratio: 0.75},syntax: butterSyntax,diff --git a/packages/themes/chocolate/src/chocolateTheme.test.ts b/packages/themes/chocolate/src/chocolateTheme.test.tsnew file mode 100644index 0000000000..010c945db5--- /dev/null+++ b/packages/themes/chocolate/src/chocolateTheme.test.ts@@ -0,0 +1,25 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++import {describe, expect, it} from 'vitest';+import {generateThemeCSS} from '@astryxdesign/core/theme';+import {chocolateTheme} from './chocolateTheme';++describe('chocolate touch + narrow type pin', () => {+ it('floors body to 16px and holds Display 1 at its desktop size', () => {+ const rule = chocolateTheme.__adaptationRules?.[0];++ expect(chocolateTheme.__adaptations.rules).toHaveLength(1);+ expect(rule?.when).toEqual({pointer: 'coarse', width: {below: 'lg'}});+ expect(rule?.query).toBe('(width < 1024px) and (pointer: coarse)');+ // Pinned ladder: base 16 (was 14), ratio 1.2 -> 1.1736, Display 1 pinned.+ expect(rule?.tokens['--font-size-base']).toBe('1rem');+ expect(rule?.tokens['--font-size-lg']).toBe('1.1875rem');+ expect(rule?.tokens['--font-size-5xl']).toBe('2.625rem');+ // Desktop scale is untouched.+ expect(chocolateTheme.tokens['--font-size-base']).toBe('0.875rem');+ expect(chocolateTheme.tokens['--font-size-5xl']).toBe('2.625rem');+ expect(generateThemeCSS(chocolateTheme).component).toContain(+ '@media (width < 1024px) and (pointer: coarse)',+ );+ });+});diff --git a/packages/themes/chocolate/src/chocolateTheme.ts b/packages/themes/chocolate/src/chocolateTheme.tsindex 75ec917b9c..cc724c1ab4 100644--- a/packages/themes/chocolate/src/chocolateTheme.ts+++ b/packages/themes/chocolate/src/chocolateTheme.ts@@ -55,6 +55,18 @@ export const chocolateTheme = defineTheme({},},+ // Touch + narrow (Mobile Type, Pin): floor base to the 16px floor and+ // re-derive the ratio so Display 1 keeps its desktop size:+ // ratio' = 1.2 * (14/16)^(1/6) ~= 1.1736. Body 14 -> 16px, display-1 stays 42px.+ adaptations: {+ rules: [+ {+ when: {pointer: 'coarse', width: {below: 'lg'}},+ value: {typography: {scale: {base: 16, ratio: 1.1736}}},+ },+ ],+ },+motion: {fast: 125, medium: 300, slow: 700, ratio: 0.75},syntax: chocolateSyntax,diff --git a/packages/themes/neutral/src/neutralTheme.test.ts b/packages/themes/neutral/src/neutralTheme.test.tsindex cfed792271..12f542d465 100644--- a/packages/themes/neutral/src/neutralTheme.test.ts+++ b/packages/themes/neutral/src/neutralTheme.test.ts@@ -1,6 +1,7 @@// Copyright (c) Meta Platforms, Inc. and affiliates.import {describe, expect, it} from 'vitest';+import {generateThemeCSS} from '@astryxdesign/core/theme';import {neutralPalettes} from './neutralPalettes';import {neutralTheme} from './neutralTheme';@@ -407,3 +408,23 @@ describe('neutral theme component overrides', () => {});});});++describe('neutral touch + narrow type pin', () => {+ it('floors body to 16px and holds Display 1 at its desktop size', () => {+ const rule = neutralTheme.__adaptationRules?.[0];++ expect(neutralTheme.__adaptations.rules).toHaveLength(1);+ expect(rule?.when).toEqual({pointer: 'coarse', width: {below: 'lg'}});+ expect(rule?.query).toBe('(width < 1024px) and (pointer: coarse)');+ // Pinned ladder: base 16 (was 14), ratio 1.2 -> 1.1736, Display 1 pinned.+ expect(rule?.tokens['--font-size-base']).toBe('1rem');+ expect(rule?.tokens['--font-size-lg']).toBe('1.1875rem');+ expect(rule?.tokens['--font-size-5xl']).toBe('2.625rem');+ // Desktop scale is untouched.+ expect(neutralTheme.tokens['--font-size-base']).toBe('0.875rem');+ expect(neutralTheme.tokens['--font-size-5xl']).toBe('2.625rem');+ expect(generateThemeCSS(neutralTheme).component).toContain(+ '@media (width < 1024px) and (pointer: coarse)',+ );+ });+});diff --git a/packages/themes/neutral/src/neutralTheme.ts b/packages/themes/neutral/src/neutralTheme.tsindex af615504cd..57c7778bdd 100644--- a/packages/themes/neutral/src/neutralTheme.ts+++ b/packages/themes/neutral/src/neutralTheme.ts@@ -95,6 +95,18 @@ export const neutralTheme = defineTheme({},},+ // Touch + narrow (Mobile Type, Pin): floor base to the 16px floor and+ // re-derive the ratio so Display 1 keeps its desktop size:+ // ratio' = 1.2 * (14/16)^(1/6) ~= 1.1736. Body 14 -> 16px, display-1 stays 42px.+ adaptations: {+ rules: [+ {+ when: {pointer: 'coarse', width: {below: 'lg'}},+ value: {typography: {scale: {base: 16, ratio: 1.1736}}},+ },+ ],+ },+// Motion: snappier than default to match shadcn/Tailwind conventions.// Produces: fast-min=95ms, fast=125ms, fast-max=165ms,// medium-min=225ms, medium=300ms, medium-max=400ms.diff --git a/packages/themes/stone/src/stoneTheme.test.ts b/packages/themes/stone/src/stoneTheme.test.tsnew file mode 100644index 0000000000..feafc980f3--- /dev/null+++ b/packages/themes/stone/src/stoneTheme.test.ts@@ -0,0 +1,25 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++import {describe, expect, it} from 'vitest';+import {generateThemeCSS} from '@astryxdesign/core/theme';+import {stoneTheme} from './stoneTheme';++describe('stone touch + narrow type pin', () => {+ it('floors body to 16px and holds Display 1 at its desktop size', () => {+ const rule = stoneTheme.__adaptationRules?.[0];++ expect(stoneTheme.__adaptations.rules).toHaveLength(1);+ expect(rule?.when).toEqual({pointer: 'coarse', width: {below: 'lg'}});+ expect(rule?.query).toBe('(width < 1024px) and (pointer: coarse)');+ // Pinned ladder: base 16 (was 14), ratio 1.25 -> 1.2225, Display 1 pinned.+ expect(rule?.tokens['--font-size-base']).toBe('1rem');+ expect(rule?.tokens['--font-size-lg']).toBe('1.25rem');+ expect(rule?.tokens['--font-size-5xl']).toBe('3.3125rem');+ // Desktop scale is untouched.+ expect(stoneTheme.tokens['--font-size-base']).toBe('0.875rem');+ expect(stoneTheme.tokens['--font-size-5xl']).toBe('3.3125rem');+ expect(generateThemeCSS(stoneTheme).component).toContain(+ '@media (width < 1024px) and (pointer: coarse)',+ );+ });+});diff --git a/packages/themes/stone/src/stoneTheme.ts b/packages/themes/stone/src/stoneTheme.tsindex a1e5d2ef1d..3f741a23db 100644--- a/packages/themes/stone/src/stoneTheme.ts+++ b/packages/themes/stone/src/stoneTheme.ts@@ -81,6 +81,18 @@ export const stoneTheme = defineTheme({},},+ // Touch + narrow (Mobile Type, Pin): floor base to the 16px floor and+ // re-derive the ratio so Display 1 keeps its desktop size:+ // ratio' = 1.25 * (14/16)^(1/6) ~= 1.2225. Body 14 -> 16px, display-1 stays 53px.+ adaptations: {+ rules: [+ {+ when: {pointer: 'coarse', width: {below: 'lg'}},+ value: {typography: {scale: {base: 16, ratio: 1.2225}}},+ },+ ],+ },+motion: {fast: 125, medium: 300, slow: 700, ratio: 0.75},syntax: stoneSyntax,