Skip to content
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): Implement css switch component #235

Merged
merged 7 commits into from
Feb 1, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions demos/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<li><a href="list.html">List</a></li>
<li><a href="select.html">Select</a></li>
<li><a href="simple-menu.html">Menu (simple)</a></li>
<li><a href="switch.html">Switch</a></li>
<li><a href="radio.html">Radio</a></li>
<li><a href="ripple.html">Ripple</a></li>
<li><a href="snackbar.html">Snackbar</a></li>
Expand Down
110 changes: 110 additions & 0 deletions demos/switch.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<!DOCTYPE html>
<!--
Copyright 2016 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License
-->
<html>
<head>
<meta charset="utf-8">
<title>MDC Switch Demo</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<style>
/* Make the demo look a little bit better */
section {
margin-bottom: 0.5rem;
margin: 16px;
padding: 40px;
background-color: #eee;
}

h2 {
margin: 0;
margin-bottom: 20px;
}

label {
font-size: 16px;
}

.mdc-theme--dark {
background-color: #333;
}

.mdc-theme--dark > h2 {
color: white;
}

.mdc-theme--dark > label {
color: white;
}
</style>
<script src="assets/material-components-web.css.js" charset="utf-8"></script>
</head>
<body class="mdc-typography">
<main>
<h1>MDC Switch - CSS Only</h1>

<section>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: indentation for this HTML looks off

<h2>Switch on Light Theme</h2>
<div class="mdc-switch">
<input type="checkbox" id="basic-switch" class="mdc-switch__native-control" />
<div class="mdc-switch__background">
<div class="mdc-switch__knob"></div>
</div>
</div>
<label for="basic-switch" class="mdc-switch-label">off/on</label>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What purpose does .mdc-switch-label serve? It has no CSS definition

</section>
<section>
<h2>Switch on Light Theme - Disabled</h2>
<div class="mdc-switch mdc-switch--disabled">
<input type="checkbox"
id="basic-switch--disabled"
class="mdc-switch__native-control"
disabled />
<div class="mdc-switch__background">
<div class="mdc-switch__knob"></div>
</div>
</div>
<label for="basic-switch--disabled" class="mdc-switch-label">off/on</label>
</section>

<section class="mdc-theme--dark">
<h2>Switch on Light Theme</h2>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think you mean "Dark Theme" here and below

<div class="mdc-switch">
<input type="checkbox"
id="basic-switch--dark"
class="mdc-switch__native-control"/>
<div class="mdc-switch__background">
<div class="mdc-switch__knob"></div>
</div>
</div>
<label for="basic-switch--dark" class="mdc-switch-label">off/on</label>
</section>

<section class="mdc-theme--dark">
<h2>Switch on Light Theme - Disabled</h2>
<div class="mdc-switch mdc-switch--disabled">
<input type="checkbox"
id="basic-switch--dark--disabled"
class="mdc-switch__native-control"
disabled />
<div class="mdc-switch__background">
<div class="mdc-switch__knob"></div>
</div>
</div>
<label for="basic-switch--dark--disabled" class="mdc-switch-label">off/on</label>
</section>

</main>
<script src="assets/material-components-web.js" charset="utf-8"></script>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
@import "@material/ripple/mdc-ripple";
@import "@material/select/mdc-select";
@import "@material/snackbar/mdc-snackbar";
@import "@material/switch/mdc-switch";
@import "@material/textfield/mdc-textfield";
@import "@material/theme/mdc-theme";
@import "@material/typography/mdc-typography";
1 change: 1 addition & 0 deletions packages/material-components-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@material/ripple": "^0.1.2",
"@material/select": "^0.2.0",
"@material/snackbar": "^0.1.2",
"@material/switch": "^0.3.0",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be "^0.1.0" since it's the first release

"@material/textfield": "^0.2.0",
"@material/theme": "^0.1.1",
"@material/typography": "^0.1.1"
Expand Down
49 changes: 49 additions & 0 deletions packages/mdc-switch/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# MDC Switch

The MDC Switch component is a spec-aligned switch component adhering to the
[Material Design Switch requirements](https://material.io/guidelines/components/selection-controls.html#selection-controls-switch).
It works without JavaScript.

## Installation

```
npm install --save @material/switch
```

## Usage

### CSS Only
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can just get rid of saying CSS Only here, since usually we say CSS Only when we want to specify how to use a component that requires Javascript, but gracefully degrades to a CSS Only implementation, e.g. textfields

```html
<div class="mdc-switch">
<input type="checkbox" id="basic-switch" class="mdc-switch__native-control" />
<div class="mdc-switch__background">
<div class="mdc-switch__knob"></div>
</div>
</div>
<label for="basic-switch" class="mdc-switch-label">off/on</label>
```

### CSS Only - Disabled
```html
<div class="mdc-switch mdc-switch--disabled">
<input type="checkbox" id="another-basic-switch" class="mdc-switch__native-control" disabled />
<div class="mdc-switch__background">
<div class="mdc-switch__knob"></div>
</div>
</div>
<label for="another-basic-switch" class="mdc-switch-label">off/on</label>
```

## Classes

### Block

The block class is `mdc-switch`. This defines the top-level switch element.

### Modifier

The provided modifiers are:

| Class | Description |
| ----------------------| -------------------------------------------- |
| `mdc-fab--disabled` | Applies disabled style to disabled switches. |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you mean mdc-switch--disabled here :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hahaha sure do!

18 changes: 18 additions & 0 deletions packages/mdc-switch/_mixins.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// Copyright 2016 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

@function mdc-switch-transition($property, $timing-function, $duration: $mdc-switch-transition-duration) {
@return $property $duration $timing-function;
}
32 changes: 32 additions & 0 deletions packages/mdc-switch/_variables.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// Copyright 2016 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

$mdc-switch-track-width: 34px;
$mdc-switch-track-height: 14px;
$mdc-switch-knob-diameter: 20px;
$mdc-switch-focus-ring-diameter: 48px;
$mdc-switch-knob-active-margin: $mdc-switch-track-width - $mdc-switch-knob-diameter;

$mdc-switch-unchecked-background-color: #221f1f;
$mdc-switch-unchecked-knob-color: #f1f1f1;
$mdc-switch-unchecked-focus-ring-color: #9e9e9e;
$mdc-switch-disabled-focus-ring-color: #000;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you probably don't need this since a disabled component can't have a focus ring

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's in the spec, but the only way to achieve focus is by using chrome dev tools to force the state. Take it out?

$mdc-switch-unchecked-disabled-knob-color: #bdbdbd;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should probably just be $mdc-switch-disabled-knob-color, and it needs an $mdc-switch-disabled-track-color as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unchecked track color and disabled unchecked track color are the same value with different opacities, so I just made it mdc-switch-unchecked-track-color

$mdc-switch-unchecked-knob-color-dark: #424242;
$mdc-switch-unchecked-track-color-dark: #484848;

$mdc-switch-transition-duration: 175ms;
$mdc-switch-transition-curve: cubic-bezier(.4, 0, .2, 1);
Loading