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

mithril-select

v0.5.1

Published

Mithril custom select component

Downloads

7

Readme

Mithril Custom Select Component

A custom select widget component for Mithril.js. Built to mimic native browser select behaviour (if not looks) as closely as possible, including acessibility and keyboard features. The minimal CSS included can be easily overridden and customized by your own styles.

Try a live demo here.

Install:

npm install mithril-select

(TypeScript types are included.)

You will need to include the css file for some basic working styles.

Using PostCSS with postcss-import allows you to import the stylesheet from node_modules:

@import "mithril-select";

If you're using a sass compiler, you can add:

@import "node_modules/mithril-select/index";

to one of your sass files.

Otherwise you can copy the node_modules/mithril-select/index.css file to your project and add it to your html page.

See the example in the git repository for examples of style customization.

Example use:

import mithrilSelect from "mithril-select"
// var mithrilSelect = require("mithril-select").default

// Data to be used in select
const colours = [
  {id: 'red', text: 'Red'},
  {id: 'blue', text: 'Blue'},
  {id: 'green', text: 'Green'},
  {id: 'yellow', text: 'Yellow'},
  {id: 'orange', text: 'Orange'},
  {id: 'pink', text: 'Pink'}
]

let colour = ""

const component = {
  view() {
    return m(mithrilSelect, {
      options: [
        {value: null, view: 'Select a colour...'}
      ].concat(
        colours.map(c => ({value: c.id, view: c.text}))
      ),
      // A CSS class to add to the root element of the select
      class: 'my-select',
      // Respond to selection changes
      onchange: (val) => {
        colour = val != null
          ? colours.find(c => c.id === val).text
          : ""
      }
    })
  }
}

All options and component Attrs:

(See src/index.ts)

/** Represents a single option in a select */
interface Option {
  /**
   * Unique value that identifies this option.
   * Can be any type except `undefined`.
   */
  value: any
  /**
   * Either a string to display for the option or a callback
   * that renders vnodes.
   */
  view?: string | (() => m.Children)
}

/** Attrs object for Select component */
interface Attrs {
  /**
   * Array of `Option` objects
   */
  options: Option[]
  /**
   * Optional prompt to display until the user selects an option.
   * Supply either a string to display or a callback that renders vnodes.
   */
  promptView?: string | (() => m.Children)
  /**
   * Optional value to use for element id attribute.
   */
  id?: string
  /**
   * Optional name of hidden input for form. If none supplied, no hidden
   * input will be rendered. Hidden input value will be coerced to string.
   */
  name?: string
  /**
   * Current selected option value. Omitting or setting to `undefined`
   * is the same as supplying no value. (`null` can be a value.)
   */
  value?: any
	/**
   * Optional aria-labelledby attribute
   */
	ariaLabelledby?: string
  /**
   * Value of option that will be selected on creation. Overridden
   * by `value` if supplied, otherwise will be first option.
   */
  initialValue?: any
  /**
   * Additional class string to use on containing element.
   */
  class?: string
  /**
   * Callback that will be passed the value of the selected option
   * when selection changes.
   */
  onchange?(value: any): void
}

Development Install:

First git clone this repo. Then:

npm install

Build module

npm run build

Serve, compile & watch example app:

npm start

Then go to http://localhost:3000/ in your browser.

Build a plain ES2015 version of the library:

npm run build-es2015

Will output src/index.js


Thanks to barneycarroll for providing an initial POC demo using global focus/blur listeners.