Skip to main content
import { applyHostStyleVariables } from "sunpeak";
In React components, useHostStyles applies CSS variables, theme, and fonts automatically. The function below is useful for non-React usage or custom control.

Overview

Hosts provide CSS custom properties for colors, typography, borders, and more via McpUiHostContext.styles.variables. Use applyHostStyleVariables to set these on the document, then reference them in your CSS with var().

applyHostStyleVariables

Sets each CSS variable from the host as a custom property on the specified element.

Signature

function applyHostStyleVariables(
  styles: McpUiStyles,
  root?: HTMLElement,
): void
styles
McpUiStyles
required
Style variables object from McpUiHostContext.styles.variables.
root
HTMLElement
default:"document.documentElement"
Element to apply styles to.

Usage

// Apply when host context changes
app.onhostcontextchanged = (ctx) => {
  if (ctx.styles?.variables) {
    applyHostStyleVariables(ctx.styles.variables);
  }
};

// Apply initial styles after connecting
await app.connect();
const ctx = app.getHostContext();
if (ctx?.styles?.variables) {
  applyHostStyleVariables(ctx.styles.variables);
}
Then use the variables in CSS:
body {
  background-color: var(--color-background-primary);
  color: var(--color-text-primary);
}

.card {
  background-color: var(--color-background-secondary);
  border: 1px solid var(--color-border-primary);
  border-radius: var(--border-radius-lg);
  box-shadow: var(--shadow-sm);
}

Available Variables

All variables are optional — hosts may provide any subset.

Background Colors

VariablePurpose
--color-background-primaryMain background
--color-background-secondaryElevated surfaces
--color-background-tertiaryFurther elevated surfaces
--color-background-inverseInverse background
--color-background-ghostGhost/transparent background
--color-background-infoInfo state
--color-background-dangerError/danger state
--color-background-successSuccess state
--color-background-warningWarning state
--color-background-disabledDisabled state

Text Colors

VariablePurpose
--color-text-primaryMain text
--color-text-secondarySecondary text
--color-text-tertiaryTertiary text
--color-text-inverseText on inverse background
--color-text-ghostGhost text
--color-text-infoInfo text
--color-text-dangerError/danger text
--color-text-successSuccess text
--color-text-warningWarning text
--color-text-disabledDisabled text

Border Colors

VariablePurpose
--color-border-primaryPrimary border
--color-border-secondarySecondary border
--color-border-tertiaryTertiary border
--color-border-inverseInverse border
--color-border-ghostGhost border
--color-border-infoInfo border
--color-border-dangerDanger border
--color-border-successSuccess border
--color-border-warningWarning border
--color-border-disabledDisabled border

Ring Colors

VariablePurpose
--color-ring-primaryPrimary focus ring
--color-ring-secondarySecondary focus ring
--color-ring-inverseInverse focus ring
--color-ring-infoInfo focus ring
--color-ring-dangerDanger focus ring
--color-ring-successSuccess focus ring
--color-ring-warningWarning focus ring

Typography

VariablePurpose
--font-sansSans-serif font family
--font-monoMonospace font family
--font-weight-normalNormal weight
--font-weight-mediumMedium weight
--font-weight-semiboldSemibold weight
--font-weight-boldBold weight
--font-text-xs-size / --font-text-xs-line-heightExtra small text
--font-text-sm-size / --font-text-sm-line-heightSmall text
--font-text-md-size / --font-text-md-line-heightMedium text
--font-text-lg-size / --font-text-lg-line-heightLarge text
--font-heading-xs-size / --font-heading-xs-line-heightExtra small heading
--font-heading-sm-size / --font-heading-sm-line-heightSmall heading
--font-heading-md-size / --font-heading-md-line-heightMedium heading
--font-heading-lg-size / --font-heading-lg-line-heightLarge heading
--font-heading-xl-size / --font-heading-xl-line-heightExtra large heading
--font-heading-2xl-size / --font-heading-2xl-line-height2XL heading
--font-heading-3xl-size / --font-heading-3xl-line-height3XL heading

Border Radius

VariablePurpose
--border-radius-xsExtra small radius
--border-radius-smSmall radius
--border-radius-mdMedium radius
--border-radius-lgLarge radius
--border-radius-xlExtra large radius
--border-radius-fullFull/pill radius

Other

VariablePurpose
--border-width-regularStandard border width
--shadow-hairlineHairline shadow
--shadow-smSmall shadow
--shadow-mdMedium shadow
--shadow-lgLarge shadow
See also: Theme for light/dark mode detection, and Fonts for host typography.