Collapse | jaspr_daisyui_components
jaspr_daisyui_components
pub.dev ↗

Collapse

Collapse is used for showing and hiding content.

Class name Type Description
collapse Component Collapse
collapse-title Part Title part
collapse-content Part Content part
collapse-arrow Modifier Adds arrow icon
collapse-plus Modifier Adds plus/minus icon
collapse-open Modifier Force open
collapse-close Modifier Force close

Also see accordion examples

If you want the collapsed content to be searcheable in browser use the Collapse using details and summary tag

Collapse with focus

This collapse works with focus. When div loses focus, it gets closed
How do I create an account?
Click the "Sign Up" button in the top right corner and follow the registration process.
<div tabindex="0" class="collapse bg-base-100 border-base-300 border">
  <div class="collapse-title font-semibold">How do I create an account?</div>
  <div class="collapse-content text-sm">Click the "Sign Up" button in the top right corner and follow the registration process.</div>
</div>
Collapse(
  [
    CollapseTitle([Component.text('How do I create an account?')], classes: 'font-semibold'),
    CollapseContent(
      [
        Component.text('Click the "Sign Up" button in the top right corner and follow the registration process.'),
      ],
      classes: 'text-sm',
    ),
  ],
  classes: 'bg-base-100 border-base-300 border',
  attributes: {'tabindex': '0'},
);

Collapse with checkbox

This collapse works with checkbox instead of focus. It needs to get clicked again to get closed.
How do I create an account?
Click the "Sign Up" button in the top right corner and follow the registration process.
<div class="collapse bg-base-100 border border-base-300">
  <input type="checkbox" />
  <div class="collapse-title font-semibold">How do I create an account?</div>
  <div class="collapse-content text-sm">Click the "Sign Up" button in the top right corner and follow the registration process.</div>
</div>
Collapse(
  [
    input(type: InputType.checkbox),
    CollapseTitle([Component.text('How do I create an account?')], classes: 'font-semibold'),
    CollapseContent(
      [
        Component.text('Click the "Sign Up" button in the top right corner and follow the registration process.'),
      ],
      classes: 'text-sm',
    ),
  ],
  classes: 'bg-base-100 border border-base-300',
);

Collapse with checkbox and close on click outside

An alternative where you can also click outside to close it.
How do I create an account?
Click the "Sign Up" button in the top right corner and follow the registration process.
<div class="collapse bg-base-100 border border-base-300">
  <input id="collapse-1-toggle" type="checkbox" class="peer" />
  <label for="collapse-1-toggle" class="fixed inset-0 hidden peer-checked:block"></label>
  <div class="collapse-title font-semibold">How do I create an account?</div>
  <div class="collapse-content text-sm z-1">Click the "Sign Up" button in the top right corner and follow the registration process.</div>
</div>
Collapse(
  [
    input(classes: 'peer', type: InputType.checkbox, id: 'collapse-1-toggle'),
    label(
      const [],
      classes: 'fixed inset-0 hidden peer-checked:block',
      htmlFor: 'collapse-1-toggle',
    ),
    CollapseTitle([Component.text('How do I create an account?')], classes: 'font-semibold'),
    CollapseContent(
      [
        Component.text('Click the "Sign Up" button in the top right corner and follow the registration process.'),
      ],
      classes: 'text-sm z-1',
    ),
  ],
  classes: 'bg-base-100 border border-base-300',
);

Collapse using details and summary tag

collapse-open and collapse-close doesn't work with this method. You can add/remove open attribute to the details instead
How do I create an account?
Click the "Sign Up" button in the top right corner and follow the registration process.
<details class="collapse bg-base-100 border-base-300 border">
  <summary class="collapse-title font-semibold">How do I create an account?</summary>
  <div class="collapse-content text-sm">Click the "Sign Up" button in the top right corner and follow the registration process.</div>
</details>
details(
  [
    summary(
      [Component.text('How do I create an account?')],
      classes: 'collapse-title font-semibold',
    ),
    CollapseContent(
      [
        Component.text('Click the "Sign Up" button in the top right corner and follow the registration process.'),
      ],
      classes: 'text-sm',
    ),
  ],
  classes: 'collapse bg-base-100 border-base-300 border',
);

Without border and background color

How do I create an account?
Click the "Sign Up" button in the top right corner and follow the registration process.
<div tabindex="0" class="collapse">
  <div class="collapse-title font-semibold">How do I create an account?</div>
  <div class="collapse-content text-sm">Click the "Sign Up" button in the top right corner and follow the registration process.</div>
</div>
Collapse(
  [
    CollapseTitle([Component.text('How do I create an account?')], classes: 'font-semibold'),
    CollapseContent(
      [
        Component.text('Click the "Sign Up" button in the top right corner and follow the registration process.'),
      ],
      classes: 'text-sm',
    ),
  ],
  attributes: {'tabindex': '0'},
);

With arrow icon

How do I create an account?
Click the "Sign Up" button in the top right corner and follow the registration process.
<div tabindex="0" class="collapse collapse-arrow bg-base-100 border-base-300 border">
  <div class="collapse-title font-semibold">How do I create an account?</div>
  <div class="collapse-content text-sm">Click the "Sign Up" button in the top right corner and follow the registration process.</div>
</div>
Collapse(
  [
    CollapseTitle([Component.text('How do I create an account?')], classes: 'font-semibold'),
    CollapseContent(
      [
        Component.text('Click the "Sign Up" button in the top right corner and follow the registration process.'),
      ],
      classes: 'text-sm',
    ),
  ],
  classes: 'bg-base-100 border-base-300 border',
  modifier: CollapseModifier.arrow,
  attributes: {'tabindex': '0'},
);

With arrow plus/minus icon

How do I create an account?
Click the "Sign Up" button in the top right corner and follow the registration process.
<div tabindex="0" class="collapse collapse-plus bg-base-100 border-base-300 border">
  <div class="collapse-title font-semibold">How do I create an account?</div>
  <div class="collapse-content text-sm">Click the "Sign Up" button in the top right corner and follow the registration process.</div>
</div>
Collapse(
  [
    CollapseTitle([Component.text('How do I create an account?')], classes: 'font-semibold'),
    CollapseContent(
      [
        Component.text('Click the "Sign Up" button in the top right corner and follow the registration process.'),
      ],
      classes: 'text-sm',
    ),
  ],
  classes: 'bg-base-100 border-base-300 border',
  modifier: CollapseModifier.plus,
  attributes: {'tabindex': '0'},
);

Moving collapse icon to the start

We can move the icon using utility classes like after:start-5 after:end-auto and we can customize the padding like pe-4 ps-12
How do I create an account?
Click the "Sign Up" button in the top right corner and follow the registration process.
<div tabindex="0" class="collapse collapse-arrow bg-base-100 border-base-300 border">
  <div class="collapse-title font-semibold after:start-5 after:end-auto pe-4 ps-12">How do I create an account?</div>
  <div class="collapse-content text-sm">Click the "Sign Up" button in the top right corner and follow the registration process.</div>
</div>
Collapse(
  [
    CollapseTitle(
      [Component.text('How do I create an account?')],
      classes: 'font-semibold after:start-5 after:end-auto pe-4 ps-12',
    ),
    CollapseContent(
      [
        Component.text('Click the "Sign Up" button in the top right corner and follow the registration process.'),
      ],
      classes: 'text-sm',
    ),
  ],
  classes: 'bg-base-100 border-base-300 border',
  modifier: CollapseModifier.arrow,
  attributes: {'tabindex': '0'},
);

Force open

How do I create an account?
Click the "Sign Up" button in the top right corner and follow the registration process.
<div tabindex="0" class="collapse collapse-open bg-base-100 border-base-300 border">
  <div class="collapse-title font-semibold">I have collapse-open class</div>
  <div class="collapse-content text-sm">Click the "Sign Up" button in the top right corner and follow the registration process.</div>
</div>
Collapse(
  [
    CollapseTitle([Component.text('I have collapse-open class')], classes: 'font-semibold'),
    CollapseContent(
      [
        Component.text('Click the "Sign Up" button in the top right corner and follow the registration process.'),
      ],
      classes: 'text-sm',
    ),
  ],
  classes: 'bg-base-100 border-base-300 border',
  modifier: CollapseModifier.open,
  attributes: {'tabindex': '0'},
);

Force close

How do I create an account?
Click the "Sign Up" button in the top right corner and follow the registration process.
<div tabindex="0" class="collapse collapse-close bg-base-100 border-base-300 border">
  <div class="collapse-title font-semibold">I have collapse-close class</div>
  <div class="collapse-content text-sm">Click the "Sign Up" button in the top right corner and follow the registration process.</div>
</div>
Collapse(
  [
    CollapseTitle([Component.text('I have collapse-close class')], classes: 'font-semibold'),
    CollapseContent(
      [
        Component.text('Click the "Sign Up" button in the top right corner and follow the registration process.'),
      ],
      classes: 'text-sm',
    ),
  ],
  classes: 'bg-base-100 border-base-300 border',
  modifier: CollapseModifier.close,
  attributes: {'tabindex': '0'},
);

Custom colors for collapse that works with focus

Use Tailwind CSS group and group-focus utilities to apply style when parent div is focused
How do I create an account?
Click the "Sign Up" button in the top right corner and follow the registration process.
<div tabindex="0" class="bg-primary text-primary-content focus:bg-secondary focus:text-secondary-content collapse">
  <div class="collapse-title font-semibold">How do I create an account?</div>
  <div class="collapse-content text-sm">Click the "Sign Up" button in the top right corner and follow the registration process.</div>
</div>
Collapse(
  [
    CollapseTitle([Component.text('How do I create an account?')], classes: 'font-semibold'),
    CollapseContent(
      [
        Component.text('Click the "Sign Up" button in the top right corner and follow the registration process.'),
      ],
      classes: 'text-sm',
    ),
  ],
  classes: 'bg-primary text-primary-content focus:bg-secondary focus:text-secondary-content',
  attributes: {'tabindex': '0'},
);

Custom colors for collapse that works with checkbox

Use Tailwind CSS peer and peer-checked utilities to apply style when sibling checkbox is checked
How do I create an account?
Click the "Sign Up" button in the top right corner and follow the registration process.
<div class="bg-base-100 border-base-300 collapse border">
  <input type="checkbox" class="peer" />
  <div class="collapse-title bg-primary text-primary-content peer-checked:bg-secondary peer-checked:text-secondary-content">How do I create an account?</div>
  <div class="collapse-content bg-primary text-primary-content peer-checked:bg-secondary peer-checked:text-secondary-content">Click the "Sign Up" button in the top right corner and follow the registration process.</div>
</div>
Collapse(
  [
    input(classes: 'peer', type: InputType.checkbox),
    CollapseTitle(
      [Component.text('How do I create an account?')],
      classes: 'bg-primary text-primary-content peer-checked:bg-secondary peer-checked:text-secondary-content',
    ),
    CollapseContent(
      [
        Component.text('Click the "Sign Up" button in the top right corner and follow the registration process.'),
      ],
      classes: 'bg-primary text-primary-content peer-checked:bg-secondary peer-checked:text-secondary-content',
    ),
  ],
  classes: 'bg-base-100 border-base-300 border',
);