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

Function openLink

packages/react-aria/src/utils/openLink.tsx:106–145  ·  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 (
114 !isWebKit() &&
115 isFirefox() &&
116 window.event?.type?.startsWith('key') &&
117 target.target === '_blank'
118 ) {
119 if (isMac()) {
120 metaKey = true;
121 } else {
122 ctrlKey = true;
123 }
124 }
125
126 // WebKit does not support firing click events with modifier keys, but does support keyboard events.
127 // https://github.com/WebKit/WebKit/blob/c03d0ac6e6db178f90923a0a63080b5ca210d25f/Source/WebCore/html/HTMLAnchorElement.cpp#L184
128 let event =
129 isWebKit() && isMac() && !isIPad() && process.env.NODE_ENV !== 'test'
130 ? // @ts-ignore - keyIdentifier is a non-standard property, but it's what webkit expects
131 new KeyboardEvent('keydown', {keyIdentifier: 'Enter', metaKey, ctrlKey, altKey, shiftKey})
132 : new MouseEvent('click', {
133 metaKey,
134 ctrlKey,
135 altKey,
136 shiftKey,
137 detail: 1,
138 bubbles: true,
139 cancelable: true
140 });
141 (openLink as any).isOpening = setOpening;
142 focusWithoutScrolling(target);
143 target.dispatchEvent(event);
144 (openLink as any).isOpening = false;
145}
146// https://github.com/parcel-bundler/parcel/issues/8724
147(openLink as any).isOpening = false;
148

Callers 3

onKeyUpFunction · 0.90
RouterProviderFunction · 0.85
openSyntheticLinkFunction · 0.85

Calls 1

focusWithoutScrollingFunction · 0.90

Tested by

no test coverage detected