22import { CompressIcon , ExpandIcon } from ' @/icons' ;
33import { useElementSize } from ' @vueuse/core' ;
44import { NButton , NIcon } from ' naive-ui' ;
5- import { computed , ref } from ' vue' ;
5+ import { computed , nextTick , ref } from ' vue' ;
66
77const props = withDefaults (
88 defineProps <{
@@ -20,14 +20,24 @@ const props = withDefaults(
2020);
2121
2222const contentRef = ref <HTMLElement >();
23+ const containerRef = ref <HTMLElement >();
2324const { height } = useElementSize (contentRef );
2425const collapsed = defineModel <boolean >({ required: false , default: true });
2526const isCollapsible = computed (() => props .collapsible && height .value > props .heightTreshPx );
2627const 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