npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

svg2elm

v1.2.4

Published

Generates Elm modules out of SVG files

Downloads

3,711

Readme

svg2elm

npm version GitHub license

Generates Elm modules out of SVG files. Comes with a CLI tool and a JavaScript API to build bundler plugins.

Using Parcel? Check out parcel-plugin-elm-svg.

Installation

You can install svg2elm from npm:

$ npm install -g svg2elm

How to use

Let's say we have a chevron icon that we want to embed in our Elm app:

$ cat chevron.svg
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
    <line x1="1" y1="1" x2="50" y2="50" stroke-linecap="round" />
    <line x1="50" y1="50" x2="1" y2="99" stroke-linecap="round" />
</svg>

Using svg2elm we can generate an Elm module out of it. Let's call ours Acme.Icons:

$ svg2elm --module Acme.Icons chevron.svg > Acme/Icons.elm
module Acme.Icons exposing (..)

import Svg
import VirtualDom exposing (Attribute, attribute)

chevron : List (Attribute msg) -> Svg.Svg msg
chevron attrs = Svg.node "svg" ([attribute "xmlns" "http://www.w3.org/2000/svg", attribute "viewBox" "0 0 100 100"] ++ attrs) [ Svg.node "line" ([attribute "x1" "1", attribute "y1" "1", attribute "x2" "50", attribute "y2" "50", attribute "stroke-linecap" "round"]) [], Svg.node "line" ([attribute "x1" "50", attribute "y1" "50", attribute "x2" "1", attribute "y2" "99", attribute "stroke-linecap" "round"]) []]

We are now ready to embed the icon in our app! Since the generated function returns an Svg node, we can use it like any other element:

import Acme.Icons exposing (chevron)

...

nextPage =
    button []
        [ text "Next Page"
        , chevron []
        ]

SVG Attributes

Note that the generated function takes a List of Attributes:

chevron : List (Attribute msg) -> Svg.Svg msg

This allows us to tweak our icons in a per usage basis. For example, if we wanted to point our chevron to the left, we could do the following:

previousPage =
    button []
        [ text "Previous Page"
        , chevron [ transform "rotate(180)" ]
        ]

Similarly, we could change the size, colors, stroke width, etc.

...
        , chevron [ width "30", stroke "blue", strokeWidth "2" ]
...

Attributes are appended to the top SVG node. This means that you can only override those set at that level. This is usually not a problem since child nodes inherit parent attributes. However, you might have to tweak your SVG to fit your needs.

Multiple icons per module

You likely want to generate a module with all your app icons. You can do this by passing multiple files to svg2elm:

$ svg2elm --module Acme.Icons icons/chevron.svg icons/user.svg

...or you can use globs:

$ svg2elm --module Acme.Icons icons/*.svg

A function will be generated for each SVG file.

Elm UI

If you're using the awesome mdgriffith/elm-ui, you have to use Element.html to turn your Svg node into an Element.

nextPage =
    button []
        [ text "Next Page"
        , chevron [] |> html
        ]

API

This package also exposes a JavaScript API that can be used to fit svg2elm into any build process.

It's just three functions: reference.

Inspiration

At PINATA, we have been using SVGR for years to load our icons as React components. This project aims to bring the same experience to the world of Elm.

License

BSD-3-Clause. See LICENSE file.

Humans

Thanks to rnons for building elm-svg-parser and Garados007 for making it work with Elm 0.19.

Built by Piotr Brzeziński and Agustín Zubiaga at PINATA.

♥︎