Code Monkey home page Code Monkey logo

svg-to-svelte's Introduction

svg-to-svelte

NPM Build

Convert SVG files to Svelte components.

Background

Today, a comprehensive UI design system typically ships with icons that underline its brand. Usually, icon components are generated from a folder containing raw SVG files. The reason for "componentizing" SVG files is to make it easier to consume in a library or framework like React, Vue, or Angular.

Svelte is a relatively new language; there are not many existing design systems that implement SVG icons as Svelte components.

This library uses the Svelte compiler to convert SVG icon libraries into Svelte components.

This is accomplished by the following:

  • forward $$restProps to the SVG element
  • forward common events: click, mouseover, mouseenter, mouseleave, keydown
  • enable the default slot
- <svg width="24" height="24" xmlns="http://www.w3.org/2000/svg"><path d="M17 1a3 3 0 0 1 3 3v16a3 3 0 0 1-3 3H7a3 3 0 0 1-3-3V4a3 3 0 0 1 3-3h10zM7 20h10v-4H7v4z" fill="#767676" fill-rule="evenodd"/></svg>
+ <svg width="24" height="24" xmlns="http://www.w3.org/2000/svg" {...$$restProps} on:click on:mouseover on:mouseenter on:mouseleave on:keydown><slot /><path d="M17 1a3 3 0 0 1 3 3v16a3 3 0 0 1-3 3H7a3 3 0 0 1-3-3V4a3 3 0 0 1 3-3h10zM7 20h10v-4H7v4z" fill="#767676" fill-rule="evenodd" /></svg>

More generally, this utility experiments with augmenting plain HTML into Svelte.

Icon libraries generated using svg-to-svelte:

Install

Note: this module requires Node.js version 12 or greater.

yarn add -D svg-to-svelte

Usage

generateFromFolder

The fastest way is to specify the path to a folder that contains SVG elements.

The second parameter is the output directory. By default, it is "lib."

const { generateFromFolder } = require("svg-to-svelte");

(async () => {
  await generateFromFolder("node_modules/gestalt/src/icons", "lib", {
    clean: true,
  });
  // reads all SVG files from the path "node_modules/gestalt/src/icons"
  // generates a Svelte component per SVG file in the "lib" output folder
})();

generateIndex

The generateIndex method generates static documentation listing the module names for the library in Markdown format.

interface GenerateIndexOptions {
  title?: string; // title of the generated markdown file
  pkgName: string; // name of the Svelte package name
  pkgVersion: string; // version of the Svelte icon library
  moduleNames: ModuleNames; // module names returned by `generateFromFolder`
  outputFile?: string; // name of the markdown output file
  libraryFolder?: string; // name of the folder containing generated Svelte components
}
const { generateIndex } = require("svg-to-svelte");
const { name, devDependencies } = require("./package.json");

(async () => {
  const libraryFolder = "lib";

  const { moduleNames } = await generateFromFolder(
    "node_modules/gestalt/src/icons",
    libraryFolder
  );

  // generates components from `gestalt` into the "lib" folder

  await generateIndex({
    moduleNames,
    pkgName: name,
    pkgVersion: devDependencies["gestalt"],
    outputFile: "ICON_INDEX.md",
    libraryFolder,
  });

  // writes the file to "ICON_INDEX.md"
})();

generate

The generate method executes both the generateFromFolder and generateIndex functions.

The only parameter it accepts is the path to the source folder.

require("svg-to-svelte").generate("node_modules/gestalt/src/icons");

CLI usage

svg-to-svelte --input=node_modules/gestalt/src/icons
# OR
s2s --input=node_modules/gestalt/src/icons

Options

An optional third argument passed to generateFromFolder include:

interface GenerateFromFolderOptions {
  clean: boolean; // remove and create output directory (default is `true`)
  onModuleName: (moduleName: string) => string; // called when the `moduleName` is created
}

toSvelte

The toSvelte method converts an SVG string to Svelte.

const { toSvelte } = require("svg-to-svelte");

const result = toSvelte(`<svg ...></svg>`);
/**
 * `result.template`: Svelte file as a string
 */

toModuleName

The toModuleName converts a file name to an exportable module name.

  • add-file.svg --> AddFile
  • 123--alt.svg --> _123Alt
const { toModuleName } = require("svg-to-svelte");

toModuleName("add-file.svg"); // AddFile

cleanDir

The cleanDir method is an asynchronous method that removes and creates a directory.

const { cleanDir } = require("svg-to-svelte");

cleanDir("lib");

License

MIT

svg-to-svelte's People

Contributors

metonym avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

svg-to-svelte's Issues

Typescript Support?

Hi, thank you for this handy library to generate my own icon library. It is possible for you to support typescript as well? I work only with Typescript, so it would be nice, if i have some type definitions for better developer experience and VS IntelliSense

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.