|
6 | 6 |
|
7 | 7 | # solid-create-script |
8 | 8 |
|
9 | | -Solid utility hook to dynamically load an external script. |
| 9 | +MIT Licensed |
10 | 10 |
|
11 | | -### Installation |
| 11 | +Utility function to dynamically load external scripts in both declarative and imperative styles within SolidJS. |
| 12 | + |
| 13 | +## Installation |
12 | 14 |
|
13 | 15 | ```bash |
14 | | -npm install solid-js solid-create-script |
15 | | -pnpm add solid-js solid-create-script |
16 | | -yarn add solid-js solid-create-script |
17 | | -bun add solid-js solid-create-script |
| 16 | +npm install solid-js @dschz/load-script solid-create-script |
| 17 | +pnpm install solid-js @dschz/load-script solid-create-script |
| 18 | +yarn install solid-js @dschz/load-script solid-create-script |
| 19 | +bun install solid-js @dschz/load-script solid-create-script |
18 | 20 | ``` |
19 | 21 |
|
20 | | -### Usage |
| 22 | +> These are **peer dependencies**, so they must be installed manually: |
| 23 | +> |
| 24 | +> - `solid-js` |
| 25 | +> - `@dschz/load-script` |
21 | 26 |
|
22 | | -```tsx |
23 | | -const script = createScript("https://some-library.js"); |
24 | | -const script = createScript("https://some-library.js", { async: true, ...scriptAttributes }); |
25 | | -const script = createScript("https://some-library.js", { defer: true, ...scriptAttributes }); |
| 27 | +## Summary |
| 28 | + |
| 29 | +```ts |
| 30 | +import createScript from "solid-create-script"; |
26 | 31 | ``` |
27 | 32 |
|
28 | | -Under the hood `createScript` makes use of the `createResource` Solid API when loading the desired script at the specified `src`. As such, the result of `createScript` is a `Resource<Event>` object that allows you to inspect when the script has finished loading or returned an error via `script.loading` and `script.error` respectively. |
| 33 | +## API Breakdown |
| 34 | + |
| 35 | +### `createScript` |
29 | 36 |
|
30 | | -When using `createScript`, here are some points to be aware of: |
| 37 | +A lightweight wrapper around `loadScript` with `createResource` that returns a `Resource<HTMLScriptElement>`. |
31 | 38 |
|
32 | | -- The created `<script>` tag will be appeneded to `<head>`. |
33 | | -- The created `<script>` tag will not be removed from the DOM when a component that uses this hook unmounts. (i.e. we do not make use of `onCleanup` to remove the `<script>` from `<head>`). |
34 | | -- The hook will ensure no duplicate `<script>` tags referencing the same `src` will be created. Moreover, when multiple components reference the same `src`, they will all point to the same shared resource ensuring consistency within the reactive graph. |
| 39 | +The interface signature is as follows: |
35 | 40 |
|
36 | | -### Full Example |
| 41 | +```ts |
| 42 | +const createScript = (src: string, options?: LoadScriptOptions, container?: HTMLElement | null) |
| 43 | +``` |
| 44 | + |
| 45 | +The three arguments: |
37 | 46 |
|
38 | | -```tsx |
39 | | -import { Switch, Match } from "solid-js"; |
40 | | -import { createScript } from "solid-create-script"; |
| 47 | +- `src`: the script source to download |
| 48 | +- `options`: the options to pass to `loadScript` |
| 49 | +- `container`: the HTMLElement to append the `<script />` to. |
41 | 50 |
|
42 | | -function App() { |
43 | | - const script = createScript("https://some-library.js"); |
| 51 | +It is useful for: |
| 52 | + |
| 53 | +- Conditionally rendering based on script load status |
| 54 | +- Tracking `loading`, `error`, and ready states |
| 55 | + |
| 56 | +### Usage |
| 57 | + |
| 58 | +```ts |
| 59 | +import { Switch, Match } from "solid-js" |
| 60 | +import createScript from "solid-create-script" |
| 61 | + |
| 62 | +const CustomComponent = () => { |
| 63 | + const script = createScript("https://example.com/library.js", { async: true }); |
44 | 64 |
|
45 | 65 | return ( |
46 | | - <Switch fallback={<ScriptProvider>...</ScriptProvider>}> |
| 66 | + <Switch> |
47 | 67 | <Match when={script.loading}>Loading Script...</Match> |
48 | | - <Match when={script.error}>Failed to load script: {script.error.message}</Match> |
| 68 | + <Match when={script.error}>Failed to load</Match> |
| 69 | + <Match when={script()}>Script is ready!</Match> |
49 | 70 | </Switch> |
50 | | - ); |
| 71 | + ) |
51 | 72 | } |
| 73 | + |
52 | 74 | ``` |
53 | 75 |
|
54 | | -### Feedback |
| 76 | +> ⚠️ The `<script>` that is downloaded with `createScript` will be appended to `<head>`. This API currently does not support specifying a mount target for the `<script>` tag. |
| 77 | +
|
| 78 | +## Notes |
| 79 | + |
| 80 | +- Scripts are automatically cached to prevent duplication. |
| 81 | +- The script is not removed on cleanup/unmount. |
| 82 | +- `createScript` uses `createResource` internally to manage async state. |
| 83 | + |
| 84 | +## Feedback |
55 | 85 |
|
56 | | -Feel free to post any issues or suggestions to help improve this utility hook. |
| 86 | +Feel free to post issues or suggestions to help improve this library. |
0 commit comments