Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic import support #14

Open
seahindeniz opened this issue Mar 6, 2023 · 2 comments
Open

Dynamic import support #14

seahindeniz opened this issue Mar 6, 2023 · 2 comments

Comments

@seahindeniz
Copy link

Hi
Instead of importing SVG files individually, can we use a dynamic import?

For example,

<script setup lang="ts">
import { computed, onBeforeMount, ref } from 'vue';

export interface Props {
    name: string;
    /**
     * In pixels
     */
    size?: number | string;
}

const props = withDefaults(defineProps<Props>(), {
    size: 16,
});

const iconId = ref('');
const sizeInPx = computed(() => (String(props.size).endsWith('px') ? props.size : `${props.size}px`));

onBeforeMount(async () => {
    iconId.value = (await import(`../assets/icons/${props.name}.svg`)).default;
});
</script>

<template>
    <span
        :class="$style.container">
        <svg :class="$style.svg">
            <use :xlink:href="`#${iconId}`"></use>
        </svg>
    </span>
</template>

<style module lang="scss">
.container {
    width: v-bind(sizeInPx);
    height: v-bind(sizeInPx);
    display: block;
}

.svg {
    width: 100%;
    height: 100%;
}

.container, .svg {
    color: inherit;
}
</style>
@meowtec
Copy link
Owner

meowtec commented Jul 15, 2023

It seems to have been implemented indeed.

@meowtec
Copy link
Owner

meowtec commented Jul 15, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants