Files
fnx_web/svelte/src/util/Expandable.svelte
T

105 lines
1.9 KiB
Svelte
Raw Normal View History

2025-10-13 16:05:50 +02:00
<script lang="ts">
import { stopPropagation } from 'svelte/legacy';
2021-05-11 16:14:16 +02:00
export const toggle = () => {
expanded = !expanded
}
2022-01-17 13:36:00 +01:00
const header_click = () => {
if (click_expand) {
toggle()
}
}
const keypress = e => {
if (e.code === "Space") {
if (click_expand) {
toggle()
}
}
}
2025-10-13 16:05:50 +02:00
let {
expanded = $bindable(false),
click_expand = false,
highlight = false,
header,
children
}: {
// Whether the Expandable is expanded by default
expanded?: boolean;
// Allow the user to expand the Expandable by clicking the title. Use
// stopPropagation if you want to use other interactive elements in the
// title bar
click_expand?: boolean;
// Highlight the title bar if the user moves their mouse over it
highlight?: boolean;
header?: import('svelte').Snippet;
children?: import('svelte').Snippet;
} = $props();
2021-05-11 16:14:16 +02:00
</script>
<div class="expandable">
2023-09-14 12:29:06 +02:00
<div
class="header"
class:click_expand
class:highlight
role="button"
tabindex="0"
2025-10-13 16:05:50 +02:00
onclick={header_click}
onkeypress={keypress}
2023-09-14 12:29:06 +02:00
>
2021-05-11 16:14:16 +02:00
<div class="title">
2025-10-13 16:05:50 +02:00
{@render header?.()}
2021-05-11 16:14:16 +02:00
</div>
2025-10-13 16:05:50 +02:00
<button class="bucket_expand" onclick={stopPropagation(toggle)}>
2021-05-11 16:14:16 +02:00
{#if expanded}
<i class="icon">expand_less</i>
2021-05-11 16:14:16 +02:00
{:else}
<i class="icon">expand_more</i>
2021-05-11 16:14:16 +02:00
{/if}
</button>
</div>
{#if expanded}
<div class="body">
2025-10-13 16:05:50 +02:00
{@render children?.()}
</div>
{/if}
2021-05-11 16:14:16 +02:00
</div>
<style>
.expandable {
text-decoration: none;
2022-04-01 20:27:38 +02:00
background-color: var(--card_color);
2022-01-17 13:36:00 +01:00
margin: 0.6em 0;
2022-01-11 13:28:22 +01:00
border-radius: 6px;
2021-08-18 13:00:07 +02:00
overflow: hidden;
2021-05-11 16:14:16 +02:00
}
.header {
display: flex;
flex-direction: row;
2022-03-29 21:41:46 +02:00
color: var(--body_text_color);
2021-05-11 16:14:16 +02:00
}
2022-01-17 13:36:00 +01:00
.click_expand:hover, .highlight:hover {
2022-03-29 21:41:46 +02:00
background: var(--input_background);
color: var(--input_text);
2021-05-11 16:14:16 +02:00
}
2022-01-17 13:36:00 +01:00
.click_expand {
cursor: pointer;
}
2021-05-11 16:14:16 +02:00
.title {
flex: 1 1 auto;
text-align: left;
}
.bucket_expand {
flex: 0 0 auto;
align-self: center;
height: auto;
}
.body {
2023-11-09 10:44:17 +01:00
padding: 0.5em;
2022-03-29 21:41:46 +02:00
border-top: 1px solid var(--separator);
2021-05-11 16:14:16 +02:00
}
</style>