Sub topic 5
Component hooks for individual base components
This chapter explains how component hooks (--slds-c-*) customize one kind of base component, such as lightning-button or lightning-card. It assumes you know the basic structure of a Lightning web component (HTML template, JavaScript class, CSS file), CSS selectors and inheritance, and the idea of a CSS custom property from the previous chapters. You will see how a component hook reaches inside a base component, how to read and apply one, and which mistakes make a hook silently do nothing.
What a component hook is
A component hook is a CSS custom property that a specific base component reads in its own stylesheet. The component's authors chose which visual properties to expose, such as background color, border color or corner radius. They wrote those properties as var() lookups with default values. When you define the custom property, the component uses your value. When you don't, it uses its default.
This differs from overriding CSS. You never target the component's internal class names, which are private and can change between releases. You only set a value on a documented name. The component decides how that value is used.
The hook only works because custom properties inherit. A value set on a parent element passes down through the shadow boundary to elements inside the child's shadow tree. Ordinary properties like background-color don't get through that way, because they are not inherited into the shadow tree by default.
Anatomy of a component hook name
Component hook names follow a pattern: --slds-c- followed by the component, then the thing being styled, then the property type.
| Segment | Example | Meaning |
|---|---|---|
| `--slds-c-` | `--slds-c-` | Prefix for component hooks. Global hooks use `--slds-g-`. |
| Component | `button` | The base component or SLDS component blueprint the hook belongs to. |
| Variant or part (optional) | `brand` | A variant or sub-part of the component. |
| Property group | `color-background` | What is styled, such as `color-background`, `color-border`, `radius-border`. |
So --slds-c-button-brand-color-background reads as: component hook, button, brand variant, background color. And --slds-c-button-radius-border has no variant segment, so it applies to the button's corner radius in every variant that reads it.
The variant segment matters. A hook that names brand is only read by brand-variant buttons. If you set --slds-c-button-brand-color-background on a variant="neutral" button, nothing changes, because the neutral button's CSS never looks that name up.
Applying a hook: a worked example
Start with a template that gives the button a class. The class on the host element is the selector your CSS will attach hook values to.
<template>
<lightning-button
class="brand-action"
variant="brand"
label="Save"
></lightning-button>
</template>Then set the hooks in the same component's CSS file.
.brand-action {
--slds-c-button-brand-color-background: #5a1ba9;
--slds-c-button-brand-color-border: #5a1ba9;
--slds-c-button-radius-border: 1rem;
}This changes the button's background, border color and corner radius. It does not touch the button's internal markup. The selector .brand-action matches the lightning-button element in your template, which is the host element of the base component. The three custom properties are set on that host and inherit into its shadow tree, where the button's own CSS reads them.
Three details are worth noting:
- The class must be on the element in your template. A component's CSS file only styles elements in its own template, plus its own
:host. It can't reach into another component's shadow tree, so you can't write.brand-action button { ... }and expect it to work. - Values are ordinary CSS values. Colors, lengths and shadows all work, as long as the type fits the property the component uses. Setting a color hook to
1remproduces an invalid value for that property and the browser falls back to the inherited or initial value. - You set the border color and the background separately. Changing only the background of a brand button leaves its blue border in place, and the mismatch is easy to see. Check the documentation for each related hook, such as the hover and active states, which have their own names.
Why not set the property directly?
A direct override such as the following has no effect from a parent component, and it is the failure that hooks exist to avoid.
.brand-action button {
background-color: #5a1ba9;
}The selector tries to match a button element that lives inside the base component's shadow tree. Your component's stylesheet is scoped to your own template, so the rule never matches. Hooks give you a sanctioned path through that boundary, and they are the only one the base component promises to keep stable.
Applying hooks to many components at once
Because the hooks are inherited custom properties, you can set them on an ancestor. Every matching base component inside that ancestor picks the values up. A more specific selector can then override a value for a single 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 lightning-button inside an element with the class compact-region gets small corners. The .brand-action button inside that region also gets a different background. The cascade works the same as for any other CSS property: for the same element, the more specific selector wins, and if specificity is equal, the later rule wins. Inheritance fills in for elements that have no rule of their own. A value set directly on an element beats a value inherited from an ancestor, regardless of the ancestor's specificity.
The next chapters cover scoping and global hooks in more detail. For now, remember that a hook set on .compact-region reaches buttons that are descendants of it in your template, and also buttons rendered inside child components placed within that region, because inheritance crosses shadow boundaries.
Finding the hook names you need
Do not guess names. A misspelled hook is still a valid custom property, so the browser accepts it and ignores it, and you get no error. Use these sources:
- The component's reference page in the Component Library. Many base components list their supported styling hooks.
- The styling hooks reference on the SLDS site, which groups hooks by component blueprint and by state.
- The browser developer tools. Select the rendered element, then look in the Styles pane for
var(--slds-c-...)references in the internal rules. This shows which names the shipped CSS reads.
The SLDS blueprint name and the base component name usually match, but not always. A base component can be built from several blueprints, so a hook you expect may be listed under a different component name. If a hook from one blueprint's page does nothing, check whether the base component actually reads it.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Nothing changes | Misspelled hook name, or a hook the component does not read | Compare the name character by character with the documentation, and check the Styles pane for the `var()` reference. |
| Works on one button, not another | The hook names a variant, such as `brand`, that the other button does not use | Set the hook for the variant that button has. |
| Rule does not apply | The class is on a wrapper, not the base component, and no ancestor rule sets the value | Put the selector on the base component or on an ancestor that contains it. |
| Hook applies in the wrong places | Set on a broad ancestor | Narrow the selector, or reset the value on the inner element. |
| Value ignored | The value is the wrong type for the property | Use a value valid for the property, such as a color for a `color-*` hook. |
| Only part of the button changes | Related hooks for border, hover or active states were not set | Set each related hook the documentation lists. |
Limits to keep in mind
Component hooks only cover what the component's authors chose to expose. If there is no hook for the property you want, such as a change to the internal layout, the hook approach cannot do it. The chapter on limits and alternatives covers what to do then. Also check contrast after changing colors. A custom background on a brand button can leave the default text color unreadable, and hooks for the text color are separate from the background hooks.
Exercise
A developer writes .save-btn { --slds-c-button-brand-color-background: #5a1ba9; } and puts class="save-btn" on a lightning-button with variant="neutral". The background does not change. Give the cause and one fix.
Show answer
The neutral button's CSS does not read the brand hook, so the value is ignored. Either change the variant to brand, or set the hook the neutral variant reads, such as --slds-c-button-neutral-color-background, after confirming the name in the documentation.
Exercise
You want every lightning-button inside a .toolbar element to have square corners, except the one with class primary, which should keep the default radius. Write the CSS and explain why the second rule works.
Show answer
Use .toolbar { --slds-c-button-radius-border: 0; } and .toolbar .primary { --slds-c-button-radius-border: 0.25rem; }. The second rule sets the value directly on the primary button, and a value set directly on an element beats the value it would inherit from .toolbar. Use whatever radius value matches the default you want to keep. If you need the exact built-in default, remove the hook value from that element's rule by not matching it with the first rule at all.