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

Improve PopUpPanel Performance #888

Merged
merged 2 commits into from
Sep 12, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ abstract class PopUpPanel<C : HTMLElement>(
*
* @see PopUpPanel.size
*/
var size: PopUpPanelSize = PopUpPanelSize.None
var size: PopUpPanelSize = None

private val computedPositionStore: Store<ComputePositionReturn> = storeOf(obj {})

Expand Down Expand Up @@ -310,16 +310,28 @@ abstract class PopUpPanel<C : HTMLElement>(
.then { computedPositionStore.update(it) }
}

val cleanup = autoUpdate(
reference.domNode,
domNode,
options = obj { animationFrame = true },
update = { computePosition() }
)
// call it once to initialize some position definitely inside the page's content.
// otherwise the page would grow with invisible space in bottom direction.
computePosition()

afterMount { _, _ -> computePosition() }
var cleanup: () -> Unit = {}

beforeUnmount { _, _ -> cleanup.invoke() }
opened handledBy {
if (it) {
cleanup = autoUpdate(
reference.domNode,
domNode,
options = obj {
animationFrame = true
// apply workaround in order to bypass [issue](https://github.com/floating-ui/floating-ui/issues/1740)
elementResize = false
},
update = { computePosition() }
)
} else {
cleanup()
}
}

afterMount { _, _ ->
var parent: Node? = reference.domNode
Expand All @@ -337,18 +349,18 @@ abstract class PopUpPanel<C : HTMLElement>(
}

attr("data-popup-placement", computedPosition.map { it.placement ?: "" })
inlineStyle(computedPosition.map {
listOfNotNull(
"position: ${it.strategy}",
it.x?.let { x -> "left: ${x}px" },
it.y?.let { y -> "top: ${y}px" },
inlineStyle(computedPosition.map { positionReturn ->
buildString {
append("position: ${positionReturn.strategy}; ")
positionReturn.x?.let { x -> append("left: ${x}px; ") }
positionReturn.y?.let { y -> append("top: ${y}px; ") }
when (size) {
PopUpPanelSize.Min -> "min-width: ${reference.domNode.offsetWidth}px"
PopUpPanelSize.Max -> "max-width: ${reference.domNode.offsetWidth}px"
PopUpPanelSize.Exact -> "width: ${reference.domNode.offsetWidth}px"
PopUpPanelSize.None -> null
}
).joinToString("; ")
Min -> "min-width: ${reference.domNode.offsetWidth}px"
Max -> "max-width: ${reference.domNode.offsetWidth}px"
Exact -> "width: ${reference.domNode.offsetWidth}px"
None -> null
}?.let { style -> append(style) }
}
})

reference.apply {
Expand All @@ -360,7 +372,6 @@ abstract class PopUpPanel<C : HTMLElement>(
className(
opened.transform {
if (it) {
computePosition()
emit(true)
this@PopUpPanel.waitForAnimation()
} else {
Expand Down
Loading