MCPcopy Create free account
hub / github.com/adobe/react-spectrum / openLink

Function openLink

packages/react-aria/src/utils/openLink.tsx:106–140  ·  view source on GitHub ↗
(target: HTMLAnchorElement, modifiers: Modifiers, setOpening = true)

Source from the content-addressed store, hash-verified

104}
105
106export function openLink(target: HTMLAnchorElement, modifiers: Modifiers, setOpening = true): void {
107 let {metaKey, ctrlKey, altKey, shiftKey} = modifiers;
108
109 // Firefox does not recognize keyboard events as a user action by default, and the popup blocker
110 // will prevent links with target="_blank" from opening. However, it does allow the event if the
111 // Command/Control key is held, which opens the link in a background tab. This seems like the best we can do.
112 // See https://bugzilla.mozilla.org/show_bug.cgi?id=257870 and https://bugzilla.mozilla.org/show_bug.cgi?id=746640.
113 if (isFirefox() && window.event?.type?.startsWith('key') && target.target === '_blank') {
114 if (isMac()) {
115 metaKey = true;
116 } else {
117 ctrlKey = true;
118 }
119 }
120
121 // WebKit does not support firing click events with modifier keys, but does support keyboard events.
122 // https://github.com/WebKit/WebKit/blob/c03d0ac6e6db178f90923a0a63080b5ca210d25f/Source/WebCore/html/HTMLAnchorElement.cpp#L184
123 let event =
124 isWebKit() && isMac() && !isIPad() && process.env.NODE_ENV !== 'test'
125 ? // @ts-ignore - keyIdentifier is a non-standard property, but it's what webkit expects
126 new KeyboardEvent('keydown', {keyIdentifier: 'Enter', metaKey, ctrlKey, altKey, shiftKey})
127 : new MouseEvent('click', {
128 metaKey,
129 ctrlKey,
130 altKey,
131 shiftKey,
132 detail: 1,
133 bubbles: true,
134 cancelable: true
135 });
136 (openLink as any).isOpening = setOpening;
137 focusWithoutScrolling(target);
138 target.dispatchEvent(event);
139 (openLink as any).isOpening = false;
140}
141// https://github.com/parcel-bundler/parcel/issues/8724
142(openLink as any).isOpening = false;
143

Callers 3

onKeyUpFunction · 0.90
RouterProviderFunction · 0.85
openSyntheticLinkFunction · 0.85

Calls 1

focusWithoutScrollingFunction · 0.90

Tested by

no test coverage detected