1
- import { defineComponent , h } from 'vue'
1
+ import { defineComponent , h , onMounted , ref } from 'vue'
2
2
3
3
const CSidebar = defineComponent ( {
4
4
name : 'CSidebar' ,
@@ -71,7 +71,31 @@ const CSidebar = defineComponent({
71
71
default : undefined ,
72
72
} ,
73
73
} ,
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
+
75
99
return ( ) =>
76
100
h (
77
101
'div' ,
@@ -87,10 +111,11 @@ const CSidebar = defineComponent({
87
111
} `] : props . selfHiding ,
88
112
[ `sidebar-${ props . size } ` ] : props . size ,
89
113
'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 ,
92
116
} ,
93
117
] ,
118
+ ref : sidebarRef ,
94
119
} ,
95
120
slots . default && slots . default ( ) ,
96
121
)
0 commit comments