Sub topic 3
What styling hooks are and why they exist
This chapter assumes you can build a basic Lightning web component (HTML template, JavaScript class, CSS file). It also assumes you know CSS selectors, the cascade, and inheritance, and that you have used base components such as lightning-button and lightning-card. It builds on two earlier ideas: CSS custom properties, and the style encapsulation that Lightning web components use.
The problem hooks solve
You add a lightning-button to your component and the design team asks for a purple brand button with rounded corners. The obvious move is to write a CSS rule in your component's stylesheet that targets the button's internal elements. That rule has no effect.
The reason is style encapsulation. A base component renders its markup inside a shadow tree. Selectors in your stylesheet stop at the boundary of that tree. Your CSS can style the lightning-button host element, but it cannot reach the <button> element inside it. The next chapter section on encapsulation covers the mechanism. For now, treat the boundary as a rule: selectors do not cross it.
Salesforce needed a way to let you change the look of base components while still controlling their internals. Letting every developer write selectors against internal markup would make every internal change a breaking change. Styling hooks are the answer.
What a styling hook is
A styling hook is a CSS custom property that SLDS (Salesforce Lightning Design System) publishes as a supported customization point. A base component reads the property inside its own stylesheet. If you set a value, the component uses it. If you don't, the component uses its default.
Conceptually, a base component's internal CSS looks something like this. This is a simplified illustration, not the real source:
.slds-button_brand {
background-color: var(--slds-c-button-brand-color-background, #0176d3);
border-radius: var(--slds-c-button-radius-border, 0.25rem);
}The var() function reads a custom property and falls back to the second argument when the property is not set. The component author chose which properties to read, which makes them the public surface for styling.
Custom properties work across the boundary because they inherit. An inherited property set on a host element, or on any ancestor, flows down into the shadow tree. Selectors cannot cross the boundary, but inherited values can. A hook is a value passed in through that gap.
A first example
Here is a brand button with a class on the host element. The class gives your CSS file a selector to attach hook values to.
<template>
<lightning-button
class="brand-action"
variant="brand"
label="Save"
></lightning-button>
</template>Now set hooks on that class:
.brand-action {
--slds-c-button-brand-color-background: #5a1ba9;
--slds-c-button-brand-color-border: #5a1ba9;
--slds-c-button-radius-border: 1rem;
}The button now has a purple background, a purple border, and a larger corner radius. You did not touch its internal markup. Each declaration is an ordinary custom property assignment on the host element. It inherits into the shadow tree, where the component's own var() calls pick it up.
Notice what you did not do. You did not use !important, :host-context, or ::part. You used a plain selector for an element in your own template and set values on it.
Why Salesforce built hooks this way
Hooks exist for several reasons, and each reason affects how you should use them.
- Stable contract. Salesforce can rewrite a component's internal markup and class names. As long as the documented hooks keep working, your customization keeps working. Overrides written against internal selectors would break.
- Works with encapsulation. Custom properties are the one styling channel that already passes through the shadow boundary, so no new browser feature or Salesforce-specific mechanism is required.
- Theming by value. A change to one hook affects every instance in scope. You can theme a region of a page without editing every component inside it.
- Defaults stay intact. Unset hooks fall back to the SLDS defaults. You override only what you need, and the rest of the component still follows the design system.
The trade-off is that you can only change what the component author exposed. If a component has no hook for the property you want, hooks cannot help. A later chapter covers that limit and the alternatives.
Two families of hooks
SLDS publishes two families of hooks, and the prefix tells you which one you are looking at.
| Prefix | Name | Scope | Example |
|---|---|---|---|
| `--slds-c-` | Component hook | One kind of base component | `--slds-c-button-radius-border` |
| `--slds-g-` | Global hook | A shared design decision used by many components | `--slds-g-color-brand-base-50` |
A component hook targets one component and one visual property, such as the corner radius of buttons. A global hook represents a design decision that many components share, such as a brand color. Later chapters cover naming, finding hooks, and global hooks in detail. For this chapter, the point is that both are ordinary custom properties and both follow the same inheritance rules.
How scope works
Because hooks inherit, where you set them decides which elements they affect. Setting a hook on an ancestor applies it to every matching base component inside that ancestor. A more specific selector overrides the value for one instance.
.compact-region {
--slds-c-button-radius-border: 0.25rem;
}
.compact-region .brand-action {
--slds-c-button-brand-color-background: #014486;
}In this example, every button inside an element with the class compact-region gets the smaller radius. The button with the class brand-action inside that region additionally gets a different background color.
This is normal cascade and inheritance behavior. It is the same reasoning you would use for color or font-family. The first rule sets a value on a region, and the value flows down to descendants. The second rule sets a value closer to one element, and the nearest value wins.
Where hooks apply from
You write hook declarations in the CSS file of the component whose template contains the base component. Your CSS file styles the elements in your template, which includes host elements of the base components you place there. It does not style anything inside a base component's shadow tree. That is exactly the situation hooks are designed for, because you only need to set a property on the host.
A hook set in a parent component's CSS reaches base components in the parent's template. It also inherits into the shadow trees of child components, since inheritance passes through every boundary. That is why you can set a hook on an outer element and affect nested components.
Defining your own hooks
The same technique works for components you write. A custom component can read a custom property with var() and supply a fallback, and then a parent can set that property.
:host {
--c-badge-color-background: #eef4ff;
}
.badge {
background-color: var(--c-badge-color-background, #f3f3f3);
padding: 0.25rem 0.5rem;
border-radius: 0.25rem;
}The fallback in var() is used only when the property is not defined at all. A parent that sets --c-badge-color-background on the element or on an ancestor overrides the value declared in :host. The --c- prefix here is a convention for custom component properties. It is not an SLDS-reserved prefix, so choose names that will not collide with --slds-c- or --slds-g-. A later chapter covers this pattern in full.
Common errors
These problems come up often when you first use hooks.
- Setting the hook on the wrong element. The property must be set on the base component's host element or on one of its ancestors. Setting it on an unrelated sibling, or on a child inside your own template that does not contain the base component, does nothing.
- Misspelled or invented hook names. A custom property with an unknown name is valid CSS, so the browser raises no error. The base component simply never reads it. Copy names from the documentation.
- Using a hook the component does not support. Hooks are per component and per variant. A hook for the brand button background does not change a neutral button.
- Expecting selectors to work. Rules such as
.brand-action button { ... }do not match anything, because the inner<button>is inside the shadow tree. - Losing to a more specific value. A hook set closer to the element wins over one set on an ancestor. If your value seems ignored, look for another declaration of the same property nearer to the component.
Summary of the working model
- A base component's shadow tree blocks selectors from your stylesheet.
- Custom properties inherit, so values you set on the host or an ancestor reach the shadow tree.
- The base component reads certain custom properties with
var()and a default. These are the hooks. - Hook prefixes tell you the scope:
--slds-c-for one component,--slds-g-for shared design decisions. - You control the reach of a value by choosing which element or ancestor sets it.
Exercise
Your component template contains <lightning-button class="save-btn" variant="brand" label="Save"></lightning-button>. A colleague wrote .save-btn button { background-color: #5a1ba9; } in the component's CSS file and the color does not change. Explain why, and write the CSS that changes the brand background color using a styling hook.
Show answer
The inner <button> element lives in the base component's shadow tree, and selectors from your stylesheet do not cross that boundary, so the rule matches nothing. Set the hook on the host element instead: .save-btn { --slds-c-button-brand-color-background: #5a1ba9; }. The custom property inherits into the shadow tree, where the component reads it with var().
Exercise
A page region has the class .toolbar and contains five lightning-button elements. You want all five to have a corner radius of 0.5rem, and one of them, with the class .primary, to also have a background of #014486. Which selectors would you write, and which hooks would you set where?
Show answer
Set the radius hook on the region: .toolbar { --slds-c-button-radius-border: 0.5rem; }. It inherits to all five buttons. Then set the background hook on the one instance with a more specific selector: .toolbar .primary { --slds-c-button-brand-color-background: #014486; }. The .primary button keeps the region's radius and gets the extra background value. This assumes .primary is a brand variant button, since the brand background hook only affects that variant.
Exercise
You set --slds-c-button-brand-colour-background: red; on a brand button and nothing changes, and the browser console shows no error. Why is there no error, and what is the likely cause?
Show answer
Any --name: value declaration is valid CSS, so the browser never reports unknown custom properties. The likely cause is a misspelled hook name (colour instead of color). The base component never reads that property, so it has no effect. Check the name against the component's documented hooks.