HapplyUI

Components

Section Toggle

A toggleable section that expands to reveal content when a switch is activated.


Installation

bunx @happlyui/cli@latest add section-toggle

Dependencies

npm packages

  • framer-motion

Registry dependencies

These are automatically installed when you add this component.

  • happly-ui-utils
  • switch

Usage

import * as SectionToggle from "@/components/ui/section-toggle"

// Composed (recommended)
<SectionToggle.Composed
  title="Title"
  description="Description"
  open={open}
  onOpenChange={setOpen}
>
  {children}
</SectionToggle.Composed>

// Namespace
<SectionToggle.Root open={open}>
  <SectionToggle.Header open={open}>
    <SectionToggle.TextGroup>
      <SectionToggle.Title>Title</SectionToggle.Title>
      <SectionToggle.Description>Description</SectionToggle.Description>
    </SectionToggle.TextGroup>
    <Switch.Root variant="neutral" checked={open} onCheckedChange={setOpen} />
  </SectionToggle.Header>
  <SectionToggle.Content open={open}>{children}</SectionToggle.Content>
</SectionToggle.Root>

Examples

Composed

Simplified single-component API with built-in switch toggle.

Do you want to set a closing date for offers?

Set the date when offers stop being accepted for this program.

Select a date
To
Select a time
<SectionToggle.Composed
  title="Set a deadline?"
  description="Offers close after this date."
  open={open}
  onOpenChange={setOpen}
>
  {children}
</SectionToggle.Composed>

Default (Expanded)

Section toggle in its expanded state using namespace pattern.

Do you want to set a closing date for offers?

Set the date when offers stop being accepted for this program.

Select a date
To
Select a time
<SectionToggle.Root open={open}>
  <SectionToggle.Header open={open}>
    <SectionToggle.TextGroup>
      <SectionToggle.Title>Set a deadline?</SectionToggle.Title>
      <SectionToggle.Description>Description here.</SectionToggle.Description>
    </SectionToggle.TextGroup>
    <Switch.Root variant="neutral" checked={open} onCheckedChange={setOpen} />
  </SectionToggle.Header>
  <SectionToggle.Content open={open}>{children}</SectionToggle.Content>
</SectionToggle.Root>

Collapsed

Section toggle in its collapsed state.

Do you want to set a closing date for offers?

Set the date when offers stop being accepted for this program.

<SectionToggle.Root open={open}>
  <SectionToggle.Header open={open}>
    <SectionToggle.TextGroup>
      <SectionToggle.Title>Limit offers?</SectionToggle.Title>
      <SectionToggle.Description>Description here.</SectionToggle.Description>
    </SectionToggle.TextGroup>
    <Switch.Root variant="neutral" checked={open} onCheckedChange={setOpen} />
  </SectionToggle.Header>
  <SectionToggle.Content open={open}>{children}</SectionToggle.Content>
</SectionToggle.Root>

API Reference

SectionToggle.Composed

Composed version — renders the full toggle section with built-in switch from props.

PropTypeDefaultDescription
titleReactNode-The toggle section title
descriptionReactNode-The toggle section description
openbooleanfalseControls the expanded/collapsed state
onOpenChange(open: boolean) => void-Callback when the switch is toggled
contentClassNamestring-Additional classes for the content area
headerClassNamestring-Additional classes for the header row

SectionToggle.Root

The outer container with subtle background, border, and rounded corners.

PropTypeDefaultDescription
openbooleanfalseControls the visual state — white background when closed, gray when open
classNamestring-Additional classes for the root container

SectionToggle.Header

The header row containing the text group and the switch. The switch is the section’s only control — it owns the role, the accessible name and the keyboard path. When collapsed and onOpenChange is provided, the header also acts as a pointer-only click target that opens the section; it claims no role and takes no tab stop.

PropTypeDefaultDescription
openbooleanfalseCurrent open state — when false and onOpenChange is provided, clicking anywhere in the header opens the section.
onOpenChange(open: boolean) => void-Callback fired when the collapsed header is clicked to expand the section. Pass the same handler to the switch inside the header so both paths stay in sync.

SectionToggle.TextGroup

Groups the title and description together, filling available space.

PropTypeDefaultDescription

SectionToggle.Title

The toggle section title.

PropTypeDefaultDescription

SectionToggle.Description

The toggle section description.

PropTypeDefaultDescription

SectionToggle.Content

The collapsible content area with animated height transition via framer-motion.

PropTypeDefaultDescription
openbooleanfalseControls visibility with animated expand/collapse transition

Previous
Section