carbon-icons-svelte

repository·master·Indexed 19 days ago

https://github.com/carbon-design-system/carbon-icons-svelte

A zero-dependency library providing Carbon Design System SVG icons as standalone Svelte components. It supports custom sizing via the size prop, accessibility attributes, and TypeScript types through CarbonIconProps. The library can be used independently or with carbon-components-svelte.

Tokens
1.1K
Snippets
6
Records
8
Agent score
16%

What's inside carbon-icons-svelte

  1. Optimize imports with carbon-preprocess-svelte

    master

    You can use the optimizeImports preprocessor from carbon-preprocess-svelte to allow using concise barrel file import syntax while maintaining fast development times. The preprocessor automatically rewrites imports to the specific file path.

    Example transformation:

    - import { Add } from "carbon-icons-svelte";
    + import Add from "carbon-icons-svelte/lib/Add.svelte";
  2. Use CarbonIconProps with TypeScript

    master

    The library provides TypeScript support for Svelte 4 and Svelte 5. You can import the CarbonIconProps type to type your icon property objects.

    <script lang="ts">
      import Add from "carbon-icons-svelte/lib/Add.svelte";
      import type { CarbonIconProps } from "carbon-icons-svelte";
    
      const props: CarbonIconProps = {
        size: 16,
        fill: "red",
        "data-test": "id",
      };
    </script>
    
    <Add {...props} />
  3. Icon Component API Reference

    master

    All props are optional. The component forwards $$restProps to the underlying svg element, allowing you to use attributes like fill, class, or any valid SVG attribute.

    Props

    NameTypeDefaultDescription
    size16 | 20 | 24 | 32 | number16Icon size in pixels
    titlestringundefinedAccessible title

    Common Usage Patterns

    Customizing color and classes:

    <Add fill="red" class="icon" />

    Accessibility (Labelled):

    <Add aria-label="Add" />

    Accessibility (Labelled by):

    <label id="add-file">Add file</label>
    <Add aria-labelledby="add-file" />

    Focusable icons:

    <Add aria-label="Add" tabindex={0} />
  4. Build icons with buildIcons()

    master

    The buildIcons function is an asynchronous utility used to generate the icon library files (Svelte components, TypeScript definitions, and index files) from the @carbon/icons metadata. It handles icon deprecations, renames (aliases), and generates the ICON_INDEX.md and build information files.

    import { buildIcons } from "@carbon/icons-svelte";
    
    const iconNames = await buildIcons();
    console.log("Generated icons:", iconNames);
  5. Use CarbonIconProps for icon component typing

    master

    When working with icons from this library, you can use the CarbonIconProps type to define the expected properties for an icon component. This type extends standard Svelte SVG HTML elements and includes specific Carbon icon properties.

    import type { CarbonIconProps } from "@carbon/icons-svelte";
    
    // CarbonIconProps includes:
    // - title?: string (The icon title, defaults to undefined)
    // - size?: 16 | 20 | 24 | 32 | (number & {}) (The icon size, defaults to 16)
    // - All standard SvelteHTMLElements["svg"] attributes