Sub topic 4
Reading hook names
This chapter assumes you know CSS custom properties and var(). It also assumes you know why base components hide their internal CSS inside the shadow DOM, and that you have used lightning-button in a template. Here you learn to read a styling hook name and tell what it controls and where it applies.
Why names matter
Styling hooks are CSS custom properties, and their names follow a pattern. If you know the pattern, you can read a hook name and tell what it changes and how widely. You can also guess a name before you look it up. Treat guesses as leads only, because a hook works only if the component supports it.
The two prefixes
Every SLDS styling hook starts with --slds- followed by one letter that sets its scope.
| Prefix | Name | Scope |
|---|---|---|
| `--slds-c-` | Component hook | One base component, such as the button |
| `--slds-g-` | Global hook | A shared design decision used by many components |
The c stands for component and the g for global. Hooks that don't start with --slds- are not part of SLDS. Your own custom hooks use --c-, covered in a later chapter.
Anatomy of a component hook
Component hook names are built from parts, separated by hyphens:
.brand-action {
--slds-c-button-brand-color-background: #5a1ba9;
--slds-c-button-radius-border: 1rem;
}Read the first name left to right:
--slds-c-: a component hook.button: the component it belongs to.brand: the variant or state it applies to. This part is optional.color: the category of style.background: the property inside that category.
The second name, --slds-c-button-radius-border, has no variant part. It reads as component button, category radius, property border. It sets the corner radius of the button's border and applies to every variant of the button.
Common categories you will meet are color, radius, spacing, sizing, shadow and text. The property part then says which piece is affected, such as background, border or padding.
Variant and state parts
The optional middle part is where names differ the most. For lightning-button, the brand in --slds-c-button-brand-color-background limits the hook to buttons with variant="brand". The same property for the neutral variant has a different name, so a brand hook has no effect on a neutral button. States work the same way. A hook can be limited to a hover or focus state.
<template>
<lightning-button class="brand-action" variant="brand" label="Save"></lightning-button>
</template>If your button uses variant="brand" but you set a hook without brand in its name, check the documentation. You may be setting a hook that controls a different part of the component.
Global hooks
Global hook names drop the component part. They describe a design decision by category and a scale value:
:root {
--slds-g-color-brand-base-50: #5a1ba9;
--slds-g-spacing-4: 1rem;
}Here color is the category, brand-base-50 is a position on the brand color scale, and spacing-4 is a step on the spacing scale. Many components read these values, so changing one affects all of them. The exact set of global hooks changes between SLDS releases, so confirm names in the current documentation before you rely on them.
Names you can trust
- The prefix tells you the scope.
caffects one component.gcan affect many. - The names are not a guarantee. A hook you assemble from the pattern but that the component does not support does nothing, and the browser raises no error.
- Copy names from the documentation instead of typing them from memory. One misspelled word gives a hook that silently does nothing.
Exercise
Break the name --slds-c-button-brand-color-border into its parts. What does it control, and would it change a button with variant="neutral"?
Show answer
The parts are the component prefix --slds-c-, the component button, the variant brand, the category color and the property border. It sets the border color of brand buttons. It would not change a neutral button, because the brand part limits it to the brand variant.
Exercise
A teammate sets --slds-g-color-brand-base-50 and another sets --slds-c-button-brand-color-background in the same page. Which has the wider reach, and how can you tell from the name?
Show answer
The global hook has the wider reach. Its --slds-g- prefix marks it as a shared design decision that many components read. The component hook with the --slds-c- prefix names one component, button, so it affects only that component.