Radio Button allow the user to select one option from a set of items. They can be used in forms, filters, lists etc.
Each radio button component is based on a global CSS class called “msds-radio-button”. It is important to include it first as it is the main CSS class. Without it, the UI of the component won’t work.
The radio button makes use of custom styling by replacing the default input element with a pseudo-element selector, yet it keeps the native “checked” boolean property to toggle its state on/off (or selected/unselected).
<div class="col-6">
<div class="msds-radio-button">
<input class="msds-radio-button__input" id="defaul-radio-button" type="radio" name="radio-button-group">
<label class="msds-radio-button__label msds-text-body-1 msds-text-gray-10" for="defaul-radio-button">
Default one
</label>
</div>
<div class="msds-radio-button">
<input class="msds-radio-button__input" id="defaul-radio-button-two" type="radio" name="radio-button-group">
<label class="msds-radio-button__label msds-text-body-1 msds-text-gray-10" for="defaul-radio-button-two">
Default two
</label>
</div>
</div>
The styling of states are defined in the CSS file. Some of the states can be triggered programmatically by setting the relative property to the input element.
State of the hovered radio button.
It can be checked programmatically by setting the “checked” property to the input element.
<div class="col-6">
<div class="msds-radio-button">
<input class="msds-radio-button__input" id="checked-radio-button" type="radio" checked>
<label class="msds-radio-button__label msds-text-body-1 msds-text-gray-10" for="checked-radio-button">
Selected
</label>
</div>
</div>
State of the focused radio button
It can be disabled programmatically by setting the “disabled” attribute to the input element.
<div class="col-6">
<div class="msds-radio-button">
<input class="msds-radio-button__input" id="disabled-radio-button" type="radio" disabled>
<label class="msds-radio-button__label msds-text-body-1 msds-text-gray-10" for="disabled-radio-button">
Disabled
</label>
</div>
</div>
In order to use the radio button as read-only, you need to add the following to the input element:
"msds-radio-button__readonly"
"tabindex"
attribute and its value set to “-1”."checked"
attribute (optional).<div class="col-6">
<div class="msds-radio-button">
<input class="msds-radio-button__input msds-radio-button__readonly" id="readonly-radio-button" tabindex="-1"
type="radio" checked>
<label class="msds-radio-button__label msds-text-body-1 msds-text-gray-10" for="readonly-radio-button">
Readonly
</label>
</div>
</div>