Skip to content

Commit fb7423f

Browse files
committed
Auto-scroll collapsible content on collapse
Fixes #1478
1 parent 71759b1 commit fb7423f

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

Tekst-Web/src/components/CollapsibleContent.vue

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { CompressIcon, ExpandIcon } from '@/icons';
33
import { useElementSize } from '@vueuse/core';
44
import { NButton, NIcon } from 'naive-ui';
5-
import { computed, ref } from 'vue';
5+
import { computed, nextTick, ref } from 'vue';
66
77
const props = withDefaults(
88
defineProps<{
@@ -20,14 +20,24 @@ const props = withDefaults(
2020
);
2121
2222
const contentRef = ref<HTMLElement>();
23+
const containerRef = ref<HTMLElement>();
2324
const { height } = useElementSize(contentRef);
2425
const collapsed = defineModel<boolean>({ required: false, default: true });
2526
const isCollapsible = computed(() => props.collapsible && height.value > props.heightTreshPx);
2627
const isCollapsed = computed(() => isCollapsible.value && collapsed.value);
28+
29+
async function toggleCollapse(collapse: boolean) {
30+
collapsed.value = collapse;
31+
if (collapse) {
32+
nextTick(() => {
33+
containerRef.value?.scrollIntoView({ behavior: 'smooth', block: 'center' });
34+
});
35+
}
36+
}
2737
</script>
2838

2939
<template>
30-
<div>
40+
<div ref="containerRef">
3141
<div
3242
:class="{ collapsed: isCollapsed }"
3343
:style="{
@@ -47,7 +57,7 @@ const isCollapsed = computed(() => isCollapsible.value && collapsed.value);
4757
class="mt-sm"
4858
:focusable="false"
4959
:size="showBtnText ? undefined : 'large'"
50-
@click.stop.prevent="collapsed = !collapsed"
60+
@click.stop.prevent="() => toggleCollapse(!collapsed)"
5161
>
5262
<template #icon>
5363
<n-icon :component="isCollapsed ? ExpandIcon : CompressIcon" />

0 commit comments

Comments
 (0)