Sub topic 11
Customizing appearance with styling hooks
This chapter assumes you can build a Lightning web component with an HTML template, a JavaScript class, and a CSS file. It also assumes you know what a base component such as lightning-button is. Here you will learn how to change the look of base components safely.
Why base components resist normal CSS
A base component renders its internals inside a shadow DOM. Your component's CSS cannot reach those internal elements. A rule like lightning-button button { background: red; } does nothing, because the inner button is hidden from your stylesheet.
Salesforce offers styling hooks for this. A styling hook is a CSS custom property (a variable that starts with --). The base component reads the property and applies its value to its own internal styles. Custom properties inherit across shadow boundaries, so a value you set from the outside reaches the inside.
Setting a hook
Set the hook on the base component's tag in your component's CSS file:
lightning-button {
--slds-c-button-brand-color-background: #0b5cab;
}The name follows a pattern: --slds-c- plus the component name, the part being styled, and the property. In this example, button-brand is the brand variant of the button, and color-background is its background color. Hooks that begin with --slds-c- are component hooks. They belong to one component. Hooks that begin with --slds-g- are global hooks, which cover shared values such as colors and spacing.
Applying a hook in a template
This template uses the brand button. The CSS above then changes its background:
<template>
<lightning-button
variant="brand"
label="Save"
onclick={handleSave}
></lightning-button>
</template>The hook changes only the brand variant's background. Other variants, such as neutral, keep their normal look. A hook is tied to a specific part of a specific component, so your change stays small.
Scoping a hook
You can limit a hook to part of your component by using a class selector:
.primary-actions lightning-button {
--slds-c-button-brand-color-background: #0b5cab;
--slds-c-button-brand-color-border: #0b5cab;
}Only buttons inside an element with the class primary-actions change. Buttons elsewhere in the template stay the same.
Finding the right hook
Each base component's documentation in the Component Library lists the styling hooks it supports. Check the page for your component before you write CSS. If a hook does not exist for the property you want, no hook-based option is available for it.
Common errors
- Misspelled hook name. CSS ignores unknown custom properties without warning. If nothing changes, compare the name with the documentation.
- Wrong variant. The hook
--slds-c-button-brand-color-backgroundhas no effect on aneutralbutton. - Setting the hook on the wrong element. Set it on the base component's tag or on one of its ancestors, not on an element inside the base component.
- Using deep selectors to reach internals. These selectors do not work in Lightning Web Components, and the internal markup can change between releases. Hooks are the supported route.
Summary
Styling hooks are the supported way to change a base component's look. Set the custom property on the component's tag, scope it with a selector if needed, and look up the exact name in the Component Library.
Exercise
A developer writes lightning-button button { background: #0b5cab; } in a component's CSS file, and the button does not change. Why, and what should they use instead?
Show answer
The inner button lives in the base component's shadow DOM, so the component's stylesheet cannot select it. They should set a styling hook on the lightning-button tag, such as --slds-c-button-brand-color-background: #0b5cab;, and use the brand variant.
Exercise
You set --slds-c-button-brand-color-background on a button with variant="neutral", and nothing changes. What is the likely cause?
Show answer
The hook applies to the brand variant only. The neutral variant uses different hooks, so look up the neutral button hooks in the Component Library.