Skip to content

Commit 8de4342

Browse files
committed
force onExpanded/onCollapsed for edge cases, fix #25, 2
1 parent 5e8dc2a commit 8de4342

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

packages/vue-collapsed/src/Collapse.vue

+9
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ onMounted(() => {
137137
// Collapse / Expand handler
138138
139139
watch(isExpanded, (isExpanding) => {
140+
if (!collapseRef.value) return
141+
140142
if (isExpanding) {
141143
if (isReducedOrDisabled(collapseRef)) return onExpanded()
142144
@@ -161,7 +163,11 @@ watch(isExpanded, (isExpanding) => {
161163
})
162164
163165
requestAnimationFrame(() => {
166+
/** If for any unknown edge case the scrollHeight === 0, abort transition force expand */
167+
if (collapseRef.value!.scrollHeight === 0) return onExpanded()
168+
164169
/** Set height to scrollHeight and trigger the transition. */
170+
165171
addStyles({
166172
...getHeightProp(collapseRef),
167173
...getTransitionProp(collapseRef),
@@ -186,6 +192,9 @@ watch(isExpanded, (isExpanding) => {
186192
...getHeightProp(collapseRef),
187193
})
188194
195+
/** Same as for expand, abort transition and force collapse */
196+
if (collapseRef.value.scrollHeight === 0) return onCollapsed()
197+
189198
requestAnimationFrame(() => {
190199
/** Set height to baseHeight and trigger the transition. */
191200
addStyles({

packages/vue-collapsed/src/utils.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ export function isReducedOrDisabled(el: RefEl) {
4343
typeof window.requestAnimationFrame === 'undefined' ||
4444
window.matchMedia('(prefers-reduced-motion: reduce)').matches ||
4545
transition.includes('none') ||
46-
transition.includes('height 0s') ||
47-
el.value.scrollHeight === 0
46+
transition.includes('height 0s')
4847
)
4948
}
5049

0 commit comments

Comments
 (0)