1 |
|
2 | title: "Radio Buttons"
|
3 | layout: detail
|
4 | section: components
|
5 | iconId: radio_button
|
6 | path: /catalog/input-controls/radio-buttons/
|
7 | -->
|
8 |
|
9 | # Selection controls: radio buttons
|
10 |
|
11 | [Selection controls](https://material.io/components/selection-controls#usage) allow the user to select options.
|
12 |
|
13 | Use radio buttons to:
|
14 |
|
15 | * Select a single option from a list
|
16 | * Expose all available options
|
17 | * If available options can be collapsed, consider using a dropdown menu instead to use less space.
|
18 |
|
19 | ![Radio button hero example for menu options](images/radio-button-hero.png)
|
20 |
|
21 | **Contents**
|
22 |
|
23 | * [Using radio buttons](#using-radio-buttons)
|
24 | * [Radio buttons](#radio-buttons)
|
25 | * [Other variants](#other-variants)
|
26 | * [API](#api)
|
27 | * [Usage within web frameworks](#usage-within-web-frameworks)
|
28 |
|
29 | ## Using radio buttons
|
30 |
|
31 | Radio buttons allow the user to select one option from a set. Use radio buttons when the user needs to see all available options. If available options can be collapsed, consider using a dropdown menu because it uses less space.
|
32 |
|
33 | ### Installing radio buttons
|
34 |
|
35 | ```
|
36 | npm install @material/radio
|
37 | ```
|
38 |
|
39 | ### Styles
|
40 |
|
41 | ```scss
|
42 | @use "@material/radio/styles";
|
43 | @use "@material/form-field";
|
44 |
|
45 | @include form-field.core-styles;
|
46 | ```
|
47 |
|
48 | **Note: The form field styles are only required when the radio button is used with the form field.**
|
49 |
|
50 | ### JavaScript instantiation
|
51 |
|
52 | The radio button will work without JavaScript, but you can enhance it with a ripple interaction effect by instantiating `MDCRadio` on the `mdc-radio` 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 `MDCRadio` instance as its `input`.
|
53 |
|
54 | ```js
|
55 | import {MDCFormField} from '@material/form-field';
|
56 | import {MDCRadio} from '@material/radio';
|
57 |
|
58 | const radio = new MDCRadio(document.querySelector('.mdc-radio'));
|
59 | const formField = new MDCFormField(document.querySelector('.mdc-form-field'));
|
60 | formField.input = radio;
|
61 | ```
|
62 |
|
63 | **Note: See [Importing the JS component](../../docs/importing-js.md) for more information on how to import JavaScript.**
|
64 |
|
65 | ### Making radio buttons accessible
|
66 |
|
67 | Material Design spec advises that touch targets should be at least 48px x 48px.
|
68 | To meet this requirement, add the `mdc-radio--touch` class to your radio as follows:
|
69 |
|
70 | ```html
|
71 | <div class="mdc-touch-target-wrapper">
|
72 | <div class="mdc-radio mdc-radio--touch">
|
73 | <input class="mdc-radio__native-control" type="radio" id="radio-1" name="radios" checked>
|
74 | <div class="mdc-radio__background">
|
75 | <div class="mdc-radio__outer-circle"></div>
|
76 | <div class="mdc-radio__inner-circle"></div>
|
77 | </div>
|
78 | <div class="mdc-radio__ripple"></div>
|
79 | <div class="mdc-radio__focus-ring"></div>
|
80 | </div>
|
81 | </div>
|
82 | ```
|
83 |
|
84 | 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).
|
85 |
|
86 | The `mdc-radio__focus-ring` element ensures that a focus indicator is displayed in high contrast mode around the active/focused radio button.
|
87 |
|
88 | ## Radio buttons
|
89 |
|
90 | We recommend using MDC Radio with [MDC Form Field](../mdc-form-field) for enhancements such as label alignment, label activation of the ripple interaction effect, and RTL-awareness.
|
91 |
|
92 | ### Radio button example
|
93 |
|
94 | ```html
|
95 | <div class="mdc-form-field">
|
96 | <div class="mdc-radio">
|
97 | <input class="mdc-radio__native-control" type="radio" id="radio-1" name="radios" checked>
|
98 | <div class="mdc-radio__background">
|
99 | <div class="mdc-radio__outer-circle"></div>
|
100 | <div class="mdc-radio__inner-circle"></div>
|
101 | </div>
|
102 | <div class="mdc-radio__ripple"></div>
|
103 | <div class="mdc-radio__focus-ring"></div>
|
104 | </div>
|
105 | <label for="radio-1">Radio 1</label>
|
106 | </div>
|
107 | ```
|
108 |
|
109 | ### Radio button states
|
110 |
|
111 | Radio buttons can be selected or unselected. Radio buttons have enabled, disabled, hover, focused, and pressed states.
|
112 |
|
113 | ![Radio button states in a table. Columns are enabled, disabled, hover, focused, pressed. Rows are selected or unselected](images/radio-button-states.png)
|
114 |
|
115 | ## Other variants
|
116 |
|
117 | ### Disabled radio buttons
|
118 |
|
119 | To disable a radio button, add the `mdc-radio--disabled` class to the root element and set the `disabled` attribute on the `<input>` element.
|
120 | Disabled radio buttons cannot be interacted with and have no visual interaction effect.
|
121 |
|
122 | ```html
|
123 | <div class="mdc-form-field">
|
124 | <div class="mdc-radio mdc-radio--disabled">
|
125 | <input class="mdc-radio__native-control" type="radio" id="radio-1" name="radios" disabled>
|
126 | <div class="mdc-radio__background">
|
127 | <div class="mdc-radio__outer-circle"></div>
|
128 | <div class="mdc-radio__inner-circle"></div>
|
129 | </div>
|
130 | <div class="mdc-radio__ripple"></div>
|
131 | <div class="mdc-radio__focus-ring"></div>
|
132 | </div>
|
133 | <label for="radio-1">Radio 1</label>
|
134 | </div>
|
135 | ```
|
136 |
|
137 | ## API
|
138 |
|
139 | ### Sass mixins
|
140 |
|
141 | MDC Radio uses [MDC Theme](../mdc-theme)'s `secondary` color by default. Use the following mixins to customize it.
|
142 |
|
143 | Mixin | Description
|
144 | --- | ---
|
145 | `unchecked-stroke-color($color)` | Sets the stroke color of an unchecked, enabled radio button
|
146 | `checked-stroke-color($color)` | Sets the stroke color of a checked, enabled radio button
|
147 | `ink-color($color)` | Sets the ink color of an enabled radio button
|
148 | `disabled-unchecked-stroke-color($color)` | Sets the stroke color of an unchecked, disabled radio button
|
149 | `disabled-checked-stroke-color($color)` | Sets the stroke color of a checked, disabled radio button
|
150 | `disabled-ink-color($color)` | Sets the ink color of a disabled radio button
|
151 | `focus-indicator-color($color)` | Sets the color of the focus indicator
|
152 | `touch-target($size, $ripple-size)` | Sets radio touch target size which can be more than the ripple size. Param `$ripple-size` is required for custom ripple size, defaults to `$ripple-size`.
|
153 | `ripple-size($size)` | Sets custom ripple size of radio.
|
154 | `density($density-scale)` | Sets density scale for radio. Supported density scale values are `-3`, `-2`, `-1` and `0` (default).
|
155 |
|
156 | ## `MDCRadio` properties and methods
|
157 |
|
158 | Property | Value Type | Description
|
159 | --- | --- | ---
|
160 | `checked` | Boolean | Setter/getter for the radio's checked state
|
161 | `disabled` | Boolean | Setter/getter for the radio's disabled state. Setter proxies to foundation's `setDisabled` method
|
162 | `value` | String | Setter/getter for the radio's value
|
163 |
|
164 | ## Usage within web frameworks
|
165 |
|
166 | If you are using a JavaScript framework, such as React or Angular, you can create a Radio button 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](../../docs/integrating-into-frameworks.md).
|
167 |
|
168 | ### `MDCRadioAdapter`
|
169 |
|
170 | Method Signature | Description
|
171 | --- | ---
|
172 | `setNativeControlDisabled(disabled: boolean) => void` | Sets the input's `disabled` property to the given value
|
173 | `addClass(className: string) => void` | Adds a class to the root element
|
174 | `removeClass(className: string) => void` | Removes a class from the root element
|
175 |
|
176 | ### `MDCRadioFoundation`
|
177 |
|
178 | Method Signature | Description
|
179 | --- | ---
|
180 | `setDisabled(disabled: boolean) => void` | Sets the disabled value of the native control
|