Sub topic 7
Finding the hooks a component supports
Applying a styling hook is easy once you know its name. Finding the right name is the harder part. This chapter covers where hook names are documented, how to confirm a hook exists in the running page, and how to tell a supported hook from a guess.
It assumes you can read a Lightning web component (HTML template, JavaScript class, CSS file). It also assumes you know what --slds-c-* and --slds-g-* mean from the previous chapters, and that you can use browser developer tools at a basic level.
Why guessing fails
A component hook name follows a pattern, so it is tempting to build one from the pattern. --slds-c-button-brand-color-background suggests --slds-c-button-brand-color-text. That name may not exist. A custom property that nothing reads is not an error. The browser accepts the declaration and ignores it, so you get no warning.
A hook only works if the component's own stylesheet reads it with var(). Finding hooks therefore means finding what the component reads. Three sources answer that question, in this order of reliability:
- The published documentation for the component.
- The component's own CSS, as loaded in the browser.
- Your editor's tooling, which helps but depends on version.
Source 1: the SLDS documentation
The Salesforce Lightning Design System site documents each component blueprint. Many blueprint pages include a styling hooks section. It lists each supported hook with its name and the CSS property it controls. This is the best place to start because the list is curated. Names that appear there are intended to be stable.
When you read an entry, look at three things:
- The full name. Copy it exactly, including the prefix and every segment.
- The variant segment. Button hooks are split by variant. Names containing
brandaffect onlyvariant="brand". Names without a variant segment usually affect every variant. Applying a brand hook to a neutral button changes nothing. - The state segment. Hover, focus and active states usually have their own hooks, such as a
-hoversuffix. Overriding the base color does not change the hover color, so hover can look unchanged.
The Component Library reference for a base component, such as lightning-button, may also list styling hooks on its page. Coverage varies between components. If the blueprint page has no hook section and the component page does not either, move on to inspection. Do not assume the component has no hooks.
Source 2: the component's own stylesheet
The browser shows exactly what a base component reads. This is the ground truth, because a hook works only if it appears in a var() call. You can check it in four steps:
- Render the component on a page, in a sandbox or scratch org.
- Open developer tools and select the component in the Elements panel.
- In the Styles pane, find the rules from the component's stylesheet.
- Look for
var(--slds-c-...)inside the declarations.
A rule for a brand button might look like this:
.slds-button_brand {
background-color: var(--slds-c-button-brand-color-background, #0176d3);
border: 1px solid var(--slds-c-button-brand-color-border, #0176d3);
}Each var() call tells you three things. The first argument is the hook name. The second is the fallback, which is the value used when the hook is not set. The declaration it sits in tells you which CSS property the hook controls. The pasted rule above is a simplified illustration. The real stylesheet has more rules, and exact values and fallbacks vary by SLDS version.
Rules for hover and focus states appear as separate selectors, such as :hover or :focus. Look at these as well as the base rule if you want to restyle states.
Listing hooks in bulk
Clicking through rules is slow when you want every hook for one component. This snippet, run in the browser console, collects every --slds-c-button* name found in the page's loaded stylesheets:
const found = new Set();
for (const sheet of document.styleSheets) {
let rules;
try {
rules = sheet.cssRules;
} catch (error) {
continue; // cross-origin stylesheets cannot be read
}
for (const rule of rules) {
const matches = rule.cssText.match(/--slds-c-button[\w-]*/g);
if (matches) {
matches.forEach((name) => found.add(name));
}
}
}
console.log([...found].sort().join('\n'));Change --slds-c-button to another prefix, such as --slds-c-card, to search a different component. Two limits apply:
- The snippet only sees stylesheets attached to the document. Where the platform uses native shadow DOM, a component's styles live inside its shadow root and this scan misses them. Use the Elements panel for those components.
- The page must contain the component. Styles for components that have not rendered may not be loaded yet.
Source 3: editor and tooling support
Salesforce tooling for VS Code and the SLDS linter can warn about overrides that bypass hooks. Check what your installed extension version offers instead of relying on memory. If autocomplete suggests hook names, treat the suggestions as a convenience and still confirm the name against the documentation or the browser.
Older names you may meet
Older Salesforce material and community posts use the prefix --sds-c-* instead of --slds-c-*. This is a legacy form of the component hook prefix. Many base components accept both for compatibility, because the stylesheet nests one inside the other:
.slds-button_brand {
background-color: var(
--slds-c-button-brand-color-background,
var(--sds-c-button-brand-color-background, #0176d3)
);
}Prefer --slds-c-* in new code. If you copy a snippet that uses --sds-c-*, rename it and confirm the result in the browser.
Verifying that a hook took effect
After you pick a hook, apply it as covered in the next chapter. Then check three things before you move on:
- Computed value. Select the element and open the Computed tab. The property the hook controls, for example
background-color, should show your value. - Hook visible on the element. In the Styles pane, your custom property should appear on the host element or an ancestor, with no strike-through.
- Every state. Hover, focus and disabled states may need separate hooks. Test each one.
This example sets a brand button's background and its hover background together, so no state is left behind:
<template>
<lightning-button
class="brand-action"
variant="brand"
label="Save"
></lightning-button>
</template>.brand-action {
--slds-c-button-brand-color-background: #5a1ba9;
--slds-c-button-brand-color-background-hover: #3f1478;
}If the hover hook name does not match what the documentation or the browser shows for your SLDS version, use the name those sources show.
Reading the result: what a missing hook tells you
Sometimes none of the three sources lists a hook for the property you want to change. That result is useful. It means the component does not expose that property for customization. The table below shows how to respond.
| What you find | What it means | What to do |
|---|---|---|
| Hook in documentation and in the browser CSS | Supported and active | Apply it |
| Hook in documentation, not in the browser CSS | The hook may not apply to this variant or version | Check the variant segment and your org's release |
| Hook in the browser CSS, not in documentation | Present but not promised | Use cautiously and note the risk in a comment |
| Hook in neither | The property cannot be customized this way | See the later chapter on limits and alternatives |
Resist the urge to override the component's internal class selectors instead. Because of style encapsulation, such selectors from your component's CSS do not reach inside the base component. Even where a technique appears to work, it depends on internal markup that Salesforce can change.
A short lookup routine
Use the same routine each time and you will rarely guess:
- State the target: which component, which variant, which state, which CSS property.
- Search the SLDS blueprint page for that component's hooks.
- Match the variant and state segments in the name to your target.
- Confirm in the browser that a
var()call reads that exact name. - Apply the hook, then check the computed value for every state.
Exercise
You set --slds-c-button-brand-color-text on a lightning-button with variant="neutral" and nothing changes. The browser raises no error. Give two possible reasons, and describe how you would find out which one applies.
Show answer
First, the hook name may not exist. A custom property that no stylesheet reads with var() is silently ignored. Second, the name contains the brand variant segment, so it can only affect a brand button. A neutral button reads neutral-variant or variant-less hooks. To find out, inspect the button in developer tools, look in its stylesheet rules for var() calls, and read the exact hook names the component reads for the neutral variant. Then compare with the blueprint documentation, and use the name that both sources show.
Exercise
A hook appears in the compiled CSS you see in developer tools but not in the SLDS documentation. Should you build your theme on it? Explain.
Show answer
Treat it with caution. The browser shows that the component reads the hook in your org today, but only documented hooks are the supported interface. An undocumented name can be renamed or removed in a later release, which would silently stop your override from working. If you use it, add a comment recording where you found it, and re-check after each release.