Sub topic 12
When Hooks Are Not Enough
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 component hooks (--slds-c-*) to a base component such as lightning-button. Here you learn where hooks stop working and what to use instead.
What hooks can and cannot do
A styling hook is a CSS custom property that a base component reads inside its own shadow tree. Custom properties inherit across the shadow DOM boundary, so a value you set on the host element or an ancestor reaches the internal markup. That is the only channel hooks use.
This gives hooks a fixed boundary. A hook can change a value only if the component's authors chose to read a custom property at that spot. If they did not, no selector in your CSS file can reach it. Your styles cannot enter the shadow tree, and the base component's own styles cannot be edited.
The following table lists common styling needs and whether hooks cover them.
| Need | Hooks cover it? | Why |
|---|---|---|
| Change a button's background, border color or radius | Usually | The component publishes hooks for these properties |
| Change a color shared across many components | Yes | Global hooks (`--slds-g-*`) carry shared design decisions |
| Change padding on an internal element the component does not expose | No | No custom property is read at that spot |
| Hide an internal element, such as a label or icon | No | Hooks set values. They cannot change `display` unless one is published |
| Change the DOM structure or the order of elements | No | Hooks never change markup |
| Style a state with no published hook, such as a focus ring on one element | Sometimes | It depends on the component's hook list |
| Add spacing or alignment around a component | Not needed | Style the host element or a wrapper you own |
Check the hook list first
Before you decide that hooks are not enough, confirm that the hook is really missing. Many failures come from a hook that exists under a different name, or one that applies to a different state.
- Open the component's reference page in the Component Library and read its styling hooks section.
- Check the state you want. Brand, neutral and destructive buttons have different color hooks, and hover and focus states have their own hooks.
- Check whether a global hook already covers the decision. A brand color used across your whole app is better set once as a global hook.
- Inspect the element in browser developer tools. If your hook is set on the host but the computed value of the internal property does not change, the component does not read that hook there.
If the list has no matching hook, you have reached a real limit. Move on to the alternatives below.
Alternative 1: SLDS utility classes
SLDS provides utility classes for spacing, alignment, visibility, truncation and typography. These are plain classes such as slds-m-around_medium or slds-text-align_center. They work on elements in your own template, because your component's CSS and SLDS classes both apply to the elements you own.
<template>
<div class="slds-m-top_medium slds-grid slds-grid_align-end">
<lightning-button
class="brand-action"
variant="brand"
label="Save"
></lightning-button>
</div>
</template>The utility classes handle the layout around the button. The hook in brand-action handles the button's color. Neither one changes the button's internal markup.
Utility classes have limits too. They apply to the element that carries the class. Putting slds-m-left_small on a lightning-button affects the host element's margin, which is usually what you want. Putting a class on a base component does not pass it to elements inside its shadow tree.
Alternative 2: custom CSS on elements you own
Your component's CSS file applies to elements in your own template, including the host element of a base component. You can set layout properties on a host element with normal selectors.
.toolbar-button {
margin-left: 0.5rem;
display: inline-block;
width: 10rem;
}Use this for the box around a base component: margin, width, display, position, and align-self inside a flex or grid container. Do not use it for the inside of a base component. A selector such as .toolbar-button .slds-button does not match anything, because the .slds-button element sits inside a shadow tree that your stylesheet cannot reach.
/* This rule has no effect on the button's internal element. */
.toolbar-button .slds-button {
border-radius: 0;
}For elements you render yourself, plain CSS is the right tool. You can still use hooks there. A global hook such as --slds-g-color-brand-base-50 in a var() call keeps your own elements consistent with the rest of the theme.
Alternative 3: build the element yourself
When a base component cannot be styled enough, you can build the control from standard HTML elements and style it with SLDS classes and your own CSS. A button is a good example.
<template>
<button class="slds-button slds-button_brand pill-button" onclick={handleClick}>
{label}
</button>
</template>.pill-button {
border-radius: 999px;
letter-spacing: 0.04em;
}import { LightningElement, api } from 'lwc';
export default class PillButton extends LightningElement {
@api label = 'Save';
handleClick() {
this.dispatchEvent(new CustomEvent('save'));
}
}Because the button element is in your template, your CSS can reach it directly. The cost is real, though. You take on accessibility, keyboard behavior, disabled and loading states, and future SLDS changes. A base component handles these for you. Choose this route only after hooks, utilities and host-level CSS have all failed.
The next callout covers a common trap in this approach.
Alternative 4: a wrapper component with its own hooks
If you need the same customization in many places, wrap the base component in a component of your own and publish the hooks you need. This was covered in the chapter on exposing hooks with var() and fallbacks. It gives your team a stable interface even if the internals change.
:host {
--c-action-color-background: #5a1ba9;
--c-action-radius: 1rem;
}
lightning-button {
--slds-c-button-brand-color-background: var(--c-action-color-background);
--slds-c-button-brand-color-border: var(--c-action-color-background);
--slds-c-button-radius-border: var(--c-action-radius);
}Here the wrapper translates its own hooks into the base component's hooks. If the base component later renames a hook, you fix it in one file.
Choosing an approach
Work through these questions in order.
The "ask for a design change" branch is a legitimate outcome. A design that needs a one-off visual treatment in a base component may cost more to maintain than its value. Raising this early often saves effort.
Failure modes to recognize
- Setting a hook that the component does not read. The declaration is valid CSS, so nothing warns you. The page simply does not change.
- Using the wrong prefix. A hook beginning with
--slds-c-is tied to one component. A hook beginning with--slds-g-is global. Setting a global hook name on one button will not do what a component hook does. - Fighting the cascade with
!important. Custom properties inherit, so a closer declaration wins. Adding!importantrarely fixes a missing hook, and it makes later overrides harder. - Relying on rendered DOM. A selector that works in one release can stop working in the next.
- Piercing the shadow tree with tricks. Techniques that reach into other components' shadow trees are unsupported and fragile. They can also fail under stricter security settings.
- Forgetting that hooks affect only what the component reads. If a hook changes the border color but not the focus ring, the ring comes from a different property.
Maintainability trade-offs
Each alternative moves some cost to you.
| Approach | Cost you take on | Best for |
|---|---|---|
| Hooks | Low. Supported and documented | Colors, borders, radii, and similar values that the component lists |
| Utility classes | Low. Stable SLDS classes | Spacing, alignment, visibility |
| Host-level CSS | Low to medium | Layout of the component's box |
| Wrapper with your own hooks | Medium. One extra component | Reused customizations |
| Custom element | High. You own accessibility and behavior | Designs that base components cannot express |
Document any custom approach with a short comment that names the missing hook and the reason for the workaround. Revisit it when the component library updates, because a later release may add the hook you needed.
Exercise
A designer wants every lightning-button inside a settings panel to have a square corner and 1rem of space between the buttons. The component reference lists --slds-c-button-radius-border but no hook for the space between sibling buttons. Which approaches would you use for each part, and why would you not write .settings .slds-button { border-radius: 0; }?
Show answer
Set --slds-c-button-radius-border: 0 on the panel's class, such as .settings, so every button inside inherits it. Use a utility class such as slds-m-right_small, or a host-level rule such as lightning-button { margin-right: 1rem; }, for the spacing, because spacing is the box around the component and no hook exists for it. The selector .settings .slds-button fails because .slds-button lives inside the button's shadow tree, which your stylesheet cannot reach, so the rule matches nothing.
Exercise
You set --slds-c-button-brand-color-background on a lightning-button whose variant is neutral, and the color does not change. Give two possible causes and one way to check each.
Show answer
First, the hook belongs to the brand variant, and a neutral button reads a different hook. Check the hook list on the component's reference page for the neutral variant. Second, the component may not read that hook at all in the current release. Check by adding the property in the browser's Styles pane on the host element and watching whether the computed background inside the shadow tree changes.