Skip to content

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

TypeScript

fix(docsite): render template StyleX in playground + gallery preview (#2653)

rubyycheung02a87087aa89
diff --git a/apps/docsite/postcss.config.js b/apps/docsite/postcss.config.js
index cbd703cc0f..c34836f57a 100644
--- a/apps/docsite/postcss.config.js
+++ b/apps/docsite/postcss.config.js
@@ -7,7 +7,12 @@ const babelConfig = require('./babel.config');
module.exports = {
plugins: {
'@stylexjs/postcss-plugin': {
- include: ['src/**/*.{js,jsx,ts,tsx}'],
+ // The docsite renders CLI page templates directly, so extract their
+ // StyleX too — otherwise template-only classes ship without CSS.
+ include: [
+ 'src/**/*.{js,jsx,ts,tsx}',
+ '../../packages/cli/templates/**/*.{js,jsx,ts,tsx}',
+ ],
babelConfig: {
babelrc: false,
parserOpts: {
diff --git a/apps/docsite/scripts/generate-scope.mjs b/apps/docsite/scripts/generate-scope.mjs
index bb67b3e3c9..50dd3c5c9f 100644
--- a/apps/docsite/scripts/generate-scope.mjs
+++ b/apps/docsite/scripts/generate-scope.mjs
@@ -65,11 +65,29 @@ lines.push("import React, {useState, useCallback, useMemo, useEffect, useRef} fr
lines.push('');
// ── StyleX mock ────────────────────────────────────────────────────────
-lines.push(`const stylexMock = {
+// The playground transpiles code in-browser without the StyleX compiler, so
+// props() flattens plain CSS-property objects into an inline `style`. Pseudo/
+// media entries can't be inlined: responsive objects collapse to `default`.
+lines.push(`const STYLEX_SKIP_KEY = /^(:|::|@|--)/;
+const stylexFlatten = (style: Record<string, unknown>, obj: unknown): void => {
+ if (obj == null || typeof obj !== 'object') return;
+ for (const [k, v] of Object.entries(obj as Record<string, unknown>)) {
+ if (v == null || v === false) continue;
+ if (STYLEX_SKIP_KEY.test(k)) continue;
+ if (typeof v === 'object') {
+ const d = (v as Record<string, unknown>).default;
+ if (d != null && typeof d !== 'object') style[k] = d as string;
+ continue;
+ }
+ style[k] = v as string;
+ }
+};
+const stylexMock = {
create: <T extends Record<string, unknown>>(styles: T): T => styles,
- props: (..._styles: unknown[]) => {
- const style: Record<string, string> = {};
- return {className: '', style};
+ props: (...styles: unknown[]) => {
+ const style: Record<string, unknown> = {};
+ for (const s of styles.flat(Infinity)) stylexFlatten(style, s);
+ return {className: '', style: style as Record<string, string>};
},
defineVars: <T extends Record<string, unknown>>(tokens: T): T => tokens,
keyframes: (kf: Record<string, Record<string, string>>) => kf,
diff --git a/apps/docsite/src/app/(preview)/playground-preview/page.tsx b/apps/docsite/src/app/(preview)/playground-preview/page.tsx
index 0ed02f2c2f..07b5650027 100644
--- a/apps/docsite/src/app/(preview)/playground-preview/page.tsx
+++ b/apps/docsite/src/app/(preview)/playground-preview/page.tsx
@@ -587,6 +587,8 @@ export default function PreviewPage() {
const stageStyle: CSSProperties = fill
? {
+ // Definite height so templates sized with `height: 100%` resolve.
+ height: '100%',
minHeight: '100%',
display: 'block',
backgroundColor: 'var(--color-background-surface)',
@@ -601,9 +603,9 @@ export default function PreviewPage() {
backgroundColor: 'var(--color-background-surface)',
};
- // In fill mode the wrapper has no box (display: contents) so the rendered
- // root participates directly in block flow and fills width/height naturally.
- const contentStyle: CSSProperties = fill ? {display: 'contents'} : {};
+ const contentStyle: CSSProperties = fill
+ ? {height: '100%', width: '100%'}
+ : {};
return (
<XDSTheme theme={theme} mode={themeMode}>