Svelte 5 Getting started - Flowbite Svelte Icons v2

Svelte 5 Getting started

Svelte 5 Getting started - Flowbite Svelte Icons v2

sponsor npm License npm

Requirements #

- Svelte 5:Runes
- TailwindCSS
- tailwind-merge

Installation #

Install Svelte and TailwindCSS:

// install Svelte 5
npm create svelte@latest my-project
cd my-project
npx svelte-add@latest tailwindcss
pnpm i

Install flowbite-svelte-icons v2:

pnpm i -D flowbite-svelte-icons@next

Enable Runes in svelte.config.js:

import adapter from '@sveltejs/adapter-auto';
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';

/** @type {import('@sveltejs/kit').Config} */
const config = {
	preprocess: [vitePreprocess({})],
	compilerOptions: {
		runes: true
	},
	kit: {
		adapter: adapter()
	}
};

export default config;

tailwind.config.cjs #

To make sure the classes used by flowbite-svelte-icons are included by the Tailwindcss, add the following to tailwind.config.cjs.

const config = {
  content: [
       // more lines
        "./node_modules/flowbite-svelte-icons/**/*.{html,js,svelte,ts}",
    ],
    // more lines
}  

Basic Usages #

In a svelte file:

<script>
  import { AddressBookOutline } from 'flowbite-svelte-icons';
</script>

<AddressBookOutline />

Faster compiling #

If you need only a few icons from this library in your Svelte app, import them directly. This can optimize compilation speed and improve performance by reducing the amount of code processed during compilation.

<script>
  import AddressBookOutline from 'flowbite-svelte-icons/AddressBookOutline.svelte';
</script>

<AddressBookOutline />

Passing down other attributes #

Since all icons have {...restProps} and extended SVGAttributes<SVGSVGElement> , you can pass other attibutes.

<AddressBookOutline
  id="my-svg"
  transform="rotate(45)"
  class="hover:cursor-pointer dark:text-white"
  onclick={() => alert('hello')}
/>

Using onMount (Svelte 5 legacy mode) #

<script>
  import { AddressBookOutline } from 'flowbite-svelte-icons';
  import { onMount } from 'svelte';
  const props = {
    size: '50',
    color: '#ff0000'
  };
  onMount(() => {
    const icon = new AddressBookOutline({ target: document.body, props });
  });
</script>

Import all #

Use import * as Icon from 'flowbite-svelte-icons.

<script>
  import * as Icon from 'flowbite-svelte-icons';
</script>

<Icon.AddressBookOutline />

<h1>Size</h1>
<Icon.AddressBookOutline size="30" />

<h1>Tailwind CSS</h1>
<Icon.AddressBookOutline class="text-blue-500" />

Props #

All icons are extended SVGAttributes<SVGSVGElement> from svelte/elements.

- size?: "xs" | "sm" | "md" | "lg" | "xl" = ctx.size || 'md';
- color?: string | undefined | null = ctx.color || 'currentColor'
- class?: string | undefined | null = ''
- ariaLabel?: string =  "<icon file name>"
- title?: TitleType
- desc?: DescType
- strokeWidth?: string | undefined | null = ctx.strokeWidth || '2' // only Outline icons
- ...restProps

Size #

The following table provides details about the available sizes for icons:

Size  CSS classes
xs    'w-3 h-3'
sm    'w-4 h-4'
md    'w-5 h-5'
lg    'w-6 h-6'
xl    'w-8 h-8'

To change the size of an icon, use the size prop and specify the desired size. For example:

<AddressBookOutline size="md" />

If you want to override the preconfigured size, you can add a custom size using Tailwind CSS by including the desired classes in the class prop. For example:

<AddressBookOutline class="h-24 w-24 text-blue-700 mr-4" />

Color #

You can apply Tailwind CSS color directly to the icon component or its parent tag using the class prop.

<AddressBookOutline size="md" class="text-red-700 dark:text-green-300 inline m-1"/>

<div class="text-red-700 dark:text-green-300 inline m-1">
  <AddressBookOutline size="md" />
</div>

CSS HEX Colors #

Use the color attribute to change colors with HEX color code for Filled and Outlined components.

<AddressBookOutline color="#ff0000" size="md"/> 

Dark mode #

If you are using the dark mode on your website with Tailwind CSS, add your dark mode class to the class prop.

Let’s use dark for the dark mode class as an example.

<AddressBookOutline class="text-blue-700 dark:text-red-500" />

aria-label #

All icons have aria-label. For example BxAbacus has aria-label="bx abacus". Use ariaLabel prop to modify the aria-label value.

<AddressBookOutline ariaLabel="address card outline" />

Custom Default Icons #

You can create a custom default icon, by using IconSolid or IconOutline:

Create a custom component #

Create a Svelte component named src/lib/MyIcon.svelte:

<script lang="ts">
  import { IconOutline } from 'flowbite-svelte-icons';
  import { type Component } from 'svelte';
  const config: { size: IconOutline['Props']['size'], color: string, ariaLabel: string } = {
    size: "md",
    color: '#FF5733',
    ariaLabel: "my youtube icon",
  };
  interface Props {
    Icon: Component
  }

  let { Icon }: Props = $props();
</script>
<IconOutline {...config} {Icon} />

This component, MyIcon.svelte, accepts an icon prop which you can use to pass in the specific icon component you want to display. The default configuration is also applied to the icon.

Implementation in a Page #

To use your custom default icon in a Svelte page, do the following:

<script lang="ts">
  import { AngleLeftOutline } from 'flowbite-svelte-icons';
  import MyIcon from 'path/to/MyIcon.svelte'
  const config: { size: IconOutline['Props']['size'], color: string, ariaLabel: string } = {
    size: "xl",
    color: '#FF5733',
    ariaLabel: "my addressbook icon",
  };
</script>

<MyIcon {...config} Icon={AngleLeftOutline} />

Here, we import the MyIcon component and the AngleLeftSolid icon. By passing the AngleLeftSolid icon to the icon prop of MyIcon, you apply the default configuration to the icon.

Setting Global Icon using setContext #

You can create global icon preferences in your Svelte application using setContext. This allows you to configure icon-related properties once and share them across multiple components.

Setting preferences #

In your +layout.svelte or +page.svelte, you can define and set global icon preferences as follows:

<script>
  import { setContext } from 'svelte';

  // Define your global icon settings
  const iconCtx = {
    size: 'xl', // Icon size in pixels
  };
  setContext('iconCtx', iconCtx);
</script>

The size, and role (for solid icons) and size, role, strokeLinecap, strokeLinejoin, strokeWidth (for outline icons) properties are optional, allowing you to fine-tune the appearance and accessibility of your icons as needed.

Prop size #

If you set size, icons can be customized with different color. For example:

<script>
  import { setContext } from 'svelte';
  import { MapLocationOutline } from 'flowbite-svelte-icons';
  const iconCtx = {
    size: 'xl'
  };
  setContext('iconCtx', iconCtx);
</script>

<MapLocationOutline color="#ff4488" />

Setting more than one props #

Remember that you can set one or more properties, allowing you to tailor icon settings to your specific design and accessibility requirements.