Sub topic 10
Exposing Your Own Hooks with var() and Fallbacks
This chapter assumes you know the basic structure of a Lightning web component (HTML template, JavaScript class, CSS file). It also assumes you know CSS selectors, the cascade, and inheritance. You should have seen how SLDS component hooks (--slds-c-*) are applied to base components.
Why expose hooks from your own component
SLDS base components let you change their look through styling hooks. You can give your own components the same ability. A custom hook is a CSS custom property that your component reads with var(). Any parent can set that property, and your component picks up the value.
This works for the same reason SLDS hooks work. Custom properties inherit, and inheritance passes through the shadow DOM boundary. A value set on the host element or on an ancestor is visible inside the shadow tree.
The pattern: var() with a fallback
The var() function takes a property name and an optional fallback value. If the property is not set anywhere above the element, the browser uses the fallback.
: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 example does two things:
- The
.badgerule reads--c-badge-color-backgroundthroughvar(). The second argument,#f3f3f3, is the fallback. - The
:hostrule sets a default value for the hook on the component's own host element.
Naming your hooks
Follow the SLDS naming shape so other developers can read your hooks the same way. The name is --c- for custom, then the component, then the property.
| Part | Example | Meaning |
|---|---|---|
| Prefix | `--c-` | Marks the hook as custom, not SLDS |
| Component | `badge` | The component that owns the hook |
| Property group | `color-background` | The styled property |
Do not use the --slds-c- or --slds-g- prefixes for your own hooks. Those belong to Salesforce, and a clash could cause confusing results.
Setting a hook from a parent
A parent component sets the hook on the child element or on any ancestor. The child's own :host default is the closest value, so a rule that targets the child itself wins over an ancestor.
.summary-region {
--c-badge-color-background: #d8f3dc;
}
.summary-region .warning-badge {
--c-badge-color-background: #ffe3e0;
}Here every badge inside .summary-region gets the green background, and the badge with class warning-badge gets the red one. This is the same scoping behavior you use with --slds-c-* hooks.
Default on :host or fallback in var()?
The two defaults behave differently.
- A default on
:hostis a normal rule in your component's stylesheet. Because it targets the host element itself, a value from an outer stylesheet that targets that element can override it. A value that only sits on an ancestor cannot, since the:hostdeclaration sets the property directly on the host and takes precedence over inherited values. - A fallback inside
var()only applies when the property is not set at all.
This has a real consequence. If you declare the default on :host, a parent that sets the hook on an ancestor region has no effect. The host's own declaration shadows the inherited value. To let ancestor-level overrides work, leave the hook off :host and rely on the var() fallback alone.
.badge {
background-color: var(--c-badge-color-background, #eef4ff);
}With this version, setting --c-badge-color-background on .summary-region reaches every badge inside it.
Practical guidelines
- Expose hooks for the properties consumers are likely to change, such as colors, spacing, and radius. Do not expose every property.
- Always provide a fallback, so the component renders correctly when no hook is set.
- Document each hook name and its default in the component's description, so consumers can find them.
- Prefer a stable name. Renaming a hook breaks every parent that sets it.
Exercise
A component's CSS has :host { --c-badge-color-background: #eef4ff; } and .badge { background-color: var(--c-badge-color-background, #f3f3f3); }. A parent sets --c-badge-color-background: #d8f3dc; on an ancestor div only. What background does the badge show, and how would you change the component so the ancestor's value is used?
Show answer
The badge shows #eef4ff. The :host declaration sets the property directly on the host element, so it wins over the value inherited from the ancestor. To let the ancestor value apply, remove the declaration from :host and keep the default as the var() fallback, for example var(--c-badge-color-background, #eef4ff).