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 🙏

© 2026 – Pkg Stats / Ryan Hefner

elm-element

v1.0.2

Published

Use Elm applications as web components.

Readme

elm-element

This library is meant to streamline the process of turning Elm applications into custom HTML elements. This is specially useful when mixing Elm with other frameworks, such as React, Vue, etc., and even within another Elm application.

Usage

A given date picker Elm application could be setup as follows:

index.html

<my-datepicker value="1538991615340"></my-datepicker>

index.js

import { define } from 'elm-element'
import { Elm } from './DatePicker.elm'

// Define the custom element class
const DatePicker = define(Elm.DatePicker.init, {
  properties: {
    value: 'onChangeValue'
  },
  events: {
    change: 'valueChanged'
  }
})

// Register element to be used as <my-datepicker>
customElements.define('my-datepicker', DatePicker)

DatePicker.elm

port module DatePicker exposing (main)

main : Program Flags Model Msg
main ...

-- Initial observed attributes and properties values are passed as flags
type alias Flags =
  { attributes :
    { value : Maybe String
    }
  }

-- When subscribed, notifies Elm that the attribute has been changed from the outside
port onChangeValue : ( Maybe String -> msg ) -> Sub msg

-- Triggers the "change" event with provided value
port valueChanged : Int -> Cmd msg

Disclaimer

The example above makes use of a loader such as elm-webpack-loader and rollup-plugin-elm to be able to import Elm files into JavaScript.

It's also important to note that Custom Elements and Shadow DOM are not yet completely supported by all major browsers, so it's advisable to use a polyfill when necessary.

API

define(init [, config])

init

A function that takes an object as follows:

{
  node: HTMLElement,
  flags: {
    attributes: {
      [name: string]: null | string
    },
    properties: {
      [name: string]: null | any
    }
  }
}

And then returns an Elm app instance. You can either specify the application's built-in init or your own to extend the default arguments:

define(({ node, flags }) => {
  return Elm.App.init({
    node: node,
    flags: {
      ...flags,
      someExtraFlag: Date.now()
    }
  })
})

config

Attributes

Observing an attribute can be specified as follows:

{
  attributes: {
    value: 'valueChanged'
  }
}

Where value is the attribute's name and valueChanged is the incoming port name. The example above is a shorthand for:

{
  attributes: {
    value: (app, newValue) =>
      app.ports.valueChanged.send(newValue)
  }
}

From inside the Elm application, changes to the attribute can be received by the incoming port with specified name:

-- App.elm

port valueChanged : (Maybe String -> msg) -> Sub msg

Properties

Observing a properties works much like as attributes:

{
  properties: {
    value: 'valueChanged'
  }
}

As well as:

{
  properties: {
    value: (app, newValue) =>
      app.ports.valueChanged.send(newValue)
  }
}

With the difference that properties can be any JSON serializable value:

-- App.elm

import Json.Decode as Json

port valueChanged : (Json.Value -> msg) -> Sub msg

Events

Similarly, events can be defined as:

{
  events: {
    change: 'valueChange'
  }
}

Which is a shorthand for:

{
  events: {
    change: (app, dispatch) =>
      app.ports.valueChange.subscribe(dispatch)
  }
}

And if you want to take charge of creating the custom event yourself, it can be done as follows:

{
  events: {
    change: (app, dispatch) =>
      app.ports.valueChange.subscribe(newValue => {
        dispatch(new CustomEvent('change', {
          detail: newValue
        }))
      })
  }
}

Events can be dispached from Elm using an outgoing port:

-- App.elm

port valueChange : Json.Value -> Cmd msg

Check out the examples directory for complete examples.

Two-way binding

You might have noticed that until this point there's no way for an Elm element to activelly update its own element attributes and properties. Instead, the general strategy is to dispatch an event so the parent context can pick it up and then update attributes/properties from the ouside. Doing otherwise could lead to unpredictable side-effects, due to changes being propagated in both directions.

Development

Issues, suggestions and pull requests are very much welcomed. Feel free to also contact me directly on Slack.