Skip to content

Commit

Permalink
✨ Add id param to useScript hook (from #285 by @misidoro) (#525)
Browse files Browse the repository at this point in the history
* ✨ Add `id` param to `useScript` hook (from #285 by @misidoro)

* Fix missing useEffect dependency
  • Loading branch information
juliencrn authored Mar 5, 2024
1 parent 0d99db9 commit 9b65ce8
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 3 deletions.
8 changes: 8 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -1684,8 +1684,16 @@
"avatar_url": "https://avatars.githubusercontent.com/u/54762253?v=4",
"profile": "https://github.com/kyrylo-soulandwolf",
"contributions": ["code", "bug"]
},
{
"login": "misidoro",
"name": "Miguel Isidoro",
"avatar_url": "https://avatars.githubusercontent.com/u/3635023?v=4",
"profile": "https://github.com/misidoro",
"contributions": ["code"]
}
],
"contributorsPerLine": 7,
"commitType": "docs"
}

5 changes: 5 additions & 0 deletions .changeset/long-suits-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"usehooks-ts": minor
---

Add `id` param to `useScript` hook (from #285 by @misidoro)
1 change: 1 addition & 0 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ Big thanks go to all our contributors! [[Become a contributor](https://github.co
</tr>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/kyrylo-soulandwolf"><img src="https://avatars.githubusercontent.com/u/54762253?v=4?s=64" width="64px;" alt="kyrylo-soulandwolf"/><br /><sub><b>kyrylo-soulandwolf</b></sub></a><br /><a href="https://github.com/juliencrn/usehooks-ts/commits?author=kyrylo-soulandwolf" title="Code">💻</a> <a href="https://github.com/juliencrn/usehooks-ts/issues?q=author%3Akyrylo-soulandwolf" title="Bug reports">🐛</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/misidoro"><img src="https://avatars.githubusercontent.com/u/3635023?v=4?s=64" width="64px;" alt="Miguel Isidoro"/><br /><sub><b>Miguel Isidoro</b></sub></a><br /><a href="https://github.com/juliencrn/usehooks-ts/commits?author=misidoro" title="Code">💻</a></td>
</tr>
</tbody>
</table>
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
[![License](https://badgen.net/badge/License/MIT/blue)](https://github.com/juliencrn/usehooks-ts/blob/master/LICENSE)
![npm bundle size](https://img.shields.io/bundlephobia/minzip/usehooks-ts)
![npm](https://img.shields.io/npm/v/usehooks-ts)<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-239-orange.svg?style=flat-square)](#contributors-)
[![All Contributors](https://img.shields.io/badge/all_contributors-240-orange.svg?style=flat-square)](#contributors-)
<!-- ALL-CONTRIBUTORS-BADGE:END -->

<br />
Expand Down Expand Up @@ -427,6 +427,7 @@ Big thanks go to all our contributors! [[Become a contributor](https://github.co
</tr>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/kyrylo-soulandwolf"><img src="https://avatars.githubusercontent.com/u/54762253?v=4?s=64" width="64px;" alt="kyrylo-soulandwolf"/><br /><sub><b>kyrylo-soulandwolf</b></sub></a><br /><a href="https://github.com/juliencrn/usehooks-ts/commits?author=kyrylo-soulandwolf" title="Code">💻</a> <a href="https://github.com/juliencrn/usehooks-ts/issues?q=author%3Akyrylo-soulandwolf" title="Bug reports">🐛</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/misidoro"><img src="https://avatars.githubusercontent.com/u/3635023?v=4?s=64" width="64px;" alt="Miguel Isidoro"/><br /><sub><b>Miguel Isidoro</b></sub></a><br /><a href="https://github.com/juliencrn/usehooks-ts/commits?author=misidoro" title="Code">💻</a></td>
</tr>
</tbody>
</table>
Expand Down
3 changes: 2 additions & 1 deletion packages/usehooks-ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
[![License](https://badgen.net/badge/License/MIT/blue)](https://github.com/juliencrn/usehooks-ts/blob/master/LICENSE)
![npm bundle size](https://img.shields.io/bundlephobia/minzip/usehooks-ts)
![npm](https://img.shields.io/npm/v/usehooks-ts)<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-239-orange.svg?style=flat-square)](#contributors-)
[![All Contributors](https://img.shields.io/badge/all_contributors-240-orange.svg?style=flat-square)](#contributors-)
<!-- ALL-CONTRIBUTORS-BADGE:END -->

<br />
Expand Down Expand Up @@ -427,6 +427,7 @@ Big thanks go to all our contributors! [[Become a contributor](https://github.co
</tr>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/kyrylo-soulandwolf"><img src="https://avatars.githubusercontent.com/u/54762253?v=4?s=64" width="64px;" alt="kyrylo-soulandwolf"/><br /><sub><b>kyrylo-soulandwolf</b></sub></a><br /><a href="https://github.com/juliencrn/usehooks-ts/commits?author=kyrylo-soulandwolf" title="Code">💻</a> <a href="https://github.com/juliencrn/usehooks-ts/issues?q=author%3Akyrylo-soulandwolf" title="Bug reports">🐛</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/misidoro"><img src="https://avatars.githubusercontent.com/u/3635023?v=4?s=64" width="64px;" alt="Miguel Isidoro"/><br /><sub><b>Miguel Isidoro</b></sub></a><br /><a href="https://github.com/juliencrn/usehooks-ts/commits?author=misidoro" title="Code">💻</a></td>
</tr>
</tbody>
</table>
Expand Down
1 change: 1 addition & 0 deletions packages/usehooks-ts/src/useScript/useScript.demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default function Component() {
// Load the script asynchronously
const status = useScript(`https://code.jquery.com/jquery-3.5.1.min.js`, {
removeOnUnmount: false,
id: 'jquery',
})

useEffect(() => {
Expand Down
19 changes: 19 additions & 0 deletions packages/usehooks-ts/src/useScript/useScript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,23 @@ describe('useScript', () => {

expect(result2.current).toBe('ready')
})

it('should have a `id` attribute when given', () => {
const src = '/'
const id = 'my-script'

const { result } = renderHook(() => useScript(src, { id }))

// Make sure the document is loaded
act(() => {
document
.querySelector(`script[src="${src}"]`)
?.dispatchEvent(new Event('load'))
})

expect(result.current).toBe('ready')

expect(document.querySelector(`script[id="${id}"]`)).not.toBeNull()
expect(document.querySelector(`script[src="${src}"]`)?.id).toBe(id)
})
})
7 changes: 6 additions & 1 deletion packages/usehooks-ts/src/useScript/useScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export type UseScriptStatus = 'idle' | 'loading' | 'ready' | 'error'
export interface UseScriptOptions {
shouldPreventLoad?: boolean
removeOnUnmount?: boolean
/** Script's `id` (optional) */
id?: string
}

// Cached script statuses
Expand Down Expand Up @@ -79,6 +81,9 @@ export function useScript(
scriptNode = document.createElement('script')
scriptNode.src = src
scriptNode.async = true
if (options?.id) {
scriptNode.id = options.id
}
scriptNode.setAttribute('data-status', 'loading')
document.body.appendChild(scriptNode)

Expand Down Expand Up @@ -123,7 +128,7 @@ export function useScript(
cachedScriptStatuses.delete(src)
}
}
}, [src, options?.shouldPreventLoad, options?.removeOnUnmount])
}, [src, options?.shouldPreventLoad, options?.removeOnUnmount, options?.id])

return status
}

0 comments on commit 9b65ce8

Please sign in to comment.