An open source design system that's fully customizable and agent ready
TypeScript
fix(docsite): make the gigantic preset concentric (#4412)
AK45a401a84b4c
diff --git a/apps/docsite/src/__tests__/theme-concentricity.test.ts b/apps/docsite/src/__tests__/theme-concentricity.test.tsnew file mode 100644index 0000000000..c4fbdb242a--- /dev/null+++ b/apps/docsite/src/__tests__/theme-concentricity.test.ts@@ -0,0 +1,84 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++import {describe, it, expect} from 'vitest';+import {expandRadiusScale} from '@astryxdesign/core/theme';+import {UNIFIED_PRESETS} from '../app/playground/themeEditor/constants';+import {+ buildSpacingScale,+ getConcentricityWarning,+} from '../app/playground/themeEditor/helpers';++const px = (value: string): number => parseInt(value, 10);++/**+ * Concentricity: two rounded rects nested with a gap between their edges look+ * concentric when `inner-radius = outer-radius - gap`. The theme editor nests+ * one spacing step (`--spacing-1`) between each radius rung, so the relation is+ * checked against the scales the editor actually generates rather than a+ * hardcoded table of expected px values.+ */+describe('theme editor presets — concentricity', () => {+ for (const [name, preset] of Object.entries(UNIFIED_PRESETS)) {+ it(`${name} preset nests its radius rungs concentrically`, () => {+ const radii = expandRadiusScale({base: preset.radius, multiplier: 1});+ const gap = px(buildSpacingScale(preset.spacing)['--spacing-1']);+ const inner = px(radii['--radius-inner']);+ const element = px(radii['--radius-element']);+ const container = px(radii['--radius-container']);++ // container ⊃ element+ expect(container - gap).toBe(element);+ // element ⊃ inner+ expect(element - gap).toBe(inner);+ });+ }+});++describe('getConcentricityWarning', () => {+ it('returns null when the radius and spacing bases match', () => {+ expect(getConcentricityWarning('radius', 4, 4)).toBeNull();+ expect(getConcentricityWarning('spacing', 4, 4)).toBeNull();+ expect(getConcentricityWarning('radius', 0, 0)).toBeNull();+ });++ it('returns null for every built-in preset', () => {+ for (const preset of Object.values(UNIFIED_PRESETS)) {+ expect(+ getConcentricityWarning('radius', preset.radius, preset.spacing),+ ).toBeNull();+ expect(+ getConcentricityWarning('spacing', preset.radius, preset.spacing),+ ).toBeNull();+ }+ });++ it('warns with concrete numbers when the bases diverge', () => {+ const warning = getConcentricityWarning('radius', 12, 8);+ expect(warning).not.toBeNull();+ // container(36) − spacing-1(8) = 28, but element is 24.+ expect(warning?.description).toContain('36px');+ expect(warning?.description).toContain('8px');+ expect(warning?.description).toContain('28px');+ expect(warning?.description).toContain('24px');+ });++ it('suggests changing the control it sits under', () => {+ expect(getConcentricityWarning('radius', 12, 8)?.description).toContain(+ 'Set radius to 8px',+ );+ expect(getConcentricityWarning('spacing', 12, 8)?.description).toContain(+ 'Set spacing to 12px',+ );+ });++ it('has a short title for the Banner header', () => {+ const warning = getConcentricityWarning('radius', 12, 8);+ expect(warning?.title).toBeTruthy();+ expect(warning?.title.length).toBeLessThanOrEqual(40);+ });++ it('returns null when a base is not a usable number', () => {+ expect(getConcentricityWarning('radius', Number.NaN, 8)).toBeNull();+ expect(getConcentricityWarning('spacing', 12, Number.NaN)).toBeNull();+ });+});diff --git a/apps/docsite/src/app/playground/themeEditor/BaseStylesPanel.tsx b/apps/docsite/src/app/playground/themeEditor/BaseStylesPanel.tsxindex 6ac37e9def..92dbe11c66 100644--- a/apps/docsite/src/app/playground/themeEditor/BaseStylesPanel.tsx+++ b/apps/docsite/src/app/playground/themeEditor/BaseStylesPanel.tsx@@ -13,10 +13,12 @@ import {Switch} from '@astryxdesign/core/Switch';import {ToggleButton, ToggleButtonGroup} from '@astryxdesign/core/ToggleButton';import {Tooltip} from '@astryxdesign/core/Tooltip';import {Icon} from '@astryxdesign/core/Icon';+import {Banner} from '@astryxdesign/core/Banner';import {expandRadiusScale, expandTypeScale} from '@astryxdesign/core/theme';import {ColorSwatch} from './ColorSwatch';import {SelectableCard} from '@astryxdesign/core/SelectableCard';import {FONT_OPTIONS, RATIO_OPTIONS, UNIFIED_PRESETS} from './constants';+import {getConcentricityWarning} from './helpers';const styles = stylex.create({fullWidthField: {width: '100%'},@@ -270,6 +272,21 @@ export function BaseStylesPanel({return sm === md - 4 && lg === md + 4 ? md : null;})();+ // Concentricity couples the radius and spacing bases. Warn — never block —+ // when they diverge, reading the same values the two controls display. #958+ const shownRadius = radiusMatch ?? radiusBase;+ const shownSpacing = spacingMatch ?? spacingBase;+ const radiusWarning = getConcentricityWarning(+ 'radius',+ shownRadius,+ shownSpacing,+ );+ const spacingWarning = getConcentricityWarning(+ 'spacing',+ shownRadius,+ shownSpacing,+ );+return (<VStack gap={5}>{/* Color */}@@ -499,44 +516,62 @@ export function BaseStylesPanel({{/* Shape & Layout */}<VStack gap={4}>- <ScaleControl- label="Corner Radius"- tooltip={`Linear scale: inner = ${radiusMatch ?? radiusBase}px, element = ${(radiusMatch ?? radiusBase) * 2}px, container = ${(radiusMatch ?? radiusBase) * 3}px, page = ${Math.round((radiusMatch ?? radiusBase) * 7)}px.`}- controlLabel="Radius"- options={[- {label: 'S', value: '2'},- {label: 'M', value: '4'},- {label: 'L', value: '6'},- {label: 'XL', value: '12'},- ]}- toggleValue={radiusMatch != null ? String(radiusMatch) : null}- onToggle={v => onApplyRadiusScale(Number(v))}- numberValue={radiusMatch ?? null}- onNumber={v => onApplyRadiusScale(v)}- min={0}- max={18}- step={2}- units="px"- />- <ScaleControl- label="Spacing"- tooltip={`Linear scale: step N = ${spacingMatch ?? spacingBase}px × N.`}- controlLabel="Spacing"- options={[- {label: 'S', value: '2'},- {label: 'M', value: '4'},- {label: 'L', value: '6'},- {label: 'XL', value: '8'},- ]}- toggleValue={spacingMatch != null ? String(spacingMatch) : null}- onToggle={v => onApplySpacingScale(Number(v))}- numberValue={spacingMatch ?? null}- onNumber={v => onApplySpacingScale(v)}- min={0}- max={16}- step={2}- units="px"- />+ <VStack gap={2}>+ <ScaleControl+ label="Corner Radius"+ tooltip={`Linear scale: inner = ${shownRadius}px, element = ${shownRadius * 2}px, container = ${shownRadius * 3}px, page = ${Math.round(shownRadius * 7)}px.`}+ controlLabel="Radius"+ options={[+ {label: 'S', value: '2'},+ {label: 'M', value: '4'},+ {label: 'L', value: '6'},+ {label: 'XL', value: '8'},+ ]}+ toggleValue={radiusMatch != null ? String(radiusMatch) : null}+ onToggle={v => onApplyRadiusScale(Number(v))}+ numberValue={radiusMatch ?? null}+ onNumber={v => onApplyRadiusScale(v)}+ min={0}+ max={18}+ step={2}+ units="px"+ />+ {radiusWarning && (+ <Banner+ status="warning"+ title={radiusWarning.title}+ description={radiusWarning.description}+ />+ )}+ </VStack>+ <VStack gap={2}>+ <ScaleControl+ label="Spacing"+ tooltip={`Linear scale: step N = ${shownSpacing}px × N.`}+ controlLabel="Spacing"+ options={[+ {label: 'S', value: '2'},+ {label: 'M', value: '4'},+ {label: 'L', value: '6'},+ {label: 'XL', value: '8'},+ ]}+ toggleValue={spacingMatch != null ? String(spacingMatch) : null}+ onToggle={v => onApplySpacingScale(Number(v))}+ numberValue={spacingMatch ?? null}+ onNumber={v => onApplySpacingScale(v)}+ min={0}+ max={16}+ step={2}+ units="px"+ />+ {spacingWarning && (+ <Banner+ status="warning"+ title={spacingWarning.title}+ description={spacingWarning.description}+ />+ )}+ </VStack><ScaleControllabel="Element Size"tooltip={`sm = ${(sizeMatch ?? sizeBase) - 4}px, md = ${sizeMatch ?? sizeBase}px, lg = ${(sizeMatch ?? sizeBase) + 4}px.`}diff --git a/apps/docsite/src/app/playground/themeEditor/constants.ts b/apps/docsite/src/app/playground/themeEditor/constants.tsindex 4b2d52c701..74d73c2c3f 100644--- a/apps/docsite/src/app/playground/themeEditor/constants.ts+++ b/apps/docsite/src/app/playground/themeEditor/constants.ts@@ -34,6 +34,10 @@ export const RATIO_OPTIONS = [{value: 1.618, label: '1.618 — Golden Ratio'},];+// Every preset keeps `radius === spacing` so nested corners stay concentric by+// default: the radius scale steps by `base` per rung (inner ×1, element ×2,+// container ×3) and one spacing step sits between rungs, so+// `outer-radius − spacing-1 = inner-radius` only holds when the bases match. #958export const UNIFIED_PRESETS = {compact: {typeBase: 12,@@ -60,7 +64,7 @@ export const UNIFIED_PRESETS = {typeBase: 18,typeRatio: 1.414,spacing: 8,- radius: 12,+ radius: 8,sizeMd: 48,},} as const;diff --git a/apps/docsite/src/app/playground/themeEditor/helpers.ts b/apps/docsite/src/app/playground/themeEditor/helpers.tsindex 50123fa29c..4672ec7d8f 100644--- a/apps/docsite/src/app/playground/themeEditor/helpers.ts+++ b/apps/docsite/src/app/playground/themeEditor/helpers.ts@@ -1,7 +1,7 @@// Copyright (c) Meta Platforms, Inc. and affiliates.import {COMPONENT_VAR_TO_OVERRIDE, COMPONENT_VAR_NAMES} from './constants';-import {expandColorScale} from '@astryxdesign/core/theme';+import {expandColorScale, expandRadiusScale} from '@astryxdesign/core/theme';export function parseLightDark(value: string,@@ -78,6 +78,55 @@ export function buildSpacingScale(base: number): Record<string, string> {return patch;}+export interface ConcentricityWarning {+ /** Short Banner title. */+ title: string;+ /** The conflict in concrete px, plus what to change to resolve it. */+ description: string;+}++/**+ * Copy for the warning banner shown under the radius and spacing controls when+ * the two bases diverge, or `null` while concentricity holds.+ *+ * Nested rounded rects look concentric when `inner-radius = outer-radius − gap`.+ * The radius scale steps by `base` per rung (inner ×1, element ×2, container ×3)+ * and the editor nests one spacing step (`--spacing-1` = the spacing base)+ * between rungs, so the relation collapses to `radiusBase === spacingBase`.+ *+ * Educate over enforce: the numbers come from the scales the editor actually+ * generates, so the message stays correct if those scales change. `control`+ * picks which knob the suggestion points at — the banner renders under both+ * sliders and each one offers to move itself. #958+ */+export function getConcentricityWarning(+ control: 'radius' | 'spacing',+ radiusBase: number,+ spacingBase: number,+): ConcentricityWarning | null {+ if (!Number.isFinite(radiusBase) || !Number.isFinite(spacingBase)) {+ return null;+ }+ if (radiusBase === spacingBase) {+ return null;+ }+ const radii = expandRadiusScale({base: radiusBase, multiplier: 1});+ const container = parseInt(radii['--radius-container'], 10);+ const element = parseInt(radii['--radius-element'], 10);+ const gap = parseInt(buildSpacingScale(spacingBase)['--spacing-1'], 10);+ const suggestion =+ control === 'radius'+ ? `Set radius to ${spacingBase}px`+ : `Set spacing to ${radiusBase}px`;+ return {+ title: 'Concentricity broken',+ description:+ `Container radius (${container}px) − spacing-1 (${gap}px) = ${container - gap}px, ` ++ `but element radius is ${element}px, so nested corners won't look ` ++ `concentric. ${suggestion} to match.`,+ };+}+type ComponentStyleMap = Record<string, Record<string, Record<string, string>>>;export function buildComponentOverrides(@@ -240,8 +289,13 @@ export function generateThemeCode(return lines.join('\n');}-export function getExpandedColorScale(accentHex: string): Record<string, string> {- const derived = expandColorScale({accent: accentHex}) as Record<string, string>;+export function getExpandedColorScale(+ accentHex: string,+): Record<string, string> {+ const derived = expandColorScale({accent: accentHex}) as Record<+ string,+ string+ >;// Do not override the user's custom picker selection for `--color-accent` itselfdelete derived['--color-accent'];let hex = accentHex.replace('#', '');