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

fix(slider): use secondary custom property color for slider container #5132

Merged

Conversation

svencowart
Copy link
Contributor

Overcomes css limitation of not being able to use a hex value inside of an rgba value by using the after pseudo class and applying opacity on it instead. In this way, the container and track are left alone and only the track container has the opacity applied to it.

@@ -435,8 +435,16 @@
$feat-color: mdc-feature-create-target($query, color);

.mdc-slider__track-container {
@include mdc-feature-targets($feat-color) {
@include mdc-theme-prop(background-color, rgba(mdc-theme-prop-value($color), $opacity));
&::after {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This PR does not seem to change the color of slider to secondary color?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Add the following to the test/screenshot/spec/mdc-slider/clases/baseline_continuous.html:

Inside of the head tag:

<style>
  .test-secondary-color {
    --mdc-theme-secondary: orange;
  }
</style>

Inside of the body, add a second slider with the test-secondary-color class:

<div class="mdc-slider test-slider test-secondary-color" tabindex="0" role="slider"
     aria-valuemin="0" aria-valuemax="100" aria-valuenow="0"
     aria-label="Select Value">
  <div class="mdc-slider__track-container">
    <div class="mdc-slider__track"></div>
  </div>
  <div class="mdc-slider__thumb-container">
    <svg class="mdc-slider__thumb" width="21" height="21">
      <circle cx="10.5" cy="10.5" r="7.875"></circle>
    </svg>
    <div class="mdc-slider__focus-ring"></div>
  </div>
</div>

Then run npm start and navigate to that page. You will see the added slider use the secondary color.

@@ -435,8 +435,16 @@
$feat-color: mdc-feature-create-target($query, color);

.mdc-slider__track-container {
@include mdc-feature-targets($feat-color) {
@include mdc-theme-prop(background-color, rgba(mdc-theme-prop-value($color), $opacity));
&::after {
Copy link
Collaborator

Choose a reason for hiding this comment

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

How is this using pseudo selector different from directly applying on the element?

Copy link
Contributor Author

@svencowart svencowart Sep 26, 2019

Choose a reason for hiding this comment

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

This comment on the issue over in material-components-web-components has more context: material-components/material-web#80 (comment)

Without the pseudo selector, the color of the slider track defaults to the primary color because it uses the mdc-theme-prop-value function which cannot handle secondary css variable definitions. Additionally, because rgba is used here previously, css variables will not function properly because passing in a css variable that is a hex value to an rgba function will invalid. The only way to make this work with css variables is by using a pseudo-class that fills the parent element and applying that css variable to the background-color and setting an opacity value. Applying an opacity value directly to the track container will also not work because that will then also apply to its children which means the track itself will be opaque which is not the desired state either.

@svencowart svencowart changed the title Apply secondary color to slider track fix(slider): Apply secondary color to slider track Sep 28, 2019
@TitanNano
Copy link

@svencowart @abhiomkar is there any change this will get merged in the near future? It would fix a quite serious bug material-components/material-web#80

@abhiomkar abhiomkar requested a review from asyncLiz January 9, 2020 16:56
Copy link
Contributor

@asyncLiz asyncLiz left a comment

Choose a reason for hiding this comment

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

This looks good to me, I understand why we need the ::after pseudo element.

Something to note for later on, we'll probably want to change some of these to use a border instead of background so that they show up in high contrast mode.

@asyncLiz asyncLiz self-assigned this Jan 9, 2020
@asyncLiz asyncLiz changed the title fix(slider): Apply secondary color to slider track fix(slider): use secondary custom property color for slider container Jan 9, 2020
@asyncLiz
Copy link
Contributor

asyncLiz commented Jan 9, 2020

@svencowart can you rebase master onto this branch?

# If you don't already have upstream set to this repo
git remote set-url upstream git@github.com:material-components/material-components-web.git

git pull --rebase upstream master
git push --force

@svencowart svencowart force-pushed the slider-track-secondary branch from 2902240 to f67f6d6 Compare January 16, 2020 17:44
@svencowart
Copy link
Contributor Author

@asyncLiz I have rebased master onto this branch, but it seems the unit tests are not working because of some issue with starting Chrome. My permissions do not allow me to look into it further.

@asyncLiz
Copy link
Contributor

Yea we're having some build issues, I'll look into it while I run internal tests

Copy link
Collaborator

@abhiomkar abhiomkar left a comment

Choose a reason for hiding this comment

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

LGTM.

Thanks @svencowart!

@@ -435,8 +435,16 @@
$feat-color: feature-targeting-functions.create-target($query, color);

.mdc-slider__track-container {
@include feature-targeting-mixins.targets($feat-color) {
@include theme-mixins.prop(background-color, rgba(theme-variables.prop-value($color), $opacity));
@include feature-targeting-mixins.targets($feat-color) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: fix indentation.

@abhiomkar
Copy link
Collaborator

@asyncLiz Unit tests does not work on forked repo. Let's merge this once the CL is submitted. Thanks!

@asyncLiz
Copy link
Contributor

See cl/290088778

@asyncLiz asyncLiz merged commit aa8e43e into material-components:master Jan 17, 2020
crisbeto added a commit to crisbeto/material-components-web that referenced this pull request Jan 23, 2020
These changes fix the following issues which were introduced by material-components#5132:
1. The background of track is now provided by `.mdc-slider__track-container::after`, however `::after` wasn't positioned correctly which made it invisible.
2. The `::after` structural styles were set inside the `color` feature target. This means that they'll end up being included multiple times and in the wrong place.
crisbeto added a commit to crisbeto/material-components-web that referenced this pull request Jan 23, 2020
These changes fix the following issues which were introduced by material-components#5132:
1. The background of track is now provided by `.mdc-slider__track-container::after`, however `::after` wasn't positioned correctly which made it invisible.
2. The `::after` structural styles were set inside the `color` feature target. This means that they'll end up being included multiple times and in the wrong place.
This was referenced Feb 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants