Color Settings
A collapsible advanced settings card with a two-column grid of colour swatches, each opening a colour picker and showing its hex value, built with Polaris Card, Collapsible, InlineGrid, Popover and ColorPicker.
Preview
Code
Example.jsx
import { useState } from 'react';
import { BlockStack, Box, Button, Card, Collapsible, ColorPicker, Divider, InlineGrid, InlineStack, Popover, Text, TextField, UnstyledButton, hexToRgb, hsbToHex, rgbToHsb } from '@shopify/polaris';
const COLOR_SETTINGS = [
{ id: 'message_color', label: 'Message color', field: 'textColor' },
{ id: 'background_color', label: 'Background color', field: 'bgColor' },
{ id: 'privacy_link_color', label: 'Privacy link color', field: 'privacyLinkColor' },
{ id: 'agree_btn_text_color', label: 'Accept button text color', field: 'agreeBtnTextColor' },
{ id: 'agree_btn_bg_color', label: 'Accept button background color', field: 'agreeBtnBgColor' },
{ id: 'deny_text_color', label: 'Reject/Preferences text color', field: 'denyTextColor' },
];
const PRESET_SWATCHES = [
'#FFFFFF00',
'#FFF',
'#bdbdbd',
'#455a64',
'#795548',
'#000000',
'#e53935',
'#3f51b5',
'#2196f3',
'#81d4fa',
'#01579b',
'#18ffff',
'#4dd0e1',
'#009688',
'#80cbc4',
'#1de9b6',
'#827717',
'#1b5e20',
'#a5d6a7',
'#f57f17',
'#fff176',
'#ffeb3b',
'#ff6f00',
'#ffc107',
'#ffe082',
'#ff6d00',
'#ffab40',
];
const INITIAL_COLORS = {
textColor: '#5d0e0e',
bgColor: '#F8F8FF',
privacyLinkColor: '#000000',
agreeBtnTextColor: '#F8F8F8',
agreeBtnBgColor: '#000000',
denyTextColor: '#000000',
};
const CHECKERBOARD =
'linear-gradient(45deg, #ccc 25%, transparent 25%), linear-gradient(-45deg, #ccc 25%, transparent 25%), linear-gradient(45deg, transparent 75%, #ccc 75%), linear-gradient(-45deg, transparent 75%, #ccc 75%)';
const isTransparent = (hex) => hex.length === 9 && hex.slice(7, 9).toLowerCase() === '00';
const fillStyle = (hex) =>
isTransparent(hex)
? { backgroundImage: CHECKERBOARD, backgroundSize: '0.5rem 0.5rem', backgroundPosition: '0 0, 0 0.25rem, 0.25rem -0.25rem, -0.25rem 0' }
: { background: hex };
const swatchFill = (hex) => ({
width: '2.8rem',
height: '2.8rem',
borderRadius: 'var(--p-border-radius-150, 0.375rem)',
...fillStyle(hex),
});
const presetFill = (hex) => ({
width: '1rem',
height: '1rem',
borderRadius: 'var(--p-border-radius-100, 0.25rem)',
boxShadow: 'inset 0 0 0 0.0625rem rgba(0, 0, 0, 0.15)',
...fillStyle(hex),
});
const parseColor = (hex) => {
const base = hex.length === 9 ? hex.slice(0, 7) : hex;
const alpha = hex.length === 9 ? parseInt(hex.slice(7, 9), 16) / 255 : 1;
return { ...rgbToHsb(hexToRgb(base)), alpha };
};
const formatColor = (hsb) => {
const base = hsbToHex(hsb);
if (hsb.alpha === undefined || hsb.alpha >= 1) return base;
const alpha = Math.round(hsb.alpha * 255)
.toString(16)
.padStart(2, '0');
return `${base}${alpha}`;
};
export const Example = () => {
const [open, setOpen] = useState(true);
const [colors, setColors] = useState(INITIAL_COLORS);
const [activeField, setActiveField] = useState(null);
const toggleField = (field) => setActiveField((prev) => (prev === field ? null : field));
const setColor = (field, hex) => setColors((prev) => ({ ...prev, [field]: hex }));
return (
<Card>
<BlockStack gap="400">
<InlineGrid columns="1fr auto">
<Text as="h3" variant="headingSm">
Advanced settings
</Text>
<Button
variant="monochromePlain"
disclosure={open ? 'up' : 'down'}
onClick={() => setOpen((prev) => !prev)}
accessibilityLabel={open ? 'Hide advanced settings' : 'Show advanced settings'}
/>
</InlineGrid>
<Collapsible
open={open}
id="advanced-settings-style-collapsible"
transition={{ duration: '300ms', timingFunction: 'ease-in-out' }}
>
<BlockStack gap="300">
<Text as="h4" variant="headingSm">
Colors
</Text>
<InlineGrid columns={2} gap="200">
{[0, 1].map((column) => (
<Box key={column}>
{COLOR_SETTINGS.filter((item, index) => index % 2 === column).map((item) => (
<Box key={item.id} paddingBlockEnd="400">
<InlineStack gap="300" blockAlign="center" wrap={false}>
<Popover
active={activeField === item.field}
onClose={() => setActiveField(null)}
activator={
<UnstyledButton
accessibilityLabel={`Change ${item.label}`}
onClick={() => toggleField(item.field)}
>
<Box
background="bg-surface"
borderRadius="150"
borderWidth="025"
borderColor="border"
padding="025"
>
<div style={swatchFill(colors[item.field])} />
</Box>
</UnstyledButton>
}
>
<Box padding="300">
<BlockStack gap="300">
<ColorPicker
allowAlpha
color={parseColor(colors[item.field])}
onChange={(hsb) => setColor(item.field, formatColor(hsb))}
/>
<TextField
label="Hex value"
labelHidden
autoComplete="off"
value={colors[item.field]}
onChange={(value) => setColor(item.field, value)}
/>
<Divider />
<InlineGrid columns={9} gap="150">
{PRESET_SWATCHES.map((preset) => (
<UnstyledButton
key={preset}
accessibilityLabel={preset}
style={presetFill(preset)}
onClick={() => setColor(item.field, preset)}
/>
))}
</InlineGrid>
</BlockStack>
</Box>
</Popover>
<BlockStack gap="100">
<Text as="p">{item.label}</Text>
<Text as="p" tone="subdued">
{colors[item.field]}
</Text>
</BlockStack>
</InlineStack>
</Box>
))}
</Box>
))}
</InlineGrid>
</BlockStack>
</Collapsible>
</BlockStack>
</Card>
);
};