Skip to content

Commit 6ad29a2

Browse files
committed
[Slider] Replace reversed with rtl support on horizontal sliders
This follows WAI-ARIA slider authoring practices as well as the material design specs. The vertical slider is kept regardless.
1 parent ea46282 commit 6ad29a2

8 files changed

Lines changed: 17 additions & 84 deletions

File tree

docs/src/pages/lab/slider/ReverseSlider.js

Lines changed: 0 additions & 38 deletions
This file was deleted.

docs/src/pages/lab/slider/VerticalSlider.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class VerticalSlider extends React.Component {
2626
return (
2727
<div className={classes.root}>
2828
<Slider value={value} onChange={this.handleChange} vertical />
29-
<Slider value={value} onChange={this.handleChange} vertical reverse />
3029
</div>
3130
);
3231
}

docs/src/pages/lab/slider/slider.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ Sliders reflect the current state of the settings they control.
3737

3838
## Reverse slider
3939

40-
{{"demo": "pages/lab/slider/ReverseSlider.js"}}
40+
Per [material-design specification](https://material.io/design/components/sliders.html#usage)
41+
the position of the minimum value should depend on the language direction: On the left for left-to-right
42+
and on the right for right-to-left languages. Check the [Right-to-left guide](/guides/right-to-left/)
43+
to learn how to switch to right-to-left languages.
4144

4245
## Custom thumb
4346

packages/material-ui-lab/src/Slider/Slider.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { TransitionHandlerProps } from '@material-ui/core/transitions/transition
77
export interface SliderProps
88
extends StandardProps<React.HTMLAttributes<HTMLDivElement>, SliderClassKey, 'onChange'> {
99
disabled?: boolean;
10-
reverse?: boolean;
1110
vertical?: boolean;
1211
max?: number;
1312
min?: number;
@@ -29,7 +28,6 @@ export type SliderClassKey =
2928
| 'activated'
3029
| 'disabled'
3130
| 'vertical'
32-
| 'reverse'
3331
| 'jumped';
3432

3533
declare const Slider: React.ComponentType<SliderProps>;

packages/material-ui-lab/src/Slider/Slider.js

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const styles = theme => {
1515
};
1616

1717
const commonTransitions = theme.transitions.create(
18-
['width', 'height', 'left', 'top', 'box-shadow'],
18+
['width', 'height', 'left', 'right', 'top', 'box-shadow'],
1919
commonTransitionsOptions,
2020
);
2121
// no transition on the position
@@ -130,8 +130,6 @@ export const styles = theme => {
130130
height: 'inherit',
131131
width: 'inherit',
132132
},
133-
/* Class applied to the root element to trigger JSS nested styles if `reverse={true}`. */
134-
reverse: {},
135133
/* Class applied to the track and thumb elements to trigger JSS nested styles if `disabled`. */
136134
disabled: {},
137135
/* Class applied to the track and thumb elements to trigger JSS nested styles if `jumped`. */
@@ -177,15 +175,15 @@ function getMousePosition(event) {
177175
};
178176
}
179177

180-
function calculatePercent(node, event, isVertical, isReverted) {
178+
function calculatePercent(node, event, isVertical, isRtl) {
181179
const { width, height } = node.getBoundingClientRect();
182180
const { top, left } = getOffset(node);
183181
const { x, y } = getMousePosition(event);
184182

185183
const value = isVertical ? y - top : x - left;
186184
const onePercent = (isVertical ? height : width) / 100;
187185

188-
return isReverted ? 100 - clamp(value / onePercent) : clamp(value / onePercent);
186+
return isRtl && !isVertical ? 100 - clamp(value / onePercent) : clamp(value / onePercent);
189187
}
190188

191189
function preventPageScrolling(event) {
@@ -275,8 +273,8 @@ class Slider extends React.Component {
275273
};
276274

277275
handleClick = event => {
278-
const { min, max, vertical, reverse } = this.props;
279-
const percent = calculatePercent(this.containerRef, event, vertical, reverse);
276+
const { min, max, vertical } = this.props;
277+
const percent = calculatePercent(this.containerRef, event, vertical, this.isReverted());
280278
const value = percentToValue(percent, min, max);
281279

282280
this.emitChange(event, value, () => {
@@ -320,8 +318,8 @@ class Slider extends React.Component {
320318
};
321319

322320
handleMouseMove = event => {
323-
const { min, max, vertical, reverse } = this.props;
324-
const percent = calculatePercent(this.containerRef, event, vertical, reverse);
321+
const { min, max, vertical } = this.props;
322+
const percent = calculatePercent(this.containerRef, event, vertical, this.isReverted());
325323
const value = percentToValue(percent, min, max);
326324

327325
this.emitChange(event, value);
@@ -383,6 +381,10 @@ class Slider extends React.Component {
383381
});
384382
}
385383

384+
isReverted() {
385+
return this.props.theme.direction === 'rtl';
386+
}
387+
386388
render() {
387389
const { currentState } = this.state;
388390
const {
@@ -396,7 +398,6 @@ class Slider extends React.Component {
396398
onChange,
397399
onDragEnd,
398400
onDragStart,
399-
reverse,
400401
step,
401402
theme,
402403
value,
@@ -417,7 +418,6 @@ class Slider extends React.Component {
417418
classes.root,
418419
{
419420
[classes.vertical]: vertical,
420-
[classes.reverse]: reverse,
421421
[classes.disabled]: disabled,
422422
},
423423
classNameProp,
@@ -436,7 +436,8 @@ class Slider extends React.Component {
436436
});
437437

438438
const trackProperty = vertical ? 'height' : 'width';
439-
const thumbProperty = vertical ? 'top' : 'left';
439+
const horizontalMinimumPosition = theme.direction === 'ltr' ? 'left' : 'right';
440+
const thumbProperty = vertical ? 'top' : horizontalMinimumPosition;
440441
const inlineTrackBeforeStyles = { [trackProperty]: this.calculateTrackBeforeStyles(percent) };
441442
const inlineTrackAfterStyles = { [trackProperty]: this.calculateTrackAfterStyles(percent) };
442443
const inlineThumbStyles = { [thumbProperty]: `${percent}%` };
@@ -537,10 +538,6 @@ Slider.propTypes = {
537538
* Callback function that is fired when the slider has begun to move.
538539
*/
539540
onDragStart: PropTypes.func,
540-
/**
541-
* If `true`, the slider will be reversed.
542-
*/
543-
reverse: PropTypes.bool,
544541
/**
545542
* The granularity the slider can step through values.
546543
*/

packages/material-ui-lab/src/Slider/Slider.test.js

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -93,23 +93,6 @@ describe('<Slider />', () => {
9393
});
9494
});
9595

96-
describe('prop: reverse', () => {
97-
it('should render with the default and reverse classes', () => {
98-
const wrapper = shallow(<Slider reverse value={0} />);
99-
assert.strictEqual(wrapper.hasClass(classes.root), true);
100-
assert.strictEqual(wrapper.hasClass(classes.reverse), true);
101-
});
102-
});
103-
104-
describe('props: vertical & reverse', () => {
105-
it('should render with the default, reverse and vertical classes', () => {
106-
const wrapper = shallow(<Slider reverse vertical value={0} />);
107-
assert.strictEqual(wrapper.hasClass(classes.root), true);
108-
assert.strictEqual(wrapper.hasClass(classes.reverse), true);
109-
assert.strictEqual(wrapper.hasClass(classes.vertical), true);
110-
});
111-
});
112-
11396
describe('prop: disabled', () => {
11497
const handleChange = spy();
11598
let wrapper;

pages/lab/api/slider.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import Slider from '@material-ui/lab/Slider';
2727
| <span class="prop-name">onChange</span> | <span class="prop-type">func |   | Callback function that is fired when the slider's value changed. |
2828
| <span class="prop-name">onDragEnd</span> | <span class="prop-type">func |   | Callback function that is fired when the slide has stopped moving. |
2929
| <span class="prop-name">onDragStart</span> | <span class="prop-type">func |   | Callback function that is fired when the slider has begun to move. |
30-
| <span class="prop-name">reverse</span> | <span class="prop-type">bool |   | If `true`, the slider will be reversed. |
3130
| <span class="prop-name">step</span> | <span class="prop-type">number |   | The granularity the slider can step through values. |
3231
| <span class="prop-name">thumb</span> | <span class="prop-type">element |   | The component used for the slider icon. This is optional, if provided should be a react element. |
3332
| <span class="prop-name required">value *</span> | <span class="prop-type">number |   | The value of the slider. |
@@ -51,7 +50,6 @@ This property accepts the following keys:
5150
| <span class="prop-name">thumb</span> | Styles applied to the thumb element.
5251
| <span class="prop-name">thumbIconWrapper</span> | Class applied to the thumb element if custom thumb icon provided.
5352
| <span class="prop-name">thumbIcon</span> |
54-
| <span class="prop-name">reverse</span> | Class applied to the root element to trigger JSS nested styles if `reverse={true}`.
5553
| <span class="prop-name">disabled</span> | Class applied to the track and thumb elements to trigger JSS nested styles if `disabled`.
5654
| <span class="prop-name">jumped</span> | Class applied to the track and thumb elements to trigger JSS nested styles if `jumped`.
5755
| <span class="prop-name">focused</span> | Class applied to the track and thumb elements to trigger JSS nested styles if `focused`.

pages/lab/slider.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,6 @@ module.exports = require('fs')
3434
raw: preval`
3535
module.exports = require('fs')
3636
.readFileSync(require.resolve('docs/src/pages/lab/slider/VerticalSlider'), 'utf8')
37-
`,
38-
},
39-
'pages/lab/slider/ReverseSlider.js': {
40-
js: require('docs/src/pages/lab/slider/ReverseSlider').default,
41-
raw: preval`
42-
module.exports = require('fs')
43-
.readFileSync(require.resolve('docs/src/pages/lab/slider/ReverseSlider'), 'utf8')
4437
`,
4538
},
4639
'pages/lab/slider/CustomIconSlider.js': {

0 commit comments

Comments
 (0)