Skip to content

fix: formalize container padding tokens, prevent theme access to internal CSS vars #846

Closed

Description

@cixzhang

Problem

Themes can currently override internal CSS variable names to adjust component spacing. The neutral theme does this for Card and Section:

// neutralTheme.ts — reaches into internal implementation details
card: {
  base: {
    "--layout-padding-inner-x": "var(--spacing-3)",
    "--layout-padding-inner-y": "var(--spacing-3)",
    "--layout-padding-outer-x": "var(--spacing-3)",
    "--layout-padding-outer-y": "var(--spacing-3)",
  },
},

These --layout-padding-* variables are internal implementation details of the Layout system, not public tokens. If core renames them, themes break silently.

Two things need to happen

1. Close the hole — make internal vars inaccessible to themes

Internal CSS variables like --layout-padding-inner-x, --container-padding, --component-padding-inline, --edge-start/--edge-end should be scoped using stylex.defineVars so themes cannot override them by name. They are implementation details of the edge compensation and layout systems.

2. Open a proper door — expose public container padding tokens

Add formal tokens that themes can legitimately override:

// In tokens.stylex.ts
export const layoutDefaults = {
  "--layout-padding": "16px",    // default container content padding
  // potentially more granular:
  // "--layout-padding-x": "16px",
  // "--layout-padding-y": "16px",
};
export const layoutVars = stylex.defineVars(layoutDefaults);

Layout components (Card, Section, LayoutContent, LayoutPanel, etc.) would read from these public tokens as their defaults, then use internal vars for the inner/outer split and edge compensation mechanics.

The neutral theme would then override the public token:

// Clean: overrides a public token
defineTheme(layoutVars, {
  "--layout-padding": "var(--spacing-3)",  // 12px
})

Context

Found during spacing audit for issue #839. The neutral theme's current approach works but creates fragile coupling. This is a prerequisite for a clean density system — density="compact" on XDSTheme would adjust these public tokens, which then propagate through the layout system via the internal vars.

Related

Activity

  1. added
    bug
    enhancement
    theming
    on Mar 24, 2026
  2. cixzhang commented on Mar 24, 2026

    @cixzhang
    ContributorAuthor

    Update: after reading the theme infrastructure docs, the right approach here is component-specific tokens via the existing component override system — not new global tokens.

    The theme system already supports component overrides via module augmentation:

    // Components declare their themeable slots
    declare module "../theme/types" {
      interface ComponentStyles {
        card?: {
          container?: ThemeStyleXStyles;
          content?: ThemeStyleXStyles;
        };
      }
    }

    So Card, Section, and Layout should expose their padding through this system. The neutral theme would then use the proper component override API:

    // Before (fragile — reaches into internal vars)
    card: {
      base: {
        "--layout-padding-inner-x": "var(--spacing-3)",
        // ...4 more internal variables
      },
    }
    
    // After (proper — uses component override API)  
    components: {
      card: {
        container: cardOverrides.container,  // includes padding: spacing-3
      },
    }

    Two things still need to happen:

    1. Close the hole — internal CSS vars (--layout-padding-inner-x, etc.) should be scoped via stylex.defineVars so themes can't override them by name
    2. Expose the right slots — Layout/Card/Section should expose padding as a proper component override slot, and internally derive the inner/outer padding split from it

    This also sets up cleanly for the density prop on XDSTheme — density="compact" could adjust the component-level padding tokens that components read as their defaults.

    — drafted with Navi

  3. added a commit that references this issue on Mar 24, 2026
    9396a2d
  4. added a commit that references this issue on Mar 24, 2026
    96a0d4b
  5. added a commit that references this issue on Apr 26, 2026
    5b67f79
  6. added 2 commits that reference this issue on Jun 21, 2026
    1b69713
    255519e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugenhancementtheming

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions