@salesforce-ux/sds-styling-hooks
About
Global Styling hooks use CSS custom properties which make it easy to customize application-wide styling.
Table of Contents
Relevant links (external)
- Non-normative Specifications
- Styling Hooks Documentation
Installation
Use npm to install the SDS Global Styling Hooks:
npm install --save-dev @salesforce-ux/sds-styling-hooks
Add SDS Global Hooks in HTML
You can use the HTML <link>
to link to SDS Global Styling Hooks as an external resource.
<html>
<head>
<link rel="stylesheet" href="/node_modules/@salesforce-ux/sds-styling-hooks/dist/hooks.custom-props.css">
<!-- Your application's stylesheets go below -->
<link rel="stylesheet" href="...">
</head>
<body>
<!-- Your application -->
</body>
</html>
Add SDS Global Hooks in CSS
You can use CSS @imports
to pull in SDS Global Styling Hooks.
:root {
@import "@salesforce-ux/sds-styling-hooks/dist/hooks.custom-props.css";
}
body {
/* Directly reference SDS Styling Hook */
background: var(--sds-g-color-neutral-base-1);
}
File Formats
SDS Styling Hooks have 6 different file formats for consumption. All files are located in /dist/
.
*.json
)
JSON ({
"PROP_NAME": "PROP_VALUE"
}
*.raw.json
)
Raw JSON ({
"props": {
"PROP_NAME": {
"value": "PROP_VALUE",
"type": "PROP_TYPE",
"category": "PROP_CATEGORY",
/* additional metadata */
}
}
}
*.js
)
Common JS (module.exports = {
propName: "PROP_VALUE"
};
*.js
)
ES Module (export const propName = "PROP_VALUE";
:root
(*.css
)
Custom Properties scoped to :root {
--prop-name: PROP_VALUE;
}
CSS Properties and Values API (*.js
)
Custom Property Registration using Experimental
This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
For more information, see CSS Properties and Values API Level 1.
if (CSS.registerProperty) {
CSS.registerProperty({
name: "--prop-name",
syntax: "PROP_SYNTAX",
inherits: false,
initialValue: "PROP_VALUE",
});
}
Usage Guidelines
Color
The Salesforce Design System comes with a robust color system and relationships to ensure our UIs meet WCAG 2.1 contrast ratios of 4.5:1. The semantic relationships allow for easy theme switching to handle features such as dark mode or high contrast mode.
Additionally, we generate sets of vibrant color palettes to use as accents in your designs. These values are generated in the HCL color space, which are designed to accord with human perception of color.
Using Color Relationships for UIs
The Salesforce Design System offers a set of neutral and brand color relationships. These two sets are intended to be used on your UI elements to define properties such as backgrounds, borders and text.
Each set, neutral and brand, have two groupings called base and inverse. The base group defaults to "light" UI elements. The inverse color group are the opposite values of the base group, these default to "dark" UI elements.
Neutral
--sds-g-color-neutral-base-1
--sds-g-color-neutral-base-2
--sds-g-color-neutral-base-3
--sds-g-color-neutral-base-4
--sds-g-color-neutral-base-contrast-1
--sds-g-color-neutral-base-contrast-2
--sds-g-color-neutral-base-contrast-3
--sds-g-color-neutral-base-contrast-4
Brand
--sds-g-color-brand-base-1
--sds-g-color-brand-base-2
--sds-g-color-brand-base-3
--sds-g-color-brand-base-4
--sds-g-color-brand-base-contrast-1
--sds-g-color-brand-base-contrast-2
--sds-g-color-brand-base-contrast-3
--sds-g-color-brand-base-contrast-4
Each group contains 8 color values; 4 of which are the default color values for that group and 4 are the contrast variants. These hold the color relationships in the set.
Golden Rule for Relationships
Regardless of the set, neutral or brand, there is a golden rule to ensure the color relationships are retained;
- When you use a base or inverse value as your background color, a contrast value in that group is required to be used on top of that background, i.e., if
base-2
is the background, thenbase-contrast-2
is your text color. Ifinverse-1
is the background, theninverse-contrast-1
is your text color. - The second requirement is to use the correct number value. You can only use a contrast value where the number is equal to or greater than the background color number value, i.e. if
base-2
is the background, thenbase-contrast-2
,base-contrast-3
,base-contrast-4
as your text will always ensure a 4.5:1 contrast ratio.
Generic Palettes
The Salesforce Design System provide generic color palettes to use in your designs. These values are generated in the HCL color space, which are designed to accord with human perception of color.
Important usage note
If you use a color from these palettes on a UI element, you are responsible to ensure proper contrast ratio between the background and text. Additionally, the color cannot be programmically alter if your application requires different themes such as dark mode or high contrast mode.
Typography
Font Family
The Salesforce Design System defaults to use system fonts, this improves performance and legibility on the content based on the device being used. We provide two styling hooks for font families, one of the body text content and one for the heading text of your document.
Body Text Content
--sds-g-font-family
Heading Text Content
--sds-s-heading-font-family
Font Stack
font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, 'Helvetica Neue', Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol';
Font Sizes
The Salesforce Design System offers a robust palette of font sizes. Our font sizes are independent of semantics so they are intended to be used for visual hierarchy. The sizes are based off a major second scale, 8:9 scale ratio with the base font size starting at 1rem or 16px. The scale offers 10 font sizes larger than the base and 4 font sizes smaller than the base.