AgentSkillsCN

slds2

Salesforce Lightning Design System (SLDS 2)平台参考——设计令牌、网格、实用类、组件蓝图、图标、无障碍设计,以及LWC/Aura的使用模式。

SKILL.md
--- frontmatter
name: slds2
description: Salesforce Lightning Design System (SLDS 2) platform reference — design tokens, grid, utility classes, component blueprints, icons, accessibility, and LWC/Aura usage patterns.
when_to_use: When building or modifying Salesforce Lightning UI components, applying SLDS classes, using design tokens, or referencing component blueprints and accessibility patterns.
version: "62.0"

SLDS 2 — Salesforce Lightning Design System Reference

1. Design Tokens

Design tokens are CSS custom properties following the var(--slds-g-*) naming pattern. They provide a single source of truth for color, spacing, typography, elevation, and shape values across the system.

Usage: color: var(--slds-g-color-brand-1); or padding: var(--slds-g-spacing-4);

TokenValue / PurposeExample
--slds-g-color-brand-1Primary brand color (Salesforce blue)color: var(--slds-g-color-brand-1);
--slds-g-color-error-1Error / destructive stateborder-color: var(--slds-g-color-error-1);
--slds-g-color-success-1Success / positive statebackground: var(--slds-g-color-success-1);
--slds-g-color-warning-1Warning / caution statecolor: var(--slds-g-color-warning-1);
--slds-g-font-size-10.625rem (10px) — smallestfont-size: var(--slds-g-font-size-1);
--slds-g-font-size-20.75rem (12px)font-size: var(--slds-g-font-size-2);
--slds-g-font-size-30.8125rem (13px)font-size: var(--slds-g-font-size-3);
--slds-g-font-size-40.875rem (14px) — body defaultfont-size: var(--slds-g-font-size-4);
--slds-g-font-size-51rem (16px)font-size: var(--slds-g-font-size-5);
--slds-g-font-size-61.125rem (18px)font-size: var(--slds-g-font-size-6);
--slds-g-font-size-71.25rem (20px) — large headingfont-size: var(--slds-g-font-size-7);
--slds-g-spacing-10.25rem (4px)margin: var(--slds-g-spacing-1);
--slds-g-spacing-20.5rem (8px)padding: var(--slds-g-spacing-2);
--slds-g-spacing-30.75rem (12px)gap: var(--slds-g-spacing-3);
--slds-g-spacing-41rem (16px) — base unitmargin-bottom: var(--slds-g-spacing-4);
--slds-g-spacing-51.25rem (20px)padding: var(--slds-g-spacing-5);
--slds-g-spacing-61.5rem (24px)gap: var(--slds-g-spacing-6);
--slds-g-spacing-72rem (32px)margin-top: var(--slds-g-spacing-7);
--slds-g-radius-border-1Standard border radius (0.25rem)border-radius: var(--slds-g-radius-border-1);
--slds-g-shadow-1Subtle elevation (cards, containers)box-shadow: var(--slds-g-shadow-1);
--slds-g-shadow-2Higher elevation (modals, popovers)box-shadow: var(--slds-g-shadow-2);

2. Grid System

The SLDS grid uses flexbox. Container: slds-grid. Columns: slds-col.

Responsive sizing classes: slds-size_X-of-Y where Y is 1–12.

ClassEffect
slds-gridFlex container (horizontal by default)
slds-colFlex child column
slds-size_1-of-250% width
slds-size_1-of-333.33% width
slds-size_2-of-366.67% width
slds-size_1-of-425% width
slds-size_3-of-475% width
slds-size_1-of-616.67% width
slds-size_1-of-128.33% width
slds-wrapAllow columns to wrap to next row
slds-grid_verticalStack children vertically
slds-grid_align-centerCenter-align children on main axis
slds-grid_align-spreadSpace children evenly (justify: space-between)
slds-grid_align-endAlign children to end of main axis
slds-grid_vertical-align-centerCenter-align children on cross axis
slds-grid_pull-paddedNegate padding on grid edges

Responsive breakpoint prefixes: slds-small-size_*, slds-medium-size_*, slds-large-size_*.

Code Example

html
<div class="slds-grid slds-wrap slds-grid_align-spread">
  <div class="slds-col slds-size_1-of-2 slds-medium-size_1-of-3">
    <!-- Column 1 -->
  </div>
  <div class="slds-col slds-size_1-of-2 slds-medium-size_1-of-3">
    <!-- Column 2 -->
  </div>
  <div class="slds-col slds-size_1-of-1 slds-medium-size_1-of-3">
    <!-- Column 3 -->
  </div>
</div>

3. Utility Classes by Category

Spacing

Pattern: slds-m-{direction}_{size} (margin) and slds-p-{direction}_{size} (padding).

DirectionApplies To
topTop only
rightRight only
bottomBottom only
leftLeft only
horizontalLeft + Right
verticalTop + Bottom
aroundAll four sides
SizeValue
xxx-small0.125rem (2px)
xx-small0.25rem (4px)
x-small0.5rem (8px)
small0.75rem (12px)
medium1rem (16px)
large1.5rem (24px)
x-large2rem (32px)
xx-large3rem (48px)

Examples: slds-m-top_medium, slds-p-around_small, slds-m-horizontal_large, slds-p-vertical_x-small.

Typography

ClassEffect
slds-text-heading_largeLarge heading (1.75rem, bold)
slds-text-heading_mediumMedium heading (1.25rem, bold)
slds-text-heading_smallSmall heading (1rem, bold)
slds-text-body_regularBody text (0.8125rem)
slds-text-body_smallSmall body text (0.75rem)
slds-text-titleSection title — uppercase, letter-spaced
slds-text-title_capsAll-caps title
slds-text-align_centerCenter-aligned text
slds-text-align_rightRight-aligned text
slds-text-align_leftLeft-aligned text (default)
slds-text-color_defaultDefault text color
slds-text-color_weakMuted / secondary text color
slds-text-color_errorError text color
slds-text-color_successSuccess text color
slds-text-color_inverseInverse (light on dark) text

Alignment / Display

ClassEffect
slds-showDisplay block (visible)
slds-hideDisplay none (hidden)
slds-hiddenVisibility hidden (reserves space)
slds-visibleVisibility visible
slds-is-relativePosition relative
slds-is-absolutePosition absolute
slds-is-fixedPosition fixed
slds-truncateSingle-line text truncation with ellipsis
slds-has-flexi-truncateAllow flex child truncation
slds-assistive-textVisually hidden but screen-reader accessible
slds-float_leftFloat left
slds-float_rightFloat right
slds-float_noneClear floats

4. Component Blueprints

ComponentCSS ClassKey VariantsNotes
Cardslds-cardslds-card_boundaryHeader, body, footer sections. Wraps in slds-card__header, slds-card__body, slds-card__footer.
Modalslds-modalslds-modal_large, slds-modal_mediumRequires slds-backdrop overlay. Sections: slds-modal__header, slds-modal__content, slds-modal__footer.
Popoverslds-popoverslds-popover_error, slds-popover_warning, slds-popover_walkthroughNubbin positioning: slds-nubbin_top, slds-nubbin_bottom, slds-nubbin_left, slds-nubbin_right.
Data Tableslds-tableslds-table_bordered, slds-table_striped, slds-table_col-bordered, slds-table_fixed-layoutUse slds-is-sorted on column headers. Wrap in slds-table_header-fixed for fixed headers.
Buttonslds-buttonslds-button_neutral, slds-button_brand, slds-button_destructive, slds-button_outline-brand, slds-button_success, slds-button_iconUse slds-button_stretch for full-width. Disabled via disabled attribute.
Button Groupslds-button-groupslds-button-group-row, slds-button-group-listGroup related actions. Last button uses slds-button_last.
Tabs (Default)slds-tabs_defaultslds-tabs_scopedActive tab: slds-is-active. Panels: slds-tabs_default__content, toggled via slds-show/slds-hide.
Pillslds-pillslds-pill_link, slds-pill_bareContainer: slds-pill_container or slds-listbox_selection-group. Remove button inside.
Badgeslds-badgeslds-badge_inverse, slds-badge_lightestInline status indicator. Often inside slds-badge__icon for icon+text.
Formslds-formslds-form_stacked, slds-form_horizontal, slds-form_compound, slds-form_inlineWrap fields in slds-form-element. Label: slds-form-element__label. Control: slds-form-element__control.
Inputslds-inputWrap in slds-form-element__control. Pair with slds-form-element__label. Error: add slds-has-error to slds-form-element.
Breadcrumbslds-breadcrumbOrdered list (<ol>). Items: slds-breadcrumb__item. Use <a> tags inside.
Page Headerslds-page-headerslds-page-header_record-home, slds-page-header_related-listSections: slds-page-header__title, slds-page-header__meta-text, slds-page-header__control.
Spinnerslds-spinnerslds-spinner_brand, slds-spinner_inverseSizes: slds-spinner_xx-small, slds-spinner_x-small, slds-spinner_small, slds-spinner_medium, slds-spinner_large. Wrap in slds-spinner_container.
Toastslds-notifyslds-notify_toastThemes: slds-theme_success, slds-theme_error, slds-theme_warning, slds-theme_info. Container: slds-notify_container.

5. Icons

SLDS icons are organized into five categories. In LWC, use the lightning-icon base component.

CategoryPrefixExample NamesDescription
Standardstandard:account, contact, opportunity, lead, case, task, event, campaignObject icons — colored circular backgrounds
Utilityutility:search, close, add, edit, delete, check, warning, info, chevrondown, chevronright, download, refreshUI action icons — no background
Customcustom:custom1 through custom113Custom object icons — numbered
Doctypedoctype:pdf, excel, word, ppt, csv, image, zip, xml, htmlFile type icons
Actionaction:new_task, log_a_call, new_note, email, share, approvalQuick action icons — colored circular backgrounds

LWC Icon Syntax

html
<!-- Standard icon -->
<lightning-icon icon-name="standard:account" size="small"></lightning-icon>

<!-- Utility icon -->
<lightning-icon icon-name="utility:search"></lightning-icon>

<!-- With alternative text for accessibility -->
<lightning-icon icon-name="utility:warning" alternative-text="Warning" variant="warning"></lightning-icon>

Icon sizes: xx-small, x-small, small, medium (default), large.

Icon variants (utility): default, inverse, warning, error, success.


6. Accessibility Patterns

Required ARIA Attributes

AttributeWhen to UseExample
aria-labelInteractive element with no visible text label<button aria-label="Close dialog">X</button>
aria-labelledbyElement labeled by another element's text<div aria-labelledby="heading-id">
aria-describedbyAdditional descriptive text (help text, errors)<input aria-describedby="error-msg-id">
aria-hidden="true"Decorative elements to hide from screen readers<span aria-hidden="true" class="slds-icon">
aria-liveDynamic content that updates without page reload<div aria-live="polite"> for non-urgent; "assertive" for urgent
aria-expandedCollapsible sections, dropdowns, menus<button aria-expanded="false" aria-controls="panel-id">
aria-selectedSelected tab, option, or row<li role="tab" aria-selected="true">
aria-currentCurrent item in a set (breadcrumbs, nav)<a aria-current="page">
roleDefine widget semantics when HTML element is insufficientrole="dialog", role="tablist", role="alert", role="status"

Keyboard Navigation

PatternImplementation
Focus managementMove focus into modals on open, return focus on close. Use tabindex="-1" to make non-interactive elements programmatically focusable.
Tab orderUse tabindex="0" for custom interactive elements. Avoid tabindex > 0.
Escape keyClose modals, popovers, dropdowns on Escape keypress.
Arrow keysNavigate within composite widgets (tabs, menus, listboxes).
Focus trapModals must trap focus — Tab/Shift+Tab cycle within the modal.
Skip linksProvide skip navigation link at top of page for keyboard users.

Color Contrast Requirements

Text TypeMinimum Contrast Ratio
Normal text (< 18px or < 14px bold)4.5:1
Large text (>= 18px or >= 14px bold)3:1
UI components and graphical objects3:1
Non-text contrast (icons, borders)3:1

Never rely on color alone to convey meaning — always pair with text, icons, or patterns.


7. SLDS in LWC vs Aura

AspectLWCAura
SLDS loadingAuto-loaded in Lightning Experience; base components include stylesAuto-loaded in Lightning Experience
Base components<lightning-button>, <lightning-input>, <lightning-card>, etc.<lightning:button>, <lightning:input>, <lightning:card>, etc.
Custom CSSShadow DOM scoped — styles don't leak in/out. Write in component .css file.Styles can bleed; use component bundle .css file.
Design tokens in CSS@import not needed in LWC — use var(--slds-g-*) directly in .cssUse var(--lwc-*) or t() function in design tokens
Applying SLDS classesUse class="slds-*" in template HTMLUse class="slds-*" in component markup
Dynamic classesCompute in JS getter: get myClass() { return condition ? 'slds-show' : 'slds-hide'; }Use <aura:if> or expression syntax {!v.condition ? 'slds-show' : 'slds-hide'}
Icon usage<lightning-icon icon-name="utility:close"><lightning:icon iconName="utility:close">
ScopingShadow DOM encapsulation (synthetic or native)No shadow DOM — global CSS scope within namespace
Static resourcesNot typically needed for SLDSCan load SLDS as static resource for Visualforce embedding
Component namingkebab-case in HTML: <c-my-component>camelCase in markup: <c:myComponent>

LWC Token Usage Example

css
/* myComponent.css */
.container {
  padding: var(--slds-g-spacing-4);
  color: var(--slds-g-color-brand-1);
  font-size: var(--slds-g-font-size-5);
  border-radius: var(--slds-g-radius-border-1);
  box-shadow: var(--slds-g-shadow-1);
}

Aura Component Example

html
<!-- myComponent.cmp -->
<aura:component>
  <lightning:card title="Account Details">
    <div class="slds-p-around_medium">
      <lightning:button label="Save" variant="brand" onclick="{!c.handleSave}" />
    </div>
  </lightning:card>
</aura:component>