Sub topic 13
Keeping overrides maintainable
This chapter covers the habits that keep styling hook overrides readable and safe to change as a project grows. It assumes you can already apply component hooks (--slds-c-*) in a component's CSS file, scope them with selectors, and read the hook documentation for a base component. It also assumes you know basic CSS inheritance and the cascade.
Why maintainability matters for hooks
A single override such as --slds-c-button-radius-border: 1rem; is easy to understand. A project with 80 components and several hundred hook assignments is a different matter. Common problems appear:
- The same hex color is repeated in many files, so a brand change means a search and replace.
- Nobody knows why a hook was set, so nobody dares to delete it.
- A region-level override silently changes buttons that were never meant to change.
- A hook name is mistyped, and the browser ignores it without any error.
Custom properties fail quietly. An unknown name is just another custom property that nothing reads. For this reason, maintainability for hooks is mostly about making mistakes visible and keeping decisions in one place.
Prefer the highest-level hook that expresses your intent
SLDS offers two layers. Global hooks (--slds-g-*) describe shared design decisions, such as a brand color or a spacing step. Component hooks (--slds-c-*) describe one part of one component, such as the background of a brand button.
Choose the layer by what you intend:
| Intent | Better choice | Reason |
|---|---|---|
| "Our brand color is purple everywhere" | Global hook | One change reaches every component that reads it |
| "Only the save button in this form is purple" | Component hook on one instance | The change stays local |
| "Every button in this panel is more compact" | Component hook on the region | One declaration covers the panel |
If you set a component hook for every button in the application, you are probably expressing a global decision at the wrong layer. Move it to a global hook if one exists for that decision, or to a shared definition, described next.
Name your decisions once
Hex values scattered across files are the main source of drift. Define each decision once, then refer to it with var(). A custom property defined on :host or on a wrapper element is a good place for this.
:host {
--c-brand-primary: #5a1ba9;
--c-brand-primary-dark: #3f1277;
--c-radius-control: 0.5rem;
}
.brand-action {
--slds-c-button-brand-color-background: var(--c-brand-primary);
--slds-c-button-brand-color-border: var(--c-brand-primary);
--slds-c-button-radius-border: var(--c-radius-control);
}
.brand-action:hover {
--slds-c-button-brand-color-background-hover: var(--c-brand-primary-dark);
}Two points make this work. First, --c-* is a project prefix. It is not an SLDS name, so it never collides with a hook that Salesforce defines. Second, the var() calls resolve where the property is used, so a parent that changes --c-brand-primary changes every dependent hook below it.
Check the hover hook name against the documentation for the component you are styling. Hook sets differ between components and between releases, and this chapter's example uses a name for illustration.
Keep the vocabulary small
A short list of project variables is easier to keep consistent than a long one. A reasonable starting set is:
- One or two brand colors and their darker variants.
- One radius for controls and one for containers.
- Two or three spacing steps, if your design needs more than SLDS spacing utilities provide.
Resist creating a variable for every value. A variable used once adds a layer of indirection with no benefit.
Put overrides in predictable places
Decide where each kind of override lives, and stay with that decision.
- Instance overrides live in the CSS file of the component that owns the element. They use a class on the element, as in
.brand-action. - Region overrides live in the CSS file of the component that owns the region wrapper. They use a class such as
.compact-region. - Application-wide overrides live in one dedicated place, such as a theming component or a single stylesheet, and use global hooks where possible.
The cascade decides which value wins, so specificity matters. A region rule followed by an instance rule inside it is a clear, intended pattern:
.compact-region {
--slds-c-button-radius-border: 0.25rem;
}
.compact-region .brand-action {
--slds-c-button-brand-color-background: var(--c-brand-primary-dark);
}Avoid fighting specificity with !important on a custom property. It usually means the override is in the wrong place. Move the rule closer to the element, or give the element a class that makes the intent explicit.
Use class names that describe intent
A class such as .blue-button becomes false the day the brand color changes. Name classes by role instead.
- Prefer
.primary-action,.destructive-action,.compact-region. - Avoid
.purple,.round,.big-margin.
Role names let a designer change the look in one rule without renaming markup in many templates.
Document the reason, not the value
The CSS already states the value. A comment should explain why the override exists, so a later reader can decide whether it is still needed.
/* Brand guideline 4.2: primary actions use the brand purple. */
.primary-action {
--slds-c-button-brand-color-background: var(--c-brand-primary);
}
/* Narrow side panel: tighter corners avoid clipping at 20rem width. */
.side-panel {
--slds-c-button-radius-border: 0.25rem;
}If you cannot write a reason, that is a sign to reconsider the override.
Make mistakes visible
Because a misspelled hook does nothing, build checks that catch it.
- Review against documentation. When a pull request adds a hook, compare the name with the component's documented hooks.
- Search for stale names. After a Salesforce release, search the code base for
--slds-c-and confirm each name still appears in the documentation. Hooks can be added, and names can change between releases. - Use a stylelint rule where available. Salesforce provides linting for SLDS in some tooling. If your project uses it, enable the rules that report unknown or deprecated hooks. Confirm the current rule names in the Salesforce documentation, because they can change.
- Inspect the result. In browser developer tools, select the element and confirm the computed value of the property you set. The debugging chapter covers this in detail.
Give custom components stable hooks
If you expose hooks from your own components, treat the names as a public interface.
- Use a project prefix, such as
--c-badge-color-background, and never the--slds-prefix, which belongs to Salesforce. - Always supply a fallback in
var(), so the component works when no parent sets the hook. - Rename a hook only with a plan, because every parent that sets the old name will silently stop working.
- List the supported hooks in the component's documentation or in a comment at the top of its CSS file.
/*
* Hooks supported by c-status-badge:
* --c-badge-color-background (default #f3f3f3)
* --c-badge-color-text (default #181818)
* --c-badge-radius (default 0.25rem)
*/
.badge {
background-color: var(--c-badge-color-background, #f3f3f3);
color: var(--c-badge-color-text, #181818);
border-radius: var(--c-badge-radius, 0.25rem);
padding: 0.25rem 0.5rem;
}Keep the hook list short. Each hook is a commitment, and a component with twenty hooks is hard to test.
Know when to stop using hooks
Hooks only change what a base component chooses to expose. When a design needs something a hook does not cover, such as a different layout inside the component, resist the urge to reach into the shadow tree with unsupported selectors. These approaches break when internal markup changes.
A practical order of preference:
- A documented hook, global if the decision is shared, component if it is local.
- An SLDS utility class on your own markup, such as spacing or alignment.
- Custom CSS on your own elements.
- A custom component that replaces the base component, when the design truly differs.
Record any case where you chose option 3 or 4 over a hook, and say why. When Salesforce later adds a hook that covers the need, you can find and remove the workaround.
A review checklist
Use these questions when reviewing a styling change.
- Is each hook name present in the current documentation?
- Is the decision global or local, and is the hook at the matching layer?
- Does any hex value or length appear in more than one place?
- Is the selector as narrow as the intent requires?
- Does a comment explain why the override exists?
- Was the result checked in browser developer tools?
How the pieces fit together
Values flow from the shared definitions to the narrowest rule, and the base component reads the final result. A change at the top reaches everything below it, while a change at the bottom affects one element.
Exercise
A teammate sets --slds-c-button-brand-color-background: #5a1ba9; in 14 different component CSS files. The brand color is about to change. Describe how you would restructure the CSS, and name one check you would run afterward.
Show answer
Define the color once as a project variable, for example --c-brand-primary: #5a1ba9;, in a single theming location. Replace the 14 hex values with var(--c-brand-primary). If a global hook covers the brand color, set that instead and remove the component-level overrides. Afterward, inspect a button in browser developer tools and confirm the computed background matches the new color. Also search the code base for the old hex value to confirm none remain.
Exercise
You add --c-badge-color-bg to a parent's CSS to restyle a custom badge, but nothing changes. The badge reads --c-badge-color-background. What went wrong, and what two practices would have prevented it?
Show answer
The names differ, so the badge never reads the parent's value, and the browser reports no error. Documenting the supported hooks in the component, and checking the computed value in developer tools, would each have caught it. Reviewing the name against the documentation also works.