Skip to content

Commit a3378ce

Browse files
committed
refactor(CSidebar): update responsive behavior)
1 parent 46c9db9 commit a3378ce

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

src/components/sidebar/CSidebar.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineComponent, h } from 'vue'
1+
import { defineComponent, h, onMounted, ref } from 'vue'
22

33
const 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

Comments
 (0)