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

@loipham/material-icon-button

v14.0.4

Published

The Material Components for the web icon button component

Downloads

2

Readme

Icon buttons

Icon buttons allow users to take actions, and make choices, with a single tap.

Note: For buttons with both icons and text, use the mdc-button component. For more information, see the mdc-button docs.

Using icon buttons

Installation

npm install @loipham/material-icon-button

Styles

@use "@loipham/material-icon-button/styles";

JavaScript instantiation

The icon button will work without JavaScript, but you can enhance it to have a ripple effect by instantiating MDCRipple on the root element. See MDC Ripple for details.

import {MDCRipple} from '@loipham/material-ripple';

const iconButtonRipple = new MDCRipple(document.querySelector('.mdc-icon-button'));
iconButtonRipple.unbounded = true;

Note: See Importing the JS component for more information on how to import JavaScript.

Icons

We recommend using Material Icons from Google Fonts:

<head>
  <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
</head>

However, you can also use SVG, Font Awesome, or any other icon library you wish.

Icon button

<button class="mdc-icon-button material-icons">
  <div class="mdc-icon-button__ripple"></div>
  <span class="mdc-icon-button__focus-ring"></span>
  favorite
</button>

Note: The MDC Icon Button can be used with both <button> and <a> tags.

Note: IE11 will not center the icon properly if there is a newline or space after the material icon text.

Making icon buttons accessible

Material Design spec advises that touch targets should be at least 48 x 48 px. To meet this requirement, add the following to your button:

<div class="mdc-touch-target-wrapper">
  <button class="mdc-icon-button material-icons">
    <div class="mdc-icon-button__ripple"></div>
    <span class="mdc-icon-button__focus-ring"></span>
    favorite
    <div class="mdc-icon-button__touch"></div>
  </button>
</div>

Note: The outer mdc-touch-target-wrapper element is only necessary if you want to avoid potentially overlapping touch targets on adjacent elements (due to collapsing margins).

The mdc-icon-button__focus-ring element ensures that a focus indicator is displayed in high contrast mode around the active/focused icon button.

Icon button toggle

The icon button can be used to toggle between an on and off icon.

To style an icon button as an icon button toggle, add both icons as child elements and place the mdc-icon-button__icon--on class on the icon that represents the on element. If the button should be initialized in the "on" state, then add the mdc-icon-button--on class to the parent button.

<button id="add-to-favorites"
   class="mdc-icon-button"
   aria-label="Add to favorites"
   aria-pressed="false">
   <div class="mdc-icon-button__ripple"></div>
   <span class="mdc-icon-button__focus-ring"></span>
   <i class="material-icons mdc-icon-button__icon mdc-icon-button__icon--on">favorite</i>
   <i class="material-icons mdc-icon-button__icon">favorite_border</i>
</button>

Then, instantiate an MDCIconButtonToggle on the root element.

import {MDCIconButtonToggle} from '@loipham/material-icon-button';
const iconToggle = new MDCIconButtonToggle(document.querySelector('.mdc-icon-button'));

Icon button toggle with SVG

The icon button toggle can be used with SVGs.

<button id="star-this-item"
   class="mdc-icon-button mdc-icon-button--on"
   aria-label="Unstar this item"
   aria-pressed="true">
   <div class="mdc-icon-button__ripple"></div>
   <span class="mdc-icon-button__focus-ring"></span>
   <svg class="mdc-icon-button__icon">
     ...
   </svg>
   <svg class="mdc-icon-button__icon mdc-icon-button__icon--on">
     ...
  </svg>
</button>

Icon button toggle with an image

The icon button toggle can be used with img tags.

<button id="star-this-item"
   class="mdc-icon-button mdc-icon-button--on"
   aria-label="Unstar this item"
   aria-pressed="true">
   <div class="mdc-icon-button__ripple"></div>
   <span class="mdc-icon-button__focus-ring"></span>
   <img src="" class="mdc-icon-button__icon"/>
   <img src="" class="mdc-icon-button__icon mdc-icon-button__icon--on"/>
</button>

Icon button toggle with toggled aria label

Some designs may call for the aria label to change depending on the icon button state. In this case, specify the data-aria-label-on (aria label in on state) and aria-data-label-off (aria label in off state) attributes, and omit the aria-pressed attribute.

<button id="add-to-favorites"
   class="mdc-icon-button"
   aria-label="Add to favorites"
   data-aria-label-on="Remove from favorites"
   data-aria-label-off="Add to favorites">
   <div class="mdc-icon-button__ripple"></div>
   <span class="mdc-icon-button__focus-ring"></span>
   <i class="material-icons mdc-icon-button__icon mdc-icon-button__icon--on">favorite</i>
   <i class="material-icons mdc-icon-button__icon">favorite_border</i>
</button>

API

CSS classes

CSS Class | Description --- | --- mdc-icon-button | Mandatory. mdc-icon-button__ripple | Mandatory. Indicates the element which shows the ripple styling. mdc-icon-button--on | This class is applied to the root element and is used to indicate if the icon button toggle is in the "on" state. mdc-icon-button__icon | This class is applied to each icon element for the icon button toggle. mdc-icon-button__icon--on | This class is applied to a icon element and is used to indicate the toggle button icon that is represents the "on" icon. mdc-icon-button__focus-ring | Recommended. Indicates the element which shows the high contrast mode focus ring styling.

Sass mixins

To customize an icon button's color and properties, you can use the following mixins.

Mixin | Description --- | --- density($density-scale) | Sets density scale for icon button. Supported density scales range from -5 to 0, (0 being the default). size($size) | Sets the padding for the icon button based on overall size. ink-color($color) | Sets the font color and the ripple color to the provided color value. disabled-ink-color($color) | Sets the font color to the provided color value for a disabled icon button. flip-icon-in-rtl() | Flips icon only in RTL context.

MDCIconButtonToggle properties and methods

Property | Value Type | Description --- | --- | --- on | Boolean | Sets the toggle state to the provided isOn value.

Events

Event Name | Event Data Structure | Description --- | --- | --- MDCIconButtonToggle:change | {"detail": {"isOn": boolean}} | Emits when the icon is toggled.

MDCIconButtonToggleAdapter

Method Signature | Description --- | --- addClass(className: string) => void | Adds a class to the root element. removeClass(className: string) => void | Removes a class from the root element. hasClass(className: string) => boolean | Determines whether the root element has the given CSS class name. setAttr(name: string, value: string) => void | Sets the attribute name to value on the root element. notifyChange(evtData: {isOn: boolean}) => void | Broadcasts a change notification, passing along the evtData to the environment's event handling system. In our vanilla implementation, Custom Events are used for this.

MDCIconButtonToggleFoundation

Method Signature | Description --- | --- handleClick() | Event handler triggered on the click event. It will toggle the icon from on/off and update aria attributes.