Sub topic 2
Component Hooks for Individual Base Components
Assume you know Lightning web component templates, CSS selectors, inheritance, and base components such as lightning-button. This chapter focuses on changing a supported base component without replacing its internal CSS.
Component hooks and global hooks
A styling hook is a CSS custom property that a component reads with var(). Custom properties inherit through the shadow DOM boundary, so you can set a value on the base component's host element.
Component hooks use the --slds-c- prefix. They control one component type or one component part, such as a button's brand background. Global hooks use the --slds-g- prefix. They represent shared design decisions, such as a brand color used across many components.
Use a component hook when the change belongs to one base component. Use a global hook when the same design value should apply consistently across supported components.
Finding supported hooks
Hooks are an API, not arbitrary names. Before using one, check the component's Salesforce documentation and the SLDS styling hooks reference. Confirm the hook applies to the component variant you use, such as brand or neutral. A similarly named property may have no effect if the component does not support it.
Applying hooks in component CSS
Set the hook on the base component in your template, then assign it in the custom component's CSS file:
<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;
--slds-c-button-radius-border: 1rem;
}The selector targets the host element. The button's internal styles consume the custom properties inside its shadow tree. You do not need, and should not try, to select the button's internal <button> element.
Exercise
Which prefix should you investigate for a value that changes only one button component?
Show answer
Use a component hook with the --slds-c- prefix, after confirming that the button documentation supports it.