Skip to content

Commit 570ebba

Browse files
committed
fix edge case where getAutoDuration returned Infinity
1 parent 168378b commit 570ebba

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

src/Collapse.vue

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ import {
1818
type CSSProperties as CSS,
1919
} from 'vue'
2020
21-
import { SAFE_STYLES as safeStyles, VISUALLY_HIDDEN, AUTO_DUR_VAR } from './constants'
21+
import {
22+
SAFE_STYLES as safeStyles,
23+
VISUALLY_HIDDEN,
24+
AUTO_DUR_VAR,
25+
FALLBACK_DURATION,
26+
} from './constants'
2227
import { getTransition, getHeight, getAutoDuration, isReducedOrDisaled } from './utils'
2328
2429
export type TransitionState = 'expanding' | 'expanded' | 'collapsing' | 'collapsed'
@@ -63,7 +68,7 @@ const collapseRef = ref<HTMLElement | null>(null)
6368
const state = ref<TransitionState>(isExpanded.value ? 'expanded' : 'collapsed')
6469
const style = shallowRef<CSS>({})
6570
66-
const autoDuration = ref(300)
71+
const autoDuration = ref(FALLBACK_DURATION)
6772
const autoDurationVar = computed(() => ({ [AUTO_DUR_VAR]: `${autoDuration.value}ms` }))
6873
6974
function onExpanded() {
@@ -90,7 +95,8 @@ onMounted(() => {
9095
* Autoduration cannot be calculated if collapseRef or any ancestors
9196
* have 'display:none' on mount. In this case we cast it to 300ms.
9297
*/
93-
autoDuration.value = _autoDuration <= 0 ? 300 : _autoDuration
98+
autoDuration.value = _autoDuration <= 0 ? FALLBACK_DURATION : _autoDuration
99+
94100
style.value = isExpanded.value ? safeStyles : collapsedStyles.value
95101
})
96102

src/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ export const DEFAULT_TRANSITION = `height var(${AUTO_DUR_VAR}) cubic-bezier(0.33
44

55
export const SAFE_STYLES = { padding: 0 } as const
66

7+
export const FALLBACK_DURATION = 300
8+
79
export const VISUALLY_HIDDEN = {
810
position: 'absolute',
911
width: '1px',

src/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,7 @@ export function getAutoDuration(height = 0) {
3737
if (height === 0) return 0
3838

3939
const constant = height / 36
40-
return Math.round((4 + 15 * constant ** 0.25 + constant / 5) * 10)
40+
const autoDuration = Math.round((4 + 15 * constant ** 0.25 + constant / 5) * 10)
41+
42+
return Number.isFinite(autoDuration) ? autoDuration : 0
4143
}

0 commit comments

Comments
 (0)