Sub topic 6
Global Hooks for Shared Design Decisions
This chapter assumes you know the basics of a Lightning web component (HTML template, JavaScript class, CSS file), CSS selectors and inheritance, and the earlier chapters on what styling hooks are and how their names are built. Here you will see how global hooks (--slds-g-*) differ from component hooks (--slds-c-*), and when to use each.
What a global hook is
A global hook is a CSS custom property that stores one design decision, such as a brand color, a spacing value, or a font size. Many components read these values. Change the value once and every component that reads it changes with it.
Component hooks work the other way. A hook such as --slds-c-button-radius-border affects only buttons. A global hook sits one level up and feeds many components.
| Aspect | Component hook | Global hook |
|---|---|---|
| Prefix | `--slds-c-` | `--slds-g-` |
| Reach | One base component | Many components |
| Typical use | Restyle one button or card | Set a brand color or spacing scale |
| Example | `--slds-c-button-radius-border` | `--slds-g-color-brand-base-50` |
How global hooks reach components
Global hooks are custom properties, so they inherit down the DOM tree and into shadow trees. SLDS defines default values for them. Base components then use those values, directly or through their own component hooks, to build their styles. When you set a global hook on a host element or an ancestor, you replace the default for everything inside it.
The dashed line shows the precedence rule. A component hook set for one component is more specific than the global value it would otherwise inherit, so it wins for that component.
Applying a global hook
Set a global hook in your component's CSS file, on the element whose subtree you want to change. The following example changes the brand color for a region containing a button and a card.
<template>
<div class="themed-region">
<lightning-card title="Account summary">
<lightning-button
variant="brand"
label="Save"
></lightning-button>
</lightning-card>
</div>
</template>.themed-region {
--slds-g-color-brand-base-50: #5a1ba9;
--slds-g-spacing-4: 1.25rem;
}Every base component inside .themed-region that reads these values now uses the new brand color and spacing. You did not need to know which components were involved.
A one-off exception still works. If one button must keep a different background, set the component hook on that button, as in the earlier chapter on scoping.
.themed-region .keep-original {
--slds-c-button-brand-color-background: #0176d3;
}Choosing between global and component hooks
Use a global hook when the change is a shared design decision. A brand color, a base font size, and a spacing step all fit. Use a component hook when only one kind of component should change.
Keep these points in mind:
- A global hook changes more than you may expect. Check the whole region after you change one.
- Not every visual property has a global hook. If the documentation lists none for your need, use a component hook.
- Global hook names and available values depend on the SLDS version in your org. Confirm names in the documentation instead of copying them from old examples.
Common errors
- Setting a global hook in a place that does not contain the target. Custom properties inherit downward only. A hook on a sibling element has no effect.
- Misspelled names. The browser ignores an unknown custom property without an error. The component then keeps its default look.
- Expecting a global hook to override a component hook. A component hook set on the component wins over the global value.
Exercise
A page region sets --slds-g-color-brand-base-50: #5a1ba9;. One brand button inside it also sets --slds-c-button-brand-color-background: #014486; through its own class. Which background color does that button use, and why?
Show answer
It uses #014486. The component hook is set on the button's own class and is more specific for that component than the inherited global value, so it takes precedence for that button. Other brand components in the region still use #5a1ba9.
Exercise
You want every brand-colored element inside one card region to use a new color, without naming each base component. Which kind of hook do you set, and where?
Show answer
Set a global hook, such as --slds-g-color-brand-base-50, on the element that wraps the region, for example a div with a class in your component's CSS file. Inheritance carries the value to every base component inside.