Skip to content

Commit

Permalink
Custom range input (#25600)
Browse files Browse the repository at this point in the history
* added the styling

* added the documentation

* update for one rule per line

* fix hound error: trailing whitespace

* trimmed off vendor prefixes

* Add note about track and thumb

* Psuedo-elements must be split across multiple rulesets to have an affect

* Fix firefox inner focus

* Seems that FF is the only one affected by this

* Add support for gradients

* Add labels, clarify min/max changes

* add step example

* add custom range vars
  • Loading branch information
mdo authored Feb 19, 2018
1 parent 3dd0bde commit 470b447
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 0 deletions.
23 changes: 23 additions & 0 deletions docs/4.0/components/forms.md
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,29 @@ As is the `size` attribute:
</select>
{% endexample %}

### Range

Create custom `<input type="range">` controls with `.custom-range`. The track (the background) and thumb (the value) are both styled to appear the same across browsers. As only IE and Firefox support "filling" their track from the left or right of the thumb as a means to visually indicate progress, we do not currently support it.

{% example html %}
<label for="customRange1">Example range</label>
<input type="range" class="custom-range" id="customRange1">
{% endexample %}

Range inputs have implicit values for `min` and `max``0` and `100`, respectively. You may specify new values for those using the `min` and `max` attributes.

{% example html %}
<label for="customRange2">Example range</label>
<input type="range" class="custom-range" min="0" max="5" id="customRange2">
{% endexample %}

By default, range inputs "snap" to integer values. To change this, you can specify a `step` value. In the example below, we double the number of steps by using `step="0.5"`.

{% example html %}
<label for="customRange3">Example range</label>
<input type="range" class="custom-range" min="0" max="5" step="0.5" id="customRange3">
{% endexample %}

### File browser

The file input is the most gnarly of the bunch and requires additional JavaScript if you'd like to hook them up with functional *Choose file...* and selected file name text.
Expand Down
123 changes: 123 additions & 0 deletions scss/_custom-forms.scss
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,126 @@
@include border-radius(0 $custom-file-border-radius $custom-file-border-radius 0);
}
}

// Range
//
// Style range inputs the same across browsers. Vendor-specific rules for psuedo
// elements cannot be mixed. As such, there are no shared styles for focus or
// active states on prefixed selectors.

.custom-range {
width: 100%;
padding-left: 0; // Firefox specific
background-color: transparent;
appearance: none;

&:focus {
outline: none;
}

&::-moz-focus-outer {
border: 0;
}

&::-webkit-slider-thumb {
width: $custom-range-thumb-width;
height: $custom-range-thumb-height;
margin-top: -($custom-range-thumb-width * .25); // Webkit specific?
@include gradient-bg($custom-range-thumb-bg);
border: $custom-range-thumb-border;
@include border-radius($custom-range-thumb-border-radius);
@include box-shadow($custom-range-thumb-box-shadow);
appearance: none;

&:focus {
outline: none;
box-shadow: $custom-range-thumb-focus-box-shadow; // No mixin for focus accessibility
}

&:active {
@include gradient-bg($custom-range-thumb-active-bg);
}
}

&::-webkit-slider-runnable-track {
width: $custom-range-track-width;
height: $custom-range-track-height;
color: transparent; // Why?
cursor: $custom-range-track-cursor;
background-color: $custom-range-track-bg;
border-color: transparent;
@include border-radius($custom-range-track-border-radius);
@include box-shadow($custom-range-track-box-shadow);
}

&::-moz-range-thumb {
width: $custom-range-thumb-width;
height: $custom-range-thumb-height;
@include gradient-bg($custom-range-thumb-bg);
border: $custom-range-thumb-border;
@include border-radius($custom-range-thumb-border-radius);
@include box-shadow($custom-range-thumb-box-shadow);
appearance: none;

&:focus {
outline: none;
box-shadow: $custom-range-thumb-focus-box-shadow; // No mixin for focus accessibility
}

&:active {
@include gradient-bg($custom-range-thumb-active-bg);
}
}

&::-moz-range-track {
width: $custom-range-track-width;
height: $custom-range-track-height;
color: transparent;
cursor: $custom-range-track-cursor;
background-color: $custom-range-track-bg;
border-color: transparent; // Firefox specific?
@include border-radius($custom-range-track-border-radius);
@include box-shadow($custom-range-track-box-shadow);
}

&::-ms-thumb {
width: $custom-range-thumb-width;
height: $custom-range-thumb-height;
@include gradient-bg($custom-range-thumb-bg);
border: $custom-range-thumb-border;
@include border-radius($custom-range-thumb-border-radius);
@include box-shadow($custom-range-thumb-box-shadow);
appearance: none;

&:focus {
outline: none;
box-shadow: $custom-range-thumb-focus-box-shadow; // No mixin for focus accessibility
}

&:active {
@include gradient-bg($custom-range-thumb-active-bg);
}
}

&::-ms-track {
width: $custom-range-track-width;
height: $custom-range-track-height;
color: transparent;
cursor: $custom-range-track-cursor;
background-color: transparent;
border-color: transparent;
border-width: ($custom-range-thumb-height * .5);
@include box-shadow($custom-range-track-box-shadow);
}

&::-ms-fill-lower {
background-color: $custom-range-track-bg;
@include border-radius($custom-range-track-border-radius);
}

&::-ms-fill-upper {
margin-right: 15px; // arbitrary?
background-color: $custom-range-track-bg;
@include border-radius($custom-range-track-border-radius);
}
}
16 changes: 16 additions & 0 deletions scss/_variables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,22 @@ $custom-select-height-sm: $input-height-sm !default;
$custom-select-font-size-lg: 125% !default;
$custom-select-height-lg: $input-height-lg !default;

$custom-range-track-width: 100% !default;
$custom-range-track-height: .5rem !default;
$custom-range-track-cursor: pointer !default;
$custom-range-track-bg: $gray-300 !default;
$custom-range-track-border-radius: 1rem !default;
$custom-range-track-box-shadow: inset 0 .25rem .25rem rgba($black, .1) !default;

$custom-range-thumb-width: 1rem !default;
$custom-range-thumb-height: $custom-range-thumb-width !default;
$custom-range-thumb-bg: $component-active-bg !default;
$custom-range-thumb-border: 0 !default;
$custom-range-thumb-border-radius: 1rem !default;
$custom-range-thumb-box-shadow: 0 .1rem .25rem rgba($black, .1) !default;
$custom-range-thumb-focus-box-shadow: 0 0 0 1px $body-bg, $input-btn-focus-box-shadow !default;
$custom-range-thumb-active-bg: lighten($component-active-bg, 35%) !default;

$custom-file-height: $input-height !default;
$custom-file-focus-border-color: $input-focus-border-color !default;
$custom-file-focus-box-shadow: $input-btn-focus-box-shadow !default;
Expand Down

0 comments on commit 470b447

Please sign in to comment.