Sub topic 11
Debugging a Hook That Has No Effect
This chapter assumes you can write a Lightning web component (HTML template, JavaScript class, CSS file). It also assumes you know CSS selectors, the cascade, and inheritance, and that you have applied a component hook such as --slds-c-button-brand-color-background before. The goal here is to find out why a hook you set does nothing, using browser developer tools and a short checklist.
Why hooks fail silently
A styling hook is a CSS custom property. The browser never reports an error when you set a custom property that nothing reads. A misspelled name, a name the component does not support, or a value set at the wrong place all look the same: the page renders unchanged.
So debugging a hook means answering three questions in order:
- Is the declaration reaching the element at all?
- Does the component actually read a property with that exact name?
- Is the final value valid where the component uses it, and is another rule winning?
The failure modes at a glance
| Symptom | Likely cause | How to confirm |
|---|---|---|
| Declaration is missing from the Styles pane | Selector does not match the element | Check the selector against the rendered DOM |
| Declaration is crossed out | A more specific or later rule sets the same property | Read the cascade order in the Styles pane |
| Declaration is applied, but nothing changes | The component does not read that hook name | Search the component's styles for `var(--slds-c-...)` |
| Value shows as invalid at computed-value time | The value does not fit the property that reads it | Hover the declaration and check the computed result |
| Only some instances change | The hook is set on one element, not an ancestor | Inspect an instance that did not change |
| Change appears in one org or browser but not another | Different SLDS version or a cached build | Compare the hook list for each release |
Step 1: Confirm the selector reaches the element
Start with a button that should be purple.
<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-border: #5a1ba9;
}Open the page, right-click the button, and choose Inspect. In the Elements panel, find the lightning-button element. Its class attribute should include brand-action. Then look at the Styles pane while that element is selected.
- If you see
.brand-actionwith your two declarations, the selector matches and you can move to step 2. - If you do not see the rule, the selector is not matching. Common reasons are a typo in the class name, a rule placed in the wrong component's CSS file, or a selector that targets an element inside another component's shadow tree.
The last reason matters because of style encapsulation. A component's CSS file only applies to elements in that component's own template. A selector such as .brand-action .slds-button cannot reach the button element inside the base component's shadow tree. Hooks avoid this problem because custom properties inherit across the shadow boundary, but only if you set them on the host element or an ancestor that your own CSS can reach.
Step 2: Read the cascade in the Styles pane
When the rule appears but the value is crossed out, another declaration is overriding it. This is ordinary cascade behavior for the same property name on the same element.
.compact-region {
--slds-c-button-radius-border: 0.25rem;
}
.compact-region .brand-action {
--slds-c-button-brand-color-background: #014486;
}Here the second rule is more specific than a plain .brand-action rule, so it wins for the background hook. If you also had .brand-action { --slds-c-button-brand-color-background: #5a1ba9; } in the same file, the purple value would show with a line through it.
Check these points:
- Specificity: a selector with more classes beats one with fewer.
- Order: with equal specificity, the later rule wins.
- Source: a declaration from the page's global stylesheet or a theme can compete with yours. The Styles pane lists the source file for each rule.
- Inline styles: a
styleattribute on the element beats any selector.
Because custom properties inherit, an ancestor's value only applies when the element itself has no declaration for that property. A declaration on the element itself always wins over an inherited one, even if the inherited rule is more specific.
Step 3: Check that the component reads that hook
A hook only works if the component's own CSS reads it with var(). In the Styles pane, scroll down while the inner element is selected. In Elements, expand the lightning-button and its #shadow-root node, then select the inner button element. Look for a declaration like this:
.slds-button_brand {
background-color: var(--slds-c-button-brand-color-background, #0176d3);
}If the property you set is not in any var() on that element, the component does not support it. Two quick checks help:
- Look at the Computed tab for the inner element, enable Show all, and filter by
--slds-c-button. This lists the custom properties that reach the element, including inherited ones. - In the Styles pane, find the
background-colorrule. If thevar()name differs from yours by even one word, your hook is a typo or belongs to a different variant.
Names are easy to get wrong because they encode the component, the variant, the property group, and the property. For example, --slds-c-button-brand-color-background is for the brand variant. The neutral button uses a different name for its background. A hook written for one variant does nothing on another.
Step 4: Inspect the final computed value
Sometimes the name is right, the declaration applies, and the page still looks wrong. Click the inner element and look at the property that reads the hook, for example background-color. Hover the var(...) expression. Developer tools show the resolved value.
Two problems appear here:
- The value is invalid for the property. A custom property accepts any token stream, so
--slds-c-button-radius-border: purpleis accepted when you set it. When the browser substitutes it intoborder-radius, the declaration becomes invalid at computed-value time. The property then falls back to its inherited or initial value, which is not necessarily the component's default. The Styles pane may show a warning icon next to the declaration. - The component derives other values from the hook. Some components combine a hook with other values, for example a border width plus a color. Changing one hook may not change anything visible if another property controls the effect.
A quick way to test is to edit the value live in the Styles pane. Type a new value on your declaration. If the page updates, your code path works and the issue is the value you chose. If nothing updates, return to steps 1 to 3.
Step 5: Check scope and inheritance
Hooks inherit down the tree. That makes it easy to style a whole region, and it also creates confusing partial results. Consider this layout.
If only some buttons change, inspect one that did not. Check whether it sits inside the element that carries the hook. A button rendered in a modal, a popover, or a different component's template may not be a descendant of your region, even if it appears next to it on screen. Overlays are often attached elsewhere in the DOM, so an ancestor rule in your component never reaches them.
To check, select the unchanged button in Elements and look at the Computed tab for your hook name. If the property is absent, no ancestor sets it. Walk up the tree in Elements to find where the chain breaks.
Step 6: Rule out release and caching differences
If the hook works in one environment and not another, compare the SLDS release. A hook may exist in a newer release and be absent in an older one. After editing component CSS, do a hard reload to clear cached resources. In some development setups you also need to redeploy or refresh the preview so that the new CSS is served.
A worked debugging session
Suppose a developer writes this and sees no change.
.brand-action {
--slds-c-button-brand-background: #5a1ba9;
}Walking through the steps:
- Inspect the host.
.brand-actionappears in the Styles pane with the declaration, so the selector matches. - Nothing is crossed out, so the cascade is not the cause.
- Select the inner
button. The rule readsvar(--slds-c-button-brand-color-background, ...). The developer's name is missing thecolorgroup, so no element reads it. - Fix the name to
--slds-c-button-brand-color-background.
.brand-action {
--slds-c-button-brand-color-background: #5a1ba9;
}The button turns purple. Total time was a few minutes because each step eliminated one category of cause.
The same approach works for your own components. If a parent sets --c-badge-color-background and nothing happens, inspect the inner element and look for the var(--c-badge-color-background, #f3f3f3) call. If it is absent or spelled differently, the custom component is not reading the name the parent set.
:host {
--c-badge-color-background: #eef4ff;
}
.badge {
background-color: var(--c-badge-color-background, #f3f3f3);
padding: 0.25rem 0.5rem;
border-radius: 0.25rem;
}One more gotcha applies here. A :host declaration sets the default on the host element. A parent's rule that targets the host from outside the component competes with that :host rule, and the outside rule wins in the cascade for a normal declaration. If a parent's value is ignored, check whether the parent's selector actually matches the host element in the parent's template.
Debugging checklist
Use this list in order. Stop at the first failure.
- The element in Elements has the class or ancestor your selector expects.
- The rule appears in the Styles pane and is not crossed out.
- The name matches a
var()on an inner element, character for character. - The resolved value is valid for the property that reads it.
- The target element is a descendant of the element carrying the hook.
- The SLDS release in the org includes the hook.
Exercise
A developer sets --slds-c-button-brand-color-background: #5a1ba9; on .brand-action. In Chrome DevTools the declaration appears in the Styles pane and is not crossed out, but the button color does not change. List the next two checks you would do, and what each result would tell you.
Show answer
First, select the inner button inside the lightning-button shadow root and look in the Styles pane for a var(--slds-c-button-brand-color-background, ...) call. If it is missing or spelled differently, the component does not read that name, so the hook is wrong for this variant or release. Second, filter the Computed tab for --slds-c-button on the inner element. If the property is absent, it is not inheriting to the inner element, which means the declaration is on the wrong element or the button is not a descendant. If the property is present and the var() call matches, check that the resolved value is valid for background-color.
Exercise
Two buttons sit in the same template. Only one picks up --slds-c-button-radius-border: 0.25rem; set on .compact-region. What do you check first, and why can a button that looks adjacent still be unaffected?
Show answer
Inspect the unchanged button in Elements and walk up its ancestors to see whether .compact-region is among them. Hooks inherit only down the DOM tree. A button that appears next to the region on screen may be outside it in the DOM, or may be rendered in an overlay such as a modal that is attached elsewhere. If the Computed tab shows no value for the hook on that button, no ancestor sets it.