File size: 2,367 Bytes
96dd062
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
---
import { Icon } from "astro-icon/components";
import I18nKey from "@/i18n/i18nKey";
import { i18n } from "@/i18n/translation";

interface Props {
	id: string;
	name?: string;
	isCollapsed?: boolean;
	collapsedHeight?: string;
	class?: string;
	style?: string;
}
const { id, name, isCollapsed, collapsedHeight, style } = Astro.props;
const className = Astro.props.class;
---
<widget-layout data-id={id} data-is-collapsed={String(isCollapsed)} class={"pb-4 card-base " + className} style={style}>

    {name && (

        <div class="widget-title font-bold transition text-lg text-neutral-900 dark:text-neutral-100 relative ml-8 mt-4 mb-2

            before:w-1 before:h-4 before:rounded-md before:bg-(--primary)

            before:absolute before:left-[-16px] before:top-[5.5px] flex items-center justify-between">

            <span class="widget-name">{name}</span>

            <slot name="title-icon" />

        </div>

    )}

    <div id={id} class:list={["collapse-wrapper px-4 overflow-hidden", {"collapsed": isCollapsed, "pt-4": !name}]}>

        <slot></slot>

    </div>

    {isCollapsed && <div class="expand-btn px-4 -mb-2">

        <button class="btn-plain rounded-lg w-full h-9" title={i18n(I18nKey.more)} aria-label={i18n(I18nKey.more)}>

            <div class="text-(--primary) flex items-center justify-center gap-2 -translate-x-2">

                <Icon name="material-symbols:more-horiz" class="text-[1.75rem]" aria-hidden="true"></Icon> {i18n(I18nKey.more)}

            </div>

        </button>

    </div>}

</widget-layout>

<style define:vars={{ collapsedHeight }}>

    .collapsed {

        height: var(--collapsedHeight);

    }

</style>

<script>

    class WidgetLayout extends HTMLElement {

        constructor() {

            super();



            if (this.dataset.isCollapsed !== "true")

                return;



            const id = this.dataset.id;

            const btn = this.querySelector('.expand-btn');

            const wrapper = this.querySelector(`#${id}`)

            btn!.addEventListener('click', () => {

                wrapper!.classList.remove('collapsed');

                btn!.classList.add('hidden');

            })

        }

    }



    if (!customElements.get("widget-layout")) {

        customElements.define("widget-layout", WidgetLayout);

    }

</script>