From e9d1e7d3c4ff937ed4f8913ab7280aee28a9e3eb Mon Sep 17 00:00:00 2001 From: Andrew Jakubowicz Date: Tue, 12 Sep 2023 17:21:18 -0700 Subject: [PATCH] fix(slider): make tickmarks visible when slider is disabled PiperOrigin-RevId: 564880169 --- slider/internal/_slider.scss | 42 ++++++++++++++++------- slider/internal/forced-colors-styles.scss | 12 +++++-- slider/internal/slider.ts | 6 ++-- tokens/_md-comp-slider.scss | 4 ++- 4 files changed, 46 insertions(+), 18 deletions(-) diff --git a/slider/internal/_slider.scss b/slider/internal/_slider.scss index ed1115d43e..a1ac793921 100644 --- a/slider/internal/_slider.scss +++ b/slider/internal/_slider.scss @@ -148,7 +148,8 @@ $_md-sys-shape: tokens.md-sys-shape-values(); touch-action: none; } - .track { + .track, + .tickmarks { position: absolute; inset: 0; display: flex; @@ -157,8 +158,10 @@ $_md-sys-shape: tokens.md-sys-shape-values(); // inactive-track .track::before, + .tickmarks::before, // active-track - .track::after { + .track::after, + .tickmarks::after { position: absolute; content: ''; // pad the track inward by half the ripple size offset by the tick container size. @@ -176,15 +179,19 @@ $_md-sys-shape: tokens.md-sys-shape-values(); } // inactive-track - .track::before { + .track::before, + .tickmarks::before { block-size: var(--_inactive-track-height); border-radius: var(--_inactive-track-shape); + } + + .track::before { background-color: var(--_inactive-track-color); } - .track.tickmarks::before { + .tickmarks::before { background-image: _get-tick-image( - 'var(--_with-tick-marks-inactive-container-color)' + var(--_with-tick-marks-inactive-container-color) ); } @@ -199,13 +206,23 @@ $_md-sys-shape: tokens.md-sys-shape-values(); } // active-track - .track::after { + .track::after, + .tickmarks::after { block-size: var(--_active-track-height); border-radius: var(--_active-track-shape); clip-path: inset(0 $_active-track-end-clip 0 $_active-track-start-clip); + } + + .track::after { background-color: var(--_active-track-color); } + .tickmarks::after { + background-image: _get-tick-image( + var(--_with-tick-marks-active-container-color) + ); + } + // rtl for active track clipping @each $_rtl-selectors in _get-rtl-selectors('.track', '::after') { #{$_rtl-selectors} { @@ -213,20 +230,19 @@ $_md-sys-shape: tokens.md-sys-shape-values(); } } - .track.tickmarks::after { - background-image: _get-tick-image( - 'var(--_with-tick-marks-active-container-color)' - ); + @each $_rtl-selectors in _get-rtl-selectors('.tickmarks', '::after') { + #{$_rtl-selectors} { + clip-path: inset(0 $_active-track-start-clip 0 $_active-track-end-clip); + } } :host([disabled]) .track::after { background-color: var(--_disabled-active-track-color); } - :host([disabled]) .track.tickmarks::before, - :host([disabled]) .track.tickmarks::after { + :host([disabled]) .tickmarks::before { background-image: _get-tick-image( - 'var(--_with-tick-marks-disabled-container-color)' + var(--_with-tick-marks-disabled-container-color) ); } diff --git a/slider/internal/forced-colors-styles.scss b/slider/internal/forced-colors-styles.scss index bb34e67f73..2d738f9675 100644 --- a/slider/internal/forced-colors-styles.scss +++ b/slider/internal/forced-colors-styles.scss @@ -49,7 +49,7 @@ } // inactive-track - .track.tickmarks::before { + .tickmarks::before { // A url must be used when forced-colors is active as it's the only value // that is respected. The radial-gradient is not displayed. This is a // stop-gap solution so ticks are displayed at all when forced-colors is @@ -70,11 +70,19 @@ } // active-track - .track.tickmarks::after { + .tickmarks::after { // See inactive-track documentation for background-image. // stylelint-disable function-url-quotes -- SVG data URI // TODO(b/298051946): Tick marks cannot be resized in HCM background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='Canvas'%3E%3Ccircle cx='2' cy='2' r='1'/%3E%3C/svg%3E"); // stylelint-enable function-url-quotes } + + :host([disabled]) .tickmarks::before { + // TODO(b/298051946): Tick marks cannot be resized in HCM + // stylelint-disable function-url-quotes -- SVG data URI + // SVG is optimized for data URI (https://codepen.io/tigt/post/optimizing-svgs-in-data-uris) + background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='Canvas'%3E%3Ccircle cx='2' cy='2' r='1'/%3E%3C/svg%3E"); + // stylelint-enable function-url-quotes + } } diff --git a/slider/internal/slider.ts b/slider/internal/slider.ts index 4646081446..4a42118eb4 100644 --- a/slider/internal/slider.ts +++ b/slider/internal/slider.ts @@ -416,8 +416,10 @@ export class Slider extends LitElement { } private renderTrack() { - const trackClasses = {'tickmarks': this.ticks}; - return html`
`; + return html` +
+ ${this.ticks ? html`
` : nothing} + `; } private renderLabel(value: string) { diff --git a/tokens/_md-comp-slider.scss b/tokens/_md-comp-slider.scss index 9f7dabb80b..279a7fae3d 100644 --- a/tokens/_md-comp-slider.scss +++ b/tokens/_md-comp-slider.scss @@ -68,8 +68,10 @@ $unsupported-tokens: ( 'track-elevation', // for efficiency, tick marks are rendered as radial-gradients and // have more limited customization - 'with-tick-marks-active-container-opacity', 'with-tick-marks-container-shape', + // Due to how opacity is multiplied together in the browser, using + // these tokens results in low contrast tick marks. + 'with-tick-marks-active-container-opacity', 'with-tick-marks-disabled-container-opacity', 'with-tick-marks-inactive-container-opacity', // focus tokens no longer used.