Accordion
v0.0.10
mix mishka.ui.gen.component accordion
Overview of the Phoenix and LiveView Accordion Component
The Mishka Chelekom Accordion module offers a wide variety of styles and colors for Phoenix, this module allows you to add different variants and colors for a personalized appearance. With options to include icons and media, the accordion is ideal for managing and showcasing data efficiently. Its ability to divide content into collapsible sections provides a clean and functional way to present large volumes of information.
Overview of the Phoenix and LiveView Accordion Component
The Mishka Chelekom Accordion component also seamlessly integrates with Phoenix LiveView applications, offering enhanced interactivity. With this Phoenix LiveView component users can show or hide content effortlessly, while the module’s flexibility with colors and styles ensures that it fits within any design. Whether you’re adding icons, media, or simply dividing complex data into more manageable sections, the accordion component is a versatile solution for improving both functionality and user experience.
How to use Phoenix and LiveView accordion component
The Mishka Chelekom Accordion module includes a required :item slot, which defines each individual accordion entry. This slot gives you control over elements like the title, description, icons, and more, allowing for easy customization of your accordion items to suit your design and content needs. Each item has several attributes:
- title (Required): Specifies the title of the accordion item, which is displayed when the accordion is closed.
- description: A brief description that appears under the title, offering additional context.
- icon: Adds an icon next to the title, enhancing visual representation.
- image: Displays an image next to the title.
- alt: The alternative text of the image. Describe the picture for screen reader users. Leave it out when the image only decorates the item: the image is then drawn with an empty
altand screen readers skip it. - open: A boolean that controls whether the accordion item is open
trueor closedfalseby default. - icon_class, icon_wrapper_class, image_class, trigger_class and content_class: Tailwind classes for the icon, the icon wrapper, the image, the clickable header and the panel content of that item.
You can check other props of this slot in the props tab.
How to use Phoenix and LiveView accordion component
<!-- an id is optional; set a unique one on each .accordion to keep the id stable -->
<.accordion id="example">
<:item
id="item1"
title="Custom Accordion Item one"
description="Description of slot"
>
<!--Content goes here-->
</:item>
<:item
id="item2"
title="Custom Accordion Item two"
description="Description of slot"
>
<!--Content goes here-->
</:item>
</.accordion>
<.accordion id="example-one" initial_open={["icon1"]}>
<:item
id="icon1"
title="Custom Accordion Item one"
description="Description of slot"
icon="hero-chat-bubble-left-right-solid"
>
<!--Content goes here-->
</:item>
<:item
id="icon2"
title="Custom Accordion Item two"
description="Description of slot"
icon="hero-chat-bubble-oval-left-ellipsis-solid"
>
<!--Content goes here-->
</:item>
</.accordion>Base & Base separated Variant
Base & Base separated Variant
<!-- an id is optional; set a unique one on each .accordion to keep the id stable -->
<.accordion id="accordion-base" variant="base">
<:item
title="Tinted Split variant item one"
>
<!--Content goes here-->
</:item>
<:item
title="Tinted Split variant item two"
>
<!--Content goes here-->
</:item>
</.accordion>
<.accordion id="accordion-base-separated" variant="base_separated">
<:item
title="Tinted Split variant item one"
>
<!--Content goes here-->
</:item>
<:item
title="Tinted Split variant item two"
>
<!--Content goes here-->
</:item>
</.accordion>Default Variant
The default variant includes basic styles with a background and borders, providing a clean and simple appearance. You can customize it with different color options, including natural, primary, secondary, success, warning, danger, info, silver, misc, and dawn to match your design needs.
Default Variant
<!-- an id is optional; set a unique one on each .accordion to keep the id stable -->
<.accordion id="example-two" color="secondary">
<:item
id="sec1"
title="Default variant item one"
>
<!--Content goes here-->
</:item>
<:item
id="sec2"
title="Default variant item two"
>
<!--Content goes here-->
</:item>
</.accordion>outline Variant
The outline variant applies borders only with no background color, giving the component a clean and minimal look. You can customize the border colors with options like primary, secondary, natural, success, warning, danger, info, silver, misc, and dawn.
outline Variant
<!-- an id is optional; set a unique one on each .accordion to keep the id stable -->
<.accordion id="example-three" variant="outline">
<:item
id="out1"
title="outline variant item one"
>
<!--Content goes here-->
</:item>
<:item
id="out2"
title="outline variant item two"
>
<!--Content goes here-->
</:item>
</.accordion>bordered Variant
The bordered variant includes a background color and supports various customizable color options. You can customize the background colors with options like primary, secondary, natural, success, warning, danger, info, silver, misc, and dawn.
bordered Variant
<!-- an id is optional; set a unique one on each .accordion to keep the id stable -->
<.accordion id="example-four" variant="bordered">
<:item
id="bord1"
title="bordered variant item one"
>
<!--Content goes here-->
</:item>
<:item
id="bord2"
title="bordered variant item two"
>
<!--Content goes here-->
</:item>
</.accordion>Outline separated Variant
The outline separated variant has a customizable border colors. You can customize the background colors with options like primary, secondary, natural, success, warning, danger, info, silver, misc, and dawn.
Outline separated Variant
<!-- an id is optional; set a unique one on each .accordion to keep the id stable -->
<.accordion id="example-five" variant="outline_separated">
<:item
id="osep1"
title="outline_separated variant item one"
>
<!--Content goes here-->
</:item>
<:item
id="osep2"
title="outline_separated variant item two"
>
<!--Content goes here-->
</:item>
</.accordion>Bordered separated Variant
The bordered_separated variant includes both a background color and a border for each item, with space between them for a clean, structured layout. You can customize the background colors using options such as primary, secondary, natural, success, warning, danger, info, silver, misc, and dawn.
Bordered separated Variant
<!-- an id is optional; set a unique one on each .accordion to keep the id stable -->
<.accordion id="example-six" variant="bordered_separated">
<:item
id="bsep1"
title="Bordered separated variant item one"
>
<!--Content goes here-->
</:item>
<:item
id="bsep2"
title="Bordered separated variant item two"
>
<!--Content goes here-->
</:item>
</.accordion>Transparent Variant
The transparent variant features transparent borders and background, with only the content's color changing. You can customize the content color using options such as primary, secondary, natural, success, warning, danger, info, silver, misc, and dawn. Each item maintains a clean, minimal layout with space between items.
Transparent Variant
<!-- an id is optional; set a unique one on each .accordion to keep the id stable -->
<.accordion id="example-seven" variant="transparent">
<:item
id="trans1"
title="Transparent variant item one"
>
<!--Content goes here-->
</:item>
<:item
id="trans2"
title="Transparent variant item two"
>
<!--Content goes here-->
</:item>
</.accordion>Shadow Variant
Shadow Variant
<!-- an id is optional; set a unique one on each .accordion to keep the id stable -->
<.accordion id="accordion-shadow" variant="shadow">
<:item
title="Shadow variant item one"
>
<!--Content goes here-->
</:item>
<:item
title="Shadow variant item two"
>
<!--Content goes here-->
</:item>
<:item
title="Shadow variant item three"
>
<!--Content goes here-->
</:item>
</.accordion>Gradient Variant
Gradient Variant
<!-- an id is optional; set a unique one on each .accordion to keep the id stable -->
<.accordion id="accordion-gradient" variant="gradient">
<:item
title="Gradient variant item one"
>
<!--Content goes here-->
</:item>
<:item
title="Gradient variant item two"
>
<!--Content goes here-->
</:item>
<:item
title="Gradient variant item three"
>
<!--Content goes here-->
</:item>
</.accordion>Padding prop in Phoenix LiveView Accordion Component
The padding prop offers values such as extra_small, small, medium, large, and extra_large, allowing for flexible adjustment of the padding applied to the content and summary sections of the accordion. These padding options enable the creation of either compact or spacious layouts.
Padding prop in Phoenix LiveView Accordion Component
<!-- an id is optional; set a unique one on each .accordion to keep the id stable -->
<.accordion id="example-nine" padding="medium">
<:item
id="pad1"
title="outline variant item one"
>
<!--Content goes here-->
</:item>
<:item
id="pad2"
title="outline variant item two"
>
<!--Content goes here-->
</:item>
</.accordion>Size prop in Phoenix LiveView Accordion Component
The size prop adjusts the text size of the accordion and uses Tailwind's text size classes to apply these changes. It offers values such as extra_small, small, medium, large, and extra_large.
Size prop in Phoenix LiveView Accordion Component
<!-- an id is optional; set a unique one on each .accordion to keep the id stable -->
<.accordion id="example-ten" size="large">
<:item
id="size1"
title="Size prop item one"
>
<!--Content goes here-->
</:item>
<:item
id="size2"
title="Size prop item two"
>
<!--Content goes here-->
</:item>
<:item
id="size3"
title="Size prop item three"
>
<!--Content goes here-->
</:item>
</.accordion>Space prop
The space prop is designed for the outline_separated and bordered_separated variants. It controls the space between each accordion item, helping to create a more organized and distinct layout, ensuring that items remain clearly outline_separated. It offers values such as extra_small, small, medium, large, and extra_large.
Space prop
<!-- an id is optional; set a unique one on each .accordion to keep the id stable -->
<.accordion id="example-11" space="large">
<:item
id="space1"
title="Space prop item one"
>
<!--Content goes here-->
</:item>
<:item
id="space2"
title="Space prop item two"
>
<!--Content goes here-->
</:item>
<:item
id="space3"
title="Space prop item three"
>
<!--Content goes here-->
</:item>
</.accordion>Rounded prop
The rounded prop offers values like extra_small, small, medium, large, and extra_large. These values control the border radius of the accordion items, allowing for different levels of rounding from subtle to more pronounced corners, depending on the design needs.
Rounded prop
<!-- an id is optional; set a unique one on each .accordion to keep the id stable -->
<.accordion id="example-12" rounded="small">
<:item
id="round1"
title="Rounded prop item one"
>
<!--Content goes here-->
</:item>
<:item
id="round2"
title="Rounded prop item two"
>
<!--Content goes here-->
</:item>
<:item
id="round3"
title="Rounded prop item three"
>
<!--Content goes here-->
</:item>
</.accordion>Media size prop
The media_size prop allows control over the width and height of media elements, such as images, in the summary section of the accordion. It accepts values like extra_small, small, medium, large, and extra_large. Passing the media_size prop to the .accordion will apply the specified size to all images in the item slots, ensuring consistent image dimensions across the accordion. Each item's alt is written on its image; an image with no alt is drawn with alt="", which tells screen readers to skip it.
Media size prop
<!-- an id is optional; set a unique one on each .accordion to keep the id stable -->
<.accordion id="example-13" media_size="extra_small">
<:item
title="Media size prop item one"
image="/path/to/image"
alt="User avatar"
>
<!--Content goes here-->
</:item>
<:item
title="Media size prop item two"
image="/path/to/image"
alt="User avatar"
>
<!--Content goes here-->
</:item>
</.accordion>Chevron props
The chevron_icon prop allows customization of the accordion's chevron icon using LiveView Hero Icons. Additionally, options like left_chevron, right_chevron, and hide_chevron enable control over the chevron’s position or visibility. By default, the chevron icon is positioned on the right; left_chevron and chevron_position="left" move it to the left.
Chevron props
<!-- an id is optional; set a unique one on each .accordion to keep the id stable -->
<.accordion id="example-14" chevron_icon="hero-cog-8-tooth">
<:item
id="chev1"
title="Chevron prop item one"
>
<!--Content goes here-->
</:item>
</.accordion>
<.accordion id="example-15" left_chevron>
<:item
id="chev2"
title="Left chevron item one"
>
<!--Content goes here-->
</:item>
</.accordion>
<.accordion id="example-16" hide_chevron>
<:item
id="chev3"
title="Hide chevron item one"
>
<!--Content goes here-->
</:item>
</.accordion>One item open at a time
An accordion keeps one item open by default: opening an item closes the one that was open. Set multiple to let several stay open, duration to change the open and close animation, in milliseconds (200 by default), and collapsible={false} to stop the last open item from being closed. Every header is a button that carries aria-expanded, and Arrow Up, Arrow Down, Home and End move focus between the headers.
One item open at a time
<.accordion id="faq" variant="bordered">
<:item title="First item">Opening the second item closes this one.</:item>
<:item title="Second item">Opening the first item closes this one.</:item>
</.accordion>
<.accordion id="faq-many" variant="bordered" multiple duration={300}>
<:item title="First item">Several items can stay open.</:item>
<:item title="Second item">Open this one as well.</:item>
</.accordion>Multiple Items with Initial Open State
The accordion can handle multiple items with specific ones open initially. Use the initial_open prop to specify which items should be open when the component mounts, or set open={expr} on individual items.
Multiple Items with Initial Open State
<!-- Multiple items with initial open state -->
<.accordion
id="example-multiple-open"
variant="bordered_separated"
color="primary"
multiple={true}
initial_open={["item1", "item3"]}
>
<:item
id="item1"
title="First Item (Initially Open)"
icon="hero-document-text"
>
This is the first accordion item that is initially open.
</:item>
<:item
id="item2"
title="Second Item (Initially Closed)"
icon="hero-cog-6-tooth"
>
This item starts closed and can be opened by clicking.
</:item>
<:item
id="item3"
title="Third Item (Initially Open)"
icon="hero-star"
>
This is another initially open item.
</:item>
<:item
id="item4"
title="Fourth Item (Initially Closed)"
icon="hero-heart"
>
This final item demonstrates the complete flexibility.
</:item>
</.accordion>Server Events Integration
Enable server-side event handling by setting server_events={expr}. This allows your LiveView to respond to accordion open/close events, perfect for analytics, state synchronization, or triggering server-side actions. The events go to the LiveComponent that renders the accordion, or to the LiveView when the accordion is not inside one.
The example below is drawn live, but nothing on this page handles accordion-handler, so the events it sends are ignored. The code ends with the clause that receives them.
Server Events Integration
<!-- Server events integration -->
<.accordion
id="server-events-accordion"
variant="outline_separated"
color="info"
server_events={true}
event_handler="accordion-handler"
space="medium"
>
<:item
id="analytics"
title="Analytics Tracking"
description="Track user interactions with server events"
icon="hero-chart-bar"
>
When this item is opened or closed, an event is sent to the LiveView server.
</:item>
<:item
id="notifications"
title="Real-time Notifications"
description="Trigger server actions on accordion interactions"
icon="hero-bell"
>
Server events enable real-time features like notifications and updates.
</:item>
<:item
id="sync"
title="State Synchronization"
description="Keep server and client state in sync"
icon="hero-arrow-path"
>
Perfect for maintaining consistent state across components.
</:item>
</.accordion>
# In the LiveView or LiveComponent that renders the accordion
def handle_event("accordion-handler", %{"open_ids" => open_ids}, socket) do
{:noreply, assign(socket, :open_items, open_ids)}
endAttributes
| Name | Type | Default | Values | Required | Description |
|---|---|---|---|---|---|
id
|
string
| — | — | No | A unique identifier for the accordion |
class
|
any
| — | — | No | Custom CSS class for additional styling |
multiple
|
boolean
|
false
| — | No | Allow multiple panels open simultaneously |
collapsible
|
boolean
|
true
| — | No | Allow all panels to be closed |
duration
|
integer
|
200
| — | No | Animation duration in milliseconds |
keep_mounted
|
boolean
|
false
| — | No | Keep content mounted after first open |
server_events
|
boolean
|
false
| — | No | Send open/close events to LiveView |
event_handler
|
string
| — | — | No | Specify event handler for accordion events |
initial_open
|
list
|
[]
| — | No | List of initially open item IDs |
variant
|
string
|
base
|
defaultborderedoutlineshadowgradientoutline_separatedbordered_separatedtransparentbasebase_separated
| No | Visual style variant |
color
|
string
|
natural
|
basenaturalwhiteprimarysecondarydarksuccesswarningdangerinfosilvermiscdawn
| No | Color theme |
size
|
string
|
medium
|
extra_smallsmallmediumlargeextra_large
| No | Overall size |
rounded
|
string
|
medium
|
extra_smallsmallmediumlargeextra_largefullnone
| No | Border radius |
border
|
string
|
extra_small
| — | No | Border style |
space
|
string
|
small
|
extra_smallsmallmediumlargeextra_largenone
| No | Space between separated items |
media_size
|
string
|
small
| — | No | Size of media elements like icons and images |
padding
|
string
|
small
|
extra_smallsmallmediumlargeextra_large
| No | Padding for accordion items |
chevron_icon
|
string
|
hero-chevron-down
| — | No | Chevron icon |
chevron_position
|
string
|
right
| — | No | Chevron position (left/right) |
left_chevron
|
boolean
|
false
| — | No | Position chevron on the left |
right_chevron
|
boolean
|
false
| — | No | Position chevron on the right |
hide_chevron
|
boolean
|
false
| — | No | Hide the chevron icon |
chevron_class
|
string
| — | — | No | Additional CSS classes for the chevron |
Slots
| Name | Required | Description |
|---|---|---|
item
| No | Accordion items as slots |
Attributes of the item slot
| Name | Type | Default | Values | Required | Description |
|---|---|---|---|---|---|
id
|
string
| — | — | No | Unique identifier for the item (auto-generated if not provided) |
title
|
string
| — | — | Yes | Title of the accordion item |
description
|
string
| — | — | No | Optional description/subtitle |
icon
|
string
| — | — | No | Optional icon name |
icon_class
|
string
| — | — | No | Additional CSS classes for the item icon |
icon_wrapper_class
|
string
| — | — | No | Additional CSS classes for the item icon wrapper |
image
|
string
| — | — | No | Optional image source URL |
alt
|
string
| — | — | No | Alternative text of the image, an empty string marks it as decorative |
image_class
|
string
| — | — | No | Additional CSS classes for the image |
trigger_class
|
string
| — | — | No | Additional CSS classes for the trigger |
content_class
|
string
| — | — | No | Additional CSS classes for the content |
open
|
boolean
| — | — | No | Whether this item should be initially open especially when you do not set id manually |
Options
Each option takes one or more of its values, separated by commas.
| Option | Values |
|---|---|
--variant
|
defaultborderedoutlineshadowgradientoutline_separatedbordered_separatedtransparentbasebase_separated
|
--color
|
basenaturalwhiteprimarysecondarydarksuccesswarningdangerinfosilvermiscdawn
|
--size
|
extra_smallsmallmediumlargeextra_large
|
--padding
|
extra_smallsmallmediumlargeextra_large
|
--space
|
extra_smallsmallmediumlargeextra_largenone
|
--type
|
accordion
|
--rounded
|
extra_smallsmallmediumlargeextra_largefullnone
|
--module
| A module name of your own |
What it generates
mix mishka.ui.gen.component accordion