Card Radio Group

Installation

Add the following Soul components

The card-radio-group component uses the field-error and label components. Make sure you have added them to your project.

Install the following dependencies

npm install clsx @radix-ui/react-radio-group

Copy and paste the following code into your project

form/card-radio-group/index.tsx

import * as RadioGroupPrimitive from '@radix-ui/react-radio-group';import { clsx } from 'clsx';import Image from 'next/image';import * as React from 'react';import { FieldError } from '@/vibes/soul/form/field-error';import { Label } from '@/vibes/soul/form/label';interface Option {  value: string;  label: string;  image: { src: string; alt: string };  disabled?: boolean;}/** * This component supports various CSS variables for theming. Here's a comprehensive list, along * with their default values: * * ```css *  :root { *   --card-radio-group-focus: hsl(var(--primary)); *   --card-radio-group-light-unchecked-border: hsl(var(--contrast-100)); *   --card-radio-group-light-unchecked-border-hover: hsl(var(--contrast-200)); *   --card-radio-group-light-unchecked-background: hsl(var(--background)); *   --card-radio-group-light-unchecked-text: hsl(var(--foreground)); *   --card-radio-group-light-unchecked-background-hover: hsl(var(--contrast-100)); *   --card-radio-group-light-checked-background: hsl(var(--foreground)); *   --card-radio-group-light-checked-text: hsl(var(--background)); *   --card-radio-group-light-border-error: hsl(var(--error)); *   --card-radio-group-dark-unchecked-border: hsl(var(--contrast-500)); *   --card-radio-group-dark-unchecked-border-hover: hsl(var(--contrast-400)); *   --card-radio-group-dark-unchecked-background: hsl(var(--foreground)); *   --card-radio-group-dark-unchecked-background-hover: hsl(var(--contrast-500)); *   --card-radio-group-dark-unchecked-text: hsl(var(--background)); *   --card-radio-group-dark-checked-background: hsl(var(--background)); *   --card-radio-group-dark-checked-text: hsl(var(--foreground)); *   --card-radio-group-dark-border-error: hsl(var(--error)); *  } * ``` */export const CardRadioGroup = React.forwardRef<  React.ComponentRef<typeof RadioGroupPrimitive.Root>,  React.ComponentPropsWithoutRef<typeof RadioGroupPrimitive.Root> & {    label?: string;    options: Option[];    errors?: string[];    onOptionMouseEnter?: (value: string) => void;    colorScheme?: 'light' | 'dark';  }>(  (    { label, options, errors, className, onOptionMouseEnter, colorScheme = 'light', ...rest },    ref,  ) => {    const id = React.useId();    return (      <div className={clsx('space-y-2', className)}>        {label !== undefined && label !== '' && (          <Label colorScheme={colorScheme} id={id}>            {label}          </Label>        )}        <RadioGroupPrimitive.Root {...rest} aria-labelledby={id} className="space-y-2" ref={ref}>          {options.map((option) => (            <RadioGroupPrimitive.Item              aria-label={option.label}              className={clsx(                'relative flex h-12 w-full items-center overflow-hidden rounded-lg border font-body text-sm font-normal leading-normal transition-colors focus-visible:outline-0 focus-visible:ring-2 focus-visible:ring-[var(--card-radio-group-focus,hsl(var(--primary)))] data-[disabled]:pointer-events-none data-[disabled]:opacity-50',                {                  light:                    'border-[var(--card-radio-group-light-unchecked-border,hsl(var(--contrast-100)))] text-[var(--card-radio-group-light-unchecked-text,hsl(var(--foreground)))] data-[state=checked]:bg-[var(--card-radio-group-light-checked-background,hsl(var(--foreground)))] data-[state=unchecked]:bg-[var(--card-radio-group-light-unchecked-background,hsl(var(--background)))] data-[state=checked]:text-[var(--card-radio-group-light-checked-text,hsl(var(--background)))] data-[state=unchecked]:hover:border-[var(--card-radio-group-light-unchecked-border-hover,hsl(var(--contrast-200)))] data-[state=unchecked]:hover:bg-[var(--card-radio-group-light-unchecked-background-hover,hsl(var(--contrast-100)))]',                  dark: 'border-[var(--card-radio-group-dark-unchecked-border,hsl(var(--contrast-500)))] text-[var(--card-radio-group-dark-unchecked-text,hsl(var(--background)))] data-[state=checked]:bg-[var(--card-radio-group-dark-checked-background,hsl(var(--background)))] data-[state=unchecked]:bg-[var(--card-radio-group-dark-unchecked-background,hsl(var(--foreground)))] data-[state=checked]:text-[var(--card-radio-group-dark-checked-text,hsl(var(--foreground)))] data-[state=unchecked]:hover:border-[var(--card-radio-group-dark-unchecked-border-hover,hsl(var(--contrast-400)))] data-[state=unchecked]:hover:bg-[var(--card-radio-group-dark-unchecked-background-hover,hsl(var(--contrast-500)))]',                }[colorScheme],                {                  light:                    errors && errors.length > 0                      ? 'data-[state=unchecked]:border-[var(--card-radio-group-light-border-error,hsl(var(--error)))]'                      : 'data-[state=checked]:border-[var(--card-radio-group-light-checked-background,hsl(var(--foreground)))]',                  dark:                    errors && errors.length > 0                      ? 'data-[state=unchecked]:border-[var(--card-radio-group-dark-border-error,hsl(var(--error)))]'                      : 'data-[state=checked]:border-[var(--card-radio-group-dark-checked-background,hsl(var(--foreground)))]',                }[colorScheme],              )}              disabled={option.disabled}              id={option.value}              key={option.value}              onMouseEnter={() => {                onOptionMouseEnter?.(option.value);              }}              value={option.value}            >              <div className="relative aspect-square h-full">                <Image                  alt={option.image.alt}                  className="bg-background object-fill"                  fill                  src={option.image.src}                />              </div>              <span className="flex-1 truncate text-ellipsis px-4 text-left">{option.label}</span>            </RadioGroupPrimitive.Item>          ))}        </RadioGroupPrimitive.Root>        {errors?.map((error) => <FieldError key={error}>{error}</FieldError>)}      </div>    );  },);CardRadioGroup.displayName = 'CardRadioGroup';

Usage

'use client';import { CardRadioGroup } from '@/vibes/soul/form/card-radio-group';function Usage() {  return (      <CardRadioGroup        options={[          {            label: 'Philodendron Imperial Red',            value: 'option-1',            image: {              src: 'https://rstr.in/monogram/vibes/-kv08IvX08j',              alt: 'Philodendron Imperial Red',            },          },          {            label: 'Monstera',            value: 'option-2',            image: {              src: 'https://rstr.in/monogram/vibes/n0P83RMnClS',              alt: 'Monstera',            },          },          {            label: 'Pink Caladium',            value: 'option-3',            image: {              src: 'https://rstr.in/monogram/vibes/AaZW4j2VTd4',              alt: 'Pink Caladium',            },          },        ]}      />  );}

API Reference

This component uses the RadioGroup component from Radix UI. Refer to the RadioGroup documentation for more information.

CardRadioGroupProps

PropTypeDefault
className
string
label
string
options*
Option[]
errors
string[]
onOptionMouseEnter
(value: string) => void
colorScheme
'light' | 'dark'
'light'

Option

PropTypeDefault
value*
string
label*
string
image*
{ src: string; alt: string }
disabled
boolean

CSS Variables

This component supports various CSS variables for theming. Here's a comprehensive list.

:root {  --card-radio-group-focus: hsl(var(--primary));  --card-radio-group-light-unchecked-border: hsl(var(--contrast-100));  --card-radio-group-light-unchecked-border-hover: hsl(var(--contrast-200));  --card-radio-group-light-unchecked-background: hsl(var(--background));  --card-radio-group-light-unchecked-text: hsl(var(--foreground));  --card-radio-group-light-unchecked-background-hover: hsl(var(--contrast-100));  --card-radio-group-light-checked-background: hsl(var(--foreground));  --card-radio-group-light-checked-text: hsl(var(--background));  --card-radio-group-light-border-error: hsl(var(--error));  --card-radio-group-dark-unchecked-border: hsl(var(--contrast-500));  --card-radio-group-dark-unchecked-border-hover: hsl(var(--contrast-400));  --card-radio-group-dark-unchecked-background: hsl(var(--foreground));  --card-radio-group-dark-unchecked-background-hover: hsl(var(--contrast-500));  --card-radio-group-dark-unchecked-text: hsl(var(--background));  --card-radio-group-dark-checked-background: hsl(var(--background));  --card-radio-group-dark-checked-text: hsl(var(--foreground));  --card-radio-group-dark-border-error: hsl(var(--error));}