Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
feat(switch): Add color theme mixins and update default color to seco…
Browse files Browse the repository at this point in the history
…ndary (#1411)

Resolves #1144
  • Loading branch information
acdvorak authored Oct 17, 2017
1 parent da9d48f commit e4b4fa7
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 76 deletions.
71 changes: 49 additions & 22 deletions demos/switch.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@
background-color: #333;
}

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

section.example.mdc-theme--dark > label {
section.example.mdc-theme--dark label {
color: white;
}
</style>
Expand Down Expand Up @@ -82,39 +82,66 @@

<section class="example">
<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 class="demo-switch-wrapper">
<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>
</div>
<div class="demo-switch-wrapper">
<div class="mdc-switch demo-switch--custom">
<input type="checkbox" id="basic-switch-custom" class="mdc-switch__native-control" checked/>
<div class="mdc-switch__background">
<div class="mdc-switch__knob"></div>
</div>
</div>
<label for="basic-switch-custom" class="mdc-switch-label">custom color</label>
</div>
<label for="basic-switch" class="mdc-switch-label">off/on</label>
</section>
<section class="example">
<h2>Switch on Light Theme - Disabled</h2>
<div class="mdc-switch">
<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 class="demo-switch-wrapper">
<div class="mdc-switch">
<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>
</div>
<label for="basic-switch--disabled" class="mdc-switch-label">off/on</label>
</section>

<section class="example mdc-theme--dark">
<h2>Switch on Dark Theme</h2>
<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 class="demo-switch-wrapper">
<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>
</div>
<div class="demo-switch-wrapper">
<div class="mdc-switch demo-switch--custom">
<input type="checkbox"
id="basic-switch--dark-custom"
class="mdc-switch__native-control"
checked />
<div class="mdc-switch__background">
<div class="mdc-switch__knob"></div>
</div>
</div>
<label for="basic-switch--dark-custom" class="mdc-switch-label">custom color</label>
</div>
<label for="basic-switch--dark" class="mdc-switch-label">off/on</label>
</section>

<section class="example mdc-theme--dark">
Expand Down
14 changes: 14 additions & 0 deletions demos/switch.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,18 @@

@import "./common";
@import "../packages/mdc-switch/mdc-switch";
@import "../packages/mdc-theme/color_palette";
@import "../packages/mdc-typography/mdc-typography";

.demo-switch-wrapper {
display: inline-block;
margin-right: 16px;
}

.demo-switch--custom {
$color: $material-color-red-500;

@include mdc-switch-track-color($color);
@include mdc-switch-knob-color($color);
@include mdc-switch-focus-indicator-color($color);
}
23 changes: 17 additions & 6 deletions packages/mdc-switch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,27 @@ npm install --save @material/switch
<label for="another-basic-switch" class="mdc-switch-label">off/on</label>
```

## Classes
### Classes

### Block
#### Block

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

### Modifier
#### Modifier

The provided modifiers are:

| Class | Description |
| ----------------------| -------------------------------------------- |
| `mdc-switch--disabled` | Applies disabled style to disabled switches. |
| Class | Description |
| -----------------------| -------------------------------------------- |
| `mdc-switch--disabled` | Applies disabled style to disabled switches. |

### 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.

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
21 changes: 21 additions & 0 deletions packages/mdc-switch/_functions.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// 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.
//

@import "@material/animation/variables";

@function mdc-switch-transition($property) {
@return $property 90ms $mdc-animation-standard-curve-timing-function;
}
74 changes: 74 additions & 0 deletions packages/mdc-switch/_mixins.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//
// 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.
//

@import "@material/theme/mixins";
@import "./variables";

//
// Public
//

@mixin mdc-switch-track-color($color) {
.mdc-switch__native-control:enabled:checked ~ .mdc-switch__background::before {
@include mdc-theme-prop(background-color, $color);
}
}

@mixin mdc-switch-knob-color($color) {
// stylelint-disable-next-line selector-max-specificity
.mdc-switch__native-control:enabled:checked ~ .mdc-switch__background .mdc-switch__knob {
@include mdc-theme-prop(background-color, $color);
}
}

@mixin mdc-switch-focus-indicator-color($color) {
// stylelint-disable-next-line selector-max-specificity
.mdc-switch__native-control:enabled:checked ~ .mdc-switch__background .mdc-switch__knob::before {
@include mdc-theme-prop(background-color, $color);
}
}

//
// Private
//

@mixin mdc-switch-unchecked-track-color_($color) {
.mdc-switch__native-control:enabled:not(:checked) ~ .mdc-switch__background::before {
@include mdc-theme-prop(background-color, $color);
}
}

@mixin mdc-switch-unchecked-knob-color_($color) {
// stylelint-disable-next-line selector-max-specificity
.mdc-switch__native-control:enabled:not(:checked) ~ .mdc-switch__background .mdc-switch__knob {
@include mdc-theme-prop(background-color, $color);
}
}

@mixin mdc-switch-unchecked-focus-indicator-color_($color) {
// stylelint-disable-next-line selector-max-specificity
.mdc-switch__native-control:enabled:not(:checked) ~ .mdc-switch__background .mdc-switch__knob::before {
@include mdc-theme-prop(background-color, $color);
}
}

@mixin mdc-switch-tap-target_ {
position: absolute;
top: -14px;
left: -14px;
width: $mdc-switch-focus-ring-diameter;
height: $mdc-switch-focus-ring-diameter;
}
14 changes: 9 additions & 5 deletions packages/mdc-switch/_variables.scss
Original file line number Diff line number Diff line change
@@ -1,18 +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.
//
//

$mdc-switch-track-width: 34px;
$mdc-switch-track-height: 14px;
Expand All @@ -28,3 +28,7 @@ $mdc-switch-unchecked-track-color-dark: #fff;
$mdc-switch-unchecked-focus-ring-color-dark: #f1f1f1;
$mdc-switch-disabled-knob-color: #bdbdbd;
$mdc-switch-disabled-knob-color-dark: #424242;

$mdc-switch-baseline-theme-color: secondary;

$mdc-switch-knob-vertical-offset_: -3px;
Loading

0 comments on commit e4b4fa7

Please sign in to comment.