-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(switch): Merge updated switch into master #3214
Changes from 24 commits
133698c
e3e400d
bedd674
e071d07
235b132
c508c29
c6e37a5
293bdba
5b0f71c
4584f00
47c4147
35dd8b0
7b473b5
eabcc62
72ec918
209b557
6d61cb9
288a183
82b8490
a3332e4
2413731
ae3c90a
9becfc8
de825d9
b5570a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ path: /catalog/input-controls/switches/ | |
</a> | ||
</div>--> | ||
|
||
Switches toggle the state of a single settings option on or off, and are mobile preferred. | ||
Switches toggle the state of a single setting on or off. They are the preferred way to adjust settings on mobile. | ||
|
||
## Design & API Documentation | ||
|
||
|
@@ -40,24 +40,47 @@ npm install @material/switch | |
|
||
```html | ||
<div class="mdc-switch"> | ||
<input type="checkbox" id="basic-switch" class="mdc-switch__native-control" role="switch"> | ||
<div class="mdc-switch__background"> | ||
<div class="mdc-switch__knob"></div> | ||
<div class="mdc-switch__track"></div> | ||
<div class="mdc-switch__thumb-underlay"> | ||
<div class="mdc-switch__thumb"> | ||
<input type="checkbox" id="basic-switch" class="mdc-switch__native-control" role="switch"> | ||
</div> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The basic usage section seems to be missing the Styles (for imports) and JavaScript Instantiation sections There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, will add! |
||
</div> | ||
</div> | ||
<label for="basic-switch">off/on</label> | ||
``` | ||
|
||
### Styles | ||
|
||
```scss | ||
@import "@material/switch/mdc-switch"; | ||
``` | ||
|
||
### JavaScript Instantiation | ||
|
||
The Switch requires JavaScript to function, so it is necessary to instantiate MDCSwitch with the HTML. | ||
|
||
```js | ||
import {MDCSwitch} from '@material/switch'; | ||
|
||
const switchControl = new MDCSwitch(document.querySelector('.mdc-switch')); | ||
``` | ||
|
||
> See [Importing the JS component](../../docs/importing-js.md) for more information on how to import JavaScript. | ||
|
||
## Variant | ||
|
||
### Disabled Switch | ||
|
||
Users can add the `disabled` attribute directly to the `<input>` element or a parent `<fieldset>` element to disable a switch. | ||
Users can add the class 'mdc-switch--disabled' to the 'mdc-switch' element to disable the switch. | ||
|
||
```html | ||
<div class="mdc-switch"> | ||
<input type="checkbox" id="another-basic-switch" class="mdc-switch__native-control" role="switch" disabled> | ||
<div class="mdc-switch__background"> | ||
<div class="mdc-switch__knob"></div> | ||
<div class="mdc-switch mdc-switch--disabled"> | ||
<div class="mdc-switch__track"></div> | ||
<div class="mdc-switch__thumb-underlay"> | ||
<div class="mdc-switch__thumb"> | ||
<input type="checkbox" id="another-basic-switch" class="mdc-switch__native-control" role="switch" disabled> | ||
</div> | ||
</div> | ||
</div> | ||
<label for="another-basic-switch">off/on</label> | ||
|
@@ -70,17 +93,63 @@ Users can add the `disabled` attribute directly to the `<input>` element or a pa | |
CSS Class | Description | ||
--- | --- | ||
`mdc-switch` | Mandatory, for the parent element. | ||
`mdc-switch__native-control` | Mandatory, for the input checkbox. | ||
`mdc-switch__background` | Mandatory, for the background element. | ||
`mdc-switch__knob` | Mandatory, for the knob element. | ||
`mdc-switch__track` | Mandatory, for the track element. | ||
`mdc-switch__thumb-underlay` | Mandatory, for the ripple effect. | ||
`mdc-switch__thumb` | Mandatory, for the thumb element. | ||
`mdc-switch__native-control` | Mandatory, for the hidden input checkbox. | ||
|
||
### Sass Mixins | ||
|
||
The following mixins apply only to _enabled_ switches in the _on_ (checked) state. | ||
It is not currently possible to customize the color of a _disabled_ or _off_ (unchecked) switch. | ||
MDC Switch uses [MDC Theme](../mdc-theme)'s `secondary` color by default for the checked (toggled on) state. | ||
Use the following mixins to customize _enabled_ switches. It is not currently possible to customize the color of a | ||
_disabled_ switch. Disabled switches use the same colors as enabled switches, but with a different opacity value. | ||
|
||
Mixin | Description | ||
--- | --- | ||
`mdc-switch-track-color($color)` | Sets the track color. | ||
`mdc-switch-knob-color($color)` | Sets the knob color. | ||
`mdc-switch-focus-indicator-color($color)` | Sets the focus indicator color. | ||
`mdc-switch-toggled-on-color($color)` | Sets the base color of the track, thumb, and ripple when the switch is toggled on. | ||
`mdc-switch-toggled-off-color($color)` | Sets the base color of the track, thumb, and ripple when the switch is toggled off. | ||
`mdc-switch-toggled-on-track-color($color)` | Sets color of the track when the switch is toggled on. | ||
`mdc-switch-toggled-off-track-color($color)` | Sets color of the track when the switch is toggled off. | ||
`mdc-switch-toggled-on-thumb-color($color)` | Sets color of the thumb when the switch is toggled on. | ||
`mdc-switch-toggled-off-thumb-color($color)` | Sets color of the thumb when the switch is toggled off. | ||
`mdc-switch-toggled-on-ripple-color($color)` | Sets the color of the ripple surrounding the thumb when the switch is toggled on. | ||
`mdc-switch-toggled-off-ripple-color($color)` | Sets the color of the ripple surrounding the thumb when the switch is toggled off. | ||
|
||
## `MDCSwitch` Properties and Methods | ||
|
||
Property | Value Type | Description | ||
--- | --- | --- | ||
`checked` | Boolean | Setter/getter for the switch's checked state | ||
`disabled` | Boolean | Setter/getter for the switch's disabled state | ||
|
||
## Usage within Web Frameworks | ||
|
||
If you are using a JavaScript framework, such as React or Angular, you can create a Switch 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). | ||
|
||
### `MDCSwitchAdapter` | ||
|
||
| 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. | | ||
| `setNativeControlChecked(checked: boolean)` | Sets the checked state of the native control. | | ||
| `isNativeControlChecked() => boolean` | Returns the checked state of the native control. | | ||
| `setNativeControlDisabled(disabled: boolean)` | Sets the disabled state of the native control. | | ||
| `isNativeControlDisabled() => boolean` | Returns the disabled state of the native control. | | ||
|
||
### `MDCSwitchFoundation` | ||
|
||
| Method Signature | Description | | ||
| --- | --- | | ||
| `isChecked() => boolean` | Returns whether the native control is checked. | | ||
| `setChecked(checked: boolean) => void` | Sets the checked value of the native control and updates styling to reflect the checked state. | | ||
| `isDisabled() => boolean` | Returns whether the native control is disabled. | | ||
| `setDisabled(disabled: boolean) => void` | Sets the disabled value of the native control and updates styling to reflect the disabled state. | | ||
| `handleChange() => void` | Handles a change event from the native control. | | ||
|
||
### `MDCSwitchFoundation` Event Handlers | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be documented under the component rather than the foundation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just re-read this and realized why it's here; I had misread it earlier. This is basically an example of the sort of thing Pat was saying we should add, so this can stay here. |
||
If wrapping the switch component it is necessary to add an event handler for native control change events that calls the `handleChange` foundation method. For an example of this, see the [MDCSwitch](index.js) component `initialSyncWithDOM` method. | ||
|
||
| Event | Element Selector | Foundation Handler | | ||
| --- | --- | --- | | ||
| `change` | `.mdc-switch__native-control` | `handleChange()` | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a newline before this line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do.