Jhernandez has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/379520 )

Change subject: New: Improve Link component
......................................................................

New: Improve Link component

* Don't handle external links
  * Inspired on [1]
* Don't handle links without href, prevented events, clicks with
  modifier keys pressed. Only handle left button clicks
  * Inspired on [2]

This allows now to Cmd+click links (broken before) among other things
and makes the link component more robust over all.

[1]: 
https://github.com/joakin/protowiki/blob/master/src/components/capture-clicks/index.js#L27-L29
[2]: 
https://github.com/ReactTraining/react-router/blob/7c0481966e9f79da47ea483e083a4f694ecf35fe/packages/react-router-dom/modules/Link.js#L44-L49

Change-Id: I573e39f4695b436da28b2621f982117b2aa252f5
---
M src/common/components/link.tsx
1 file changed, 15 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/marvin refs/changes/20/379520/1

diff --git a/src/common/components/link.tsx b/src/common/components/link.tsx
index c815a9d..f6da9fc 100644
--- a/src/common/components/link.tsx
+++ b/src/common/components/link.tsx
@@ -6,6 +6,10 @@
   href: string;
 }
 
+function isModifiedEvent(event: MouseEvent): boolean {
+  return !!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);
+}
+
 /**
  * A single page app link that intercepts navigation (click) events and passes
  * control to History. All local hyperlinks should use a Link component.
@@ -19,8 +23,17 @@
       class={classOf("Link", props.class)}
       href={href}
       onClick={event => {
-        // todo: check that link is internal (relative).
-        if (context.history) {
+        const origin = window.location.origin;
+        const link = event.target as HTMLAnchorElement;
+        if (
+          link.href && // The link has a href
+          link.href.indexOf(origin) === 0 && // The href is internal
+          !event.defaultPrevented && // onClick not prevented default
+          event.button === 0 && // Ignore everything but left clicks
+          !link.target && // Let browser handle "target=_blank" etc.
+          !isModifiedEvent(event) && // Ignore clicks with modifier keys
+          context.history // We have a context history
+        ) {
           event.preventDefault();
           context.history.push(href);
         }

-- 
To view, visit https://gerrit.wikimedia.org/r/379520
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I573e39f4695b436da28b2621f982117b2aa252f5
Gerrit-PatchSet: 1
Gerrit-Project: marvin
Gerrit-Branch: master
Gerrit-Owner: Jhernandez <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to