Skip to content

refactor(mcp-ui): co-locate component CSS so views ship only what they import - #576

Merged
charislam merged 2 commits into
mainfrom
refactor/mcp-ui-colocated-css
Nov 19, 2026
Merged

charislam merged 2 commits into
mainfrom
refactor/mcp-ui-colocated-css

Conversation

@charislam

@charislam charislam commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #573

What kind of change does this PR introduce?

Refactor (no intended change to rendered output; see Verification).

What is the current behavior?

Every view imports one shared stylesheet (src/shared/styles.css), so each view's single-file HTML ships CSS for every component whether or not it renders that component. That does not scale as views are added.

Related issues: none linked.

What is the new behavior?

  • src/shared/styles.css is split into src/shared/base.css (the global core: layer order statement, tokens, native element styles, .sr-only) plus one CSS file per component. Each component file owns one class in the components layer and is imported by the TSX component that renders it. A view now ships base.css plus the CSS of the components it imports. Rules were moved verbatim; the authoring model (plain CSS, cascade layers, specificity caps, :where(), data-slot/data-state/data-tone) is unchanged.
  • The layer order statement lives only in base.css, which must be the first import in every view's main.tsx.
  • Component custom properties are renamed from --sb-local-* to --sb-component-<component>-*, namespaced by the component's file (for example --sb-component-progress-card-radius). A component reads its own properties without a fallback. A component reading another component's properties must provide a fallback, because its file can ship without the other's. ready-actions.css does this for the progress-card properties it reads. --sb-view-<view>-* is reserved for a view's view.css.
  • scripts/check-css.mjs (pnpm lint:css) now checks every CSS file under src/ and enforces: one block per layer per file; which layers each kind of file may contain; a component class styled in only one file; the layer statement only in base.css, and base.css first in each main.tsx; and the custom property namespace and fallback rules.
  • README.md and .agents/skills/mcp-ui-css/SKILL.md describe the new layout and rules.

How to Review

  1. Shared core and tokens

    • packages/mcp-ui/src/shared/base.css
    • Layer order statement, tokens, and native element styles that every view now imports.
  2. Component ownership and imports

    • packages/mcp-ui/src/shared/badge.css, button.css, copy-field.css, icon.css, message.css, progress-card.css, ready-actions.css, summary.css
    • Each file owns one component class in the components layer; confirm the matching TSX file in packages/mcp-ui/src/shared/ imports it.
  3. Cross-component property fallbacks

    • packages/mcp-ui/src/shared/ready-actions.css
    • Reads of progress-card custom properties, which carry fallbacks.
  4. View entrypoints

    • packages/mcp-ui/src/views/create-project/main.tsx
    • packages/mcp-ui/src/views/create-branch/main.tsx
    • base.css is the first import in each.
  5. Rules enforced by the lint

    • packages/mcp-ui/scripts/check-css.mjs
    • The checks that pnpm lint:css now runs over every CSS file under src/.

Verification

  • pnpm build in packages/mcp-ui passes (typecheck, lint:css on 9 files, both views).
  • pnpm exec biome check packages/mcp-ui from the repository root passes.
  • The built CSS of both views has the same rules per layer with identical declarations as before the change, apart from the renamed properties and the added fallbacks. Rule order differs only inside the components layer.
  • A local side-by-side regression page rendered 55 states of the two views (server-rendered from the real components) with the old and new CSS, and compared computed styles and element geometry for every element: 0 differences. It was checked in Chrome in light/narrow and dark/wide against the final CSS.
  • A throwaway view importing only Badge shipped about 4.9 KB of CSS and none of the other components' rules.
  • Size today: the two existing views use the same components, so nothing is pruned yet. Their CSS grows from 12,029 to about 12,416 bytes each, from the fallbacks and longer property names.

Additional context

The two existing views use the same components, so this change does not reduce their output yet; the benefit applies to views that import a subset.

@charislam
charislam requested a review from a team as a code owner November 18, 2026 16:51
Comment thread packages/mcp-ui/scripts/check-css.mjs Resolved
Comment thread packages/mcp-ui/README.md Resolved

@Rodriguespn Rodriguespn left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks! Left a small suggestion on the lint and a README nit, neither blocking. The failing Management API types check isn't from this PR: the regenerated types are already in main (chore: regenerate management API types), so rebasing the stack on main should turn it green.

@charislam
charislam force-pushed the feat/create-project-tools branch from 93964b5 to eabaf9f Compare November 18, 2026 18:28
@charislam
charislam force-pushed the refactor/mcp-ui-colocated-css branch from 4a5ae6d to e9bb151 Compare November 18, 2026 18:28
@charislam charislam changed the title refactor(mcp-ui): co-locate component CSS so views ship only what they render refactor(mcp-ui): co-locate component CSS so views ship only what they import Nov 18, 2026
@charislam
charislam force-pushed the refactor/mcp-ui-colocated-css branch 2 times, most recently from 5988843 to 9028401 Compare November 18, 2026 19:21
…y import

Every view imported one shared stylesheet, so each view's single-file HTML
shipped CSS for every component whether it used it or not. That does not
scale as views are added.

- Split src/shared/styles.css into base.css (layer order statement, tokens,
  native element styles, .sr-only) and one CSS file per component, each owning
  its class in the components layer and imported by the TSX component that
  renders it. Rules moved verbatim; authoring model unchanged.
- The layer order statement lives only in base.css, which must be the first
  import in every view's main.tsx.
- Rename component custom properties from --sb-local-* to
  --sb-component-<component>-*. A component reading another component's
  properties must provide a fallback. --sb-view-<view>-* is reserved for a
  view's view.css.
- scripts/check-css.mjs (pnpm lint:css) checks every CSS file under src/ for
  layer rules, layer placement per file kind, single-file ownership of
  component classes, the base.css import rule, and the property namespace and
  fallback rules.
- Update README.md and the mcp-ui-css skill to describe the new layout.
- check-css.mjs now checks classes in nested components rules too: a nested
  :where(.icon) inside .ready-actions styled .icon from a second file, and
  with every components rule at zero specificity the winner followed import
  order.
- Move the icon rule to the native layer of base.css (svg:where(.icon)) so a
  component that styles an icon inside it always wins, and drop icon.css.
- Icons take a slot prop (data-slot); the connection details disclosure
  styles its indicator as [data-slot="disclosure-indicator"].
- README and skill: a view ships the CSS of the components it imports, not
  only those it renders.
@charislam
charislam force-pushed the refactor/mcp-ui-colocated-css branch from 9028401 to 0f63cdd Compare November 19, 2026 21:10
@charislam
charislam changed the base branch from feat/create-project-tools to main November 19, 2026 21:10
@charislam
charislam merged commit 43fc508 into main Nov 19, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants