Skip to content

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

TypeScript

feat: add example-nextjs app for source distribution consumers (#159)

Cindy Zhang58d25d244635
diff --git a/apps/example-nextjs/.gitignore b/apps/example-nextjs/.gitignore
new file mode 100644
index 0000000000..d56a7011bc
--- /dev/null
+++ b/apps/example-nextjs/.gitignore
@@ -0,0 +1,2 @@
+next-env.d.ts
+.next
diff --git a/apps/example-nextjs/README.md b/apps/example-nextjs/README.md
new file mode 100644
index 0000000000..25e08f9e2d
--- /dev/null
+++ b/apps/example-nextjs/README.md
@@ -0,0 +1,134 @@
+# XDS Example — Next.js
+
+Reference application for consuming **@xds/core** as a source distribution in a Next.js project.
+
+XDS ships as raw TypeScript + StyleX source. Consumers compile it at the application level — there's no pre-built CSS or JS bundle. This example shows the complete setup.
+
+## Setup Steps
+
+### 1. Install dependencies
+
+```bash
+npm install @stylexjs/stylex @xds/core next react react-dom
+npm install --save-dev @stylexjs/babel-plugin @stylexjs/postcss-plugin \
+ @babel/preset-react @babel/preset-typescript typescript @types/react @types/react-dom
+```
+
+### 2. Babel config
+
+`babel.config.js` — configure StyleX as a **plugin** (not a preset) with `next/babel`:
+
+```js
+const path = require('path');
+
+module.exports = {
+ presets: ['next/babel'],
+ plugins: [
+ [
+ '@stylexjs/babel-plugin',
+ {
+ dev: process.env.NODE_ENV === 'development',
+ runtimeInjection: false,
+ genConditionalClasses: true,
+ treeshakeCompensation: true,
+ aliases: {
+ '@xds/core/*': [path.join(__dirname, 'node_modules/@xds/core/*')],
+ '@xds/core': [path.join(__dirname, 'node_modules/@xds/core')],
+ },
+ unstable_moduleResolution: {
+ type: 'commonJS',
+ rootDir: __dirname,
+ },
+ },
+ ],
+ ],
+};
+```
+
+> **Important:** The `aliases` config is required for `stylex.createTheme` to resolve token definitions correctly. Without it, theme overrides silently fail.
+
+### 3. PostCSS config
+
+`postcss.config.js` — this is how Next.js extracts StyleX CSS:
+
+```js
+const babelConfig = require('./babel.config');
+
+module.exports = {
+ plugins: {
+ '@stylexjs/postcss-plugin': {
+ include: [
+ 'src/**/*.{js,jsx,ts,tsx}',
+ 'node_modules/@xds/core/**/*.{ts,tsx}',
+ ],
+ babelConfig: {
+ babelrc: false,
+ parserOpts: {plugins: ['typescript', 'jsx']},
+ presets: [
+ ['@babel/preset-react', {runtime: 'automatic'}],
+ '@babel/preset-typescript',
+ ],
+ plugins: babelConfig.plugins,
+ },
+ useCSSLayers: true,
+ },
+ },
+};
+```
+
+### 4. CSS entry point with `@stylex;` directive
+
+`src/app/globals.css`:
+
+```css
+@stylex;
+```
+
+Import it in your root layout:
+
+```tsx
+import './globals.css';
+```
+
+### 5. Next.js config
+
+`next.config.mjs` — add `transpilePackages` so Next.js compiles XDS source:
+
+```js
+const nextConfig = {
+ transpilePackages: ['@xds/core', '@xds/theme'],
+ typescript: {ignoreBuildErrors: true},
+};
+export default nextConfig;
+```
+
+> **Note (monorepo only):** When consuming `@xds/core` inside the XDS monorepo, you may need a webpack alias to resolve `@xds/core/theme/tokens.stylex` to the source file. See `next.config.mjs` in this example for details. External consumers using the source tarball won't need this.
+
+### 6. Theme provider (client boundary)
+
+```tsx
+// src/app/providers.tsx
+'use client';
+import {XDSTheme} from '@xds/core/theme';
+import {defaultTheme} from '@xds/theme/default';
+
+export function Providers({children}) {
+ return <XDSTheme theme={defaultTheme}>{children}</XDSTheme>;
+}
+```
+
+## Gotchas
+
+| Issue | Symptom | Fix |
+| ----------------------------------- | ------------------------------------------- | ---------------------------------------------------------------- |
+| Missing `@stylex;` directive | PostCSS produces empty CSS with no error | Add `@stylex;` to your CSS entry file |
+| PostCSS `include` doesn't cover XDS | XDS component styles are missing | Add `node_modules/@xds/core/**/*.{ts,tsx}` to `include` |
+| Missing `aliases` in babel config | `createTheme` token overrides silently fail | Add `aliases` mapping for `@xds/core` and `@xds/core/*` |
+| No `'use client'` on theme provider | Server component error from `createContext` | Mark the provider file with `'use client'` |
+| StyleX as preset instead of plugin | Build errors or missing styles | Use `plugins` array, not `presets`, for `@stylexjs/babel-plugin` |
+
+## Related
+
+- [Issue #145 — Add example-nextjs project](https://github.com/facebookexperimental/xds/issues/145)
+- [StyleX examples](https://github.com/facebook/stylex/tree/main/examples)
+- [Working prototype (cixzhang/xds_sandbox)](https://github.com/cixzhang/xds_sandbox)
diff --git a/apps/example-nextjs/babel.config.js b/apps/example-nextjs/babel.config.js
new file mode 100644
index 0000000000..9312f6b6ba
--- /dev/null
+++ b/apps/example-nextjs/babel.config.js
@@ -0,0 +1,25 @@
+/* global module, process, __dirname */
+const path = require('path');
+
+module.exports = {
+ presets: ['next/babel'],
+ plugins: [
+ [
+ '@stylexjs/babel-plugin',
+ {
+ dev: process.env.NODE_ENV === 'development',
+ runtimeInjection: false,
+ genConditionalClasses: true,
+ treeshakeCompensation: true,
+ aliases: {
+ '@xds/core/*': [path.join(__dirname, 'node_modules/@xds/core/*')],
+ '@xds/core': [path.join(__dirname, 'node_modules/@xds/core')],
+ },
+ unstable_moduleResolution: {
+ type: 'commonJS',
+ rootDir: __dirname,
+ },
+ },
+ ],
+ ],
+};
diff --git a/apps/example-nextjs/next.config.mjs b/apps/example-nextjs/next.config.mjs
new file mode 100644
index 0000000000..558382711d
--- /dev/null
+++ b/apps/example-nextjs/next.config.mjs
@@ -0,0 +1,29 @@
+import path from 'path';
+import {fileURLToPath} from 'url';
+
+const __dirname = path.dirname(fileURLToPath(import.meta.url));
+
+/** @type {import('next').NextConfig} */
+const nextConfig = {
+ transpilePackages: ['@xds/core', '@xds/theme'],
+ typescript: {
+ // XDS core has a known type issue in Popover internals;
+ // safe to ignore for this example.
+ ignoreBuildErrors: true,
+ },
+ webpack: (config) => {
+ // The @xds/core package exports ./theme/tokens.stylex but tsup
+ // bundles it into chunks rather than producing a separate file.
+ // Alias it to the source file so @xds/theme can resolve it.
+ config.resolve.alias = {
+ ...config.resolve.alias,
+ '@xds/core/theme/tokens.stylex': path.resolve(
+ __dirname,
+ '../../packages/core/src/theme/tokens.stylex.ts',
+ ),
+ };
+ return config;
+ },
+};
+
+export default nextConfig;
diff --git a/apps/example-nextjs/package.json b/apps/example-nextjs/package.json
new file mode 100644
index 0000000000..1ffceb7b18
--- /dev/null
+++ b/apps/example-nextjs/package.json
@@ -0,0 +1,27 @@
+{
+ "name": "@xds/example-nextjs",
+ "version": "0.0.1",
+ "private": true,
+ "scripts": {
+ "dev": "next dev",
+ "build": "next build",
+ "start": "next start"
+ },
+ "dependencies": {
+ "@stylexjs/stylex": "^0.17.4",
+ "@xds/core": "*",
+ "@xds/theme": "*",
+ "next": "^15.0.0",
+ "react": "^18.3.0",
+ "react-dom": "^18.3.0"
+ },
+ "devDependencies": {
+ "@babel/preset-react": "^7.25.0",
+ "@babel/preset-typescript": "^7.25.0",
+ "@stylexjs/babel-plugin": "^0.17.4",
+ "@stylexjs/postcss-plugin": "^0.17.4",
+ "@types/react": "^18.3.0",
+ "@types/react-dom": "^18.3.0",
+ "typescript": "^5.7.0"
+ }
+}
diff --git a/apps/example-nextjs/postcss.config.js b/apps/example-nextjs/postcss.config.js
new file mode 100644
index 0000000000..f3bab9eb4e
--- /dev/null
+++ b/apps/example-nextjs/postcss.config.js
@@ -0,0 +1,25 @@
+/* global module */
+const babelConfig = require('./babel.config');
+
+module.exports = {
+ plugins: {
+ '@stylexjs/postcss-plugin': {
+ include: [
+ 'src/**/*.{js,jsx,ts,tsx}',
+ 'node_modules/@xds/core/**/*.{ts,tsx}',
+ ],
+ babelConfig: {
+ babelrc: false,
+ parserOpts: {
+ plugins: ['typescript', 'jsx'],
+ },
+ presets: [
+ ['@babel/preset-react', {runtime: 'automatic'}],
+ '@babel/preset-typescript',
+ ],
+ plugins: babelConfig.plugins,
+ },
+ useCSSLayers: true,
+ },
+ },
+};
diff --git a/apps/example-nextjs/src/app/globals.css b/apps/example-nextjs/src/app/globals.css
new file mode 100644
index 0000000000..867d14b250
--- /dev/null
+++ b/apps/example-nextjs/src/app/globals.css
@@ -0,0 +1 @@
+@stylex;
diff --git a/apps/example-nextjs/src/app/layout.tsx b/apps/example-nextjs/src/app/layout.tsx
new file mode 100644
index 0000000000..6149a4007d
--- /dev/null
+++ b/apps/example-nextjs/src/app/layout.tsx
@@ -0,0 +1,21 @@
+import type {Metadata} from 'next';
+import '@xds/core/reset.css';
+import '@xds/core/typography.css';
+import './globals.css';
+import {Providers} from './providers';
+
+export const metadata: Metadata = {
+ title: 'XDS Example — Next.js',
+ description:
+ 'Reference example for consuming @xds/core as a source distribution',
+};
+
+export default function RootLayout({children}: {children: React.ReactNode}) {
+ return (
+ <html lang="en">
+ <body>
+ <Providers>{children}</Providers>
+ </body>
+ </html>
+ );
+}
diff --git a/apps/example-nextjs/src/app/page.tsx b/apps/example-nextjs/src/app/page.tsx
new file mode 100644
index 0000000000..06db9bc510
--- /dev/null
+++ b/apps/example-nextjs/src/app/page.tsx
@@ -0,0 +1,90 @@
+'use client';
+
+import * as stylex from '@stylexjs/stylex';
+import {XDSVStack, XDSHStack} from '@xds/core/Layout';
+import {XDSButton} from '@xds/core/Button';
+import {XDSText, XDSHeading} from '@xds/core/Text';
+import {XDSTextInput} from '@xds/core/TextInput';
+import {XDSBadge} from '@xds/core/Badge';
+import {XDSDivider} from '@xds/core';
+
+const styles = stylex.create({
+ main: {
+ display: 'flex',
+ alignItems: 'center',
+ justifyContent: 'center',
+ minHeight: '100vh',
+ padding: '2rem',
+ },
+ container: {
+ maxWidth: 640,
+ width: '100%',
+ },
+});
+
+export default function Home() {
+ return (
+ <main {...stylex.props(styles.main)}>
+ <div {...stylex.props(styles.container)}>
+ <XDSVStack gap="space6">
+ <XDSVStack gap="space2">
+ <XDSHeading level={1}>XDS Example — Next.js</XDSHeading>
+ <XDSText color="secondary">
+ This is a reference example for consuming{' '}
+ <XDSText weight="bold">@xds/core</XDSText> as a source
+ distribution in a Next.js application. Components are compiled
+ from raw TypeScript source using StyleX at build time.
+ </XDSText>
+ </XDSVStack>
+
+ <XDSDivider />
+
+ {/* Buttons */}
+ <XDSVStack gap="space3">
+ <XDSHeading level={2}>Buttons</XDSHeading>
+ <XDSHStack gap="space3" align="center">
+ <XDSButton variant="primary">Primary</XDSButton>
+ <XDSButton variant="secondary">Secondary</XDSButton>
+ <XDSButton variant="ghost">Ghost</XDSButton>
+ </XDSHStack>
+ </XDSVStack>
+
+ <XDSDivider />
+
+ {/* Badges */}
+ <XDSVStack gap="space3">
+ <XDSHeading level={2}>Badges</XDSHeading>
+ <XDSHStack gap="space3" align="center">
+ <XDSBadge variant="info">Info</XDSBadge>
+ <XDSBadge variant="success">Success</XDSBadge>
+ <XDSBadge variant="warning">Warning</XDSBadge>
+ <XDSBadge variant="error">Error</XDSBadge>
+ </XDSHStack>
+ </XDSVStack>
+
+ <XDSDivider />
+
+ {/* Text Input */}
+ <XDSVStack gap="space3">
+ <XDSHeading level={2}>Text Input</XDSHeading>
+ <XDSTextInput label="Email address" placeholder="[email protected]" />
+ </XDSVStack>
+
+ <XDSDivider />
+
+ {/* Typography */}
+ <XDSVStack gap="space3">
+ <XDSHeading level={2}>Typography</XDSHeading>
+ <XDSText type="large" weight="bold">
+ Large bold text
+ </XDSText>
+ <XDSText>Default body text</XDSText>
+ <XDSText type="detail" color="secondary">
+ Detail text in secondary color
+ </XDSText>
+ </XDSVStack>
+ </XDSVStack>
+ </div>
+ </main>
+ );
+}
diff --git a/apps/example-nextjs/src/app/providers.tsx b/apps/example-nextjs/src/app/providers.tsx
new file mode 100644
index 0000000000..1ab11e829a
--- /dev/null
+++ b/apps/example-nextjs/src/app/providers.tsx
@@ -0,0 +1,8 @@
+'use client';
+
+import {XDSTheme} from '@xds/core/theme';
+import {defaultTheme} from '@xds/theme/default';
+
+export function Providers({children}: {children: React.ReactNode}) {
+ return <XDSTheme theme={defaultTheme}>{children}</XDSTheme>;
+}
diff --git a/apps/example-nextjs/tsconfig.json b/apps/example-nextjs/tsconfig.json
new file mode 100644
index 0000000000..c1334095f8
--- /dev/null
+++ b/apps/example-nextjs/tsconfig.json
@@ -0,0 +1,27 @@
+{
+ "compilerOptions": {
+ "target": "ES2017",
+ "lib": ["dom", "dom.iterable", "esnext"],
+ "allowJs": true,
+ "skipLibCheck": true,
+ "strict": true,
+ "noEmit": true,
+ "esModuleInterop": true,
+ "module": "esnext",
+ "moduleResolution": "bundler",
+ "resolveJsonModule": true,
+ "isolatedModules": true,
+ "jsx": "preserve",
+ "incremental": true,
+ "plugins": [
+ {
+ "name": "next"
+ }
+ ],
+ "paths": {
+ "@/*": ["./src/*"]
+ }
+ },
+ "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
+ "exclude": ["node_modules"]
+}
diff --git a/eslint.config.js b/eslint.config.js
index fce660a233..b1bfb48f22 100644
--- a/eslint.config.js
+++ b/eslint.config.js
@@ -32,6 +32,8 @@ export default tseslint.config(
"scripts/**",
"**/*.mjs",
"**/*.test-violations.tsx",
+ "apps/example-nextjs/*.js",
+ "apps/example-nextjs/next-env.d.ts",
],
},
{