Skip to content

Commit 5bbff44

Browse files
committed
feat: add is-safari and open-in-new-tab
1 parent 2f9e1a2 commit 5bbff44

File tree

5 files changed

+81
-0
lines changed

5 files changed

+81
-0
lines changed

docs/src/pages/_meta.json

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
"rem": {},
8383
"request-idle-callback": {},
8484
"typescript-happy-forward-ref": {},
85+
"is-safari": {},
8586
"-- separator extensions": {
8687
"type": "separator",
8788
"title": "Extensions"

docs/src/pages/is-safari.mdx

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
title: isSafari
3+
---
4+
5+
# isSafari
6+
7+
import ExportMetaInfo from '../components/export-meta-info';
8+
9+
<ExportMetaInfo />
10+
11+
Since Safari browser is a known modern Internet Explorer, you should just check if the browser is Safari and apply the necessary polyfills or special logics.
12+
13+
## Usage
14+
15+
```tsx copy
16+
import { isSafari } from 'foxact/is-safari';
17+
18+
isSafari(); // true or false
19+
```

docs/src/pages/open-in-new-tab.mdx

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
title: Open in New Tab
3+
---
4+
5+
# Open in New Tab
6+
7+
import ExportMetaInfo from '../components/export-meta-info';
8+
9+
<ExportMetaInfo />
10+
11+
Open a URL in a new tab. Use `window.open` on the Safari browser (since nowadays Safari is known as a modern Internet Explorer) and `document.createElement('a')` on other browsers.
12+
13+
## Usage
14+
15+
```tsx copy
16+
import { openInNewTab } from 'foxact/open-in-new-tab';
17+
18+
openInNewTab('https://skk.moe');
19+
```

src/is-safari/index.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export function isSafari() {
2+
if (typeof window === 'undefined') {
3+
return false;
4+
}
5+
if (typeof navigator === 'undefined') {
6+
return false;
7+
}
8+
if (typeof navigator.userAgent !== 'string') {
9+
return false;
10+
}
11+
if (/version\/[\d._].*?safari/i.test(navigator.userAgent)) {
12+
return true;
13+
}
14+
// eslint-disable-next-line sukka/prefer-single-boolean-return -- cleaner code
15+
if (/mobile safari [\d._]+/i.test(navigator.userAgent)) {
16+
return true;
17+
}
18+
return false;
19+
}

src/open-new-tab/index.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { isSafari } from '../is-safari';
2+
3+
export function openInNewTab(url: string) {
4+
if (typeof window === 'undefined') {
5+
return;
6+
}
7+
8+
if (isSafari()) {
9+
window.open(url, '_blank');
10+
return;
11+
}
12+
13+
const a = document.createElement('a');
14+
a.href = url;
15+
a.target = '_blank';
16+
a.rel = 'noopener noreferrer';
17+
document.body.appendChild(a);
18+
a.click();
19+
20+
Promise.resolve().finally(() => {
21+
a.remove();
22+
});
23+
}

0 commit comments

Comments
 (0)