AccordionItem
A heading that shows and hides a region of content. Use several inside an Accordion.
The preview needs JavaScript. The code is in the Code tab.
<template>
<Accordion>
<AccordionItem>
<span slot="title">What is a contract?</span>
A component's description of itself: variants, states, slots and events.
</AccordionItem>
<AccordionItem open>
<span slot="title">Who checks it?</span>
The compiler, on every build.
</AccordionItem>
</Accordion>
</template>
Installation
npx @pixelscriptio/tessera add AccordionItempnpm exec tessera add AccordionItemyarn tessera add AccordionItembunx @pixelscriptio/tessera add AccordionItem- Works with Tessera 0.9.0 or newer.
Copy and paste the code into your project
Put it in
system/components.system/components/AccordionItem.tess <contract> { "name": "AccordionItem", "role": "generic", "description": "A heading that shows and hides a region of content. Use several inside an Accordion.", "since": "0.5.0", "parts": ["root", "heading", "trigger", "title", "icon", "content"], "states": ["hover", "focus-visible", "expanded", "disabled"], "behavior": "disclosure", "slots": { "title": "the text of the heading, which is also the button that shows the content", "default": "the content that is shown and hidden" }, "events": { "expand": "fired with { expanded, source } when the person opens or closes it; the open prop sets it from outside" }, "preview": { "props": { "open": true }, "slots": { "title": "Section title", "default": "The content that is shown and hidden." } } } </contract> <script> const { open, disabled } = props({ open: false, disabled: false }) // The component owns the state, starts from the prop and follows it when it changes. const isOpen = state(open.value) effect(() => { isOpen.value = open.value }) function change(detail) { isOpen.value = detail.expanded emit('expand', detail) } </script> <template> <div class="root"> <h3 class="heading"> <button class="trigger" type="button" :aria-expanded="isOpen ? 'true' : 'false'" :disabled="disabled" @expand="change($event)" > <span class="title"><slot name="title" /></span> <span class="icon" aria-hidden="true">{isOpen ? 'โ' : '+'}</span> </button> </h3> <div class="content" :hidden="!isOpen"><slot /></div> </div> </template> <style> .root { border-bottom: 1px solid $color.border.default; color: $color.text.default; font-family: var(--t-font-family-sans); } .heading { margin: 0; font-size: $font.size.base; font-weight: $font.weight.bold; } .trigger { display: flex; align-items: center; justify-content: space-between; gap: $space.4; width: 100%; padding: $space.4 0; border: 0; background: transparent; color: $color.text.default; font: inherit; text-align: left; cursor: pointer; &:hover:not(:disabled) { color: $color.action.primary.text; } &:focus-visible { outline: 2px solid $color.focus.ring; outline-offset: 2px; } &:disabled { opacity: $opacity.disabled; cursor: not-allowed; } } .icon { flex: none; color: $color.text.muted; } .content { padding: 0 0 $space.4; color: $color.text.muted; font-size: $font.size.base; line-height: 1.6; } .content[hidden] { display: none; } </style>Check your design tokens
Your design system needs these.
tessera addreports the ones that are missing; by hand, runnpx @pixelscriptio/tessera check.color.action.primary.textcolor.border.defaultcolor.focus.ringcolor.text.defaultcolor.text.mutedfont.size.basefont.weight.boldopacity.disabledspace.4
Usage
Using AccordionItem
An item is a heading that shows and hides a region of content. Put it in an Accordion when there are several.
- Fill the
titleslot with the heading text, and the default slot with the content. - It starts closed. Set
opento start it open, or to change it from outside. - The
expandevent says where it is going:{ expanded, source }. - The heading is an
h3. Use it where anh3fits the page outline.
The preview needs JavaScript. The code is in the Code tab.
<template>
<Accordion>
<AccordionItem>
<span slot="title">What is a contract?</span>
A component's description of itself: variants, states, slots and events.
</AccordionItem>
<AccordionItem open>
<span slot="title">Who checks it?</span>
The compiler, on every build.
</AccordionItem>
</Accordion>
</template>
API reference
Slots
| Slot | Content |
|---|---|
title | the text of the heading, which is also the button that shows the content |
default | the content that is shown and hidden |
Events
| Event | When |
|---|---|
expand | fired with { expanded, source } when the person opens or closes it; the open prop sets it from outside |
Anatomy
| Role | generic |
| Behavior | disclosure |
| Parts | root heading trigger title icon content |
| States | hover focus-visible expanded disabled |
| Since | 0.5.0 |
Design tokens it uses
Your design system needs these. tessera add checks that it has them, and can add the ones it lacks.
color.action.primary.text color.border.default color.focus.ring color.text.default color.text.muted font.size.base font.weight.bold opacity.disabled space.4
Checksum
271514db3c76efde0611ce29153c66b077722b4c2852daee78411fac1c355fac