Collapsible
shadcn-lit-registry/collapsibleFreeComponent
An interactive component which expands/collapses a panel. Useful for showing and hiding content with smooth animations and accessibility features.
Install it
npx shadcn@latest add https://lit-registry.lloydrichards.dev/r/collapsible.jsonSource
1import { html, LitElement, nothing, type PropertyValues } from "lit";2import { customElement, property, state } from "lit/decorators.js";3import { TW } from "@/registry/lib/tailwindMixin";4import { cn } from "@/registry/lib/utils";56const TwLitElement = TW(LitElement);78/**9 * Collapsible component properties and events10 */11export interface CollapsibleProperties {12 open?: boolean;13 defaultOpen?: boolean;14 disabled?: boolean;15}1617export interface CollapsibleTriggerProperties {18 disabled?: boolean;19}2021export interface CollapsibleContentProperties {22 forceMount?: boolean;23}2425export interface CollapsibleOpenChangeEvent extends CustomEvent {26 detail: { open: boolean };27}2829/**30 * Root collapsible container managing state31 */32@customElement("ui-collapsible")33export class Collapsible extends TwLitElement implements CollapsibleProperties {34 @property({ type: Boolean, reflect: true }) open = false;35 @property({ type: Boolean, attribute: "default-open" }) defaultOpen = false;36 @property({ type: Boolean }) disabled = false;3738 override connectedCallback() {39 super.connectedCallback();4041 // Initialize from defaultOpen42 if (this.defaultOpen && !this.hasAttribute("open")) {43 this.open = this.defaultOpen;44 }4546 this.addEventListener(47 "collapsible-trigger-click",48 this.handleTriggerClick as EventListener,49 );50 }5152 override disconnectedCallback() {53 super.disconnectedCallback();54 this.removeEventListener(55 "collapsible-trigger-click",56 this.handleTriggerClick as EventListener,57 );58 }5960 override updated(changedProperties: PropertyValues) {61 super.updated(changedProperties);6263 if (changedProperties.has("open")) {64 this.updateContent();6566 this.dispatchEvent(67 new CustomEvent("open-change", {68 detail: { open: this.open },69 bubbles: true,70 composed: true,71 }),72 );73 }74 }7576 private handleTriggerClick = () => {77 if (!this.disabled) {78 this.open = !this.open;79 }80 };8182 private updateContent() {83 const content = this.querySelector(84 "ui-collapsible-content",85 ) as CollapsibleContent | null;86 if (content) {87 content._open = this.open;88 }89 }9091 /**92 * Public method to toggle open/closed state93 */94 public toggle() {95 if (!this.disabled) {96 this.open = !this.open;97 }98 }99100 /**101 * Public method to open the collapsible102 */103 public show() {104 if (!this.disabled) {105 this.open = true;106 }107 }108109// … truncated
What it pulls in
npm packages
Files it writes
More from shadcn-lit-registry
All 26 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Accordionaccordion | shadcn-lit-registry | Components | Free | 1 dep · 2 files | |
| Avataravatar | shadcn-lit-registry | Components | Free | no deps · 2 files | |
| Badgebadge | shadcn-lit-registry | Components | Free | no deps · 2 files | |
| Buttonbutton | shadcn-lit-registry | Components | Free | 1 dep · 2 files | |
| Cardcard | shadcn-lit-registry | Components | Free | no deps · 2 files | |
| Checkboxcheckbox | shadcn-lit-registry | Components | Free | 1 dep · 2 files | |
| Commandcommand | shadcn-lit-registry | Components | Free | 1 dep · 2 files | |
| Context Menucontext-menu | shadcn-lit-registry | Components | Free | 2 deps · 2 files |
