Skip to content
Esc
↑↓navigate↵open⌘Jpreview
On this page

Checkbox for Astro

Native Astro checkboxes for independent choices and form values.

Attach the current review notes when you share this project.

Installation

bunx --bun shadcn@latest add @gnostic/checkbox
npx shadcn@latest add @gnostic/checkbox
pnpm dlx shadcn@latest add @gnostic/checkbox

Component classes: Default Gnostic.

Complete the Installation guide first. Then copy the component files below.

No extra packages are required.

Copy the files

src/components/ui/checkbox/checkbox.astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = Omit<HTMLAttributes<"input">, "type">

const { class: className, ...props } = Astro.props
---

<input
  type="checkbox"
  role="checkbox"
  data-slot="checkbox"
  class={cn("relative inline-flex size-4.5 sm:size-4 shrink-0 appearance-none items-center justify-center rounded-[.25rem] border border-input bg-background not-dark:bg-clip-padding shadow-xs/5 outline-none ring-ring transition-shadow focus-visible:ring-2 focus-visible:ring-offset-1 focus-visible:ring-offset-background aria-invalid:border-destructive/36 focus-visible:aria-invalid:border-destructive/64 focus-visible:aria-invalid:ring-destructive/48 disabled:cursor-not-allowed disabled:opacity-64 checked:border-primary checked:bg-primary checked:shadow-none indeterminate:shadow-none dark:not-checked:bg-input/32 dark:aria-invalid:ring-destructive/24 not-disabled:not-checked:not-aria-invalid:inset-shadow-[0_1px_--theme(--color-black/4%)] dark:not-disabled:not-checked:not-aria-invalid:inset-shadow-[0_-1px_--theme(--color-white/6%)] peer", className)}
  {...props}
/>

<style>
  /*
    The mark is a masked ::before on the input.
    Keep opacity and color in this sheet. These rules are unlayered, so they
    win over Tailwind utilities. A class such as checked:before:opacity-100
    cannot override opacity: 0, and its before: variant also replaces content.
  */
  [data-slot="checkbox"] {
    color: inherit;
  }

  [data-slot="checkbox"]::before {
    content: "";
    position: absolute;
    inset: 0;
    margin: auto;
    width: 14px;
    height: 14px;
    opacity: 0;
    background-color: currentColor;
    background-repeat: no-repeat;
    background-position: center;
    background-size: 14px 14px;
    -webkit-mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='black' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M3.5 8.5l3 3 6-6'/%3E%3C/svg%3E");
    -webkit-mask-repeat: no-repeat;
    -webkit-mask-position: center;
    -webkit-mask-size: 14px 14px;
    mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='black' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M3.5 8.5l3 3 6-6'/%3E%3C/svg%3E");
    mask-repeat: no-repeat;
    mask-position: center;
    mask-size: 14px 14px;
  }

  [data-slot="checkbox"]:checked {
    color: var(--color-primary-foreground);
  }

  [data-slot="checkbox"]:checked::before {
    opacity: 1;
  }

  [data-slot="checkbox"]:indeterminate {
    color: var(--color-foreground);
  }

  [data-slot="checkbox"]:indeterminate::before {
    opacity: 1;
    -webkit-mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none'%3E%3Cpath d='M3.5 8h9' stroke='black' stroke-width='2.5' stroke-linecap='round'/%3E%3C/svg%3E");
    mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none'%3E%3Cpath d='M3.5 8h9' stroke='black' stroke-width='2.5' stroke-linecap='round'/%3E%3C/svg%3E");
  }
</style>
src/components/ui/checkbox/index.ts
export { default as Checkbox } from "./checkbox.astro"

Usage

import { Checkbox } from "@/components/ui/checkbox"
<Checkbox />

Notes

  • Pair Checkbox with Label for simple cases, or use Field when you need descriptions, validation, or grouped controls.
  • Use the native disabled attribute to prevent interaction and aria-invalid when the control should display an error state.
  • For multi-line rows, wrap the label and supporting text together so the control stays easy to scan.

API Reference

Read the installed source for the complete prop types.

When to use Checkbox

Pair each Checkbox with a Label using matching id and for values. Give submitted controls a name and value. Use separate checkboxes when several choices can be selected together.

Keyboard and behavior

The rendered input uses native form submission, focus, Space activation, and disabled behavior. Set the DOM indeterminate property from your script for a mixed selection; an HTML attribute alone cannot set that property. A parent “select all” checkbox does not manage child values automatically.

Theme and layout

Checked, invalid, and disabled styles use the shared tokens. The check mark is a CSS mask on the input. Keep a text label visible and use FieldError for an explanation of invalid input.