-
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
Open
xerial
wants to merge
1
commit into
main
Choose a base branch
from
feature/transition-click-outside
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
uni-dom-test/src/test/scala/wvlet/uni/dom/ClickOutsideTest.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package wvlet.uni.dom | ||
|
|
||
| import wvlet.uni.test.UniTest | ||
| import wvlet.uni.rx.Rx | ||
| import wvlet.uni.dom.all.* | ||
| import wvlet.uni.dom.all.given | ||
|
|
||
| class ClickOutsideTest extends UniTest: | ||
|
|
||
| test("ClickOutside.detect creates ClickOutsideBinding"): | ||
| val binding = ClickOutside.detect(_ => ()) | ||
| binding shouldMatch { case _: ClickOutsideBinding => | ||
| } | ||
|
|
||
| test("ClickOutside.hide creates ClickOutsideBinding"): | ||
| val visible = Rx.variable(true) | ||
| val binding = ClickOutside.hide(visible) | ||
| binding shouldMatch { case _: ClickOutsideBinding => | ||
| } | ||
|
|
||
| test("ClickOutside.onClickOutside creates ClickOutsideBinding"): | ||
| var called = false | ||
| val binding = ClickOutside.onClickOutside(() => called = true) | ||
| binding shouldMatch { case _: ClickOutsideBinding => | ||
| } | ||
|
|
||
| test("ClickOutsideBinding stores callback"): | ||
| var received: Option[org.scalajs.dom.MouseEvent] = None | ||
| val binding = ClickOutside.detect(e => received = Some(e)) | ||
| binding shouldMatch { case cb: ClickOutsideBinding => | ||
| cb.callback shouldMatch { case _: Function1[?, ?] => | ||
| } | ||
| } | ||
|
|
||
| test("ClickOutside.detect can be used as DomNode modifier"): | ||
| val elem = div(ClickOutside.detect(_ => ()), span("content")) | ||
| elem shouldMatch { case _: DomElement => | ||
| } | ||
|
|
||
| test("ClickOutside.hide can be used as DomNode modifier"): | ||
| val visible = Rx.variable(true) | ||
| val elem = div(ClickOutside.hide(visible), span("content")) | ||
| elem shouldMatch { case _: DomElement => | ||
| } | ||
|
|
||
| // Note: Full integration tests for click outside detection require a browser | ||
| // environment with real mouse events. The bindings are created correctly | ||
| // (tested above) and the handler code in DomRenderer will work in a real | ||
| // browser environment. | ||
|
|
||
| end ClickOutsideTest |
131 changes: 131 additions & 0 deletions
131
uni-dom-test/src/test/scala/wvlet/uni/dom/TransitionTest.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package wvlet.uni.dom | ||
|
|
||
| import wvlet.uni.test.UniTest | ||
| import wvlet.uni.rx.Rx | ||
| import wvlet.uni.dom.all.* | ||
| import wvlet.uni.dom.all.given | ||
|
|
||
| class TransitionTest extends UniTest: | ||
|
|
||
| test("TransitionConfig has sensible defaults"): | ||
| val config = TransitionConfig() | ||
| config.name shouldBe "v" | ||
| config.duration shouldBe None | ||
| config.appear shouldBe false | ||
|
|
||
| test("TransitionConfig can be customized"): | ||
| val config = TransitionConfig(name = "fade", duration = Some(300), appear = true) | ||
| config.name shouldBe "fade" | ||
| config.duration shouldBe Some(300) | ||
| config.appear shouldBe true | ||
|
|
||
| test("TransitionConfig withName"): | ||
| val config = TransitionConfig().withName("slide") | ||
| config.name shouldBe "slide" | ||
|
|
||
| test("TransitionConfig withDuration"): | ||
| val config = TransitionConfig().withDuration(500) | ||
| config.duration shouldBe Some(500) | ||
|
|
||
| test("TransitionConfig noDuration"): | ||
| val config = TransitionConfig(duration = Some(300)).noDuration | ||
| config.duration shouldBe None | ||
|
|
||
| test("TransitionConfig withAppear"): | ||
| val config = TransitionConfig().withAppear | ||
| config.appear shouldBe true | ||
|
|
||
| test("Transition.apply with name returns RxElement"): | ||
| val visible = Rx.variable(false) | ||
| val transition = Transition("fade", visible)(div("content")) | ||
| transition shouldMatch { case _: RxElement => | ||
| } | ||
|
|
||
| test("Transition.apply with config returns RxElement"): | ||
| val visible = Rx.variable(false) | ||
| val config = TransitionConfig(name = "slide", duration = Some(200)) | ||
| val transition = Transition(config, visible)(div("content")) | ||
| transition shouldMatch { case _: RxElement => | ||
| } | ||
|
|
||
| test("Transition.fade returns RxElement"): | ||
| val visible = Rx.variable(false) | ||
| val transition = Transition.fade(visible)(div("content")) | ||
| transition shouldMatch { case _: RxElement => | ||
| } | ||
|
|
||
| test("Transition.slide returns RxElement"): | ||
| val visible = Rx.variable(false) | ||
| val transition = Transition.slide(visible)(div("content")) | ||
| transition shouldMatch { case _: RxElement => | ||
| } | ||
|
|
||
| test("Transition renders wrapper div with children"): | ||
| val visible = Rx.variable(true) | ||
| val transition = Transition("fade", visible)(span("hello")) | ||
| val (node, cancel) = DomRenderer.createNode(transition) | ||
|
|
||
| node match | ||
| case elem: org.scalajs.dom.Element => | ||
| elem.tagName.toLowerCase shouldBe "div" | ||
| val spans = elem.querySelectorAll("span") | ||
| spans.length shouldBe 1 | ||
| case _ => | ||
| fail("Expected Element") | ||
|
|
||
| cancel.cancel | ||
|
|
||
| test("Transition renders hidden when initially false"): | ||
| val visible = Rx.variable(false) | ||
| val transition = Transition("fade", visible)(span("hello")) | ||
| val (node, cancel) = DomRenderer.createNode(transition) | ||
|
|
||
| node match | ||
| case elem: org.scalajs.dom.HTMLElement => | ||
| elem.style.display shouldBe "none" | ||
| case _ => | ||
| fail("Expected HTMLElement") | ||
|
|
||
| cancel.cancel | ||
|
|
||
| test("Transition renders visible when initially true"): | ||
| val visible = Rx.variable(true) | ||
| val transition = Transition("fade", visible)(span("hello")) | ||
| val (node, cancel) = DomRenderer.createNode(transition) | ||
|
|
||
| node match | ||
| case elem: org.scalajs.dom.HTMLElement => | ||
| elem.style.display shouldNotBe "none" | ||
| case _ => | ||
| fail("Expected HTMLElement") | ||
|
|
||
| cancel.cancel | ||
|
|
||
| test("Transition with multiple children"): | ||
| val visible = Rx.variable(true) | ||
| val transition = Transition("fade", visible)(span("a"), span("b"), span("c")) | ||
| val (node, cancel) = DomRenderer.createNode(transition) | ||
|
|
||
| node match | ||
| case elem: org.scalajs.dom.Element => | ||
| val spans = elem.querySelectorAll("span") | ||
| spans.length shouldBe 3 | ||
| case _ => | ||
| fail("Expected Element") | ||
|
|
||
| cancel.cancel | ||
|
|
||
| end TransitionTest |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package wvlet.uni.dom | ||
|
|
||
| import org.scalajs.dom | ||
| import wvlet.uni.rx.{Cancelable, RxVar} | ||
|
|
||
| import scala.scalajs.js | ||
|
|
||
| /** | ||
| * Binding for click outside detection. Handled by DomRenderer to register a document-level | ||
| * mousedown listener that checks if the click target is outside the host element. | ||
| */ | ||
| case class ClickOutsideBinding(callback: dom.MouseEvent => Unit) extends DomNode | ||
|
|
||
| /** | ||
| * Click outside detection for dismissing dropdowns, modals, and other overlays. | ||
| * | ||
| * Usage: | ||
| * {{{ | ||
| * import wvlet.uni.dom.all.* | ||
| * | ||
| * val isOpen = Rx.variable(true) | ||
| * | ||
| * // Dismiss dropdown on outside click | ||
| * isOpen.map { open => | ||
| * if open then | ||
| * div(cls -> "dropdown", | ||
| * ClickOutside.hide(isOpen), | ||
| * ul(li("Option 1"), li("Option 2")) | ||
| * ) | ||
| * else DomNode.empty | ||
| * } | ||
| * | ||
| * // Custom callback | ||
| * div( | ||
| * ClickOutside.detect { event => | ||
| * println(s"Clicked outside at (${event.clientX}, ${event.clientY})") | ||
| * }, | ||
| * "Click outside me" | ||
| * ) | ||
| * | ||
| * // Simple no-arg callback | ||
| * div( | ||
| * ClickOutside.onClickOutside(() => println("Outside!")), | ||
| * "Content" | ||
| * ) | ||
| * }}} | ||
| */ | ||
| object ClickOutside: | ||
|
|
||
| /** | ||
| * Detect clicks outside the host element and invoke a callback with the mouse event. | ||
| * | ||
| * @param callback | ||
| * Function called with the MouseEvent when a click occurs outside | ||
| */ | ||
| def detect(callback: dom.MouseEvent => Unit): DomNode = ClickOutsideBinding(callback) | ||
|
|
||
| /** | ||
| * Set an RxVar to false when a click occurs outside the host element. Convenient for hiding | ||
| * dropdowns and modals. | ||
| * | ||
| * @param visible | ||
| * The RxVar to set to false on outside click | ||
| */ | ||
| def hide(visible: RxVar[Boolean]): DomNode = ClickOutsideBinding(_ => visible := false) | ||
|
|
||
| /** | ||
| * Invoke a no-arg callback when a click occurs outside the host element. | ||
| * | ||
| * @param callback | ||
| * Function called on outside click | ||
| */ | ||
| def onClickOutside(callback: () => Unit): DomNode = ClickOutsideBinding(_ => callback()) | ||
|
|
||
| end ClickOutside |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.targetproperty can benullin some edge cases. The current code'sasInstanceOf[dom.Node]on anullvalue would result innull, and!elem.contains(null)evaluates totrue, incorrectly triggering theonClickOutsidecallback. It's safer to add a null check to prevent this.