Skip to content

Commit 0c04073

Browse files
authored
Fix ctrl click on links (#33)
1 parent 0fe17fb commit 0c04073

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

CHANGELOG.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ Using the following categories, list your changes in this order:
3636

3737
### Changed
3838

39-
- Bump GitHub workflows
4039
- Rename `use_query` to `use_search_params`.
4140
- Rename `simple.router` to `browser_router`.
4241
- Rename `SimpleResolver` to `StarletteResolver`.
@@ -58,7 +57,7 @@ Using the following categories, list your changes in this order:
5857
- Fix bug where "Match Any" pattern wouldn't work when used in complex or nested paths.
5958
- Fix bug where `link` elements could not have `@component` type children.
6059
- Fix bug where the ReactPy would not detect the current URL after a reconnection.
61-
- Fixed flakey tests on GitHub CI by adding click delays.
60+
- Fix bug where `ctrl` + `click` on a `link` element would not open in a new tab.
6261

6362
## [0.1.1] - 2023-12-13
6463

src/reactpy_router/components.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from urllib.parse import urljoin
66
from uuid import uuid4
77

8-
from reactpy import component, event, html, use_connection
8+
from reactpy import component, html, use_connection
99
from reactpy.backend.types import Location
1010
from reactpy.core.component import Component
1111
from reactpy.core.types import VdomDict
@@ -63,8 +63,10 @@ def _link(attributes: dict[str, Any], *children: Any) -> VdomDict:
6363
# https://github.com/reactive-python/reactpy/pull/1224
6464
current_path = use_connection().location.pathname
6565

66-
@event(prevent_default=True)
6766
def on_click(_event: dict[str, Any]) -> None:
67+
if _event.get("ctrlKey", False):
68+
return
69+
6870
pathname, search = to.split("?", 1) if "?" in to else (to, "")
6971
if search:
7072
search = f"?{search}"

src/reactpy_router/static/link.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
document.querySelector(".UUID").addEventListener(
22
"click",
33
(event) => {
4-
let to = event.target.getAttribute("href");
5-
window.history.pushState({}, to, new URL(to, window.location));
4+
// Prevent default if ctrl isn't pressed
5+
if (!event.ctrlKey) {
6+
event.preventDefault();
7+
let to = event.target.getAttribute("href");
8+
window.history.pushState({}, to, new URL(to, window.location));
9+
}
610
},
711
{ once: true },
812
);

tests/test_core.py renamed to tests/test_router.py

+18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
from typing import Any
33

4+
from playwright.async_api._generated import Browser, Page
45
from reactpy import Ref, component, html, use_location
56
from reactpy.testing import DisplayFixture
67

@@ -277,3 +278,20 @@ def sample():
277278

278279
_link = await display.page.wait_for_selector("#root")
279280
assert "/a" in await _link.get_attribute("href")
281+
282+
283+
async def test_ctrl_click(display: DisplayFixture, browser: Browser):
284+
@component
285+
def sample():
286+
return browser_router(
287+
route("/", link({"to": "/a", "id": "root"}, "Root")),
288+
route("/a", link({"to": "/a", "id": "a"}, "a")),
289+
)
290+
291+
await display.show(sample)
292+
293+
_link = await display.page.wait_for_selector("#root")
294+
await _link.click(delay=CLICK_DELAY, modifiers=["Control"])
295+
browser_context = browser.contexts[0]
296+
new_page: Page = await browser_context.wait_for_event("page")
297+
await new_page.wait_for_selector("#a")

0 commit comments

Comments
 (0)