Colore JS

Search documentation

Find a function or jump to a section.
v2.0.1

generateStates

generateStates creates hover, active, disabled and focus colors from a base color. Set the background to let Colore check the disabled and focus colors as they appear after opacity is applied.

Example

import { generateStates } from 'colore-js';

const button = generateStates('#2563eb', { background: '#fff' });

button.hover.css();     // '#0b48cf'
button.active.css();    // 'color(display-p3 0.04512 0.18278 0.68986)'
button.disabled.css();  // '#526c9f'
button.focusRing.css(); // '#00298f80'

Syntax

generateStates(base: ColorInput | Color, options?: StateOptions): ColorStates

Parameters

NameTypeDescription
baseColorInput | ColorColor to derive the states from.
options.backgroundColorInput | ColorColor behind the states. Defaults to #fafafa. Passing it enables contrast checks.
options.hoverFactor / options.activeFactornumberAbsolute OKLCH lightness changes from 0 to 1. Defaults to 0.08 and 0.15.
options.disabledOpacity / options.focusOpacitynumberAlpha from 0 to 1. Defaults to the base alpha and 0.5, respectively.
options.focusColorColorInput | ColorCustom focus color. Defaults to the base.
options.intent / options.algorithm / options.level / options.targetTargetOptionsSet a contrast target for disabled and focus colors. Default target when checking is WCAG 2 'ui', or 3:1.

Returns

ColorStates includes base, hover, active, disabled and focusRing as Color values. Active and hover move away from the background in lightness.

Throws

A ColorError for invalid colors or options, or when the requested opacity prevents disabled or focus contrast from reaching the target.

Create a focus ring for dark mode

import { generateStates } from 'colore-js';

const darkButton = generateStates('#2563eb', {
  background: '#0a0a0a',
  focusOpacity: 1,
});

darkButton.focusRing.css(); // '#2563eb'
darkButton.focusRing.contrastRatio('#0a0a0a').toFixed(2); // '3.83'