Sub topic 7
Finding the hooks a component supports
This chapter assumes you can write a basic Lightning web component (template, JavaScript class, CSS file). It also assumes you know CSS selectors and inheritance, and that you have read the earlier chapters on hook naming. Component hooks start with --slds-c- and global hooks start with --slds-g-.
You cannot guess a hook name. Each base component supports only a fixed set of component hooks, and a misspelled or unsupported name does nothing. It produces no error, so you need a reliable way to look names up.
Where hooks are documented
Salesforce publishes the list of supported hooks for each component. Use these sources in order:
- The component's reference page. In the Component Library, open the page for the base component, such as
lightning-button. If the component supports styling hooks, the page has a Styling Hooks section that lists each hook name. - The SLDS site. The Lightning Design System site has a Styling Hooks section for components. It describes the hooks that belong to the blueprint behind the base component.
- The browser developer tools. These show which custom properties a rendered element actually reads. Use them when the documentation is unclear or you want to confirm a name.
Not every base component has hooks. If the reference page has no Styling Hooks section, the component does not support them. In that case, see the chapter on when hooks are not enough.
Reading a hook entry
Hook names follow the pattern --slds-c-<component>-<category>-<property>. Reading a name tells you what it changes.
| Hook name | Component | What it changes |
|---|---|---|
| `--slds-c-button-brand-color-background` | Button, brand variant | Background color |
| `--slds-c-button-brand-color-border` | Button, brand variant | Border color |
| `--slds-c-button-radius-border` | Button, all variants | Corner radius |
Notice that some hooks name a variant (brand) and some do not. A hook without a variant applies to every variant of that component. A hook with a variant applies only to that variant. Check which one you are copying before you apply it.
Checking a hook in the browser
Documentation can lag behind the rendered markup, so confirm a name before you rely on it.
- Render the component on a page and open the browser developer tools.
- Select the element in the Elements panel and expand the shadow tree to find the inner element that carries the visual style, such as the
buttoninsidelightning-button. - In the Styles pane, look for declarations that use
var(--slds-c-...). - Copy the custom property name from inside
var().
A declaration in the Styles pane looks like this:
background-color: var(--slds-c-button-brand-color-background, var(--slds-g-color-brand-base-40));The name before the first comma is the component hook. The fallback after it is often a global hook. Setting the component hook overrides the fallback for that component only.
Common errors
- Typing a name from memory. One wrong word in the name means the declaration is ignored without any warning. Copy names from the documentation or the Styles pane.
- Using a hook from a different component. A
--slds-c-card-*hook does not affect a button inside the card. - Assuming a variant hook applies to all variants. Setting a brand hook does not change a neutral button.
- Looking only at the element you can see. The host element, such as
lightning-button, often has no style of its own. Thevar()usually appears on an inner element.
Exercise
You set --slds-c-button-brand-color-background on a class applied to a lightning-button with variant="neutral", and nothing changes. Give two reasons this could happen and how you would confirm each.
Show answer
First, the hook is for the brand variant, so a neutral button never reads it. Confirm by checking the Styles pane on the inner button element and seeing which var(--slds-c-...) names it uses. Second, the name may be misspelled or not supported by that component. Confirm by comparing it with the Styling Hooks section on the component's reference page. Use the neutral-variant hook listed there instead.
Exercise
In the Styles pane you see border-radius: var(--slds-c-button-radius-border, 0.25rem);. Which part is the hook name and which part is the fallback?
Show answer
The hook name is --slds-c-button-radius-border. The fallback is 0.25rem, which the browser uses when the hook is not set.