2
0
mirror of https://github.com/iconify/iconify.git synced 2024-09-20 09:19:02 +00:00
iconify/packages/svelte/src/Icon.svelte

55 lines
903 B
Svelte
Raw Normal View History

<script>
2021-05-01 20:38:56 +00:00
import { onMount, onDestroy } from 'svelte';
import { checkIconState, generateIcon } from './functions';
// State
const state = {
// Last icon name
name: '',
// Loading status
loading: null,
};
// Mounted status
let mounted = false;
// Callback counter
let counter = 0;
// Generated data
let data;
2021-05-01 20:38:56 +00:00
// Generate data
$: {
2021-05-01 20:38:56 +00:00
counter;
data = mounted ? generateIcon(checkIconState($$props.icon, state, loaded), $$props) : null;
}
2021-05-01 20:38:56 +00:00
// Increase counter when loaded to force re-calculation of data
function loaded() {
counter ++;
}
// Force re-render
onMount(() => {
mounted = true;
});
// Abort loading when component is destroyed
onDestroy(() => {
mounted = false;
if (state.loading) {
state.loading.abort();
state.loading = null;
}
})
</script>
{#if data === null}
<slot />
{:else}
<svg {...data.attributes}>
{@html data.body}
</svg>
{/if}