Sub topic 10
Exposing your own hooks with var() and fallbacks
This chapter assumes you can build a basic Lightning web component (template, JavaScript class and CSS file). It also assumes you know the CSS cascade and inheritance, and have applied --slds-c-* hooks to base components such as lightning-button. Here you reverse the roles. Your component becomes the one that offers styling hooks to its consumers.
Why expose hooks at all
Styling hooks work because CSS custom properties inherit across the shadow DOM boundary. A base component reads --slds-c-button-radius-border internally, and you set it from outside. Your own components can use the same mechanism.
Suppose you build a status badge that is reused across a dozen pages. Without hooks, a consumer who wants a different background has two bad options. They can ask you to add a variant property for every color. Or they can reach into your shadow tree with CSS, which encapsulation blocks. A hook is a third option: a named value that you read inside the component and the consumer sets from outside.
A hook is a contract. The name, its meaning and its default value are part of your component's public interface, just like an @api property.
The building block: var() with a fallback
The var() function reads a custom property. Its second argument is a fallback, used when the property is not defined at that point in the cascade.
.badge {
background-color: var(--c-badge-color-background, #f3f3f3);
}If an ancestor or the host element sets --c-badge-color-background, the badge uses that value. Otherwise it uses #f3f3f3. The component renders correctly on its own and becomes customizable without any extra code.
Three details about the syntax matter:
- Everything after the first comma is the fallback.
var(--x, 0 0 4px rgba(0, 0, 0, 0.2))works because the commas inside the fallback belong to it. - A fallback can itself be a
var(), which lets you chain defaults. - A fallback applies only when the property is undefined (or the keyword
initial). It does not apply when the property is defined with an invalid value. That case is covered under the failure modes below.
A complete example
This is a status badge with three hooks: background, text color and corner radius. The template is minimal.
<template>
<span class="badge">{label}</span>
</template>import { LightningElement, api } from 'lwc';
export default class StatusBadge extends LightningElement {
@api label;
}The CSS reads each hook with a fallback.
.badge {
display: inline-block;
padding: 0.25rem 0.5rem;
background-color: var(--c-badge-color-background, #f3f3f3);
color: var(--c-badge-color-text, #181818);
border-radius: var(--c-badge-radius-border, 0.25rem);
}A parent component can now restyle one instance from its own CSS file. It targets the host element with a class.
<template>
<c-status-badge class="late" label="Overdue"></c-status-badge>
<c-status-badge label="On track"></c-status-badge>
</template>.late {
--c-badge-color-background: #ba0517;
--c-badge-color-text: #ffffff;
}The first badge turns red with white text. The second keeps the defaults. The parent never touched the badge's internal markup, and the badge never needed a new property for each color.
How the value reaches the inner element
The custom property is set on the host element, c-status-badge. Custom properties inherit by default, so the .badge span inside the shadow tree receives the same value. The inner var() call then resolves it.
The :host default and why it can get in the way
A common pattern is to declare the default on :host and read it in inner rules.
:host {
--c-badge-color-background: #eef4ff;
}
.badge {
background-color: var(--c-badge-color-background, #f3f3f3);
padding: 0.25rem 0.5rem;
border-radius: 0.25rem;
}This works for a consumer who targets the host element directly, as .late did above. Styles from the outer context normally take precedence over :host rules for the same property on the host.
It has a cost for region-level overrides. An ancestor's value reaches the badge only through inheritance. Inheritance applies only when the element has no value of its own. Once :host declares the property, the host element has its own value, and the ancestor's value is never inherited. A rule such as .compact-region { --c-badge-color-background: #014486; } on a parent container then has no effect on badges inside it.
If you want both instance-level and region-level control, skip the :host declaration. Put the default only in the var() fallback, as in the status badge example. The property stays undefined until a consumer defines it, so inheritance can do its work.
Chaining hooks and global hooks
Fallbacks can point at other hooks. This lets your component follow SLDS global hooks by default and still allow a local override.
.badge {
background-color: var(
--c-badge-color-background,
var(--slds-g-color-neutral-base-95, #f3f3f3)
);
}The browser resolves this in order:
- Your component hook, if a consumer set it.
- The global hook, if the surrounding theme defines it.
- The literal
#f3f3f3.
Components built this way follow the org's theme automatically and still support per-instance exceptions. Keep the final literal fallback. Global hooks may not be defined in every context, such as an older Experience Cloud site or a local preview.
Another pattern is a derived hook. You expose one hook that controls several properties.
.badge {
--_border: var(--c-badge-color-border, var(--c-badge-color-background, #f3f3f3));
background-color: var(--c-badge-color-background, #f3f3f3);
border: 1px solid var(--_border);
}The border follows the background unless the consumer sets a border color. The leading underscore is a convention for an internal variable that is not part of the contract. Because it is declared on .badge, inside the shadow tree, consumers cannot set it by accident.
Naming and documenting your hooks
The --slds-c-* and --slds-g-* prefixes belong to Salesforce. Do not use them for your own hooks. Defining a name in that namespace could collide with a future official hook. The examples here use --c- followed by the component name, then a property group, then a property: --c-badge-color-background. This mirrors the shape of SLDS names, so teams that know SLDS can read yours.
A few rules keep hooks usable:
- Name by purpose, not by implementation.
--c-badge-color-backgroundstays valid if you later change how the badge draws its background. - Expose a small set. Every hook is a commitment. Start with the properties that consumers genuinely vary, such as color, radius and spacing.
- Document each hook with its default and the property it affects. A short comment block at the top of the CSS file or a README entry is enough.
- Do not rename or remove a hook casually. Consumers will have set it in their own CSS, and a removed hook fails silently.
Failure modes
| Symptom | Likely cause | Fix |
|---|---|---|
| Region-level override has no effect | Default declared on `:host`, which blocks inheritance | Move the default into the `var()` fallback |
| Property looks unset, not defaulted | Consumer set the hook to an invalid value, such as `--c-badge-radius-border: red` | Fix the value. Fallbacks do not apply to defined but invalid values |
| Hook works in one place only | Consumer set it on a sibling or child, not on the host or an ancestor | Set it on the host element or an ancestor |
| Hook ignored entirely | Typo in the name, or the inner CSS reads a different name | Compare the names character by character in developer tools |
| Fallback never appears | An ancestor defines the same property with another meaning | Use a component-specific name |
When a custom property resolves to an invalid value for the property that uses it, the browser treats that declaration as if you had written unset. The declaration does not fall back to your var() default. This is why a wrong value can look more broken than a missing one.
Testing a hook in the browser
Open developer tools and select the host element, c-status-badge, in the Elements panel. In the Styles pane, add a declaration such as --c-badge-color-background: tomato. The badge should update immediately. If it does not, select the inner .badge span and look at the computed background-color. The pane shows which var() resolved and what it resolved to. This is the same technique you use when a Salesforce hook has no effect.
Exercise
A component's CSS contains :host { --c-card-color-border: #c9c9c9; } and .card { border: 1px solid var(--c-card-color-border, #c9c9c9); }. A page author sets --c-card-color-border: #014486; on a parent div that wraps several cards, but the cards keep the grey border. Why, and how do you change the component so the page author's rule works?
Show answer
The :host rule gives the host element its own value for the property, so the cards never inherit the value from the div. Remove the :host declaration and keep the default in the var() fallback. With the property undefined on the host, the value set on the parent div inherits into the shadow tree and var() uses it.
Exercise
Write the CSS for a .tag element that reads a hook --c-tag-color-background. If the hook is not set, it should use the global hook --slds-g-color-neutral-base-95. If neither is defined, it should use #f3f3f3.
Show answer
.tag { background-color: var(--c-tag-color-background, var(--slds-g-color-neutral-base-95, #f3f3f3)); }