Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(docsite): a fluid Overview showcase keeps an inset from its card's edges (#10027)

Xinzi Zhou38f1a1d40a28
diff --git a/apps/docsite/e2e/showcase-inset.spec.ts b/apps/docsite/e2e/showcase-inset.spec.ts
new file mode 100644
index 0000000000..0699b32da0
--- /dev/null
+++ b/apps/docsite/e2e/showcase-inset.spec.ts
@@ -0,0 +1,113 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file showcase-inset.spec.ts
+ * @input Production docsite component pages at phone and desktop widths
+ * @output Proof that the Overview showcase keeps an inline inset in its card
+ * @position Docsite browser tests; jsdom cannot measure layout
+ */
+
+import {expect, test, type Page} from '@playwright/test';
+
+/** The stage's gutter, `--spacing-4` in the neutral preview theme, in px. */
+const GUTTER = 16;
+
+/**
+ * Measures the showcase's target element against the hero card's padding
+ * box. The hero is the first example anchor on a component page.
+ */
+async function measureHero(page: Page, targetSelector: string) {
+ const hero = page.locator('[id^="example-"]').first();
+ await expect(hero.locator(targetSelector).first()).toBeVisible();
+ return hero.evaluate((anchor, selector) => {
+ const card = anchor.querySelector('.astryx-card');
+ const stage = card?.firstElementChild;
+ const target = anchor.querySelector(selector);
+ if (card == null || stage == null || target == null) {
+ throw new Error('The hero has no card, stage, or target.');
+ }
+ const cardBox = card.getBoundingClientRect();
+ const stageBox = stage.getBoundingClientRect();
+ const targetBox = target.getBoundingClientRect();
+ const border = parseFloat(getComputedStyle(card).borderLeftWidth);
+ return {
+ startGap: targetBox.left - (cardBox.left + border),
+ endGap: cardBox.right - border - targetBox.right,
+ targetWidth: targetBox.width,
+ innerWidth: cardBox.width - 2 * border,
+ stageWidth: stageBox.width,
+ stageHeight: stageBox.height,
+ cardWidth: cardBox.width,
+ scrolls: stage.scrollWidth > stage.clientWidth,
+ };
+ }, targetSelector);
+}
+
+test('a fluid showcase on a phone stops at the gutters', async ({page}) => {
+ await page.setViewportSize({width: 390, height: 844});
+ await page.goto('/components/Tokenizer');
+ const hero = await measureHero(page, '.astryx-tokenizer');
+
+ expect(hero.startGap).toBeCloseTo(GUTTER, 0);
+ expect(hero.endGap).toBeCloseTo(GUTTER, 0);
+ expect(hero.scrolls).toBe(false);
+});
+
+test('a fixed-width showcase that fits a phone card stays centered without scrolling', async ({
+ page,
+}) => {
+ await page.setViewportSize({width: 390, height: 844});
+ await page.goto('/components/Typeahead');
+ const hero = await measureHero(page, '.astryx-typeahead');
+ const centeredGap = (hero.innerWidth - hero.targetWidth) / 2;
+
+ expect(hero.targetWidth).toBeCloseTo(320, 0);
+ expect(Math.abs(hero.startGap - centeredGap)).toBeLessThanOrEqual(1);
+ expect(Math.abs(hero.endGap - centeredGap)).toBeLessThanOrEqual(1);
+ expect(hero.scrolls).toBe(false);
+});
+
+test('a fluid showcase on the desktop stage stops at the gutters inside the 16:9 frame', async ({
+ page,
+}) => {
+ await page.setViewportSize({width: 1280, height: 900});
+ await page.goto('/components/AppShell');
+ const hero = await measureHero(page, '.astryx-app-shell');
+
+ // A shrinking showcase takes a small share of the gutters' space too.
+ expect(hero.startGap).toBeGreaterThanOrEqual(GUTTER - 2);
+ expect(hero.startGap).toBeLessThanOrEqual(GUTTER);
+ expect(Math.abs(hero.startGap - hero.endGap)).toBeLessThanOrEqual(1);
+ expect(hero.stageWidth).toBeCloseTo(hero.cardWidth, 0);
+ expect(hero.stageHeight).toBeCloseTo((hero.stageWidth * 9) / 16, 0);
+});
+
+test('a fixed-width showcase that nearly fills the desktop card stays centered without scrolling', async ({
+ page,
+}) => {
+ // At this width the card is 12px wider than the 525px showcase, so
+ // full-width gutters would make the stage scroll.
+ await page.setViewportSize({width: 845, height: 900});
+ await page.goto('/components/AspectRatio');
+ // The showcase's root is the stage's first child.
+ const hero = await measureHero(page, '.astryx-card > div > *');
+ const centeredGap = (hero.innerWidth - hero.targetWidth) / 2;
+
+ expect(hero.targetWidth).toBeCloseTo(525, 0);
+ expect(centeredGap).toBeLessThan(GUTTER);
+ expect(Math.abs(hero.startGap - centeredGap)).toBeLessThanOrEqual(1);
+ expect(Math.abs(hero.endGap - centeredGap)).toBeLessThanOrEqual(1);
+ expect(hero.scrolls).toBe(false);
+});
+
+test('a narrow fixed-width showcase keeps its place on the desktop stage', async ({
+ page,
+}) => {
+ await page.setViewportSize({width: 1280, height: 900});
+ await page.goto('/components/TextInput');
+ const hero = await measureHero(page, '.astryx-text-input');
+ const centeredGap = (hero.innerWidth - hero.targetWidth) / 2;
+
+ expect(hero.targetWidth).toBeCloseTo(300, 0);
+ expect(Math.abs(hero.startGap - centeredGap)).toBeLessThanOrEqual(1);
+});
diff --git a/apps/docsite/src/components/component-detail/ShowcasePreview.tsx b/apps/docsite/src/components/component-detail/ShowcasePreview.tsx
index b674149b8d..aeb61f1333 100644
--- a/apps/docsite/src/components/component-detail/ShowcasePreview.tsx
+++ b/apps/docsite/src/components/component-detail/ShowcasePreview.tsx
@@ -3,13 +3,39 @@
'use client';
import {useEffect, useState, type ComponentType} from 'react';
+import * as stylex from '@stylexjs/stylex';
import {Center} from '@astryxdesign/core/Center';
import {Text} from '@astryxdesign/core/Text';
import {Spinner} from '@astryxdesign/core/Spinner';
import {useMediaQuery} from '@astryxdesign/core/hooks';
+import {spacingVars} from '@astryxdesign/core/theme/tokens.stylex';
import {showcaseRegistry} from '../../generated/showcaseRegistry';
import {preventPreviewNavigation} from './previewNavigation';
+// The hero card has no padding, so each inline edge of the stage holds a
+// gutter. A showcase that can shrink stops at the gutters. A showcase that
+// cannot shrink takes the gutters' space first. It stays centered, and the
+// stage scrolls only when the showcase is wider than the card. The gutters'
+// shrink factors add up to 1, so together they can give up all their space.
+const styles = stylex.create({
+ stage: {
+ '::before': {
+ content: '""',
+ flexGrow: 0,
+ flexShrink: 0.5,
+ flexBasis: spacingVars['--spacing-4'],
+ minWidth: 0,
+ },
+ '::after': {
+ content: '""',
+ flexGrow: 0,
+ flexShrink: 0.5,
+ flexBasis: spacingVars['--spacing-4'],
+ minWidth: 0,
+ },
+ },
+});
+
interface ShowcasePreviewProps {
name: string;
}
@@ -58,6 +84,7 @@ export function ShowcasePreview({name}: ShowcasePreviewProps) {
if (isSmall) {
return (
<div
+ {...stylex.props(styles.stage)}
style={{
width: '100%',
overflow: 'auto',
@@ -71,7 +98,14 @@ export function ShowcasePreview({name}: ShowcasePreviewProps) {
justifyContent: 'safe center',
}}
{...previewNavigationProps}>
- <div style={{minWidth: 'fit-content'}}>
+ <div
+ style={{
+ // `fit-content` would size the wrapper to the whole stage and
+ // leave the gutters no space. The high shrink factor makes the
+ // wrapper shrink before the gutters do.
+ minWidth: 'min-content',
+ flexShrink: 1000,
+ }}>
<Component />
</div>
</div>
@@ -80,6 +114,7 @@ export function ShowcasePreview({name}: ShowcasePreviewProps) {
return (
<div
+ {...stylex.props(styles.stage)}
style={{
width: '100%',
aspectRatio: '16 / 9',
diff --git a/packages/lab/blocks/CommentThreadShowcase.tsx b/packages/lab/blocks/CommentThreadShowcase.tsx
index fa0ec2268b..a5907b2c31 100644
--- a/packages/lab/blocks/CommentThreadShowcase.tsx
+++ b/packages/lab/blocks/CommentThreadShowcase.tsx
@@ -36,7 +36,7 @@ export default function CommentThreadShowcase() {
});
return (
- <Card padding={0} width={340}>
+ <Card padding={0} width="100%" maxWidth={340}>
<CommentThread
comment={thread}
currentUser={VIEWER}