Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Slider] Feature Custom Icon #12600

Merged
merged 31 commits into from
Sep 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
979c0cd
feature slider icon
adeelibr Aug 20, 2018
cac07cd
[docs] Update Slider API docs
mbrookes Aug 20, 2018
49b716f
updated demo
adeelibr Aug 20, 2018
201177e
added custom description
adeelibr Aug 20, 2018
1efcbf3
corrected heading
adeelibr Aug 20, 2018
5933838
ran prettier to format code
adeelibr Aug 20, 2018
b30e4b5
improvements on thumb icon while draggin it
adeelibr Aug 21, 2018
a3c4f77
improvement on active state for thumb
adeelibr Aug 21, 2018
4fa5ffa
ran prettier
adeelibr Aug 21, 2018
6b91465
improvements
adeelibr Aug 21, 2018
fad0478
[docs] Update Slider API docs
mbrookes Aug 21, 2018
abe9c9c
updated documentation & improvements in thumb
adeelibr Aug 21, 2018
3889145
improved custom icon demo & slider variable names better use
adeelibr Aug 21, 2018
3a858e8
active, focus state working properly now
adeelibr Aug 22, 2018
06ba37f
Merge branch 'master' into slider-thumbnail-icon
adeelibr Aug 23, 2018
c4639d0
Merge branch 'master' into slider-thumbnail-icon
adeelibr Sep 2, 2018
46750f3
Merge branch 'master' into slider-thumbnail-icon
adeelibr Sep 4, 2018
cd6f752
Merge branch 'master' into slider-thumbnail-icon
adeelibr Sep 6, 2018
617bf64
Merge branch 'master' into slider-thumbnail-icon
adeelibr Sep 8, 2018
ba8c1e4
removed extra space
adeelibr Sep 9, 2018
fea9ed5
Merge branch 'master' into slider-thumbnail-icon
adeelibr Sep 9, 2018
9c6d598
Merge branch 'master' into slider-thumbnail-icon
adeelibr Sep 9, 2018
5f29eb3
Update API docs
mbrookes Sep 9, 2018
ab55c29
updated icon
adeelibr Sep 11, 2018
185aa2e
added different id's for different labels
adeelibr Sep 11, 2018
d19e900
removed bloated styling
adeelibr Sep 11, 2018
8e503c4
removed withIcon variable
adeelibr Sep 11, 2018
385b56c
Merge branch 'master' into slider-thumbnail-icon
adeelibr Sep 11, 2018
7a92eaa
removed redundant git status
adeelibr Sep 11, 2018
507e0e2
demo revamped
adeelibr Sep 12, 2018
7f7611e
merged master and resolved conflicts
adeelibr Sep 12, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions docs/src/pages/lab/slider/CustomIconSlider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import Typography from '@material-ui/core/Typography';
import Slider from '@material-ui/lab/Slider';
import LensIcon from '@material-ui/icons/LensOutlined';

const styles = {
root: {
width: 300,
},
thumbIcon: {
borderRadius: '50%',
},
thumbIconWrapper: {
backgroundColor: '#fff',
},
};

class CustomIconSlider extends React.Component {
state = {
value: 50,
};

handleChange = (event, value) => {
this.setState({ value });
};

render() {
const { classes } = this.props;
const { value } = this.state;

return (
<div className={classes.root}>
<Typography id="slider-image">Image thumb</Typography>
<Slider
value={value}
aria-labelledby="slider-image"
onChange={this.handleChange}
thumb={
<img
alt="slider thumb icon"
src="/static/images/misc/circle.png"
className={classes.thumbIcon}
/>
}
/>
<Typography id="slider-icon">Icon thumb</Typography>
<Slider
value={value}
aria-labelledby="slider-icon"
onChange={this.handleChange}
classes={{
thumbIconWrapper: classes.thumbIconWrapper,
}}
thumb={<LensIcon style={{ color: '#2196f3' }} />}
/>
</div>
);
}
}

CustomIconSlider.propTypes = {
classes: PropTypes.object.isRequired,
};

export default withStyles(styles)(CustomIconSlider);
4 changes: 4 additions & 0 deletions docs/src/pages/lab/slider/slider.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ Sliders reflect the current state of the settings they control.
## Reverse slider

{{"demo": "pages/lab/slider/ReverseSlider.js"}}

## Custom thumb

{{"demo": "pages/lab/slider/CustomIconSlider.js"}}
39 changes: 35 additions & 4 deletions packages/material-ui-lab/src/Slider/Slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,15 @@ export const styles = theme => {
height: 17,
},
},
/* Class applied to the root element to trigger JSS nested styles if `reverse={true}` . */
/* Class applied to the thumb element if custom thumb icon provided. */
thumbIconWrapper: {
backgroundColor: 'transparent',
},
thumbIcon: {
height: 'inherit',
width: 'inherit',
},
/* Class applied to the root element to trigger JSS nested styles if `reverse={true}`. */
reverse: {},
/* Class applied to the track and thumb elements to trigger JSS nested styles if `disabled`. */
disabled: {},
Expand Down Expand Up @@ -380,6 +388,7 @@ class Slider extends React.Component {
className: classNameProp,
classes,
component: Component,
thumb: thumbIcon,
disabled,
max,
min,
Expand Down Expand Up @@ -425,14 +434,29 @@ class Slider extends React.Component {
[classes.vertical]: vertical,
});

const thumbClasses = classNames(classes.thumb, commonClasses);

const trackProperty = vertical ? 'height' : 'width';
const thumbProperty = vertical ? 'top' : 'left';
const inlineTrackBeforeStyles = { [trackProperty]: this.calculateTrackBeforeStyles(percent) };
const inlineTrackAfterStyles = { [trackProperty]: this.calculateTrackAfterStyles(percent) };
const inlineThumbStyles = { [thumbProperty]: `${percent}%` };

/** Start Thumb Icon Logic Here */
const ThumbIcon = thumbIcon
? React.cloneElement(thumbIcon, {
...thumbIcon.props,
className: classNames(thumbIcon.props.className, classes.thumbIcon),
})
: null;
/** End Thumb Icon Logic Here */

const thumbClasses = classNames(
classes.thumb,
{
[classes.thumbIconWrapper]: thumbIcon,
},
commonClasses,
);

return (
<Component
role="slider"
Expand Down Expand Up @@ -461,7 +485,9 @@ class Slider extends React.Component {
onTouchStartCapture={this.handleTouchStart}
onTouchMove={this.handleMouseMove}
onFocusVisible={this.handleFocus}
/>
>
{ThumbIcon}
</ButtonBase>
<div className={trackAfterClasses} style={inlineTrackAfterStyles} />
</div>
</Component>
Expand Down Expand Up @@ -522,6 +548,11 @@ Slider.propTypes = {
* @ignore
*/
theme: PropTypes.object.isRequired,
/**
* The component used for the slider icon.
* This is optional, if provided should be a react element.
*/
thumb: PropTypes.element,
/**
* The value of the slider.
*/
Expand Down
5 changes: 4 additions & 1 deletion pages/lab/api/slider.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import Slider from '@material-ui/lab/Slider';
| <span class="prop-name">onDragStart</span> | <span class="prop-type">func |   | Callback function that is fired when the slider has begun to move. |
| <span class="prop-name">reverse</span> | <span class="prop-type">bool |   | If `true`, the slider will be reversed. |
| <span class="prop-name">step</span> | <span class="prop-type">number |   | The granularity the slider can step through values. |
| <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. |
| <span class="prop-name required">value *</span> | <span class="prop-type">number |   | The value of the slider. |
| <span class="prop-name">vertical</span> | <span class="prop-type">bool |   | If `true`, the slider will be vertical. |

Expand All @@ -48,7 +49,9 @@ This property accepts the following keys:
| <span class="prop-name">trackBefore</span> | Styles applied to the track element before the thumb.
| <span class="prop-name">trackAfter</span> | Styles applied to the track element after the thumb.
| <span class="prop-name">thumb</span> | Styles applied to the thumb element.
| <span class="prop-name">reverse</span> | Class applied to the root element to trigger JSS nested styles if `reverse={true}` .
| <span class="prop-name">thumbIconWrapper</span> | Class applied to the thumb element if custom thumb icon provided.
| <span class="prop-name">thumbIcon</span> |
| <span class="prop-name">reverse</span> | Class applied to the root element to trigger JSS nested styles if `reverse={true}`.
| <span class="prop-name">disabled</span> | Class applied to the track and thumb elements to trigger JSS nested styles if `disabled`.
| <span class="prop-name">jumped</span> | Class applied to the track and thumb elements to trigger JSS nested styles if `jumped`.
| <span class="prop-name">focused</span> | Class applied to the track and thumb elements to trigger JSS nested styles if `focused`.
Expand Down
7 changes: 7 additions & 0 deletions pages/lab/slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ module.exports = require('fs')
raw: preval`
module.exports = require('fs')
.readFileSync(require.resolve('docs/src/pages/lab/slider/ReverseSlider'), 'utf8')
`,
},
'pages/lab/slider/CustomIconSlider.js': {
js: require('docs/src/pages/lab/slider/CustomIconSlider').default,
raw: preval`
module.exports = require('fs')
.readFileSync(require.resolve('docs/src/pages/lab/slider/CustomIconSlider'), 'utf8')
`,
},
}}
Expand Down
Binary file added static/images/misc/circle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.