4.0.0 • Published 4 years ago

@limetech/mdc-p2-checkbox v4.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

Checkboxes

Checkboxes allow the user to select one or more items from a set.

Design & API Documentation

Installation

npm install @limetech/mdc-p2-checkbox

Basic Usage

We recommend using MDC Checkbox with MDC Form Field for enhancements such as label alignment, label activation of the ripple interaction effect, and RTL-awareness.

<div class="mdc-form-field">
  <div class="mdc-checkbox">
    <input type="checkbox"
           class="mdc-checkbox__native-control"
           id="checkbox-1"/>
    <div class="mdc-checkbox__background">
      <svg class="mdc-checkbox__checkmark"
           viewBox="0 0 24 24">
        <path class="mdc-checkbox__checkmark-path"
              fill="none"
              d="M1.73,12.91 8.1,19.28 22.79,4.59"/>
      </svg>
      <div class="mdc-checkbox__mixedmark"></div>
    </div>
    <div class="mdc-checkbox__ripple"></div>
  </div>
  <label for="checkbox-1">Checkbox 1</label>
</div>

Note: If you are using IE, you need to include a closing </path> tag if you wish to avoid console warnings.

Styles

@import "@limetech/mdc-p2-form-field/mdc-form-field";
@import "@limetech/mdc-p2-checkbox/mdc-checkbox";

JavaScript Instantiation

The checkbox will work without JavaScript, but you can enhance it with a ripple interaction effect by instantiating MDCCheckbox on the mdc-checkbox element. To activate the ripple effect upon interacting with the label, you must also instantiate MDCFormField on the mdc-form-field element and set the MDCCheckbox instance as its input.

import {MDCFormField} from '@limetech/mdc-p2-form-field';
import {MDCCheckbox} from '@limetech/mdc-p2-checkbox';

const checkbox = new MDCCheckbox(document.querySelector('.mdc-checkbox'));
const formField = new MDCFormField(document.querySelector('.mdc-form-field'));
formField.input = checkbox;

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

Variants

Disabled

Note that mdc-checkbox--disabled is necessary on the root element of CSS-only checkboxes to prevent hover states from activating. Checkboxes that use the JavaScript component do not need this class; a disabled attribute on the <input> element is sufficient.

<div class="mdc-checkbox mdc-checkbox--disabled">
  <input type="checkbox"
         id="basic-disabled-checkbox"
         class="mdc-checkbox__native-control"
         disabled />
  <div class="mdc-checkbox__background">
    <svg class="mdc-checkbox__checkmark"
         viewBox="0 0 24 24">
      <path class="mdc-checkbox__checkmark-path"
            fill="none"
            d="M1.73,12.91 8.1,19.28 22.79,4.59"/>
    </svg>
    <div class="mdc-checkbox__mixedmark"></div>
  </div>
  <div class="mdc-checkbox__ripple"></div>
</div>
<label for="basic-disabled-checkbox" id="basic-disabled-checkbox-label">This is my disabled checkbox</label>

Additional Information

Accessibility

Material Design spec advises that touch targets should be at least 48 x 48 px. To meet this requirement, add the mdc-checkbox--touch class to your checkbox as follows:

<div class="mdc-touch-target-wrapper">
  <div class="mdc-checkbox mdc-checkbox--touch">
    <input type="checkbox"
           class="mdc-checkbox__native-control"
           id="checkbox-1"/>
    <div class="mdc-checkbox__background">
      <svg class="mdc-checkbox__checkmark"
           viewBox="0 0 24 24">
        <path class="mdc-checkbox__checkmark-path"
              fill="none"
              d="M1.73,12.91 8.1,19.28 22.79,4.59"/>
      </svg>
      <div class="mdc-checkbox__mixedmark"></div>
    </div>
    <div class="mdc-checkbox__ripple"></div>
  </div>
</div>

Note that 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).

Style Customization

MDC Checkbox uses MDC Theme's secondary color by default for "marked" states (i.e., checked or indeterminate).

Sass Mixins

MixinDescription
mdc-checkbox-container-colors($unmarked-stroke-color, $unmarked-fill-color, $marked-stroke-color, $marked-fill-color, $generate-keyframes)Sets stroke & fill colors for both marked and unmarked state of enabled checkbox. Set $generate-keyframes to false to prevent the mixin from generating @keyframes.
mdc-checkbox-disabled-container-colors($unmarked-stroke-color, $unmarked-fill-color, $marked-stroke-color, $marked-fill-color)Sets stroke & fill colors for both marked and unmarked state of disabled checkbox.
mdc-checkbox-ink-color($color)Sets the ink color of the checked and indeterminate icons for an enabled checkbox
mdc-checkbox-disabled-ink-color($color)Sets the ink color of the checked and indeterminate icons for a disabled checkbox
mdc-checkbox-focus-indicator-color($color)Sets the color of the focus indicator (ripple) when checkbox is selected or is in indeterminate state.
mdc-checkbox-ripple-size($ripple-size)Sets the ripple size of the checkbox.
mdc-checkbox-density($density-scale)Sets density scale for checkbox, Supported density scales are -3, -2, -1, and 0 (default).

The ripple effect for the Checkbox component is styled using MDC Ripple mixins.

Caveat: Edge and CSS Variables

In browsers that fully support CSS variables, MDC Checkbox references CSS variables wherever theme properties are used. However, due to Edge's buggy CSS variable support, the background-color for .mdc-checkbox__background::before will not honor CSS variables in Edge. This means you will need to override this style manually for Edge if you alter the CSS variable for the primary color.

MDCCheckbox Properties and Methods

Property NameTypeDescription
checkedbooleanSetter/getter for the checkbox's checked state
indeterminatebooleanSetter/getter for the checkbox's indeterminate state
disabledbooleanSetter/getter for the checkbox's disabled state
valuestringSetter/getter for the checkbox's

Usage within Web Frameworks

If you are using a JavaScript framework, such as React or Angular, you can create a Checkbox for your framework. Depending on your needs, you can use the Simple Approach: Wrapping MDC Web Vanilla Components, or the Advanced Approach: Using Foundations and Adapters. Please follow the instructions here.

MDCCheckboxAdapter

Method SignatureDescription
addClass(className: string) => voidAdds a class to the root element.
removeClass(className: string) => voidRemoves a class from the root element.
forceLayout() => voidForce-trigger a layout on the root element. This is needed to restart animations correctly. If you find that you do not need to do this, you can simply make it a no-op.
isAttachedToDOM() => booleanReturns true if the component is currently attached to the DOM, false otherwise.
isIndeterminate() => booleanReturns true if the component is in the indeterminate state.
isChecked() => booleanReturns true if the component is checked.
hasNativeControl() => booleanReturns true if the input is present in the component.
setNativeControlDisabled(disabled: boolean) => voidSets the input to disabled.
setNativeControlAttr(attr: string, value: string) => voidSets an HTML attribute to the given value on the native input element.
removeNativeControlAttr(attr: string) => voidRemoves an attribute from the native input element.

MDCCheckboxFoundation

Method SignatureDescription
setDisabled(disabled: boolean) => voidUpdates the disabled property on the underlying input. Does nothing when the underlying input is not present.
handleAnimationEnd() => voidanimationend event handler that should be applied to the root element.
handleChange() => voidchange event handler that should be applied to the checkbox element.