14.0.0 • Published 5 months ago

@material/radio v14.0.0

Weekly downloads
46,383
License
MIT
Repository
github
Last release
5 months ago

Radio Buttons

The MDC Radio Button component provides a radio button adhering to the Material Design Specification. It requires no Javascript out of the box, but can be enhanced with Javascript to provide better interaction UX as well as a component-level API for state modification.

Design & API Documentation

Installation

npm install @material/radio

Usage

<div class="mdc-radio">
  <input class="mdc-radio__native-control" type="radio" id="radio-1" name="radios" checked>
  <div class="mdc-radio__background">
    <div class="mdc-radio__outer-circle"></div>
    <div class="mdc-radio__inner-circle"></div>
  </div>
</div>
<label id="radio-1-label" for="radio-1">Radio 1</label>

<div class="mdc-radio">
  <input class="mdc-radio__native-control" type="radio" id="radio-2" name="radios">
  <div class="mdc-radio__background">
    <div class="mdc-radio__outer-circle"></div>
    <div class="mdc-radio__inner-circle"></div>
  </div>
</div>
<label id="radio-2-label" for="radio-2">Radio 2</label>

TODO(TK): Talk about mdc-form-field here.

Disabled Radios

<div class="mdc-radio mdc-radio--disabled">
  <input class="mdc-radio__native-control" type="radio" id="radio-1" name="radios" disabled>
  <div class="mdc-radio__background">
    <div class="mdc-radio__outer-circle"></div>
    <div class="mdc-radio__inner-circle"></div>
  </div>
</div>
<label id="radio-1-label" for="radio-1">Disabled Radio 1</label>

Note that mdc-radio--disabled is necessary on the root element in order to avoid having the ripple elements intercept pointer events when using JS. When using the CSS-only variation, this is also necessary to prevent hover states from activating.

Using the JS Component

MDC Radio ships with Component / Foundation classes which provide enhanced interaction UX via mdc-ripple, as well as APIs for programmatically altering the radio's state.

Including in code

ES2015
import {MDCRadio, MDCRadioFoundation} from '@material/radio';
CommonJS
const mdcRadio = require('mdc-radio');
const MDCRadio = mdcRadio.MDCRadio;
const MDCRadioFoundation = mdcRadio.MDCRadioFoundation;
AMD
require(['path/to/mdc-radio'], mdcRadio => {
  const MDCRadio = mdcRadio.MDCRadio;
  const MDCRadioFoundation = mdcRadio.MDCRadioFoundation;
});
Global
const MDCRadio = mdc.radio.MDCRadio;
const MDCRadioFoundation = mdc.radio.MDCRadioFoundation;

Automatic Instantiation

If you do not care about retaining the component instance for the radio, simply call attachTo() and pass it a DOM element.

mdc.radio.MDCRadio.attachTo(document.querySelector('.mdc-radio'));

Manual Instantiation

Radios can easily be initialized using their default constructors as well, similar to attachTo.

import {MDCRadio} from '@material/radio';

const radio = new MDCRadio(document.querySelector('.mdc-radio'));

MDCRadio API

Similar to regular DOM elements, the MDCRadio functionality is exposed through accessor methods.

MDCRadio.checked

Boolean. Proxies to the foundation's isChecked/setChecked methods when retrieved/set respectively.

MDCRadio.disabled

Boolean. Proxies to the foundation's isDisabled/setDisabled methods when retrieved/set respectively.

MDCRadio.value

String. Proxies to the foundation's getValue/setValue methods when retrieved/set respectively.

Using the Foundation Class

Since MDC Radio is primarily driven by its native control, the adapter API is extremely simple.

Method SignatureDescription
getNativeControl() => HTMLInputElement?Returns the native radio control, if available. Note that if this control is not available, the methods that rely on it will exit gracefully.
addClass(className: string) => voidAdds a class to the root element.
removeClass(className: string) => voidRemoves a class from the root element.

The full foundation API

MDCRadioFoundation.isChecked() => boolean

Returns the value of adapter.getNativeControl().checked. Returns false if getNativeControl() does not return an object.

MDCRadioFoundation.setChecked(checked: boolean) => void

Sets the value of adapter.getNativeControl().checked. Does nothing if getNativeControl() does not return an object.

MDCRadioFoundation.isDisabled() => boolean

Returns the value of adapter.getNativeControl().disabled. Returns false if getNativeControl() does not return an object.

MDCRadioFoundation.setDisabled(disabled: boolean) => void

Sets the value of adapter.getNativeControl().disabled. Also adds/removes the mdc-radio--disabled class based whether or not disabled is true. Gracefully handles the absence of a return value of getNativeControl().

MDCRadioFoundation.getValue() => string

Returns the value of adapter.getNativeControl().value. Returns null if getNativeControl() does not return an object.

MDCRadioFoundation.setValue(value: string) => void

Sets the value of adapter.getNativeControl().value. Does nothing if getNativeControl() does not return an object.

Theming

MDC Radios use the theme's secondary color by default for checked states.

Sass Mixins

The following mixins apply only to enabled radio buttons. It is not currently possible to customize the color of a disabled radio button.

MixinDescription
mdc-radio-unchecked-stroke-color($color)Sets the stroke color of an unchecked radio
mdc-radio-checked-stroke-color($color)Sets the stroke color of a checked radio
mdc-radio-ink-color($color)Sets the ink color
mdc-radio-focus-indicator-color($color)Sets the color of the focus indicator

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

Caveat: Edge and CSS Variables

In browsers that fully support CSS variables, MDC Radio references CSS variables wherever theme properties are used. However, due to Edge's buggy CSS variable support, the background-color for .mdc-radio__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.

@zentek/radiomdwrapper@beezydev/radiomaterial-components-web@everything-registry/sub-chunk-584zarinpal-story-bookvue-material-design-components@gmvdev/materialssolid-material-componentssnabbdom-material-componentstest-rsmdcteststeppersvelte-arcadiasvmdfeedback-form-demo@inovex.de/elements@o-rango/orango-material-design@onecomponent/radio@mcwv/radio@material/react-radio@materials-elements/core@materials-elements/site@materials-ui/core@materials-ui/site@mhamrah/svelte-material-ui@materialr/radio@mdc-stencil/radio@material/mwc-radio@infinitebrahmanuniverse/nolb-_mateidm-reservation-search-results-details-lib@react-mdc/radio@rmwc/radio@robertkern/vue-materialember-cli-mdc-segmented-buttonember-material-componentsember-cli-mdc-radio@emuanalytics/flow-rdf@pitaya-components/list@pitaya-components/radio@plonquo/ember-material-components@exmg/exmg-radio-group@pmwcs/radio@preact-material-components/radio@betazuul/radio@blackpurl/web-components@angular/material-experimental@angular/materialmyg-radio@smui/radio@aurelia-mdc-web/radio@aurelia2-mdc-web/radio@authentic/mwc-radio@arterial/radioaurelia-material-uiaurelia-mdc-uimaterial-toolboxmaterial-imbamaterial-react-jsbw-material@codeadraas/vue3-material@betsybot/betsy-web-components@bitchin/react-material-web@bringhub/fabric.jsnextime-delivery-date@types/material__radio@worm425/betsy-web-componentsmaple-material-vue@xdam/ember-partials@detachhead/smui-radiocomponents-library-v1react-mdcuireact-mdcreact-material-web-componentspreact-material-componentspreact-material-components-mgrclosure-react-radio@dev.mohe/mwc-radioqpilot-delivery-date
14.0.0

2 years ago

13.0.0

3 years ago

12.0.0

3 years ago

11.0.0

3 years ago

10.0.0

3 years ago

9.0.0

3 years ago

8.0.0

3 years ago

7.0.0

4 years ago

6.0.0

4 years ago

5.1.0

4 years ago

5.0.0

4 years ago

4.0.0

4 years ago

3.2.0

5 years ago

4.0.0-canary.1

5 years ago

4.0.0-canary.0

5 years ago

4.0.0-alpha.0

5 years ago

3.1.0

5 years ago

3.1.0-alpha.0

5 years ago

3.0.0

5 years ago

3.0.0-alpha.1

5 years ago

3.0.0-alpha.0

5 years ago

2.3.0

5 years ago

2.1.1

5 years ago

2.0.0

5 years ago

1.1.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

1.0.0-1

5 years ago

1.0.0-0

5 years ago

0.44.1

5 years ago

0.44.0

5 years ago

0.43.0

5 years ago

0.42.0

5 years ago

0.41.0

5 years ago

0.40.1

6 years ago

0.40.0

6 years ago

0.39.3

6 years ago

0.39.2

6 years ago

0.39.1

6 years ago

0.39.0

6 years ago

0.38.1

6 years ago

0.38.0

6 years ago

0.37.1

6 years ago

0.37.0

6 years ago

0.36.0

6 years ago

0.36.0-0

6 years ago

0.35.0

6 years ago

0.34.1

6 years ago

0.34.0

6 years ago

0.33.0

6 years ago

0.32.0

6 years ago

0.31.0

6 years ago

0.30.0

6 years ago

0.29.0

6 years ago

0.28.0

6 years ago

0.27.0

6 years ago

0.26.0

6 years ago

0.25.0

6 years ago

0.24.0

6 years ago

0.23.0

7 years ago

0.2.15

7 years ago

0.2.14

7 years ago

0.2.13

7 years ago

0.2.12

7 years ago

0.2.11

7 years ago

0.2.10

7 years ago

0.2.9

7 years ago

0.2.8

7 years ago

0.2.7

7 years ago

0.2.6

7 years ago

0.2.5

7 years ago

0.2.4

7 years ago

0.2.3

7 years ago

0.2.2

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.6

7 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago