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