-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: loadCSS, loadScript to support options
- Loading branch information
1 parent
3c79808
commit 727d5bb
Showing
7 changed files
with
83 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,51 @@ | ||
/* eslint-disable unicorn/prefer-add-event-listener */ | ||
|
||
export async function loadScript(src: string, async = true): Promise<void> { | ||
// eslint-disable-next-line @typescript-eslint/return-await | ||
return new Promise<void>((resolve, reject) => { | ||
const s = document.createElement('script') | ||
s.src = src | ||
s.onload = resolve as any | ||
s.onerror = (_event, _source, _lineno, _colno, error) => { | ||
reject(error || new Error(`loadScript failed: ${src}`)) | ||
} | ||
if (async) s.async = true | ||
import { _objectAssign, isServerSide } from '@naturalcycles/js-lib' | ||
|
||
export type LoadScriptOptions = Partial<HTMLScriptElement> | ||
export type LoadCSSOptions = Partial<HTMLLinkElement> | ||
|
||
/** | ||
* opt.async defaults to `true`. | ||
* No other options are set by default. | ||
*/ | ||
export async function loadScript(src: string, opt?: LoadScriptOptions): Promise<void> { | ||
if (isServerSide()) return | ||
|
||
return await new Promise<void>((resolve, reject) => { | ||
const s = _objectAssign(document.createElement('script'), { | ||
src, | ||
async: true, | ||
...opt, | ||
onload: resolve as any, | ||
onerror: (_event, _source, _lineno, _colno, err) => { | ||
reject(err || new Error(`loadScript failed: ${src}`)) | ||
}, | ||
}) | ||
document.head.append(s) | ||
}) | ||
} | ||
|
||
/** | ||
* Default options: | ||
* rel: 'stylesheet' | ||
* | ||
* No other options are set by default. | ||
*/ | ||
export async function loadCSS(href: string, opt?: LoadCSSOptions): Promise<void> { | ||
if (isServerSide()) return | ||
|
||
return await new Promise<void>((resolve, reject) => { | ||
const link = _objectAssign(document.createElement('link'), { | ||
href, | ||
rel: 'stylesheet', | ||
// type seems to be unnecessary: https://stackoverflow.com/a/5409146/4919972 | ||
// type: 'text/css', | ||
...opt, | ||
onload: resolve as any, | ||
onerror: (_event, _source, _lineno, _colno, err) => { | ||
reject(err || new Error(`loadCSS failed: ${href}`)) | ||
}, | ||
}) | ||
|
||
document.head.append(link) | ||
}) | ||
} |