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

switch-value

v1.0.6

Published

"switch-value" is a simple web component to select a value from a previously defined list. You can choose whether the selection should be made via radio buttons or a combo box.

Downloads

163

Readme

switch-value

Purpose

"switch-value" is a simple web component to select a value from a previously defined list. You can choose whether the selection should be made via radio buttons or a combo box.

The element can be configured so that selecting a value causes that value to be set in a predefined property of another HTML element.

When an element is selected, a custom event is also triggered, so that you can also react to value changes in this way. You can also use both ways in parallel.

Parameter

Provide values for selection

There were two ways of providing values for selection. Either the "inputArray" attribute is passed and if this attribute is not found , then the "inputObject" parameter is evaluated. In both cases, an array of the following form is expected:

[ {value: 'value'}, {value: 'value'}, ... ]

Or if value and label should not be identical, you can also define it in the following form:

[ {lable: 'label', value: 'value'}, {lable: 'label', value: 'value'}, ... ]

inputArray

In this case, the array is passed as an attribute in JSON format

<switch-value inputArray='[{"value": "12px"}, {"value": "1em"}, {"value": "x-large"}]' ... </switch-value>
// or with explicit label
<switch-value inputArray='[{"label": "font-size 12px", "value": "12px"}, {"label": "font-size 1em", "value": "1em"}, {"label": "font-size x-large", "value": "x-large"}]' 

inputObject

In this case, you first have to add the object "mk777" to the "window" object. Then you define the desired array. The name of the array is then passed in the "inputObject" attribute.

window.mk777 = {};
window.mk777.switchValue = {
    fontSize1: [
        { value: '12px' },
        { value: '1em' },
        { value: 'x-large' }
    ]
}

// or with explicit label
window.mk777 = {};
window.mk777.switchValue = {
    fontSize1: [
        { label: 'fontSize: 12em;', value: '12px' },
        { label: 'fontSize: 1em;', value: '1em' },
        { label: 'fontSize: x-large;', value: 'x-large' }
    ]
}

type (optional; default="radio")

You can choose whether the selection should be made via radio buttons or a combo box. Set the type to "radio" to use radio buttons or to "combo" to use a combo box.

destId/destPropName (optional)

You can bind another HTML element to the custom element so that the value change affects a property of the bound object. In "destId" you have to pass the ID of the HTML element and in "destPropName" the name of the property that is to be changed. The whole thing only works if both "destId" and "destPropName" are passed. <switch-value destId="testElementId" destPropName="style.fontSize" ...>

destPropChildSelector (string, optional)

If the custom element is bound (s. destId), then a property (or sub-property) of that element is usually set. But if you also pass "destPropChildSelector" then you can set properties of elements which are children (need not be direct children) of this element. The selector for the "querySelectorAll" method is expected as the value. For example, if you want to customize all children that have the CSS class "flex-item" then you have to use ".flex-item" as the value. <switch-value destId="testElementId" destPropName="style.fontSize" destPropChildSelector=".flex-items" ...>### destPropChildSelector (string, optional) ###

orientation (optional)

The "orientation" attribute controls whether the radio buttons are displayed horizontally ("horizontal") or vertically ("vertical"). The default is "horizontal".

startIndex (optional)

The "startIndex" attribute can be used to specify whether which value should be selected automatically. The start index should correspond to an index of the passed array. This resulting value is set when an HTML element is bound (see sourceId or inputObject). The custom event is also fired with this value.

caption (optional)

You can also specify an additional label. Depending on the "orientation" attribute, this is displayed either on the left side (orientation="horizontal") or above the radio buttons (orientation="vertical").

Event

Each time a value is selected, the valChange event is fired. The selected value is returned in "event.details.value".

window.onload = () => {
    document.addEventListener('valChange', (event) => {
        console.log(event.detail.value);
    })
}

In addition to detail.value, the following values are also passed in the event:

  • groupName: this value is only relevant if type="radio". It is the "name" given to all radio buttons.
  • type: is the selection made from a radio button group or a combo box ("radio" or "combo").

CSS

The "part" attribute has been assigned to elements as follows:

  • "frame" - the outermost frame
  • "caption" - the caption
  • "radio" - the input element of type "radio"
  • "radio-label" - the text of the radio button
  • "combo" - the select element (combo box)
  • "combo-label" - the option element of the combo box

HTML

Here’s how to embed the HTML custom element on the website:

<script type="module" src="./node_modules/switch-value/index.js"></script>