-
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(core): added createObjectUrl utility
- Loading branch information
Showing
5 changed files
with
99 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { normalizeValue } from '../__internal__/utils.svelte.js'; | ||
import type { CleanupFunction, MaybeGetter } from '../__internal__/types.js'; | ||
|
||
type CreateObjectUrlReturn = { | ||
readonly current: string | null; | ||
cleanup: CleanupFunction; | ||
}; | ||
|
||
/** | ||
* Creates a reactive URL representing the given object. | ||
* @param object The object to generate the url for. | ||
* @see https://svelte-librarian.github.io/sv-use/docs/create-object-url | ||
*/ | ||
export function createObjectUrl( | ||
object: MaybeGetter<Blob | MediaSource | null | undefined> | ||
): CreateObjectUrlReturn { | ||
let current = $state<string | null>(null); | ||
const _object = $derived(normalizeValue(object)); | ||
|
||
$effect(() => { | ||
if (_object) { | ||
current = URL.createObjectURL(_object); | ||
} | ||
|
||
return cleanup; | ||
}); | ||
|
||
function cleanup() { | ||
if (current) { | ||
URL.revokeObjectURL(current); | ||
} | ||
|
||
current = null; | ||
} | ||
|
||
return { | ||
get current() { | ||
return current; | ||
}, | ||
cleanup | ||
}; | ||
} |
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
25 changes: 25 additions & 0 deletions
25
packages/website/src/lib/docs/core/create-object-url/Demo.svelte
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<script lang="ts"> | ||
import { createObjectUrl } from '$sv-use/core'; | ||
let file = $state<File | null>(null); | ||
const url = createObjectUrl(() => file); | ||
function onFileChange(event: Event & { currentTarget: EventTarget & HTMLInputElement }) { | ||
file = event.currentTarget.files?.[0] ?? null; | ||
} | ||
</script> | ||
|
||
<div class="relative flex w-full flex-col gap-2"> | ||
<p class="text-sm text-zinc-500 dark:text-zinc-400">Select file :</p> | ||
<input type="file" onchange={onFileChange} class="dark:text-zinc-200" /> | ||
<p class="text-sm text-zinc-500 dark:text-zinc-400">Object URL :</p> | ||
<code> | ||
{#if url.current} | ||
<a href={url.current} target="_blank" class="text-svelte dark:text-darksvelte underline"> | ||
{url.current} | ||
</a> | ||
{:else} | ||
<p class="dark:text-zinc-200">none</p> | ||
{/if} | ||
</code> | ||
</div> |
30 changes: 30 additions & 0 deletions
30
packages/website/src/lib/docs/core/create-object-url/index.md
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--- | ||
category: browser | ||
--- | ||
|
||
# createObjectUrl | ||
|
||
Creates a reactive URL representing the given object. | ||
|
||
Automatically releases the URL when the object changes or the component is | ||
unmounted. | ||
|
||
## Usage | ||
|
||
> [!NOTE] | ||
> It uses `$effect`. | ||
```svelte | ||
<script lang="ts"> | ||
import { createObjectUrl } from '@sv-use/core'; | ||
// Get a file in some way | ||
// e.g. a dropzone, a cdn, etc. | ||
const file = $state<File | null>(null); | ||
const url = createObjectUrl(() => file); | ||
</script> | ||
<a href={url.current} download> | ||
Upload file | ||
</a> | ||
``` |