Option cards
A radio group drawn as cards, each with an icon, a title and a line of description. The cards are labels around real radio inputs, so the arrow keys and forms work as usual.
Options
Code
import { useState } from "react";
import "./option-cards.css";
/* A radio group drawn as cards. Each option has a value, a title, a line of
description and the path of a 20 by 20 filled icon.
<OptionCards legend="What do you need?" hint="Pick one." name="service" options={options} value={value} onChange={setValue} /> */
export function OptionCards({ legend, hint, name, options, value, onChange }) {
return (
<fieldset className="dh-option">
<legend>{legend}</legend>
{hint && <p>{hint}</p>}
<div>
{options.map((option) => (
<label key={option.value}>
<input
type="radio"
name={name}
value={option.value}
checked={option.value === value}
onChange={() => onChange(option.value)}
/>
<svg viewBox="0 0 20 20" aria-hidden="true">
<path d={option.icon} />
</svg>
<i />
<b>{option.title}</b>
<span>{option.text}</span>
</label>
))}
</div>
</fieldset>
);
}
const services = [
{
value: "website",
title: "Website",
text: "A new site, designed and built from scratch.",
icon: "M4.500 3A2.500 2.500 0 0 0 2 5.500V7h16V5.500A2.500 2.500 0 0 0 15.500 3h-11ZM18 8.500H2v6A2.500 2.500 0 0 0 4.500 17h11a2.500 2.500 0 0 0 2.500-2.500v-6Z",
},
{
value: "shop",
title: "Shop",
text: "Products, payments and orders in one place.",
icon: "M6 6V5a4 4 0 0 1 8 0v1h1.200a1.500 1.500 0 0 1 1.490 1.330l.900 8A1.500 1.500 0 0 1 16.100 17H3.900a1.500 1.500 0 0 1-1.490-1.670l.900-8A1.500 1.500 0 0 1 4.800 6H6Zm1.500 0h5V5a2.500 2.500 0 0 0-5 0v1Z",
},
{
value: "hosting",
title: "Hosting",
text: "We keep it fast, backed up and online.",
icon: "M4.500 3A2.500 2.500 0 0 0 2 5.500v2A2.500 2.500 0 0 0 4.500 10h11A2.500 2.500 0 0 0 18 7.500v-2A2.500 2.500 0 0 0 15.500 3h-11Zm1 2.500a1 1 0 1 1 0 2 1 1 0 0 1 0-2ZM2 13.500A2.500 2.500 0 0 1 4.500 11h11a2.500 2.500 0 0 1 2.500 2.500v1a2.500 2.500 0 0 1-2.500 2.500h-11A2.500 2.500 0 0 1 2 14.500v-1Zm3.500-.500a1 1 0 1 0 0 2 1 1 0 0 0 0-2Z",
},
];
export function Example() {
const [service, setService] = useState("website");
return (
<OptionCards
legend="What do you need?"
hint="Pick the one to start with. You can add more later."
name="dh-option-service"
options={services}
value={service}
onChange={setService}
/>
);
}/* A radio group drawn as cards inside one panel. Each card is a label around a
real radio input, so the arrow keys and forms work as usual. The chosen card
takes the accent. */
.dh-option {
--accent: var(--dh-accent, #2563ff);
--line: color-mix(in srgb, var(--dh-fg, #171717) 9%, transparent);
container: dh-option / inline-size;
width: 480px;
max-width: 100%;
min-width: 0;
margin: 0;
padding: 14px 16px 16px;
border: 0;
border-radius: 14px;
background: var(--dh-raised, #fff);
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
color: var(--dh-fg, #171717);
font-family: inherit;
font-size: 13px;
line-height: 20px;
text-align: left;
}
.dh-option legend {
float: left;
width: 100%;
padding: 0;
font-size: 14px;
font-weight: 500;
line-height: 20px;
letter-spacing: -0.015em;
}
.dh-option > p {
clear: both;
margin: 0 0 12px;
color: var(--dh-muted, #5c5c5c);
font-size: 12px;
line-height: 18px;
}
.dh-option > div {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 8px;
}
.dh-option label {
position: relative;
display: grid;
grid-template-columns: auto 1fr;
align-content: start;
padding: 10px 10px 11px;
border-radius: 10px;
background: var(--dh-raised, #fff);
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
cursor: pointer;
-webkit-tap-highlight-color: transparent;
transition:
box-shadow 150ms cubic-bezier(0.23, 1, 0.32, 1),
background-color 150ms cubic-bezier(0.23, 1, 0.32, 1),
transform 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-option label:active {
transform: scale(0.98);
}
.dh-option input {
position: absolute;
inset: 0;
margin: 0;
opacity: 0;
cursor: pointer;
}
/* The icon sits on a small tile that takes the accent when chosen. */
.dh-option svg {
box-sizing: content-box;
width: 14px;
height: 14px;
padding: 7px;
border-radius: 7px;
background: var(--dh-well, #ededeb);
fill: var(--dh-muted, #5c5c5c);
transition:
background-color 150ms cubic-bezier(0.23, 1, 0.32, 1),
fill 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
/* The radio mark. */
.dh-option i {
justify-self: end;
width: 14px;
height: 14px;
border-radius: 50%;
background: var(--dh-raised, #fff);
box-shadow:
0 0 0 1px color-mix(in srgb, var(--dh-fg, #171717) 16%, transparent),
0 1px 1px color-mix(in srgb, var(--dh-fg, #171717) 8%, transparent);
}
.dh-option i::after {
content: "";
display: block;
width: 6px;
height: 6px;
margin: 4px;
border-radius: 50%;
background: #fff;
box-shadow: 0 1px 1px rgb(0 0 0 / 0.2);
scale: 0;
transition: scale 200ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-option b {
grid-column: 1 / -1;
margin-top: 10px;
font-weight: 500;
line-height: 18px;
letter-spacing: -0.01em;
}
.dh-option label span {
grid-column: 1 / -1;
color: var(--dh-muted, #5c5c5c);
font-size: 12px;
line-height: 16px;
}
.dh-option label:has(input:checked) {
background: color-mix(in srgb, var(--accent) 5%, var(--dh-raised, #fff));
box-shadow:
0 0 0 1.5px var(--accent),
0 1px 2px color-mix(in srgb, var(--accent) 24%, transparent);
}
.dh-option label:has(input:checked) svg {
background: color-mix(in srgb, var(--accent) 14%, transparent);
fill: color-mix(in srgb, var(--accent) 85%, var(--dh-fg, #171717));
}
.dh-option input:checked ~ i {
background: linear-gradient(color-mix(in srgb, var(--accent) 84%, #fff), var(--accent));
box-shadow:
inset 0 1px 0 rgb(255 255 255 / 0.3),
0 0 0 1px color-mix(in srgb, var(--accent) 78%, #000);
}
.dh-option input:checked ~ i::after {
scale: 1;
}
.dh-option label:has(input:focus-visible) {
outline: 2px solid var(--dh-faint, #8a8a8a);
outline-offset: 2px;
}
/* In a narrow space the cards become rows. */
@container dh-option (max-width: 400px) {
.dh-option > div {
grid-template-columns: 1fr;
}
.dh-option label {
grid-template-columns: auto 1fr auto;
column-gap: 10px;
align-items: center;
}
.dh-option svg {
grid-row: 1 / 3;
}
.dh-option b {
grid-area: 1 / 2;
margin-top: 0;
}
.dh-option label span {
grid-area: 2 / 2;
}
.dh-option i {
grid-area: 1 / 3 / 3;
}
}
@media (prefers-reduced-motion: reduce) {
.dh-option label,
.dh-option svg,
.dh-option i::after {
transition: none;
}
}<fieldset class="dh-option">
<legend>What do you need?</legend>
<p>Pick the one to start with. You can add more later.</p>
<div>
<label>
<input type="radio" name="dh-option-service" value="website" checked />
<svg viewBox="0 0 20 20" aria-hidden="true"><path d="M4.500 3A2.500 2.500 0 0 0 2 5.500V7h16V5.500A2.500 2.500 0 0 0 15.500 3h-11ZM18 8.500H2v6A2.500 2.500 0 0 0 4.500 17h11a2.500 2.500 0 0 0 2.500-2.500v-6Z" /></svg>
<i></i>
<b>Website</b>
<span>A new site, designed and built from scratch.</span>
</label>
<label>
<input type="radio" name="dh-option-service" value="shop" />
<svg viewBox="0 0 20 20" aria-hidden="true"><path d="M6 6V5a4 4 0 0 1 8 0v1h1.200a1.500 1.500 0 0 1 1.490 1.330l.900 8A1.500 1.500 0 0 1 16.100 17H3.900a1.500 1.500 0 0 1-1.490-1.670l.900-8A1.500 1.500 0 0 1 4.800 6H6Zm1.500 0h5V5a2.500 2.500 0 0 0-5 0v1Z" /></svg>
<i></i>
<b>Shop</b>
<span>Products, payments and orders in one place.</span>
</label>
<label>
<input type="radio" name="dh-option-service" value="hosting" />
<svg viewBox="0 0 20 20" aria-hidden="true"><path d="M4.500 3A2.500 2.500 0 0 0 2 5.500v2A2.500 2.500 0 0 0 4.500 10h11A2.500 2.500 0 0 0 18 7.500v-2A2.500 2.500 0 0 0 15.500 3h-11Zm1 2.500a1 1 0 1 1 0 2 1 1 0 0 1 0-2ZM2 13.500A2.500 2.500 0 0 1 4.500 11h11a2.500 2.500 0 0 1 2.500 2.500v1a2.500 2.500 0 0 1-2.500 2.500h-11A2.500 2.500 0 0 1 2 14.500v-1Zm3.500-.500a1 1 0 1 0 0 2 1 1 0 0 0 0-2Z" /></svg>
<i></i>
<b>Hosting</b>
<span>We keep it fast, backed up and online.</span>
</label>
</div>
</fieldset>/* A radio group drawn as cards inside one panel. Each card is a label around a
real radio input, so the arrow keys and forms work as usual. The chosen card
takes the accent. */
.dh-option {
--accent: var(--dh-accent, #2563ff);
--line: color-mix(in srgb, var(--dh-fg, #171717) 9%, transparent);
container: dh-option / inline-size;
width: 480px;
max-width: 100%;
min-width: 0;
margin: 0;
padding: 14px 16px 16px;
border: 0;
border-radius: 14px;
background: var(--dh-raised, #fff);
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
color: var(--dh-fg, #171717);
font-family: inherit;
font-size: 13px;
line-height: 20px;
text-align: left;
}
.dh-option legend {
float: left;
width: 100%;
padding: 0;
font-size: 14px;
font-weight: 500;
line-height: 20px;
letter-spacing: -0.015em;
}
.dh-option > p {
clear: both;
margin: 0 0 12px;
color: var(--dh-muted, #5c5c5c);
font-size: 12px;
line-height: 18px;
}
.dh-option > div {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 8px;
}
.dh-option label {
position: relative;
display: grid;
grid-template-columns: auto 1fr;
align-content: start;
padding: 10px 10px 11px;
border-radius: 10px;
background: var(--dh-raised, #fff);
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
cursor: pointer;
-webkit-tap-highlight-color: transparent;
transition:
box-shadow 150ms cubic-bezier(0.23, 1, 0.32, 1),
background-color 150ms cubic-bezier(0.23, 1, 0.32, 1),
transform 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-option label:active {
transform: scale(0.98);
}
.dh-option input {
position: absolute;
inset: 0;
margin: 0;
opacity: 0;
cursor: pointer;
}
/* The icon sits on a small tile that takes the accent when chosen. */
.dh-option svg {
box-sizing: content-box;
width: 14px;
height: 14px;
padding: 7px;
border-radius: 7px;
background: var(--dh-well, #ededeb);
fill: var(--dh-muted, #5c5c5c);
transition:
background-color 150ms cubic-bezier(0.23, 1, 0.32, 1),
fill 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
/* The radio mark. */
.dh-option i {
justify-self: end;
width: 14px;
height: 14px;
border-radius: 50%;
background: var(--dh-raised, #fff);
box-shadow:
0 0 0 1px color-mix(in srgb, var(--dh-fg, #171717) 16%, transparent),
0 1px 1px color-mix(in srgb, var(--dh-fg, #171717) 8%, transparent);
}
.dh-option i::after {
content: "";
display: block;
width: 6px;
height: 6px;
margin: 4px;
border-radius: 50%;
background: #fff;
box-shadow: 0 1px 1px rgb(0 0 0 / 0.2);
scale: 0;
transition: scale 200ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-option b {
grid-column: 1 / -1;
margin-top: 10px;
font-weight: 500;
line-height: 18px;
letter-spacing: -0.01em;
}
.dh-option label span {
grid-column: 1 / -1;
color: var(--dh-muted, #5c5c5c);
font-size: 12px;
line-height: 16px;
}
.dh-option label:has(input:checked) {
background: color-mix(in srgb, var(--accent) 5%, var(--dh-raised, #fff));
box-shadow:
0 0 0 1.5px var(--accent),
0 1px 2px color-mix(in srgb, var(--accent) 24%, transparent);
}
.dh-option label:has(input:checked) svg {
background: color-mix(in srgb, var(--accent) 14%, transparent);
fill: color-mix(in srgb, var(--accent) 85%, var(--dh-fg, #171717));
}
.dh-option input:checked ~ i {
background: linear-gradient(color-mix(in srgb, var(--accent) 84%, #fff), var(--accent));
box-shadow:
inset 0 1px 0 rgb(255 255 255 / 0.3),
0 0 0 1px color-mix(in srgb, var(--accent) 78%, #000);
}
.dh-option input:checked ~ i::after {
scale: 1;
}
.dh-option label:has(input:focus-visible) {
outline: 2px solid var(--dh-faint, #8a8a8a);
outline-offset: 2px;
}
/* In a narrow space the cards become rows. */
@container dh-option (max-width: 400px) {
.dh-option > div {
grid-template-columns: 1fr;
}
.dh-option label {
grid-template-columns: auto 1fr auto;
column-gap: 10px;
align-items: center;
}
.dh-option svg {
grid-row: 1 / 3;
}
.dh-option b {
grid-area: 1 / 2;
margin-top: 0;
}
.dh-option label span {
grid-area: 2 / 2;
}
.dh-option i {
grid-area: 1 / 3 / 3;
}
}
@media (prefers-reduced-motion: reduce) {
.dh-option label,
.dh-option svg,
.dh-option i::after {
transition: none;
}
}Option cards is not in the Figma file yet.
Use the React or HTML code for now.