-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCollapsibleContent.vue
More file actions
92 lines (85 loc) · 2.24 KB
/
CollapsibleContent.vue
File metadata and controls
92 lines (85 loc) · 2.24 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<script setup lang="ts">
import { CompressIcon, ExpandIcon } from '@/icons';
import { useElementSize } from '@vueuse/core';
import { NButton, NIcon } from 'naive-ui';
import { computed, nextTick, ref } from 'vue';
const props = withDefaults(
defineProps<{
collapsible?: boolean;
heightTreshPx?: number;
collapseText?: string;
expandText?: string;
showBtnText?: boolean;
}>(),
{
collapsible: true,
heightTreshPx: 150,
showBtnText: true,
}
);
const contentRef = ref<HTMLElement>();
const containerRef = ref<HTMLElement>();
const { height } = useElementSize(contentRef);
const collapsed = defineModel<boolean>({ required: false, default: true });
const isCollapsible = computed(() => props.collapsible && height.value > props.heightTreshPx);
const isCollapsed = computed(() => isCollapsible.value && collapsed.value);
async function toggleCollapse(collapse: boolean) {
collapsed.value = collapse;
if (collapse) {
nextTick(() => {
containerRef.value?.scrollIntoView({ behavior: 'smooth', block: 'center' });
});
}
}
</script>
<template>
<div ref="containerRef">
<div
:class="{ collapsed: isCollapsed }"
:style="{
maxHeight: isCollapsed ? `${heightTreshPx}px` : undefined,
overflow: 'hidden',
}"
>
<div ref="contentRef">
<slot></slot>
</div>
<div class="collapsed-shadow"></div>
</div>
<n-button
v-if="isCollapsible"
text
block
class="mt-sm"
:focusable="false"
:size="showBtnText ? undefined : 'large'"
@click.stop.prevent="() => toggleCollapse(!collapsed)"
>
<template #icon>
<n-icon :component="isCollapsed ? ExpandIcon : CompressIcon" />
</template>
<template v-if="showBtnText">
{{
isCollapsed ? expandText || $t('common.expand') : collapseText || $t('common.collapse')
}}
</template>
</n-button>
</div>
</template>
<style scoped>
.collapsed {
position: relative;
}
.collapsed-shadow {
display: none;
}
.collapsed > .collapsed-shadow {
display: block;
position: absolute;
bottom: 0;
width: 100%;
height: 10px;
box-shadow: rgba(0, 0, 0, 0.175) 0px -40px 12px -40px inset;
border-radius: var(--border-radius);
}
</style>