open-webui/src/lib/components/common/Collapsible.svelte

39 lines
828 B
Svelte
Raw Normal View History

<script lang="ts">
export let open = false;
export let className = '';
// Manage the max-height of the collapsible content for snappy transitions
let contentElement: HTMLElement;
let maxHeight = '0px'; // Initial max-height
2024-07-04 14:15:16 +00:00
$: if (contentElement?.scrollHeight) {
if (open) {
// Ensure the element is visible before measuring
maxHeight = `${contentElement.scrollHeight}px`;
} else {
maxHeight = '0px';
}
2024-07-04 14:15:16 +00:00
}
</script>
<div class={className}>
<button on:click={() => (open = !open)}>
<slot name="head" />
</button>
2024-07-04 15:18:21 +00:00
<div
bind:this={contentElement}
class={`collapsible-content ${open ? 'mt-1' : '!mt-0'}`}
style="max-height: {maxHeight};"
>
<slot name="content" />
</div>
</div>
<style>
.collapsible-content {
overflow: hidden;
transition: all 0.3s ease-out;
max-height: 0;
}
</style>