Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow explicit target for Linking.openURL(), default to "_blank" #2277

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -66,12 +66,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 @@ -85,13 +89,13 @@ class Linking {
}
}

const open = (url) => {
const open = (url, string) => {
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, string, 'noopener');
Comment on lines -94 to +98

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finally! Thanks for adding this :)

}
}
};
Expand Down