Component
Radio Disabled
Radio variant focused on the disabled non-interactive state.
- Component
- React
- TSX
- Tailwind CSS
- MIT
Preview
Live component
9:41 100%
devsnips.dev/library/react/components/radios/radio-disabled/ fluid · no fixed width 100%
Install
Add to your project
npx devsnips add React/Components/Radios/radio-disabled React/Components/Radios/radio-disabled import type { ChangeEvent, ReactNode } from "react";
import { useId, useState } from "react";
function cx(...parts: Array<string | false | null | undefined>): string {
return parts.filter(Boolean).join(" ");
}
export interface RadioDisabledProps {
label: ReactNode;
helperText?: ReactNode;
checked?: boolean;
defaultChecked?: boolean;
onChange?: (event: ChangeEvent<HTMLInputElement>) => void;
disabled?: boolean;
name?: string;
value: string | number | readonly string[];
id?: string;
className?: string;
}
/**
* Radio variant focused on the disabled (non-interactive) state. The native
* `disabled` attribute carries the semantics; the visual treatment uses
* reduced opacity + muted foreground so the control stays perceivable
* without looking interactive. Built on the native `<input type="radio">`.
*/
export function RadioDisabled({
label,
helperText,
checked,
defaultChecked,
onChange,
disabled = true,
name,
value,
id,
className,
}: RadioDisabledProps) {
const generatedId = useId();
const inputId = id ?? `radio-${generatedId}`;
const helperId = `${inputId}-helper`;
const isControlled = checked !== undefined;
const [internal, setInternal] = useState<boolean>(defaultChecked ?? false);
const isChecked = isControlled ? checked : internal;
function handleChange(event: ChangeEvent<HTMLInputElement>) {
if (!isControlled) setInternal(event.target.checked);
onChange?.(event);
}
return (
<div className={cx("flex flex-col gap-1", className)}>
<label
htmlFor={inputId}
className="inline-flex cursor-not-allowed items-center gap-2.5 text-sm leading-5 text-[var(--ds-color-muted-foreground)] opacity-60"
>
<span className="relative inline-flex size-[18px] shrink-0 items-center justify-center">
<input
id={inputId}
type="radio"
aria-describedby={helperText ? helperId : undefined}
className="size-[18px] cursor-not-allowed appearance-none rounded-full border border-[var(--ds-color-border)] bg-[var(--ds-color-muted)] opacity-50 checked:border-[var(--ds-color-muted-foreground)] motion-reduce:transition-none"
checked={isControlled ? isChecked : undefined}
defaultChecked={isControlled ? undefined : defaultChecked}
disabled={disabled}
name={name}
value={value}
onChange={handleChange}
/>
<span
aria-hidden="true"
className={cx(
"pointer-events-none absolute inset-0 flex items-center justify-center transition-opacity duration-150 motion-reduce:transition-none",
isChecked ? "opacity-100" : "opacity-0",
)}
>
<span className="block size-[8px] rounded-full bg-[var(--ds-color-muted-foreground)]" />
</span>
</span>
<span className="select-none">{label}</span>
</label>
{helperText ? (
<p id={helperId} className="pl-[26px] text-xs leading-4 text-[var(--ds-color-muted-foreground)]">
{helperText}
</p>
) : null}
</div>
);
}
export default RadioDisabled; /* DevSnips React — JavaScript parity build.
* Same API, behavior, and classes as code.tsx; TypeScript types removed.
* Regenerated from code.tsx — edit code.tsx and re-run the generator.
*/
import { useId, useState } from "react";
function cx(...parts) {
return parts.filter(Boolean).join(" ");
}
export function RadioDisabled({
label,
helperText,
checked,
defaultChecked,
onChange,
disabled = true,
name,
value,
id,
className
}) {
const generatedId = useId();
const inputId = id ?? `radio-${generatedId}`;
const helperId = `${inputId}-helper`;
const isControlled = checked !== undefined;
const [internal, setInternal] = useState(defaultChecked ?? false);
const isChecked = isControlled ? checked : internal;
function handleChange(event) {
if (!isControlled) setInternal(event.target.checked);
onChange?.(event);
}
return <div className={cx("flex flex-col gap-1", className)}>
<label
htmlFor={inputId}
className="inline-flex cursor-not-allowed items-center gap-2.5 text-sm leading-5 text-[var(--ds-color-muted-foreground)] opacity-60"
>
<span className="relative inline-flex size-[18px] shrink-0 items-center justify-center">
<input
id={inputId}
type="radio"
aria-describedby={helperText ? helperId : undefined}
className="size-[18px] cursor-not-allowed appearance-none rounded-full border border-[var(--ds-color-border)] bg-[var(--ds-color-muted)] opacity-50 checked:border-[var(--ds-color-muted-foreground)] motion-reduce:transition-none"
checked={isControlled ? isChecked : undefined}
defaultChecked={isControlled ? undefined : defaultChecked}
disabled={disabled}
name={name}
value={value}
onChange={handleChange}
/>
<span
aria-hidden="true"
className={cx(
"pointer-events-none absolute inset-0 flex items-center justify-center transition-opacity duration-150 motion-reduce:transition-none",
isChecked ? "opacity-100" : "opacity-0"
)}
>
<span className="block size-[8px] rounded-full bg-[var(--ds-color-muted-foreground)]" />
</span>
</span>
<span className="select-none">{label}</span>
</label>
{helperText ? <p id={helperId} className="pl-[26px] text-xs leading-4 text-[var(--ds-color-muted-foreground)]">
{helperText}
</p> : null}
</div>;
}
export default RadioDisabled; # Radio Disabled
Radio variant focused on the disabled non-interactive state.
## Usage
```tsx
<RadioDisabled label="Enterprise (requires upgrade)" name="plan" value="enterprise" />
```
## JavaScript
A `code.jsx` build is provided for projects that ship plain JSX. It exposes the same API and behavior as `code.tsx` — only the TypeScript types are removed.
```jsx
<RadioDisabled label="Enterprise (requires upgrade)" name="plan" value="enterprise" />
```
## Props
| Name | Type | Default | Description |
|---|---|---:|---|
| `label` | `ReactNode` (required) | — | Visible label (omit for an icon-only / aria-label control). |
| `checked` / `defaultChecked` | `boolean` | `false` | Controlled / uncontrolled checked state. |
| `onChange` | `(event) => void` | — | Change callback. |
| `disabled` | `boolean` | — | Disables the control. |
| `required` | `boolean` | — | Marks the field required (renders `*`). |
| `invalid` | `boolean` | — | Sets `aria-invalid` + destructive styling. |
| `name` | `string` | — | Shared group name (groups radios). |
| `value` | `string \| number \| readonly string[]` (required) | — | Native form value. |
| `id` | `string` | generated | Input id (also the label `htmlFor`). |
| `aria-label` / `aria-labelledby` / `aria-describedby` | `string` | — | Override association. |
| `helperText` | `ReactNode` | — | Helper text (still readable when disabled). |
| `disabled` | `boolean` | `true` | Defaults to disabled. |
## States
Native radio with `disabled` set (defaults to `true`). The visual treatment uses reduced opacity + muted foreground so the control stays perceivable without looking interactive. Native disabled semantics are preserved (excluded from form submission, not focusable).
## Accessibility
Native `disabled` attribute carries the semantics. Helper text stays associated via `aria-describedby`. Reduced opacity + muted color keeps it perceivable.
## Styling
Built with React, Tailwind CSS, and DevSnips design tokens. The component consumes the `--ds-*` semantic tokens via arbitrary values (for example `bg-[var(--ds-color-primary)]`). Define the tokens once in your theme — no component-specific CSS file is required.
## Design Tokens
See [React/DESIGN_TOKENS.md](../../../DESIGN_TOKENS.md) for the authoritative token specification. This radio uses the semantic color, radius, spacing, and motion tokens.
## Notes
Use `radio-disabled` for options that exist but cannot be chosen in this context (e.g. a plan that requires an upgrade). 91 lines UTF-8 · LF · Spaces: 2
Continue browsing