Skip to content

Commit

Permalink
feat: implement dynamic import for icon components
Browse files Browse the repository at this point in the history
  • Loading branch information
oktaysenkan committed Oct 5, 2024
1 parent d4e97b1 commit 1ed8fc3
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 68 deletions.
2 changes: 1 addition & 1 deletion apps/vite-vue/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<template>
<main>
<Iconify name="mdi:home" :size="24" />
<Iconify name="mdi:home" :size="32" />
<Iconify name="logos:active-campaign" />
<Iconify name="logos:apache-superset-icon" />
<Iconify name="invalid:icon" />
Expand Down
5 changes: 0 additions & 5 deletions packages/core/src/constants.ts

This file was deleted.

8 changes: 0 additions & 8 deletions packages/core/src/errors/icon-not-found.error.ts

This file was deleted.

8 changes: 0 additions & 8 deletions packages/core/src/errors/invalid-icon.error.ts

This file was deleted.

20 changes: 0 additions & 20 deletions packages/core/src/errors/plugin-not-installed.error.ts

This file was deleted.

34 changes: 24 additions & 10 deletions packages/react/src/iconify.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
import React from "react";
import { getIconDetails, IconifyProps } from "@oktaytest/icon-loader";

// @ts-ignore
import icons from "oktay";

export const Iconify = (props: IconifyProps) => {
const details = getIconDetails(props, icons);

return (
<svg
{...details.attributes}
dangerouslySetInnerHTML={{ __html: details.innerHtml }}
/>
const [Component, setComponent] = React.useState<React.ReactElement | null>(
null
);

const loadComponent = React.useCallback(async () => {
// @ts-ignore
const iconsImport = await import("oktay");

const icons = iconsImport.default ?? iconsImport;

const details = getIconDetails(props, icons);

setComponent(
<svg
{...details.attributes}
dangerouslySetInnerHTML={{ __html: details.innerHtml }}
/>
);
}, [props]);

React.useEffect(() => {
loadComponent();
}, []);

return Component;
};

export default Iconify;
26 changes: 16 additions & 10 deletions packages/svelte/src/lib/Iconify.svelte
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
<script lang="ts">
import type { Icon } from "@oktaytest/core";
import { getIconDetails } from "@oktaytest/icon-loader";
export let name: string;
export let size: number | undefined = undefined;
export let color: string | undefined = undefined;
// @ts-ignore
import icons from "oktay";
let details: ReturnType<typeof getIconDetails> | null = null;
$: details = getIconDetails(
{ name, color, size },
icons as Record<string, Icon>
);
const loadIcons = async () => {
// @ts-ignore
const iconsImport = await import("oktay");
const icons = iconsImport.default ?? iconsImport;
details = getIconDetails({ name, color, size }, icons);
};
$: name, size, color, loadIcons();
</script>

<svg {...details.attributes}>
{@html details.innerHtml}
</svg>
{#if details}
<svg {...details.attributes}>
{@html details.innerHtml}
</svg>
{/if}
23 changes: 17 additions & 6 deletions packages/vue/src/Iconify.vue
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
<script setup lang="ts">
import { defineProps, computed } from "vue";
import { defineProps, ref, watch, onMounted } from "vue";
import { getIconDetails, IconifyProps } from "@oktaytest/icon-loader";
// @ts-ignore
import icons from "oktay";
const props = defineProps<IconifyProps>();
const details = computed(() => getIconDetails(props, icons));
const details = ref<ReturnType<typeof getIconDetails> | null>(null);
const loadIcons = async () => {
// @ts-ignore
const iconsImport = await import("oktay");
const icons = iconsImport.default ?? iconsImport;
details.value = getIconDetails(props, icons);
};
watch(props, loadIcons);
onMounted(loadIcons);
</script>

<template>
<svg v-html="details.innerHtml" v-bind="details.attributes"></svg>
<svg
v-if="details"
v-html="details.innerHtml"
v-bind="details.attributes"
></svg>
</template>

0 comments on commit 1ed8fc3

Please sign in to comment.