Skip to content

Commit db51ba1

Browse files
authored
docs(slider): additional documentation (#2068)
1 parent 3fd3117 commit db51ba1

File tree

1 file changed

+105
-41
lines changed

1 file changed

+105
-41
lines changed

src/lib/slider/README.md

Lines changed: 105 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,150 @@
11
# md-slider
22

3-
`MdSlider` is a component that allows users to select from a range of values by moving the slider
4-
thumb.
5-
You can read more about the slider in the
6-
[Material Design spec](https://material.google.com/components/sliders.html).
3+
`md-slider` is a component that allows users to select from a range of values by moving the slider
4+
thumb. It is similar in behavior to the native `<input type="range">` element. You can read more
5+
about the slider in the [Material Design spec](https://material.google.com/components/sliders.html).
76

8-
## Not Yet Implemented
9-
10-
* Thumb Label
11-
* Color
12-
* Invert
13-
* NgModel
14-
* Keyboard Movement
15-
* Focus Ring
16-
* Smaller/grey thumb at minimum value
7+
Simple example:
8+
```html
9+
<md-slider></md-slider>
10+
```
1711

1812
## Usage
1913

20-
### Basic Usage
14+
### Changing the min, max, step, and initial value
2115

22-
`md-slider` can be used on its own as a slider with a min of `0`, a max of `100`, and a step of `1`.
16+
By default the minimum value of the slider is `0`, the maximum value is `100`, and the thumb moves
17+
in increments of `1`. These values can be changed by setting the `min`, `max`, and `step` attributes
18+
respectively. The initial value is set to the minimum value unless otherwise specified.
2319

2420
```html
25-
<md-slider></md-slider>
21+
<md-slider min="1" max="5" step="0.5" value="1.5"></md-slider>
2622
```
2723

28-
### Slider with Minimum and Maximum Values
24+
### Setting the orientation
25+
26+
By default sliders are horizontal with the minimum value on the left and the maximum value on the
27+
right. The `vertical` attribute can be added to a slider to make it vertical with the minimum value
28+
on bottom and the maximum value on top.
29+
30+
```html
31+
<md-slider vertical></md-slider>
32+
```
2933

30-
The min and max on a `md-slider` can be set to give a different range of values.
31-
These can be set individually and do not need to both be set.
34+
An `invert` attribute is also available which can be specified to flip the axis that the thumb moves
35+
along. An inverted horizontal slider will have the minimum value on the right and the maximum value
36+
on the left, while an inverted vertical slider will have the minimum value on top and the maximum
37+
value on bottom.
3238

3339
```html
34-
<md-slider min="1" max="5"></md-slider>
40+
<md-slider invert></md-slider>
3541
```
3642

37-
### Disabled Slider
43+
### Adding a thumb label
44+
45+
By default the exact selected value of a slider is not visible to the user. However, this value can
46+
be added to the thumb by adding the `thumb-label` attribute.
3847

39-
`md-slider` can be disabled so that the value cannot be changed and the thumb cannot be moved.
48+
The [material design spec](https://material.google.com/components/sliders.html) recommends using the
49+
`thumb-label` attribute (along with `tick-interval="1"`) only for sliders that are used to display a
50+
discrete value (such as a 1-5 rating).
4051

4152
```html
42-
<md-slider disabled></md-slider>
53+
<md-slider thumb-label tick-interval="1"></md-slider>
4354
```
4455

45-
### Slider with Value
56+
### Adding ticks
57+
58+
By default a sliders do not show tick marks along the thumb track. They can be enabled using the
59+
`tick-interval` attribute. The value of `tick-interval` should be a number representing the number
60+
of steps between between ticks. For example a `tick-interval` of `3` with a `step` of `4` will draw
61+
tick marks at every `3` steps, which is the same as every `12` values.
62+
63+
```html
64+
<md-slider step="4" tick-interval="3"></md-slider>
65+
```
4666

47-
`md-slider` can have a value defined so that it starts at a specific value on the slider.
67+
The `tick-interval` can also be set to `auto` which will automatically choose the number of steps
68+
such that there is at least `30px` of space between ticks.
4869

4970
```html
50-
<md-slider value="24"></md-slider>
71+
<md-slider tick-interval="auto"></md-slider>
5172
```
73+
74+
The slider will always show a tick at the beginning and end of the track. If the remaining space
75+
doesn't add up perfectly the last interval will be shortened or lengthened so that the tick can be
76+
shown at the end of the track.
77+
78+
The [material design spec](https://material.google.com/components/sliders.html) recommends using the
79+
`tick-interval` attribute (set to `1` along with the `thumb-label` attribute) only for sliders that
80+
are used to display a discrete value (such as a 1-5 rating).
5281

53-
### Slider with Step
82+
### Disabling the slider
5483

55-
`md-slider` can have the step defined which declares where the thumb can snap to.
84+
The `disabled` attribute can be used to disable the slider, preventing the user from changing the
85+
value.
5686

5787
```html
58-
<md-slider step="5"></md-slider>
88+
<md-slider disabled value="5"></md-slider>
5989
```
6090

61-
### Slider with Tick Interval
91+
### Value binding
6292

63-
`md-slider` can have a tick interval set to a number or to `auto`.
64-
`auto` will automatically draw tick marks on steps that are at least 30px apart and will always draw
65-
tick marks at the beginning and end of the slider.
66-
Setting `tick-interval` to a number will draw a tick mark at every `tick-interval` steps. An example
67-
of this is a `tick-interval` of `3` with a `step` of `4` will draw tick marks at every `3` steps,
68-
which is the same as every `12` values.
93+
`md-slider` supports both 1-way binding and 2-way binding via `ngModel`. It also emits a `change`
94+
event when the value changes due to user interaction.
6995

7096
```html
71-
<md-slider tick-interval="auto"></md-slider>
72-
<md-slider tick-interval="3" step="4"></md-slider>
97+
<md-slider [value]="myValue" (change)="onChange()"></md-slider>
98+
```
99+
100+
```html
101+
<md-slider [(ngModel)]="myValue"></md-slider>
73102
```
74103

75-
## `<md-slider>`
104+
## Accessibility
105+
106+
The slider has the following keyboard bindings:
107+
108+
| Key | Action |
109+
| --- | --- |
110+
| Right arrow | Increment the slider value by one step. (Decrement in right-to-left-languages). |
111+
| Up arrow | Increment the slider value by one step. |
112+
| Left arrow | Decrement the slider value by one step. (Increment in right-to-left-languages). |
113+
| Down arrow | Decrement the slider value by one step. |
114+
| Page up | Increment the slider value by 10 steps. |
115+
| Page down | Decrement the slider value by 10 steps. |
116+
| End | Set the value to the maximum possible. |
117+
| Home | Set the value to the minimum possible. |
118+
119+
## Internationalization
120+
121+
In right-to-left languages the horizontal slider has the minimum value on the right and the maximum
122+
value on the left by default. The meaning of the left and right arrow keys is also swapped in
123+
right-to-left languages.
76124

77-
### Bound Properties
125+
## API Summary
126+
127+
### Inputs
78128

79129
| Name | Type | Description |
80130
| --- | --- | --- |
81131
| `min` | number | Optional, the minimum number for the slider. Default = `0`. |
82132
| `max` | number | Optional, the maximum number for the slider. Default = `100`. |
133+
| `step` | number | Optional, declares where the thumb will snap to. Default = `1`. |
83134
| `value` | number | Optional, the value to start the slider at. |
84135
| `tick-interval` | `"auto" | number` | Optional, how many steps between tick marks. |
85-
| `step` | number | Optional, declares where the thumb will snap to. Default = `1`. |
136+
| `invert` | boolean | Optional, whether to invert the axis the thumb moves along. |
137+
| `vertical` | boolean | Optional, whether the slider should be oriented vertically. |
86138
| `disabled` | boolean | Optional, whether or not the slider is disabled. Default = `false`. |
139+
140+
### Outputs
141+
142+
| Name | Description |
143+
| --- | --- |
144+
| `change` | Emitted when the value changes due to user interaction. |
145+
146+
147+
## Future work & open questions
148+
149+
* Update focused, disabled, and `value=min` states to match spec.
150+
* Should there be an option to not force the end tick to be shown?

0 commit comments

Comments
 (0)