1- import { defineComponent , h } from 'vue'
1+ import { defineComponent , h , onMounted , ref } from 'vue'
22
33const CSidebar = defineComponent ( {
44 name : 'CSidebar' ,
@@ -71,7 +71,31 @@ const CSidebar = defineComponent({
7171 default : undefined ,
7272 } ,
7373 } ,
74- setup ( props , { slots } ) {
74+ emits : [ 'visible-change' ] ,
75+ setup ( props , { slots, emit } ) {
76+ const sidebarRef = ref ( )
77+ const visible = ref ( )
78+
79+ const options = {
80+ rootMargin : '0px' ,
81+ threshold : 1.0 ,
82+ }
83+
84+ const callback = ( entries : IntersectionObserverEntry [ ] ) => {
85+ entries . forEach ( ( entry ) => {
86+ if ( entry . isIntersecting !== props . visible ) {
87+ visible . value = entry . isIntersecting
88+ emit ( 'visible-change' , visible . value )
89+ }
90+ } )
91+ }
92+
93+ const observer = new IntersectionObserver ( callback , options )
94+
95+ onMounted ( ( ) => {
96+ observer . observe ( sidebarRef . value )
97+ } )
98+
7599 return ( ) =>
76100 h (
77101 'div' ,
@@ -87,10 +111,11 @@ const CSidebar = defineComponent({
87111 } `] : props . selfHiding ,
88112 [ `sidebar-${ props . size } ` ] : props . size ,
89113 'sidebar-narrow-unfoldable' : props . unfoldable ,
90- show : props . visible === true ,
91- hide : props . visible === false ,
114+ show : props . visible === true && visible . value === false ,
115+ hide : props . visible === false && visible . value === true ,
92116 } ,
93117 ] ,
118+ ref : sidebarRef ,
94119 } ,
95120 slots . default && slots . default ( ) ,
96121 )
0 commit comments