-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #128 from appwrite/feat-spline-viewer-improvements
feat: improve spline loading
- Loading branch information
Showing
7 changed files
with
125 additions
and
144 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,51 +1,35 @@ | ||
<script lang="ts"> | ||
import { writable } from 'svelte/store'; | ||
let loaded = writable(false); | ||
type SplineElement = HTMLElement & { | ||
_loaded: boolean; | ||
}; | ||
function isSplineElement(el: HTMLElement): el is SplineElement { | ||
return '_loaded' in el; | ||
} | ||
const viewer = (node: HTMLElement) => { | ||
if (!isSplineElement(node)) { | ||
throw new Error('Spline element not found'); | ||
} | ||
const onLoad = () => { | ||
loaded.set(true); | ||
node.shadowRoot?.querySelector('#logo')?.remove(); // Remove Spline logo | ||
}; | ||
node?.addEventListener('load-complete', onLoad); | ||
if (node._loaded) { | ||
onLoad(); | ||
} | ||
return { | ||
destroy() { | ||
node?.removeEventListener('load-complete', onLoad); | ||
} | ||
}; | ||
}; | ||
const fallback = (node: HTMLElement) => { | ||
const destroy = loaded.subscribe((l) => { | ||
if (!l) return; | ||
setTimeout(() => { | ||
node?.style.setProperty('display', 'none'); | ||
}, 150); | ||
}); | ||
return { | ||
destroy | ||
}; | ||
}; | ||
import { onMount } from 'svelte'; | ||
import { writable } from 'svelte/store'; | ||
import type { SplineViewer } from '@splinetool/viewer'; | ||
import { fade } from 'svelte/transition'; | ||
export let url: SplineViewer['url']; | ||
export let width: SplineViewer['width'] = undefined; | ||
export let height: SplineViewer['height'] = undefined; | ||
export let loading: SplineViewer['loading'] = 'auto'; | ||
let loaded = writable(false); | ||
let spline: SplineViewer; | ||
onMount(async () => { | ||
await import('@splinetool/viewer'); | ||
const onLoad = () => { | ||
spline.shadowRoot?.querySelector('#logo')?.remove(); // Remove Spline logo | ||
setTimeout(() => { | ||
loaded.set(true); | ||
}, 150); | ||
}; | ||
spline.addEventListener('load-complete', onLoad); | ||
}); | ||
</script> | ||
|
||
<slot {viewer} {fallback} /> | ||
<spline-viewer style="position: absolute;" {url} {width} {height} {loading} bind:this={spline} /> | ||
|
||
{#if $$slots?.default && !$loaded} | ||
<div out:fade={{duration: 50}}> | ||
<slot /> | ||
</div> | ||
{/if} |
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
Binary file not shown.