Sub topic 5
Component hooks for individual base components
This chapter assumes you can build a basic Lightning web component with an HTML template, a JavaScript class and a CSS file. It also assumes you know CSS selectors and inheritance, and that you have used base components such as lightning-button. Earlier chapters covered CSS custom properties and why shadow DOM blocks ordinary selectors. This chapter covers the hooks that belong to one base component.
What a component hook is
A component hook is a CSS custom property that a single base component reads when it renders. The component's internal styles use var() with the hook's name. If you set the hook, the component uses your value. If you don't, it uses its default.
Component hooks start with --slds-c-, followed by the component name and the property they control. Global hooks (--slds-g-) are a separate topic, so this chapter leaves them aside.
--slds-c-button-brand-color-background
| | | | |
| | | | +-- property: background
| | | +-------- category: color
| | +-------------- variant: brand
| +--------------------- component: button
+---------------------------- component hook prefixNot every name follows the same pattern. Some hooks have no variant, such as --slds-c-button-radius-border. Treat the name as a readable label, and take the exact spelling from the documentation.
How a value reaches the component
Custom properties inherit. A value set on the host element passes into its shadow tree, where the base component's own CSS reads it with var().
A worked example
First, give the base component a class in your template. The class gives your CSS file something to select.
<template>
<lightning-button
class="brand-action"
variant="brand"
label="Save"
></lightning-button>
</template>Then set hooks on that class in the 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 three things: the background, the border color and the corner radius. You did not touch the button's markup or its internal CSS.
Hooks are tied to a variant
The hook --slds-c-button-brand-color-background only affects a button whose variant is brand. A neutral button ignores it because the neutral styles read a different hook. If a hook seems to do nothing, check that the name matches the variant you rendered.
Common mistakes
- Typing the name from memory. One wrong segment produces a valid custom property that nothing reads, and no error appears.
- Setting a hook on the wrong element. The hook must be set on the base component's host element or on an ancestor, never on an element inside its shadow tree.
- Using the wrong variant. A brand hook does not style a neutral or destructive button.
- Expecting layout control. Hooks cover colors, borders, radius and similar values. They do not move or resize elements in general.
Choosing where to set a hook
Where you set a hook decides how far it reaches. The class on the host affects that one button. The same declaration on a parent container affects every matching base component inside it. Later chapters cover this scoping in detail. For now, set hooks on a class that you put on the base component itself.
Exercise
You write .save-button { --slds-c-button-brand-color-background: #014486; }, but your lightning-button has variant="neutral" and its color does not change. Why, and what do you check first?
Show answer
The hook belongs to the brand variant, and a neutral button reads different hooks, so nothing consumes this value. Check that the hook name matches the variant you rendered, using the base component's documentation. Also confirm that the class is on the lightning-button element itself.
Exercise
Why does setting a custom property on a lightning-button host change styles inside its shadow tree, when a normal class selector for an inner element would not?
Show answer
Custom properties inherit, and inheritance crosses the shadow boundary. A selector in your CSS cannot match elements inside the shadow tree, but the inherited value is still available to the component's own var() references.