Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: jumping when aligned at bottom (#69) #211

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
37 changes: 28 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,16 @@ function observePosition(el: Element) {
/**
* Update the exact position of a given element.
* @param el - An element to update the position of.
* @param debounce - Whether or not to debounce the update. After an animation is finished, it should update as soon as possible to prevent flickering on quick toggles.
*/
function updatePos(el: Element) {
function updatePos(el: Element, debounce = true) {
clearTimeout(debounces.get(el))
const optionsOrPlugin = getOptions(el)
const delay = isPlugin(optionsOrPlugin) ? 500 : optionsOrPlugin.duration
const delay = debounce
? isPlugin(optionsOrPlugin)
? 500
: optionsOrPlugin.duration
: 0
debounces.set(
el,
setTimeout(async () => {
Expand Down Expand Up @@ -339,7 +344,8 @@ function animate(el: Element) {
const isMounted = el.isConnected
const preExisting = coords.has(el)
if (isMounted && siblings.has(el)) siblings.delete(el)
if (animations.has(el)) {

if (animations.get(el)?.playState !== "finished") {
animations.get(el)?.cancel()
}
if (NEW in el) {
Expand Down Expand Up @@ -516,15 +522,24 @@ function remain(el: Element) {
if (!oldCoords) return
const pluginOrOptions = getOptions(el)
if (typeof pluginOrOptions !== "function") {
const deltaX = oldCoords.left - newCoords.left
const deltaY = oldCoords.top - newCoords.top
let deltaLeft = oldCoords.left - newCoords.left
let deltaTop = oldCoords.top - newCoords.top
const deltaRight =
oldCoords.left + oldCoords.width - (newCoords.left + newCoords.width)
const deltaBottom =
oldCoords.top + oldCoords.height - (newCoords.top + newCoords.height)

// element is probably anchored and doesn't need to be offset
if (deltaBottom == 0) deltaTop = 0
if (deltaRight == 0) deltaLeft = 0

const [widthFrom, widthTo, heightFrom, heightTo] = getTransitionSizes(
el,
oldCoords,
newCoords
)
const start: Record<string, any> = {
transform: `translate(${deltaX}px, ${deltaY}px)`,
transform: `translate(${deltaLeft}px, ${deltaTop}px)`,
}
const end: Record<string, any> = {
transform: `translate(0, 0)`,
Expand All @@ -550,7 +565,7 @@ function remain(el: Element) {
}
animations.set(el, animation)
coords.set(el, newCoords)
animation.addEventListener("finish", updatePos.bind(null, el))
animation.addEventListener("finish", updatePos.bind(null, el, false))
}

/**
Expand Down Expand Up @@ -582,7 +597,7 @@ function add(el: Element) {
animation.play()
}
animations.set(el, animation)
animation.addEventListener("finish", updatePos.bind(null, el))
animation.addEventListener("finish", updatePos.bind(null, el, false))
}

/**
Expand Down Expand Up @@ -756,7 +771,11 @@ function deletePosition(
}
if (!offsetParent) offsetParent = document.body
const parentStyles = getComputedStyle(offsetParent)
const parentCoords = coords.get(offsetParent) || getCoords(offsetParent)
const parentCoords =
!animations.has(el) || animations.get(el)?.playState === "finished"
? getCoords(offsetParent)
: coords.get(offsetParent)!

const top =
Math.round(oldCoords.top - parentCoords.top) -
raw(parentStyles.borderTopWidth)
Expand Down