Skip to content

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

TypeScript

fix(theme): drop 'use client' from the syntax barrel so presets are data on the server (#5076)

Cindy Zhang75325ef887db
diff --git a/.changeset/syntax-theme-barrel-use-client.md b/.changeset/syntax-theme-barrel-use-client.md
new file mode 100644
index 0000000000..f1ddc2f1e0
--- /dev/null
+++ b/.changeset/syntax-theme-barrel-use-client.md
@@ -0,0 +1,11 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] `@astryxdesign/core/theme/syntax` is importable from a server component again: the presets are data, not client references.
+
+The subpath's barrel carried `'use client'`, and it is the only entry point for the syntax module. React therefore replaced *every* export with a client reference for a server importer — including `dracula`, `oneLight`, `allSyntaxPresets`, `syntaxTokenDefaults` and `defineSyntaxTheme`, none of which need a boundary. Reading a preset in a Next.js server module (deriving a code-block ground, emitting theme CSS at build time) got a proxy instead of data, so `preset.tokens` was `undefined` and the failure surfaced far from its cause — the same import under plain Node worked perfectly.
+
+The directive now sits only on `SyntaxTheme.tsx`, the provider that actually needs it, so `SyntaxTheme` and `useSyntaxTheme` keep their client boundary while the data exports resolve as data. No API change.
+
+@cixzhang
diff --git a/packages/core/src/theme/syntax/index.ts b/packages/core/src/theme/syntax/index.ts
index 9a8fbbb39d..5407065d3e 100644
--- a/packages/core/src/theme/syntax/index.ts
+++ b/packages/core/src/theme/syntax/index.ts
@@ -1,10 +1,14 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.
-'use client';
-
/**
* @file syntax/index.ts
* @position Re-exports for the syntax theme subsystem
+ *
+ * No `'use client'` here on purpose. Most of what this barrel exports is data
+ * (presets, token defaults) or pure functions, and a directive on the barrel
+ * turns every export into a client reference for a server importer — so
+ * `dracula.tokens` reads back `undefined` instead of colors. `SyntaxTheme.tsx`
+ * carries its own directive, which is what the provider actually needs.
*/
export {syntaxTokenDefaults} from './tokens';
diff --git a/packages/core/src/theme/syntax/serverSafeSyntax.test.ts b/packages/core/src/theme/syntax/serverSafeSyntax.test.ts
new file mode 100644
index 0000000000..d14139e1b1
--- /dev/null
+++ b/packages/core/src/theme/syntax/serverSafeSyntax.test.ts
@@ -0,0 +1,68 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file Guards the server-safety of the `./theme/syntax` subpath.
+ * @input Reads the source files in this directory
+ * @output One invariant per module: only the provider carries `'use client'`
+ * @position Narrow sibling of src/serverSafeComponents.test.ts, which only
+ * walks `./src/<Name>/index.ts` subpaths and so never sees this directory.
+ *
+ * `./theme/syntax` is the only export subpath for the syntax module, and it
+ * mixes a provider with presets, token defaults and pure functions. A
+ * directive on the barrel makes React hand a server importer a client
+ * reference for every one of those exports, so `dracula.tokens` reads back
+ * `undefined` — a silent failure far from its cause.
+ */
+
+import {describe, it, expect} from 'vitest';
+import {readdirSync, readFileSync} from 'node:fs';
+import {join} from 'node:path';
+
+/**
+ * The directives at the top of a module, in order. Comments and blank lines
+ * may precede them; the prologue ends at the first statement that is not a
+ * bare string literal, so a `'use strict';` cannot hide a later directive.
+ */
+function directives(file: string): string[] {
+ const source = readFileSync(join(__dirname, file), 'utf-8');
+ const found: string[] = [];
+ let rest = source;
+ for (;;) {
+ const trimmed = rest.replace(/^(?:\s+|\/\/[^\n]*|\/\*[\s\S]*?\*\/)+/, '');
+ const directive = /^(['"])([^'"]*)\1\s*;?/.exec(trimmed);
+ if (!directive) {
+ return found;
+ }
+ found.push(directive[2]);
+ rest = trimmed.slice(directive[0].length);
+ }
+}
+
+/**
+ * The modules in this directory that legitimately need a client boundary.
+ * Everything else is data or pure functions and must stay directive-free —
+ * derived from the directory, so a module added here is covered by default.
+ */
+const CLIENT_MODULES = ['SyntaxTheme.tsx'];
+
+const serverSafeModules = readdirSync(__dirname)
+ .filter(
+ name =>
+ /\.[jt]sx?$/.test(name) &&
+ !/\.(test|stories|doc|perf)\./.test(name) &&
+ !name.endsWith('.d.ts') &&
+ !CLIENT_MODULES.includes(name),
+ )
+ .sort();
+
+describe('./theme/syntax is importable from a server component', () => {
+ it.each(serverSafeModules)('%s carries no "use client"', file => {
+ expect(directives(file)).not.toContain('use client');
+ });
+
+ // Also the proof that the reader above is not blind, which would make every
+ // assertion in this file pass vacuously.
+ it.each(CLIENT_MODULES)('%s keeps the directive it needs', file => {
+ expect(directives(file)).toContain('use client');
+ });
+});