Skip to content

Want to skip the docs? Check out pandamastery.com - the best way to learn Panda CSS

Build & output

Style isolation

Load multiple Panda-built bundles on one page without class or token collisions.

Two Panda stylesheets built separately can break each other on one page, even when their rules are identical. prefix and cssVarRoot stop that.

When you need this

When two or more separately built Panda stylesheets load into one document:

  • Module Federation, single-spa, qiankun, or other micro-frontends
  • Widgets injected into pages you do not control
  • Two versions of a design system on one page, common mid-migration

Iframes and Next.js multi-zones don't need this. The cascade cannot cross documents.

Collisions

Override leakage

Two bundles load the same styles, and one adds a local override:

/* bundle 1 */
.ds-background {
  background-color: blue;
}
.local-override {
  background-color: red;
}
 
/* bundle 2 */
.ds-background {
  background-color: blue;
}

Load order decides whether .local-override wins. Put the shared styles in a cascade layer. Unlayered styles always beat layered ones.

panda.config.ts

export default defineConfig({
  layers: {
    recipes: 'ds.recipes',
    utilities: 'ds.utilities'
  }
})

Cross-version collision

Two bundles ship different versions of @acme/ds. Same selectors, different rules:

/* v1's bundle */
:root {
  --colors-brand: #ea580c;
}
.button {
  background: var(--colors-brand);
  padding: 8px 12px;
}
 
/* v2's bundle */
:root {
  --colors-brand: #2563eb;
}
.button {
  background: var(--colors-brand);
  padding: 20px 40px;
}

Both land in the same layer, so load order picks the winner.

Same rules, different order

Two apps share one design system version and one prefix. The remote loads after the host:

// host: a label, hidden on mobile
css({ display: 'none', md: { display: 'inline' } })
 
// remote: an unrelated element
css({ display: 'none' })

Same-named layers merge across stylesheets. The remote's .d_none lands after the host's md:d_inline, and the label stays hidden on desktop. padding then paddingTop, and _hover then _focus, break the same way.

All three are fixed by giving each bundle its own prefix.

Set a prefix

Panda prepends prefix to every class name and CSS variable it emits:

panda.config.ts

export default defineConfig({
  prefix: 'acme-v1'
})
:where(:root, :host) {
  --acme-v1-colors-brand: #ea580c;
}
 
.acme-v1-button {
  background: var(--acme-v1-colors-brand);
}

Your source code stays the same. The rule: one prefix per Panda build on the page, even if two builds share a config.

Derive the prefix

Key it to the package name and major version, so it can't drift:

panda.config.ts

import { defineConfig } from '@pandacss/dev'
import pkg from './package.json' with { type: 'json' }
 
const major = pkg.version.split('.')[0]
const slug = pkg.name.replace(/[@/]/g, '-').replace(/^-/, '')
 
export default defineConfig({
  prefix: `${slug}-v${major}` // @acme/ds@1.4.2 → acme-ds-v1
})

For micro-frontends, key it to the app instead (checkout, account).

Who sets the prefix

  • One build per page: the design system sets it. Apps inherit it through designSystem and leave prefix, hash, and separator unset.
  • Several builds per page: each app sets its own. See Module Federation.

Module Federation

The host and each remote ship their own CSS onto one page. Give each app its own prefix.

1. Compile the design system in each app. Use its preset and scan its source. Don't use designSystem: it forces the design system's prefix on every app.

apps/checkout/panda.config.ts

import { defineConfig } from '@pandacss/dev'
import { acmePreset } from '@acme/ds/preset'
 
export default defineConfig({
  presets: [acmePreset],
  prefix: 'checkout',
  preflight: false,
  importMap: '@acme/ds/styled-system',
  include: ['src/**/*.{ts,tsx}', '../../packages/ds/src/**/*.{ts,tsx}'],
  outdir: 'styled-system'
})

2. Alias @acme/ds/styled-system to the app's outdir. The design system and the app both import from this path, never a relative ../styled-system, so each app can swap in its own:

apps/checkout/rspack.config.ts

import path from 'node:path'
 
export default {
  resolve: {
    alias: {
      '@acme/ds/styled-system': path.resolve(import.meta.dirname, 'styled-system')
    }
  }
}

If the package is "type": "module", Rspack and webpack need file extensions in ESM imports. Set forceImportExtension: true in the Panda config so the generated files include them.

Mirror the alias in tsconfig.json so types come from the same place:

apps/checkout/tsconfig.json

{
  "compilerOptions": {
    "paths": {
      "@acme/ds/styled-system/*": ["./styled-system/*"]
    }
  }
}

The same component now gets each app's prefix:

packages/ds/src/button.tsx

import { button } from '@acme/ds/styled-system/recipes'
 
export const Button = props => <button className={button()} {...props} />
 
// in the host:     <button class="host-button">
// in the checkout: <button class="checkout-button">

3. Keep the design system out of shared. Module Federation loads one shared copy for every app, so one app's components render with the other app's class names. They look right only while that other app's CSS is on the page.

apps/checkout/rspack.config.ts

import { rspack } from '@rspack/core'
 
new rspack.container.ModuleFederationPlugin({
  name: 'checkout',
  exposes: { './Checkout': './src/checkout.tsx' },
  shared: {
    react: { singleton: true },
    'react-dom': { singleton: true }
    // no @acme/ds: each app bundles its own copy
  }
})

4. Let the host own the reset. Set preflight: false in every remote.

The two sheets can no longer reorder each other:

/* host.css */
@layer utilities {
  .host-d_none {
    display: none;
  }
  @media (width >= 48rem) {
    .md\:host-d_inline {
      display: inline;
    }
  }
}
 
/* checkout.css */
@layer utilities {
  .checkout-d_none {
    display: none;
  }
}

The cost: each app ships its own copy of the design system's CSS and runtime.

If the host and remotes always deploy together, skip all this. Build one stylesheet in the host that includes every remote's source.

Shared packages

  • Uses Panda, not the design system: import from @acme/ds/styled-system and let each app's alias resolve it.
  • Has its own config recipes: export them as a preset and add it to each app's presets. Namespace the className (charts-legend).

Scope tokens with cssVarRoot

To limit tokens to a subtree, set cssVarRoot:

panda.config.ts

export default defineConfig({
  prefix: 'acme-v1',
  cssVarRoot: '.acme-v1-scope'
})
<div className="acme-v1-scope">
  <Button>Click</Button>
</div>

Elements outside .acme-v1-scope can't resolve the tokens. Most setups only need prefix.

Verify the output

grep -E "^\.button|--colors-brand:" styled-system/styles.css

Every match should carry your prefix. If not, move prefix to the top level of defineConfig and regenerate.

What prefix does not solve

  • Same prefix. Two bundles with the same prefix collide as if neither had one.
  • Theme keyframes. prefix renames inline keyframes(), not theme.keyframes. Name app-defined ones with the app's prefix (checkout-pulse).
  • Resets. Preflight targets element selectors. Only one bundle on the page should ship it.
  • Hand-written CSS. prefix only applies to what Panda emits.

For full isolation from an untrusted host, use an iframe or Shadow DOM.

See also

Edit this page on GitHubView as markdown
Last updated on