PPolaris ComponentsConnect an agent

Color picker

A hue, saturation and brightness surface with optional alpha slider for choosing a custom color, such as a storefront brand or badge color.

Preview

Code

Example.jsx
import { useState, useCallback } from 'react';
import { Card, BlockStack, InlineStack, Text, ColorPicker, Checkbox, TextField, hsbToHex } from '@shopify/polaris';

const INITIAL_COLOR = { hue: 210, saturation: 0.78, brightness: 0.62, alpha: 1 };

const PRESETS = [
  { name: 'Ocean', color: { hue: 210, saturation: 0.78, brightness: 0.62, alpha: 1 } },
  { name: 'Moss', color: { hue: 140, saturation: 0.55, brightness: 0.5, alpha: 1 } },
  { name: 'Clay', color: { hue: 18, saturation: 0.62, brightness: 0.78, alpha: 1 } },
  { name: 'Plum', color: { hue: 288, saturation: 0.5, brightness: 0.55, alpha: 1 } },
];

const SWATCH_STYLE = {
  width: 56,
  height: 56,
  borderRadius: 8,
  border: '1px solid var(--p-color-border, #c9cccf)',
};

const PRESET_STYLE = {
  width: 28,
  height: 28,
  borderRadius: 6,
  border: '1px solid var(--p-color-border, #c9cccf)',
  padding: 0,
  cursor: 'pointer',
};

export const Example = () => {
  const [color, setColor] = useState(INITIAL_COLOR);
  const [allowAlpha, setAllowAlpha] = useState(true);

  const handleAllowAlphaChange = useCallback((checked) => {
    setAllowAlpha(checked);
    if (!checked) setColor((current) => ({ ...current, alpha: 1 }));
  }, []);

  const hex = hsbToHex(color);
  const opacity = Math.round((color.alpha ?? 1) * 100);

  return (
    <Card>
      <BlockStack gap="400">
        <BlockStack gap="100">
          <Text as="h2" variant="headingMd">
            Brand color
          </Text>
          <Text as="p" tone="subdued">
            Used for buttons and links in your storefront theme.
          </Text>
        </BlockStack>

        <InlineStack gap="400" blockAlign="start" wrap={false}>
          <ColorPicker onChange={setColor} color={color} allowAlpha={allowAlpha} />
          <BlockStack gap="300">
            <InlineStack gap="300" blockAlign="center">
              <div style={{ ...SWATCH_STYLE, background: hex, opacity: color.alpha ?? 1 }} />
              <BlockStack gap="050">
                <Text as="span" fontWeight="medium">
                  {hex.toUpperCase()}
                </Text>
                <Text as="span" variant="bodySm" tone="subdued">
                  {opacity}% opacity
                </Text>
              </BlockStack>
            </InlineStack>
            <TextField label="Hex" value={hex.toUpperCase()} autoComplete="off" readOnly />
            <BlockStack gap="200">
              <Text as="span" variant="bodySm" tone="subdued">
                Presets
              </Text>
              <InlineStack gap="200">
                {PRESETS.map((preset) => (
                  <button
                    key={preset.name}
                    type="button"
                    aria-label={preset.name}
                    onClick={() => setColor({ ...preset.color, alpha: allowAlpha ? color.alpha ?? 1 : 1 })}
                    style={{ ...PRESET_STYLE, background: hsbToHex(preset.color) }}
                  />
                ))}
              </InlineStack>
            </BlockStack>
            <Checkbox label="Allow transparency" checked={allowAlpha} onChange={handleAllowAlphaChange} />
          </BlockStack>
        </InlineStack>
      </BlockStack>
    </Card>
  );
};