Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/plenty-spies-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"leva": patch
---

fix: update folder height when content changes dynamically
7 changes: 6 additions & 1 deletion packages/leva/src/components/ValueInput/ValueInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@ export function ValueInput({
autoComplete="off"
spellCheck="false"
value={value}
onChange={update(onChange)}
onChange={update((value) => {
onChange(value)
// Only call onUpdate for non-number inputs; number inputs retain
// their original commit behavior (on blur/Enter via dragEnd)
if (type !== "number") onUpdate(value)
})}
onFocus={() => emitOnEditStart()}
onKeyPress={onKeyPress}
onKeyDown={onKeyDown}
Expand Down
50 changes: 35 additions & 15 deletions packages/leva/src/hooks/useToggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,35 +94,55 @@ export function useToggle(toggled: boolean) {
}, [])

useEffect(() => {
// prevents first animation
const ref = wrapperRef.current!
const contentEl = contentRef.current!

const updateHeight = () => {
const { height } = contentEl.getBoundingClientRect()
if (ref.style.height !== `${height}px` && height > 0) {
ref.style.height = `${height}px`
}
}

if (!toggled) {
ref.style.height = '0px'
ref.style.overflow = 'hidden'
return
}

// prevents first animation on initial expand
if (firstRender.current) {
firstRender.current = false
updateHeight()
return
}

let timeout: number
const ref = wrapperRef.current!

const fixHeight = () => {
if (toggled) {
ref.style.removeProperty('height')
ref.style.removeProperty('overflow')
contentRef.current!.scrollIntoView({ behavior: 'smooth', block: 'nearest' })
}
ref.style.removeProperty('height')
ref.style.removeProperty('overflow')
contentEl.scrollIntoView({ behavior: 'smooth', block: 'nearest' })
}

ref.addEventListener('transitionend', fixHeight, { once: true })

const { height } = contentRef.current!.getBoundingClientRect()
ref.style.height = height + 'px'
if (!toggled) {
ref.style.overflow = 'hidden'
timeout = window.setTimeout(() => (ref.style.height = '0px'), 50)
const { height } = contentEl.getBoundingClientRect()
const currentHeight = ref.style.height
ref.style.height = `${height}px`

// If no transition will occur, call fixHeight directly
if (currentHeight === `${height}px`) {
ref.removeEventListener('transitionend', fixHeight)
fixHeight()
}

const resizeObserver = new ResizeObserver(() => {
updateHeight()
})
resizeObserver.observe(contentEl)

return () => {
ref.removeEventListener('transitionend', fixHeight)
clearTimeout(timeout)
resizeObserver.disconnect()
}
}, [toggled])

Expand Down