Collapsible

Ein zusammenklappbares Element, um die Sichtbarkeit seines Inhalts zu ändern.

Bearbeiten

Verwenden Sie einen Button oder eine andere Komponente im Standard-Slot des Collapsible.

Verwenden Sie dann den #content-Steckplatz, um den Inhalt hinzuzufügen, der angezeigt wird, wenn der Collapsible geöffnet ist.

<template>
  <UCollapsible class="flex flex-col gap-2 w-48">
    <UButton
      label="Open"
      color="neutral"
      variant="subtle"
      trailing-icon="i-lucide-chevron-down"
      block
    />

    <template #content>
      <Placeholder class="h-48" />
    </template>
  </UCollapsible>
</template>

Unmount nicht verfügbar

Verwenden Sie die unmount-on-hide-Prop, um zu verhindern, dass der Inhalt nicht mehr eingehängt wird, wenn das Collapsible zusammengeklappt wird. Standardmäßig ist true.

<template>
  <UCollapsible :unmount-on-hide="false" class="flex flex-col gap-2 w-48">
    <UButton
      label="Open"
      color="neutral"
      variant="subtle"
      trailing-icon="i-lucide-chevron-down"
      block
    />

    <template #content>
      <Placeholder class="h-48" />
    </template>
  </UCollapsible>
</template>
Sie können das DOM inspizieren, um zu sehen, wie der Inhalt gerendert wird.

Disabled (nicht verfügbar)

Verwenden Sie die disabled-Prop, um das Collapsible zu deaktivieren.

<template>
  <UCollapsible class="flex flex-col gap-2 w-48" disabled>
    <UButton
      label="Open"
      color="neutral"
      variant="subtle"
      trailing-icon="i-lucide-chevron-down"
      block
    />

    <template #content>
      <Placeholder class="h-48" />
    </template>
  </UCollapsible>
</template>

Examples Bearbeiten

Control im offenen Zustand

Sie können den offenen Zustand mithilfe der Direktive default-open prop oder der Direktive v-model:open steuern.

<script setup lang="ts">
const open = ref(true)

defineShortcuts({
  o: () => open.value = !open.value
})
</script>

<template>
  <UCollapsible v-model:open="open" class="flex flex-col gap-2 w-48">
    <UButton
      label="Open"
      color="neutral"
      variant="subtle"
      trailing-icon="i-lucide-chevron-down"
      block
    />

    <template #content>
      <Placeholder class="h-48" />
    </template>
  </UCollapsible>
</template>
<script setup lang="ts">
import { ref } from 'vue'

const open = ref(true)

defineShortcuts({
  o: () => open.value = !open.value
})
</script>

<template>
  <UCollapsible v-model:open="open" class="flex flex-col gap-2 w-48">
    <UButton
      label="Open"
      color="neutral"
      variant="subtle"
      trailing-icon="i-lucide-chevron-down"
      block
    />

    <template #content>
      <Placeholder class="h-48" />
    </template>
  </UCollapsible>
</template>
In diesem Beispiel können Sie unter Nutzung von defineShortcuts das Kollapsible durch Drücken von: kbd{value="O"} umschalten.
Auf diese Weise können Sie den Auslöser außerhalb des Kollapsiblen verschieben oder vollständig entfernen.

Mit rotierendem Symbol

Hier ist ein Beispiel mit einem rotierenden Symbol im Button, das den offenen Zustand des Collapsible anzeigt.

<template>
  <UCollapsible class="flex flex-col gap-2 w-48">
    <UButton
      class="group"
      label="Open"
      color="neutral"
      variant="subtle"
      trailing-icon="i-lucide-chevron-down"
      :ui="{
        trailingIcon: 'group-data-[state=open]:rotate-180 transition-transform duration-200'
      }"
      block
    />

    <template #content>
      <Placeholder class="h-48" />
    </template>
  </UCollapsible>
</template>

API (Englisch)

Props Bearbeiten

Prop Default Type
as
'div'
any

The element or component this component should render as.

disabled
boolean

When true, prevents the user from interacting with the collapsible.

defaultOpen
boolean

The open state of the collapsible when it is initially rendered.
Use when you do not need to control its open state.

open
boolean

The controlled open state of the collapsible. Can be binded with v-model.

unmountOnHide
true
boolean

When true, the element will be unmounted on closed state.

ui
{ root?: SlotClass; content?: SlotClass; }

Slots Bearbeiten

Slot Type
default
{ open: boolean; }
content
{}

Emits (nicht)

Event Type
update:open
[value: boolean]

Theme Bearbeiten

app.config.ts
export default defineAppConfig({
  ui: {
    collapsible: {
      slots: {
        root: '',
        content: 'data-[state=open]:animate-[collapsible-down_200ms_var(--ease-out)] data-[state=closed]:animate-[collapsible-up_200ms_var(--ease-out)] data-[state=closed]:overflow-hidden'
      }
    }
  }
})
vite.config.ts
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import ui from '@nuxt/ui/vite'

export default defineConfig({
  plugins: [
    vue(),
    ui({
      ui: {
        collapsible: {
          slots: {
            root: '',
            content: 'data-[state=open]:animate-[collapsible-down_200ms_var(--ease-out)] data-[state=closed]:animate-[collapsible-up_200ms_var(--ease-out)] data-[state=closed]:overflow-hidden'
          }
        }
      }
    })
  ]
})

Changelog (englisch)

No recent changes