Sub topic 6
Global hooks for shared design decisions
This chapter covers global styling hooks, the --slds-g-* custom properties. It assumes you can already read a hook name, apply a component hook (--slds-c-*) in a component's CSS file, and use the cascade and inheritance. You should also be comfortable with lightning-button and lightning-card.
What a global hook is
A component hook targets one kind of base component. For example, --slds-c-button-radius-border only affects buttons. A global hook holds a design decision that many components share, such as the brand color, the standard corner radius, or a step on the spacing scale.
Global hooks are still CSS custom properties, so they inherit through the shadow DOM boundary just like component hooks do. The difference is in who reads them. Many base components use a global hook as the default value for their own component hooks. Changing one global hook can therefore change several components at once.
| Aspect | Component hook | Global hook |
|---|---|---|
| Prefix | `--slds-c-` | `--slds-g-` |
| Target | One base component, one part of it | Many components, plus your own CSS |
| Example name | `--slds-c-button-radius-border` | `--slds-g-radius-border-2` |
| Typical use | "Make this button rounder" | "Use our brand purple everywhere in this region" |
| Blast radius | Small | Large |
How the two layers connect
A base component resolves a style value in order. If a component hook is set, it wins. If not, the component uses a global hook. If that is also unset, the component uses the default value from SLDS.
Treat this chain as the working model. The exact wiring differs per component and per release. Some component hooks have no global hook behind them, and some visual properties are not exposed through either layer.
Kinds of global hooks
Global hooks are grouped by the design decision they carry. The names below show the pattern. Confirm the exact names and supported values in the SLDS styling hooks reference for your release, because the catalog changes between releases.
- Color. Brand, neutral, surface, error and success palettes, for example
--slds-g-color-brand-base-50. - Spacing. A numbered scale for padding and margins, for example
--slds-g-spacing-4. - Radius. Corner rounding steps, for example
--slds-g-radius-border-2. - Typography. Font scale and weight values, for example
--slds-g-font-scale-2. - Sizing and shadow. Fixed dimensions and elevation values.
A global hook name describes a role in the design, not a component. --slds-g-color-brand-base-50 means "a mid-weight step of the brand palette". It does not mean "button background". That is why one change can reach buttons, links, focus rings and other brand-colored parts together.
Setting a global hook
You set a global hook the same way you set a component hook. You declare it on an element, and descendants inherit it. The selector you choose decides how far the change reaches.
Here is a template that groups a card and a button inside one wrapper:
<template>
<div class="promo-region">
<lightning-card title="Spring promotion" icon-name="standard:campaign">
<div class="slds-p-around_medium">
<p>Register before the end of the month.</p>
<lightning-button
variant="brand"
label="Register"
></lightning-button>
</div>
</lightning-card>
</div>
</template>Here is the matching CSS file:
.promo-region {
--slds-g-color-brand-base-50: #5a1ba9;
--slds-g-radius-border-2: 0.5rem;
}Every base component inside .promo-region that reads these global hooks now uses the purple brand step and the larger radius. Components outside the wrapper keep the org defaults. You never had to look up a button-specific hook.
If the button looks unchanged, the likely cause is that its brand background is not driven by that exact global hook in your release. Section "When a global hook does nothing" below covers how to check.
Combining global and component hooks
Global hooks set the baseline. Component hooks handle the exceptions. Because a component hook wins over a global one, you can set a region-wide value and then adjust a single component type:
.promo-region {
--slds-g-color-brand-base-50: #5a1ba9;
--slds-g-radius-border-2: 0.5rem;
}
.promo-region .register-button {
--slds-c-button-radius-border: 1.5rem;
}For the button with class register-button, the radius follows the component hook (1.5rem). The card and other components in the region still follow the global radius (0.5rem). Add class="register-button" to the lightning-button element for this to apply.
This layering keeps the intent readable. The first rule says what the region looks like in general. The second rule says what is special.
Using global hooks in your own CSS
Global hooks are not limited to base components. Your own component can read them with var(). This keeps custom markup consistent with the base components around it:
.callout-box {
background-color: var(--slds-g-color-brand-base-50, #0176d3);
border-radius: var(--slds-g-radius-border-2, 0.25rem);
padding: var(--slds-g-spacing-4, 1rem);
color: #ffffff;
}The second argument to var() is a fallback. It is used when the global hook is not defined, for example when the component renders in a context that does not provide it. Always supply one. Without a fallback, an undefined hook makes the declaration invalid at computed-value time. The property then falls back to its inherited or initial value, which can give you a transparent background or no padding.
Scope: where global hooks are visible
Custom properties inherit down the tree, so a global hook affects the element where you set it and everything inside it. This has several consequences.
- Set on a wrapper, not on
:root. A component's CSS file is scoped to that component. You can set hooks on:hostor on an element in your template. Setting them on:hostaffects everything in your component, including child base components. - Setting a hook on
:hoststyles your whole component. Parent components can also set hooks on your element from outside, because inheritance reaches inside your shadow tree. - A nearer declaration wins. If an outer region sets
--slds-g-radius-border-2: 0.5remand an inner element sets it to0, the inner value applies inside that inner element. - Slotted and child content inherits. Content you pass into a child's slot is styled in the context of where it sits in the DOM, so an ancestor's hooks reach it.
Here is a :host-level version:
:host {
--slds-g-color-brand-base-50: #5a1ba9;
--slds-g-spacing-4: 1.25rem;
}Every instance of this component now carries those values to all of its descendants. That is convenient for a self-contained widget, but it also hides the change from anyone reading the parent's CSS.
When a global hook does nothing
Hooks fail silently, so use a short checklist.
- Check the name. A misspelled custom property is not an error. The browser simply ignores it. Copy names from the reference instead of typing them.
- Check that the component reads it. Open developer tools, select the inner element of the base component, and look at the computed styles. If the element's rule uses
var(--slds-c-...), follow the chain to see whether a--slds-g-...hook sits behind it. - Check that the value is inherited. In the Styles panel, confirm your declaration appears on an ancestor of the element. If you set the hook on a sibling or a child, it will not reach the target.
- Check specificity. A component hook set closer to the element beats a global hook set farther away, even if the global rule has a more specific selector. This is because the base component consumes them at different points in its own CSS.
- Check the release. A hook that exists in the documentation for one release may not be supported in your org yet.
Trade-offs
Global hooks reduce repetition, but they have costs.
- Reach. They can change more than you intend. Component hooks are safer for local fixes.
- Stability. Global hooks follow the design system, so their names and the components that read them can change between releases. A short, well-commented block of overrides is easier to revisit.
- Coverage. Not every visual property is wired to a global hook. If you need to change something that has neither a component hook nor a global one, hooks are not the right tool. The later chapter on limits and alternatives covers that case.
Exercise
A page wrapper sets --slds-g-radius-border-2: 0.5rem. Inside it, one button has --slds-c-button-radius-border: 0 set through its class. What corner radius does that button use, and what do the other base components in the wrapper use, assuming they read the global radius hook? Give the reason.
Show answer
The button uses 0 (square corners) because a component hook, when set, takes priority over the global hook the component would otherwise fall back to. The other base components that read the global hook use 0.5rem, because the wrapper's custom property inherits to all of its descendants.
Exercise
You write background-color: var(--slds-g-color-brand-base-50); in your own component's CSS with no fallback. The component is later rendered somewhere that does not define the hook. What can go wrong, and how do you prevent it?
Show answer
The var() reference is invalid at computed-value time, so the property uses its inherited or initial value, which is often a transparent background. Prevent this by adding a fallback as the second argument, for example var(--slds-g-color-brand-base-50, #0176d3).