Skip to content

Commit

Permalink
[fix] Linking.openURL supports 'target' value
Browse files Browse the repository at this point in the history
Close #2277
  • Loading branch information
avanwinkle authored and necolas committed Jun 8, 2022
1 parent f736125 commit fa45a22
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
4 changes: 2 additions & 2 deletions packages/docs/src/pages/docs/apis/linking.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ Returns a `Promise` that resolves to a boolean indicating whether the app can op
Returns a `Promise` that resolves to the string of the URL that initially loaded the app.
{% endcall %}

{% call macro.prop('openURL', '(url) => Promise<>') %}
Try to open the given url in a secure fashion. The method returns a Promise object. If the url opens, the promise is resolved. If not, the promise is rejected.
{% call macro.prop('openURL', '(url, target) => Promise<>') %}
Try to open the given url in a secure fashion. The provided target (including `undefined`) will be passed as the window target, or "_blank" if no target included. The method returns a Promise object. If the url opens, the promise is resolved. If not, the promise is rejected.
{% endcall %}

---
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* eslint-env jasmine, jest */

import Linking from '..';

describe('apis/Linking', () => {
describe('openURL', () => {
test('calls open with a url and target', done => {
jest.spyOn(window, 'open').mockImplementationOnce((url, target, opener) => {
expect(url).toBe('http://foo.com/');
expect(target).toBe('target_name');
expect(opener).toBe('noopener');
done();
});
Linking.openURL('http://foo.com', 'target_name');
});

test('defaults target to _blank if not provided', done => {
jest.spyOn(window, 'open').mockImplementationOnce((url, target, opener) => {
expect(url).toBe('http://foo.com/');
expect(target).toBe('_blank');
expect(opener).toBe('noopener');
done();
});
Linking.openURL('http://foo.com');
});

test('accepts undefined as a target', done => {
jest.spyOn(window, 'open').mockImplementationOnce((url, target, opener) => {
expect(url).toBe('http://foo.com/');
expect(target).toBe(undefined);
expect(opener).toBe('noopener');
done();
});
Linking.openURL('http://foo.com', undefined);
});
});
});
14 changes: 9 additions & 5 deletions packages/react-native-web/src/exports/Linking/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,16 @@ class Linking {

/**
* Try to open the given url in a secure fashion. The method returns a Promise object.
* If a target is passed (including undefined) that target will be used, otherwise '_blank'.
* If the url opens, the promise is resolved. If not, the promise is rejected.
* Dispatches the `onOpen` event if `url` is opened successfully
* Dispatches the `onOpen` event if `url` is opened successfully.
*/
openURL(url: string): Promise<Object | void> {
openURL(url: string, target?: string): Promise<Object | void> {
if (arguments.length === 1) {
target = '_blank';
}
try {
open(url);
open(url, target);
this._dispatchEvent('onOpen', url);
return Promise.resolve();
} catch (e) {
Expand All @@ -86,13 +90,13 @@ class Linking {
}
}

const open = (url) => {
const open = (url, target) => {
if (canUseDOM) {
const urlToOpen = new URL(url, window.location).toString();
if (urlToOpen.indexOf('tel:') === 0) {
window.location = urlToOpen;
} else {
window.open(urlToOpen, '_blank', 'noopener');
window.open(urlToOpen, target, 'noopener');
}
}
};
Expand Down

0 comments on commit fa45a22

Please sign in to comment.