Skip to content

Commit

Permalink
chore(): added type definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
Yousuf Jawwad committed May 7, 2024
1 parent d93c8f6 commit 261002f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,45 @@ All icons are imported from a single file, where [ICON SLUG] is replaced by a ca
</style>
```

## Type Definition for Dynamic Icons in Svelte Components

Sometimes, we want to provide the component dynamically to a component. We can do that by using `svelte:component` feature with the
helper type `SiComponentType` type definition.

```svelte
<script lang="ts">
import type { SiComponentType } from '@icons-pack/svelte-simple-icons';
export let icon: SiComponentType;
export let text: string;
export let click: () => void = () => console.log('do something');
</script>
<button on:click={click}>
<svelte:component
this={icon}
title={text} <!-- optional, along with size and color properties -->
/>
{text}
</button>
<style lang="scss">
button {
display: flex;
flex-direction: row;
text-decoration: none;
white-space: nowrap;
transition: border-color 0.25s;
box-shadow: none;
text-shadow: none;
}
.icon {
margin: 4px 4px 0 0;
}
</style>
```

## Faster Compilations

If you only need a few icons, you can import them individually instead of the entire file to improve compilation.
Expand Down
10 changes: 8 additions & 2 deletions scripts/generate-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ const attrsToString = (attrs) => {
.join(" ");
};

const exportType = `
import type { ComponentType, SvelteComponent } from 'svelte';
export type SiComponentType = ComponentType<SvelteComponent<{ color?: string; size?: string; title?: string; }>>;
`
fs.appendFileSync(pathIndexExport, exportType, formatFile);

ICONS.forEach((icon) => {
const baseName = String(icon);
const componentName = upperCamelCase(baseName);
Expand Down Expand Up @@ -73,8 +81,6 @@ ICONS.forEach((icon) => {

fs.writeFileSync(locationOutputComponent, component, formatFile);

console.log(`${componentName}`);

const exportComponent = `export { default as ${componentName} } from './icons/${componentName}.svelte';\r\n`;

fs.appendFileSync(pathIndexExport, exportComponent, formatFile);
Expand Down

0 comments on commit 261002f

Please sign in to comment.