Skip to content

Commit 542613b

Browse files
committed
improvement(platform): confine the embedded design system's CSS to its surface
Next keeps route CSS loaded after client navigation, so a single global rule would reach legacy pages. A PostCSS step scopes the embedded entry under [data-platform-ui], and check:platform-css compiles it through the app's real config and fails on any escape, or on a route outside /o/[organizationId] reaching platform/.
1 parent ab42827 commit 542613b

7 files changed

Lines changed: 410 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Confines the design system's embedded stylesheet to `[data-platform-ui]`.
3+
*
4+
* Next keeps route CSS loaded after client navigation, so one unscoped rule in a flagged page would
5+
* reach every legacy page afterwards. Each selector gains the scope through `:where()`, which adds
6+
* no specificity, so the cascade inside the surface stays the one the design system was written for.
7+
*
8+
* `data-theme="system"` follows the host app, not the OS: next-themes puts `.dark` on `<html>` before
9+
* first paint, so the surface needs no JavaScript to match it and cannot flash the wrong theme before
10+
* hydration. `scripts/check-platform-css.ts` compiles the entry through this plugin and verifies it.
11+
*/
12+
const EMBEDDED_ENTRY = 'platform/design-system/styles/embedded.css'
13+
const SCOPE = '[data-platform-ui]'
14+
const DARK_THEME = /^\[data-platform-ui\]\[data-theme=["']?dark["']?\]/
15+
16+
function scopeSelector(selector) {
17+
if (DARK_THEME.test(selector)) {
18+
const followsHost = selector.replace(
19+
DARK_THEME,
20+
':where(html.dark) [data-platform-ui][data-theme="system"]'
21+
)
22+
return [selector, followsHost]
23+
}
24+
if (selector.startsWith(SCOPE)) return [selector]
25+
return [`:where(${SCOPE}) ${selector}`]
26+
}
27+
28+
const plugin = () => ({
29+
postcssPlugin: 'sim-platform-embed',
30+
OnceExit(root, { result }) {
31+
if (!result.opts.from?.replaceAll('\\', '/').endsWith(EMBEDDED_ENTRY)) return
32+
root.walkRules((rule) => {
33+
rule.selectors = rule.selectors.flatMap(scopeSelector)
34+
})
35+
},
36+
})
37+
plugin.postcss = true
38+
39+
module.exports = plugin
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* The design system's stylesheet for a surface embedded in the existing app.
3+
*
4+
* Next keeps route CSS loaded after client navigation, so nothing here may reach an element outside
5+
* `[data-platform-ui]`. `lib/postcss/platform-embed.cjs` scopes every rule, and
6+
* `scripts/check-platform-css.ts` compiles this file and fails on anything that escapes. No preflight
7+
* (the host's is the same Tailwind reset) and no Tailwind utilities: components style through hashed
8+
* CSS Modules.
9+
*/
10+
@import "./tokens.css";
11+
@import "./collection-roles.css";
12+
@import "./typography.css";
13+
@import "./materials.css";
14+
@import "./chart-roles.css";
15+
@import "./control-roles.css";
16+
@import "./choice-roles.css";
17+
@import "./rhythm.css";
18+
@import "./panel-roles.css";
19+
@import "./shell-roles.css";
20+
@import "./recipe-values.css";
21+
22+
@font-face {
23+
font-family: "Geist";
24+
font-style: normal;
25+
font-weight: 100 900;
26+
font-display: swap;
27+
src: url("../fonts/geist.woff2") format("woff2");
28+
}
29+
@font-face {
30+
font-family: "Geist Mono";
31+
font-style: normal;
32+
font-weight: 100 900;
33+
font-display: swap;
34+
src: url("../fonts/geist-mono.woff2") format("woff2");
35+
}
36+
37+
[data-platform-ui] {
38+
color-scheme: light;
39+
color: var(--ui-ink);
40+
background: var(--ui-canvas);
41+
font-family: var(--font-sans);
42+
font-synthesis: none;
43+
-webkit-font-smoothing: antialiased;
44+
/* The host's body sets these for its own type; they inherit into the surface otherwise. */
45+
letter-spacing: normal;
46+
text-rendering: auto;
47+
font-feature-settings: normal;
48+
font-variation-settings: normal;
49+
}
50+
51+
[data-platform-ui][data-theme="dark"] {
52+
color-scheme: dark;
53+
}
54+
55+
[data-platform-ui] textarea {
56+
resize: none;
57+
}
58+
59+
[data-platform-ui] :focus {
60+
outline: none;
61+
}
62+
63+
[data-platform-ui][data-system-font] {
64+
--font-sans: var(--font-native);
65+
}
66+
67+
[data-platform-ui][data-system-mono] {
68+
--font-mono: ui-monospace, monospace;
69+
}

‎apps/sim/postcss.config.mjs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ import { fileURLToPath } from 'node:url'
88
const hairlineBorderWidth = fileURLToPath(
99
new URL('./lib/postcss/hairline-border-width.cjs', import.meta.url)
1010
)
11+
const platformEmbed = fileURLToPath(new URL('./lib/postcss/platform-embed.cjs', import.meta.url))
1112

1213
/** @type {import('postcss-load-config').Config} */
1314
const config = {
1415
plugins: {
1516
'@tailwindcss/postcss': {},
1617
// Must run after Tailwind: it rewrites Tailwind's own border-width output.
1718
[hairlineBorderWidth]: {},
19+
// Must run after Tailwind, which inlines the entry's imports.
20+
[platformEmbed]: {},
1821
},
1922
}
2023

‎knip.jsonc‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@
3737
"lib/execution/sandbox/bundles/*.{ts,cjs}",
3838
// Bundled by build:xlsx-worker and spawned as a worker thread by filename.
3939
"lib/file-parsers/xlsx-worker-entry.ts",
40+
// The design system's public surface: a snapshot consumers pick from, kept whole so it stays
41+
// identical to its source and rebases cleanly.
42+
"platform/design-system/**/*.{ts,tsx}",
43+
// Foundation for the organization redesign's lanes. Remove once their pages import these.
44+
"app/o/[[]organizationId[]]/_redesign/redesign-host.tsx",
45+
"platform/app/(shell)/_components/notification-tray/notification-tray.tsx",
4046
// Compile-time fixture compatibility assertion, included by tsconfig.
4147
"lib/table/fixture-contract.ts",
4248
// scripts/generate-docs.ts imports this through pathToFileURL.
@@ -78,6 +84,9 @@
7884
"app/(landing)/customers/components/customer-story-page/index.ts": ["files"]
7985
},
8086
"ignoreDependencies": [
87+
// Holds the hoisted copy at the version zustand/traditional resolved to before @base-ui/react
88+
// required ^1.7.0, which it now takes as a nested copy.
89+
"use-sync-external-store",
8190
// docker/app.Dockerfile invokes this binary to rebuild isolated-vm.
8291
"node-gyp",
8392
// Imported inside the generated source string in sandbox/bundles/build.ts.

‎package.json‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
"check:explicit-any": "bun run scripts/check-explicit-any.ts",
9090
"check:file-names": "bun run scripts/check-file-names.ts",
9191
"check:source-conventions": "bun run scripts/check-source-conventions.ts",
92+
"check:platform-css": "bun run scripts/check-platform-css.ts",
9293
"check:test-types": "bun run scripts/check-test-types.ts",
9394
"desktop-bridge-contract:update": "bun run scripts/check-desktop-bridge-contract.ts --update",
9495
"mship-contracts:generate": "bun run scripts/sync-mothership-stream-contract.ts",
@@ -203,6 +204,7 @@
203204
"lint-staged": "16.4.0",
204205
"opentype.js": "1.3.4",
205206
"pdf-lib": "1.17.1",
207+
"postcss": "8.5.30",
206208
"react": "19.2.4",
207209
"remark-gfm": "4.0.1",
208210
"remark-parse": "11.0.0",

‎scripts/check-platform-css.test.ts‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* @vitest-environment node
3+
*/
4+
import postcss from 'postcss'
5+
import { describe, expect, it } from 'vitest'
6+
import embed from '../apps/sim/lib/postcss/platform-embed.cjs'
7+
import { findEntryEscapes, findModuleEscapes } from './check-platform-css'
8+
9+
const ENTRY = '/repo/apps/sim/platform/design-system/styles/embedded.css'
10+
11+
const scoped = async (css: string, from = ENTRY) =>
12+
(await postcss([embed()]).process(css, { from })).css
13+
14+
describe('findEntryEscapes', () => {
15+
it.each([
16+
[':root { --spacing: 1px; }'],
17+
['html { color: red; }'],
18+
['[data-platform-ui] html { color: red; }'],
19+
['.flex { display: flex; }'],
20+
['[data-material="shell"] { color: red; }'],
21+
['@layer utilities { [data-platform-ui] .flex { display: flex; } }'],
22+
['@property --tw-shadow { syntax: "*"; inherits: false; }'],
23+
['@keyframes spin { to { rotate: 1turn; } }'],
24+
['@media (width > 1px) { .flex { display: flex; } }'],
25+
])('flags %s', (css) => {
26+
expect(findEntryEscapes(css, '')).not.toEqual([])
27+
})
28+
29+
it('flags a font family the host stylesheet names', () => {
30+
expect(findEntryEscapes('@font-face { font-family: "Geist"; }', 'font: "Geist"')).not.toEqual(
31+
[]
32+
)
33+
})
34+
35+
it('passes what the embed plugin emits', async () => {
36+
const css = await scoped(
37+
'[data-platform-ui] { color: red; } [data-material="shell"] { color: red; } ' +
38+
'[data-platform-ui][data-theme="dark"] svg { color: red; } :focus, a { color: red; }'
39+
)
40+
expect(findEntryEscapes(css, '')).toEqual([])
41+
expect(css).toContain(':where([data-platform-ui]) [data-material="shell"]')
42+
expect(css).toContain(':where(html.dark) [data-platform-ui][data-theme="system"] svg')
43+
})
44+
45+
it('leaves any other stylesheet to the host', async () => {
46+
const css = '.flex { display: flex; }'
47+
expect(await scoped(css, '/repo/apps/sim/app/_styles/globals.css')).toBe(css)
48+
})
49+
})
50+
51+
describe('findModuleEscapes', () => {
52+
it.each([
53+
['button { color: red; }'],
54+
['[data-platform-ui] { color: red; }'],
55+
[':global(.flex) { display: flex; }'],
56+
['.local, body { color: red; }'],
57+
['@layer base { .local { color: red; } }'],
58+
['@font-face { font-family: "X"; }'],
59+
['@property --x { syntax: "*"; inherits: false; }'],
60+
])('flags %s', (css) => {
61+
expect(findModuleEscapes(css, 'x.module.css')).not.toEqual([])
62+
})
63+
64+
it('passes local classes and their keyframes', () => {
65+
const css =
66+
'.local:hover, textarea.input { color: red; } @media (width > 1px) { .local { color: red; } } ' +
67+
'@keyframes spin { from { rotate: 0; } to { rotate: 1turn; } }'
68+
expect(findModuleEscapes(css, 'x.module.css')).toEqual([])
69+
})
70+
})

0 commit comments

Comments
 (0)