Skip to content

Commit

Permalink
feat(select): Move colors for default select to mixins (#1934)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Move colors for default select element to mixins. Refer to the documentation for guidance. 
refs: #1150

Move colors for the select into a new mixins file.
  • Loading branch information
williamernest committed Jan 17, 2018
1 parent 568fc32 commit d6c68ce
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 59 deletions.
29 changes: 8 additions & 21 deletions demos/select.html
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ <h2 class="mdc-typography--title">Fully-Featured Component</h2>
<input type="checkbox" id="rtl">
<label for="rtl">RTL</label>
</div>
<div>
<input type="checkbox" id="alternate-colors">
<label for="alternate-colors">Alternate Colors</label>
</div>
<div>
<input type="checkbox" id="disabled">
<label for="disabled">Disabled</label>
Expand All @@ -151,27 +155,6 @@ <h2 class="mdc-typography--title">CSS Only</h2>
<div class="mdc-select__bottom-line"></div>
</div>
</section>

<!--<section class="example">-->
<!--<h2>Select Multiple - CSS Only</h2>-->
<!--<select multiple size="8" class="mdc-multi-select mdc-list">-->

<!--<optgroup class="mdc-list-group" label="Fats, Oils, &amp; Sweets">-->
<!--<option class="mdc-list-item">Olive Oil</option>-->
<!--<option class="mdc-list-item">Brown Sugar</option>-->
<!--<option class="mdc-list-item">Ice Cream</option>-->
<!--</optgroup>-->
<!--<option class="mdc-list-divider" role="presentation" disabled />-->

<!--<optgroup class="mdc-list-group" label="Dairy">-->
<!--<option class="mdc-list-item">Milk</option>-->
<!--<option class="mdc-list-item">Cheese</option>-->
<!--<option class="mdc-list-item">More Cheese</option>-->
<!--</optgroup>-->

<!--</select>-->
<!--</section>-->

</main>

<script src="/assets/material-components-web.js" async></script>
Expand All @@ -193,6 +176,7 @@ <h2 class="mdc-typography--title">CSS Only</h2>
var boxDemoWrapper = document.getElementById('box-demo-wrapper');
var darkThemeCb = document.getElementById('dark-theme');
var rtlCb = document.getElementById('rtl');
var alternateColorsCb = document.getElementById('alternate-colors');
var disabledCb = document.getElementById('disabled');

darkThemeCb.addEventListener('change', function() {
Expand All @@ -205,6 +189,9 @@ <h2 class="mdc-typography--title">CSS Only</h2>
boxDemoWrapper.removeAttribute('dir');
}
});
alternateColorsCb.addEventListener('change', function() {
root.classList[alternateColorsCb.checked ? 'add' : 'remove']('demo-select-custom-colors');
});
disabledCb.addEventListener('change', function() {
select.disabled = disabledCb.checked;
});
Expand Down
13 changes: 13 additions & 0 deletions demos/select.scss
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,16 @@
@import "../packages/mdc-list/mdc-list";
@import "../packages/mdc-menu/mdc-menu";
@import "../packages/mdc-select/mdc-select";

// stylelint-disable selector-class-pattern
.demo-select-custom-colors {
@include mdc-select-container-fill-color(rgba(blue, .1));
@include mdc-select-ink-color(blue);
@include mdc-select-label-color(rgba(blue, .6));
@include mdc-select-bottom-line-color(rgba(blue, .5));

// Focused colors
@include mdc-select-focused-bottom-line-color(green);
@include mdc-select-focused-label-color(green);
}
// stylelint-enable selector-class-pattern
16 changes: 15 additions & 1 deletion packages/mdc-select/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ E.g.:
</div>
```

#### Classes
#### CSS Classes

| Class | Description |
| ------------------------ | ----------------------------------------------- |
Expand All @@ -237,6 +237,20 @@ E.g.:

It is advised that dividers also set `role="presentation"` to disable selection and not cloud accessibility.

### Sass Mixins

To customize the colors of any part of the select, use the following mixins. We recommend you use
these mixins within CSS selectors like `.foo-select` to apply styling.

Mixin | Description
--- | ---
`mdc-select-ink-color($color)` | Customizes the color of the selected item displayed in the select. On the css version, this also customizes the color of the label.
`mdc-select-container-fill-color($color)` | Customizes the background color of the select.
`mdc-select-label-color($color)` | Customizes the label color of the select in the unfocused state. This mixin is only used for the JS version of the select.
`mdc-select-focused-label-color($color, $opacity: 0.87)` | Customizes the label color of the select when focused. Changing opacity for the label when floating is optional.
`mdc-select-bottom-line-color($color)` | Customizes the color of the default bottom line of the select.
`mdc-select-focused-bottom-line-color($color)` | Customizes the color of the bottom line of the select when focused.

### MDC Select Component API

The MDC Select component API is modeled after a subset of the `HTMLSelectElement` functionality, and
Expand Down
119 changes: 119 additions & 0 deletions packages/mdc-select/_mixins.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
//
// Copyright 2018 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.
//

// Public

@mixin mdc-select-ink-color($color) {
&:not(.mdc-select--disabled) {
@include mdc-select-ink-color_($color);
}
}

@mixin mdc-select-container-fill-color($color) {
&:not(.mdc-select--disabled) {
@include mdc-select-container-fill-color_($color);
}
}

@mixin mdc-select-label-color($color) {
&:not(.mdc-select--disabled) {
@include mdc-select-label-color_($color);
}
}

@mixin mdc-select-focused-label-color($color, $opacity: .87) {
&:not(.mdc-select--disabled) {
@include mdc-select-focused-label-color_($color, $opacity);
}
}

@mixin mdc-select-bottom-line-color($color) {
&:not(.mdc-select--disabled) {
@include mdc-select-bottom-line-color_($color);
}
}

@mixin mdc-select-focused-bottom-line-color($color) {
&:not(.mdc-select--disabled) {
@include mdc-select-focused-bottom-line-color_($color);
}
}

// Private

@mixin mdc-select-focused-bottom-line_ {
// JS-enhanced and CSS-only Selects require different selectors for focused bottom-line due to different DOM structure
.mdc-select__surface:focus .mdc-select__bottom-line,
.mdc-select__surface:focus ~ .mdc-select__bottom-line {
@content;
}
}

@mixin mdc-select-ink-color_($color) {
.mdc-select__surface,
.mdc-select__selected-text {
@include mdc-theme-prop(color, $color);
}
}

@mixin mdc-select-container-fill-color_($color) {
.mdc-select__surface {
@include mdc-theme-prop(background-color, $color);
}
}

@mixin mdc-select-label-color_($color) {
.mdc-select__label {
@include mdc-theme-prop(color, $color);
}
}

@mixin mdc-select-focused-label-color_($color, $opacity: .87) {
.mdc-select__surface:focus .mdc-select__label {
@include mdc-theme-prop(color, $color);
}

// Separate parameter is used for opacity, because opacity is only applied when the
// label is floating, but the label is the same color when the select is focused
// but an option has not been selected.
.mdc-select__surface:focus .mdc-select__label--float-above {
opacity: $opacity;
}
}

@mixin mdc-select-bottom-line-color_($color) {
.mdc-select__bottom-line {
@include mdc-theme-prop(background-color, $color);
}
}

@mixin mdc-select-focused-bottom-line-color_($color) {
@include mdc-select-focused-bottom-line_ {
@include mdc-theme-prop(background-color, $color);
}

&.mdc-select--open .mdc-select__bottom-line {
@include mdc-theme-prop(background-color, $color);
}

.mdc-select__bottom-line::after {
@include mdc-theme-prop(background-color, $color);
}
}

@mixin mdc-select-dd-arrow-svg-bg_($fill-hex-number: 000000, $opacity: .54) {
background-image: url(data:image/svg+xml,%3Csvg%20width%3D%2210px%22%20height%3D%225px%22%20viewBox%3D%227%2010%2010%205%22%20version%3D%221.1%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%3E%0A%20%20%20%20%3Cpolygon%20id%3D%22Shape%22%20stroke%3D%22none%22%20fill%3D%22%23#{$fill-hex-number}%22%20fill-rule%3D%22evenodd%22%20opacity%3D%22#{$opacity}%22%20points%3D%227%2010%2012%2015%2017%2010%22%3E%3C%2Fpolygon%3E%0A%3C%2Fsvg%3E);
}
54 changes: 17 additions & 37 deletions packages/mdc-select/mdc-select.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// limitations under the License.
//

@import "./mixins";
@import "@material/animation/functions";
@import "@material/animation/variables";
@import "@material/typography/mixins";
Expand All @@ -22,19 +23,22 @@
@import "@material/ripple/mixins";
@import "@material/rtl/mixins";

@mixin mdc-select-dd-arrow-svg-bg_($fill-hex-number: 000000, $opacity: .54) {
background-image: url(data:image/svg+xml,%3Csvg%20width%3D%2210px%22%20height%3D%225px%22%20viewBox%3D%227%2010%2010%205%22%20version%3D%221.1%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%3E%0A%20%20%20%20%3Cpolygon%20id%3D%22Shape%22%20stroke%3D%22none%22%20fill%3D%22%23#{$fill-hex-number}%22%20fill-rule%3D%22evenodd%22%20opacity%3D%22#{$opacity}%22%20points%3D%227%2010%2012%2015%2017%2010%22%3E%3C%2Fpolygon%3E%0A%3C%2Fsvg%3E);
}

$mdc-select-arrow-padding: 26px;
$mdc-select-label-padding: 16px;
$mdc-select-menu-transition: transform 180ms $mdc-animation-standard-curve-timing-function;

// postcss-bem-linter: define select
.mdc-select {
@include mdc-typography(subheading2);
@include mdc-theme-prop(color, text-primary-on-light);
@include mdc-select-dd-arrow-svg-bg_;
@include mdc-select-container-fill-color(rgba(white, .1));
@include mdc-select-ink-color(text-primary-on-light);
@include mdc-select-label-color(rgba(black, .6));
@include mdc-select-bottom-line-color(rgba(black, .5));

// Focused state colors
@include mdc-select-focused-bottom-line-color(primary);
@include mdc-select-focused-label-color(primary);
@include mdc-typography(subheading2);

display: inline-flex;
position: relative;
Expand All @@ -54,12 +58,6 @@ $mdc-select-menu-transition: transform 180ms $mdc-animation-standard-curve-timin
background-position: left 10px center;
}

@include mdc-theme-dark(".mdc-select") {
@include mdc-select-dd-arrow-svg-bg_("fff", .54);

background-color: rgba(white, .1);
}

&__menu {
position: fixed;
top: 0;
Expand All @@ -71,7 +69,6 @@ $mdc-select-menu-transition: transform 180ms $mdc-animation-standard-curve-timin

&__surface {
@include mdc-typography(subheading2);
@include mdc-theme-prop(color, text-primary-on-light);
@include mdc-rtl-reflexive-property(padding, $mdc-select-label-padding, $mdc-select-arrow-padding, ".mdc-select");
@include mdc-ripple-surface;
@include mdc-ripple-radius;
Expand All @@ -93,7 +90,6 @@ $mdc-select-menu-transition: transform 180ms $mdc-animation-standard-curve-timin
border: none;
border-radius: 4px 4px 0 0;
outline: none;
background-color: rgba(black, .04);
// Resets for <select> element
appearance: none;
overflow: hidden;
Expand All @@ -106,14 +102,9 @@ $mdc-select-menu-transition: transform 180ms $mdc-animation-standard-curve-timin
bottom: 12px;
transform-origin: left top;
transition: $mdc-select-menu-transition;
color: rgba(black, .6);
pointer-events: none;
will-change: transform;

@include mdc-theme-dark(".mdc-select") {
@include mdc-theme-prop(color, text-secondary-on-dark);
}

@include mdc-rtl(".mdc-select") {
transform-origin: right top;
}
Expand All @@ -132,10 +123,6 @@ $mdc-select-menu-transition: transform 180ms $mdc-animation-standard-curve-timin
mdc-animation-exit-temporary(transform, 125ms);
white-space: nowrap;
overflow: hidden;

@include mdc-theme-dark(".mdc-select") {
@include mdc-theme-prop(color, text-secondary-on-dark);
}
}

&__bottom-line {
Expand All @@ -144,18 +131,16 @@ $mdc-select-menu-transition: transform 180ms $mdc-animation-standard-curve-timin
left: 0;
width: 100%;
height: 1px;
transform: scaleY(1);
transform-origin: bottom;
transition: $mdc-select-menu-transition;
background-color: rgba(black, .5);

&::after {
@include mdc-theme-prop(background-color, primary);

position: absolute;
bottom: -1px;
bottom: 0;
left: 0;
width: 100%;
height: 2px;
height: 1px;
transform: scaleX(0);
transition: $mdc-select-menu-transition;
opacity: 0;
Expand All @@ -166,19 +151,13 @@ $mdc-select-menu-transition: transform 180ms $mdc-animation-standard-curve-timin

&__bottom-line--active {
&::after {
transform: scale(1, 2);
opacity: 1;
}
}

// JS-enhanced and CSS-only Selects require different selectors for focused bottom-line due to different DOM structure
&__surface:focus:not(.mdc-ripple-upgraded) .mdc-select__bottom-line,
&__surface:focus:not(.mdc-ripple-upgraded) ~ .mdc-select__bottom-line {
@include mdc-theme-prop(background-color, primary);

transform: scaleY(2);

@include mdc-select-focused-bottom-line_ {
&::after {
transform: scale(1, 2);
opacity: 1;
}
}
Expand All @@ -203,14 +182,15 @@ $mdc-select-menu-transition: transform 180ms $mdc-animation-standard-curve-timin

.mdc-select__bottom-line {
&::after {
transform: scaleY(2);
opacity: 1;
}
}
}

.mdc-select--disabled,
.mdc-select[disabled] {
@include mdc-theme-prop(color, text-disabled-on-light);
@include mdc-select-label-color_(text-disabled-on-light);
@include mdc-select-dd-arrow-svg-bg_(000000, .38);

border-bottom-width: 1px;
Expand Down

0 comments on commit d6c68ce

Please sign in to comment.