Button States

dembrandt/dembrandt-skills/skills/button-states

作者 dembrandt20de5f225ea7cffe2a721ac18c1077a92769a013无许可证收录于 2026年10月9日更新于 2026年10月9日

Rest, hover, active, focus, disabled and loading states, derived from the base colour. Use when designing buttons, links, inputs or any clickable component.

仅含说明Design & Creative
AI 生成的概览

定义按钮和可点击组件的六种交互状态,并提供各状态的 CSS 写法。

功能
该技能规定了按钮、链接、输入框及其他可点击组件的静止、悬停、按下、聚焦、禁用和加载六种状态。它说明如何通过调整 HSL 亮度从基础色推导悬停色和按下色,并给出主要按钮、次要按钮、聚焦、禁用和加载状态的 CSS 示例。此外还包含可选的按下缩放效果和用于检查各状态是否齐全的审查清单。
适用场景
适用于设计或编写按钮及其他交互元素的样式,或需要添加悬停、按下、聚焦、禁用、加载行为时。也可用于审查现有组件集,确认每种交互状态都已定义且可访问。
运行要求
无需脚本或软件包,仅为说明文档。CSS 示例假定项目中已定义 --color-primary、--color-border 等自定义属性。

Button and Interactive Element States

Every interactive component must have a complete, visually distinct state for each interaction mode. Missing or ambiguous states make the UI feel unfinished and reduce user confidence.

The Six States

StateTriggerVisual signal
RestDefaultBase colour, cursor: pointer
HoverMouse overSlightly darker, subtle background shift
Active / PressedMouse down / tapNoticeably darker, slight scale-down
FocusKeyboard navigationVisible focus ring, no change to fill
DisabledNot availableLow contrast, cursor: not-allowed, no interaction
LoadingAsync action in progressSpinner or pulse, non-interactive

Deriving State Colours Algorithmically

State colours are not chosen independently — they are derived from the base colour by adjusting lightness in HSL. This guarantees coherence across the entire palette.

base:     hsl(H, S%, L%)hover:    hsl(H, S%, L% - 8%)    ← darken 8%active:   hsl(H, S%, L% - 14%)   ← darken 14%

Example: primary button #635BFF (hsl 243, 100%, 68%)

css
.btn-primary {  background: hsl(243, 100%, 68%);       /* rest    #635BFF */}.btn-primary:hover {  background: hsl(243, 100%, 60%);       /* hover   #4A40FF */}.btn-primary:active {  background: hsl(243, 100%, 54%);       /* active  #3429FF */}

For light buttons on dark backgrounds, invert the logic — lighten on hover instead of darkening.

Secondary / outlined buttons

css
.btn-secondary {  background: transparent;  border: 1px solid var(--color-border);  color: var(--color-text);}.btn-secondary:hover {  background: var(--color-grey-100);     /* subtle fill */  border-color: var(--color-grey-300);}.btn-secondary:active {  background: var(--color-grey-200);}

Focus State

Focus is a keyboard navigation requirement (WCAG 2.2). It must be visible and must not rely on the hover style alone — keyboard users do not trigger hover.

css
.btn:focus-visible {  outline: 2px solid var(--color-primary);  outline-offset: 3px;}
  • Use outline, not box-shadow, for focus rings — outline respects border-radius in modern browsers and does not affect layout
  • outline-offset: 2–4px gives the ring breathing room from the component edge
  • Never use outline: none without a replacement focus style

Disabled State

css
.btn:disabled,.btn[aria-disabled="true"] {  opacity: 0.4;  cursor: not-allowed;  pointer-events: none;}
  • Disabled elements are exempt from WCAG contrast requirements — low opacity is correct and intentional
  • Use pointer-events: none to prevent click events even if JS is bypassed
  • Do not change the shape or size of a disabled button — only colour and cursor change

Loading State

When a button triggers an async action, replace the label with a spinner and prevent re-submission.

css
.btn--loading {  pointer-events: none;  opacity: 0.7;}

The cursor does not change while loading. It signals affordance, not progress: the arrow for ordinary content, pointer for something you can act on. Progress belongs to the element and the loading region, which the user is already looking at, rather than to wherever the mouse happens to rest. cursor: wait is also inert next to pointer-events: none, so the rule reads as deliberate while doing nothing.

  • Keep the button width stable during loading — avoid layout shift when label is replaced by spinner
  • Return to rest state on completion (success or error)
  • For long-running operations, pair with a status message — a spinner alone does not tell the user what is happening

Scale on Active (Optional)

A subtle scale-down on press adds physical feedback — borrowed from Disney's squash principle.

css
.btn:active {  transform: scale(0.97);  transition: transform 80ms ease-out;}

Keep the scale value between 0.95–0.98. Below 0.95 feels like the button is breaking.

Complete Button CSS Reference

css
.btn {  cursor: pointer;  background: var(--color-primary);  color: white;  border-radius: var(--radius-button);  padding: var(--component-padding-y-md) var(--component-padding-x-md);  height: var(--component-height-md);  border: none;  transition: background 120ms ease-out, transform 80ms ease-out;}
.btn:hover           { background: var(--color-primary-hover); }.btn:active          { background: var(--color-primary-active); transform: scale(0.97); }.btn:focus-visible   { outline: 2px solid var(--color-primary); outline-offset: 3px; }.btn:disabled        { opacity: 0.4; cursor: not-allowed; pointer-events: none; }.btn.btn--loading    { opacity: 0.7; pointer-events: none; }

Review Checklist

  • Does every interactive element have all six states defined?
  • Are hover and active colours derived from the base by lightness adjustment (not chosen arbitrarily)?
  • Is focus state visible and using outline (not removed)?
  • Is disabled state low-opacity with cursor: not-allowed?
  • Does loading state prevent re-submission?
  • Are transition durations 80–150ms — not instant, not slow?
  • Does cursor: pointer appear on all interactive elements at rest?
  • Does the cursor stay unchanged while an action runs, with progress shown by the element itself?

来源与署名

来源:dembrandt/dembrandt-skills位于skills/button-states提交20de5f2

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架