Skip to content

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

TypeScript

fix(Layer): ship the dynamic-viewport cap and safe-area gutter, not only their fallbacks (#7622)

Cindy Zhang81b8df40f44d
diff --git a/.changeset/layer-cap-dynamic-viewport.md b/.changeset/layer-cap-dynamic-viewport.md
new file mode 100644
index 0000000000..b292912550
--- /dev/null
+++ b/.changeset/layer-cap-dynamic-viewport.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Anchored layers (`Popover`, `DropdownMenu`, and every component built on them) now keep their safe-area gutter and dynamic-viewport size cap in the shipped stylesheet. The compiled CSS referenced an undefined variable for the preferred value, so every browser used the fallback: on a phone with a dynamic toolbar or a notch, a tall layer could run past the visible screen or under a declared bottom bar. Desktop sizes are unchanged.
+
+@cixzhang
diff --git a/packages/core/src/DropdownMenu/DropdownMenu.tsx b/packages/core/src/DropdownMenu/DropdownMenu.tsx
index c4b2887775..8e4f4aaad1 100644
--- a/packages/core/src/DropdownMenu/DropdownMenu.tsx
+++ b/packages/core/src/DropdownMenu/DropdownMenu.tsx
@@ -229,8 +229,8 @@ const styles = stylex.create({
WebkitTouchCallout: 'none',
userSelect: 'none',
maxInlineSize: stylex.firstThatWorks(
- layerViewportInset.maxInlineSize,
- layerViewportInset.maxInlineSizeFallback,
+ `calc(${layerViewportInset.maxInlineSize})`,
+ `calc(${layerViewportInset.maxInlineSizeFallback})`,
),
maxHeight: stylex.firstThatWorks(
MENU_MAX_BLOCK_SIZE,
diff --git a/packages/core/src/DropdownMenu/DropdownMenuSubMenu.tsx b/packages/core/src/DropdownMenu/DropdownMenuSubMenu.tsx
index ba8a9c868d..f36b9fff66 100644
--- a/packages/core/src/DropdownMenu/DropdownMenuSubMenu.tsx
+++ b/packages/core/src/DropdownMenu/DropdownMenuSubMenu.tsx
@@ -166,8 +166,8 @@ const flyoutStyles = stylex.create({
flexDirection: 'column',
gap: spacingVars['--spacing-0-5'],
maxInlineSize: stylex.firstThatWorks(
- layerViewportInset.maxInlineSize,
- layerViewportInset.maxInlineSizeFallback,
+ `calc(${layerViewportInset.maxInlineSize})`,
+ `calc(${layerViewportInset.maxInlineSizeFallback})`,
),
maxHeight: stylex.firstThatWorks(
MENU_MAX_BLOCK_SIZE,
diff --git a/packages/core/src/Layer/layerViewportInset.source-build.test.mjs b/packages/core/src/Layer/layerViewportInset.source-build.test.mjs
new file mode 100644
index 0000000000..53048c89aa
--- /dev/null
+++ b/packages/core/src/Layer/layerViewportInset.source-build.test.mjs
@@ -0,0 +1,90 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file layerViewportInset.source-build.test.mjs
+ * @input Uses Babel, the core StyleX build config, the StyleX rule processor,
+ * and the sources that cap or inset anchored layers
+ * @output Verifies the shipped stylesheet carries the dynamic-viewport,
+ * safe-area gutter and caps (spec:AST-059 FR1–FR3), not only the fallbacks
+ * @position Regression test for the compiled layer viewport inset
+ *
+ * `stylex.firstThatWorks(a, b)` turns two `var()` arguments into one nested
+ * `var(a, b)`. A `defineConsts` reference compiles to such a `var()`, and the
+ * rule processor inlines a constant only where its `var()` stands alone, so a
+ * pair of bare constant references shipped as `var(--<hash>, <fallback>)`
+ * with `--<hash>` defined nowhere: every engine took the fallback.
+ */
+
+import fs from 'node:fs/promises';
+import path from 'node:path';
+import {fileURLToPath} from 'node:url';
+import {transformAsync} from '@babel/core';
+import stylexBabelPlugin from '@stylexjs/babel-plugin';
+import {describe, expect, it} from 'vitest';
+
+const __dirname = path.dirname(fileURLToPath(import.meta.url));
+const SRC = path.resolve(__dirname, '..');
+const CORE_ROOT = path.resolve(SRC, '..');
+
+const SOURCES = [
+ 'Layer/layerViewportInset.stylex.ts',
+ 'Layer/useLayer.tsx',
+ 'Popover/Popover.tsx',
+ 'DropdownMenu/DropdownMenu.tsx',
+ 'DropdownMenu/DropdownMenuSubMenu.tsx',
+];
+
+async function compileStylesheet() {
+ const rules = [];
+ for (const relative of SOURCES) {
+ const filename = path.join(SRC, relative);
+ const result = await transformAsync(await fs.readFile(filename, 'utf8'), {
+ babelrc: false,
+ configFile: path.join(CORE_ROOT, 'babel.config.json'),
+ filename,
+ });
+ rules.push(...(result?.metadata?.stylex ?? []));
+ }
+ return stylexBabelPlugin.processStylexRules(rules, false);
+}
+
+/** Every declaration of `property` in the compiled sheet. */
+function declarations(css, property) {
+ return [...css.matchAll(new RegExp(`[{;]${property}:([^;}]+)`, 'g'))].map(
+ match => match[1],
+ );
+}
+
+describe('layer viewport inset stylesheet (spec:AST-059)', () => {
+ it('leaves no constant reference unresolved behind a fallback', async () => {
+ const css = await compileStylesheet();
+ const defined = new Set(
+ [...css.matchAll(/(--[\w-]+)\s*:/g)].map(match => match[1]),
+ );
+ const undefinedReferences = [
+ ...css.matchAll(/var\((--x[a-z0-9]+),/g),
+ ].flatMap(match => (defined.has(match[1]) ? [] : [match[0]]));
+ // Covers the caps and the four alignment-axis gutter margins.
+ expect(undefinedReferences).toEqual([]);
+ });
+
+ it('caps both axes against the dynamic viewport and the safe-area gutters', async () => {
+ const css = await compileStylesheet();
+ const blockCaps = declarations(css, 'max-height');
+ const inlineCaps = declarations(css, 'max-width');
+
+ expect(blockCaps).toContainEqual(
+ expect.stringMatching(/^calc\(calc\(100dvb - .*safe-area-inset-top/),
+ );
+ expect(inlineCaps).toContainEqual(
+ expect.stringMatching(/^calc\(calc\(100vi - .*safe-area-inset-left/),
+ );
+ // The older-engine fallbacks still ship, ahead of the preferred value.
+ expect(blockCaps).toContainEqual(
+ expect.stringMatching(/^calc\(calc\(100vh - /),
+ );
+ expect(inlineCaps).toContainEqual(
+ expect.stringMatching(/^calc\(calc\(100vw - /),
+ );
+ });
+});
diff --git a/packages/core/src/Layer/layerViewportInset.stylex.ts b/packages/core/src/Layer/layerViewportInset.stylex.ts
index cc66b6ac8f..defcbdc121 100644
--- a/packages/core/src/Layer/layerViewportInset.stylex.ts
+++ b/packages/core/src/Layer/layerViewportInset.stylex.ts
@@ -56,8 +56,12 @@ const GUTTER_BLOCK = `calc(max(${SPACING}, env(safe-area-inset-top, 0px), env(sa
/**
* The viewport gutter and the caps derived from it. Every value has an
- * `env()`-less fallback for engines without safe-area support; pair them with
- * `stylex.firstThatWorks(cap, capFallback)`.
+ * `env()`-less fallback for engines without safe-area support; pair them as
+ * `stylex.firstThatWorks(`calc(${cap})`, `calc(${capFallback})`)`. Pass
+ * each inside a `calc()` (or another function), never as a bare reference: a
+ * constant reference compiles to a `var()`, and `firstThatWorks` folds two
+ * `var()` arguments into one `var(cap, capFallback)` whose constant is never
+ * inlined, so every engine would take the fallback.
*/
export const layerViewportInset = stylex.defineConsts({
/** Gutter from the block-start viewport edge. */
diff --git a/packages/core/src/Layer/useLayer.tsx b/packages/core/src/Layer/useLayer.tsx
index 694e422902..243877d66f 100644
--- a/packages/core/src/Layer/useLayer.tsx
+++ b/packages/core/src/Layer/useLayer.tsx
@@ -102,15 +102,17 @@ const styles = stylex.create({
// The cap is on the layer box itself: content wider than the viewport
// overflows inside the layer, where the composing component decides
// whether it scrolls or clips; the box never leaves the viewport.
+ // Each constant is wrapped in `calc()` so both declarations ship (see
+ // layerViewportInset).
viewportFit: {
boxSizing: 'border-box',
maxInlineSize: stylex.firstThatWorks(
- layerViewportInset.maxInlineSize,
- layerViewportInset.maxInlineSizeFallback,
+ `calc(${layerViewportInset.maxInlineSize})`,
+ `calc(${layerViewportInset.maxInlineSizeFallback})`,
),
maxBlockSize: stylex.firstThatWorks(
- layerViewportInset.maxBlockSize,
- layerViewportInset.maxBlockSizeFallback,
+ `calc(${layerViewportInset.maxBlockSize})`,
+ `calc(${layerViewportInset.maxBlockSizeFallback})`,
),
},
// Alignment-axis gutter (spec:AST-059 FR1): a margin on the far viewport
@@ -120,26 +122,26 @@ const styles = stylex.create({
// (see layerSlideRules.ts).
gutterInlineEnd: {
marginInlineEnd: stylex.firstThatWorks(
- layerViewportInset.gutterInline,
- layerViewportInset.gutterInlineFallback,
+ `calc(${layerViewportInset.gutterInline})`,
+ `calc(${layerViewportInset.gutterInlineFallback})`,
),
},
gutterInlineStart: {
marginInlineStart: stylex.firstThatWorks(
- layerViewportInset.gutterInline,
- layerViewportInset.gutterInlineFallback,
+ `calc(${layerViewportInset.gutterInline})`,
+ `calc(${layerViewportInset.gutterInlineFallback})`,
),
},
gutterBlockEnd: {
marginBlockEnd: stylex.firstThatWorks(
- layerViewportInset.gutterBlock,
- layerViewportInset.gutterBlockFallback,
+ `calc(${layerViewportInset.gutterBlock})`,
+ `calc(${layerViewportInset.gutterBlockFallback})`,
),
},
gutterBlockStart: {
marginBlockStart: stylex.firstThatWorks(
- layerViewportInset.gutterBlock,
- layerViewportInset.gutterBlockFallback,
+ `calc(${layerViewportInset.gutterBlock})`,
+ `calc(${layerViewportInset.gutterBlockFallback})`,
),
},
});
diff --git a/packages/core/src/Popover/Popover.test.tsx b/packages/core/src/Popover/Popover.test.tsx
index a0c4cdbe97..c4a16cd555 100644
--- a/packages/core/src/Popover/Popover.test.tsx
+++ b/packages/core/src/Popover/Popover.test.tsx
@@ -280,10 +280,10 @@ describe('Popover', () => {
'utf8',
);
expect(popoverSource).toMatch(
- /surfaceViewportFit:[\s\S]*?maxInlineSize: stylex\.firstThatWorks\(\s*layerViewportInset\.maxInlineSize/,
+ /surfaceViewportFit:[\s\S]*?maxInlineSize: stylex\.firstThatWorks\(\s*`calc\(\$\{layerViewportInset\.maxInlineSize\}\)`/,
);
expect(popoverSource).toMatch(
- /surfaceViewportFit:[\s\S]*?maxBlockSize: stylex\.firstThatWorks\(\s*layerViewportInset\.maxBlockSize/,
+ /surfaceViewportFit:[\s\S]*?maxBlockSize: stylex\.firstThatWorks\(\s*`calc\(\$\{layerViewportInset\.maxBlockSize\}\)`/,
);
expect(popoverSource).not.toMatch(/safe-area-inset|calc\(100%/);
expect(surface?.className).not.toContain(
diff --git a/packages/core/src/Popover/Popover.tsx b/packages/core/src/Popover/Popover.tsx
index e17ad6ff27..9a21038de9 100644
--- a/packages/core/src/Popover/Popover.tsx
+++ b/packages/core/src/Popover/Popover.tsx
@@ -272,12 +272,12 @@ const styles = stylex.create({
surfaceViewportFit: {
boxSizing: 'border-box',
maxInlineSize: stylex.firstThatWorks(
- layerViewportInset.maxInlineSize,
- layerViewportInset.maxInlineSizeFallback,
+ `calc(${layerViewportInset.maxInlineSize})`,
+ `calc(${layerViewportInset.maxInlineSizeFallback})`,
),
maxBlockSize: stylex.firstThatWorks(
- layerViewportInset.maxBlockSize,
- layerViewportInset.maxBlockSizeFallback,
+ `calc(${layerViewportInset.maxBlockSize})`,
+ `calc(${layerViewportInset.maxBlockSizeFallback})`,
),
},
surfaceScrollable: {