-
Notifications
You must be signed in to change notification settings - Fork 0
feature: Add Element Transitions and Click Outside detection to uni-dom #410
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
base: main
Are you sure you want to change the base?
Conversation
Add CSS-class-based transition system (Transition) driven by Rx[Boolean] visibility, and click outside detection (ClickOutside) for dismissing dropdowns/modals. Closes #409. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Summary of ChangesHello @xerial, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces valuable Transition and ClickOutside features to uni-dom. The implementation is well-structured and follows the existing patterns in the library. The accompanying tests are thorough and cover the new functionalities well. I've identified a couple of potential race conditions and a null-safety issue in the new code. Addressing these points will enhance the robustness of these new features. Overall, this is a great contribution.
| val target = me.target.asInstanceOf[dom.Node] | ||
| if !elem.contains(target) then | ||
| callback(me) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The event.target property can be null in some edge cases. The current code's asInstanceOf[dom.Node] on a null value would result in null, and !elem.contains(null) evaluates to true, incorrectly triggering the onClickOutside callback. It's safer to add a null check to prevent this.
val targetNode = me.target.asInstanceOf[dom.Node]
if targetNode != null && !elem.contains(targetNode) then
callback(me)| private def waitForTransitionEnd(elem: dom.HTMLElement)(onEnd: () => Unit): Unit = | ||
| val timeoutMs = config.duration.getOrElse(5000) | ||
|
|
||
| val listener: js.Function1[dom.Event, Unit] = | ||
| (_: dom.Event) => | ||
| pendingTimeout.foreach(id => dom.window.clearTimeout(id)) | ||
| pendingTimeout = js.undefined | ||
| elem.removeEventListener("transitionend", transitionListener.get) | ||
| elem.removeEventListener("animationend", transitionListener.get) | ||
| transitionListener = js.undefined | ||
| onEnd() | ||
| transitionListener = listener | ||
|
|
||
| elem.addEventListener("transitionend", listener) | ||
| elem.addEventListener("animationend", listener) | ||
|
|
||
| // Safety timeout fallback | ||
| pendingTimeout = dom | ||
| .window | ||
| .setTimeout( | ||
| () => | ||
| transitionListener.foreach { l => | ||
| elem.removeEventListener("transitionend", l) | ||
| elem.removeEventListener("animationend", l) | ||
| } | ||
| transitionListener = js.undefined | ||
| onEnd() | ||
| , | ||
| timeoutMs | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation of waitForTransitionEnd has race conditions related to the use of the shared transitionListener variable:
- If the safety timeout fires before the
transitionendevent, it clearstransitionListener. Whentransitionendthen fires,transitionListener.getwill throw aNoSuchElementException. - If a new transition starts on the same element,
transitionListeneris updated. If an event (eithertransitionendor timeout) from the old transition fires after this, it will incorrectly remove the listener of the new transition.
To fix this, the listener should be a lazy val to be able to reference itself, and both the event handler and the timeout handler should use this local listener for removal and check against the shared transitionListener before modifying it.
private def waitForTransitionEnd(elem: dom.HTMLElement)(onEnd: () => Unit): Unit =
val timeoutMs = config.duration.getOrElse(5000)
lazy val listener: js.Function1[dom.Event, Unit] = { (_: dom.Event) =>
pendingTimeout.foreach(id => dom.window.clearTimeout(id))
pendingTimeout = js.undefined
elem.removeEventListener("transitionend", listener)
elem.removeEventListener("animationend", listener)
// Only clear the shared listener if it's still this one
if (transitionListener.toOption.contains(listener)) {
transitionListener = js.undefined
}
onEnd()
}
transitionListener = listener
elem.addEventListener("transitionend", listener)
elem.addEventListener("animationend", listener)
// Safety timeout fallback
pendingTimeout = dom
.window
.setTimeout(
() => {
// Only act if the listener hasn't been replaced by a new transition
if (transitionListener.toOption.contains(listener)) {
elem.removeEventListener("transitionend", listener)
elem.removeEventListener("animationend", listener)
transitionListener = js.undefined
onEnd()
}
},
timeoutMs
)
Summary
Transitionsystem driven byRx[Boolean]visibility, with enter/leave class sequences, double-rAF frame precision, and safety timeout fallbacksClickOutsidedetection via document-levelmousedownlistener withsetTimeout(0)delay to avoid catching the opening clickFocusTrapRxElement pattern for Transition,DomNodebinding pattern for ClickOutside)Closes #409
Test plan
scalafmtAllpasses🤖 Generated with Claude Code