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

Conversation

adeelibr
Copy link
Contributor

I have added a new prop called thumb to the Slider component, which you can use like this.

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 DeleteIcon from '@material-ui/icons/Delete';

const styles = {
  root: {
    width: 300,
  },
};

class SimpleSlider 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="label">Slider Image</Typography>
        <Slider
          value={value}
          aria-labelledby="label"
          onChange={this.handleChange}
          thumb={<img src="/static/images/cards/live-from-space.jpg" alt="" />}
        />
        <Typography id="label">Slider Simple</Typography>
        <Slider value={value} aria-labelledby="label" onChange={this.handleChange} />
        <Typography id="label">Slider Icon</Typography>
        <Slider
          value={value}
          aria-labelledby="label"
          onChange={this.handleChange}
          thumb={<DeleteIcon style={{ color: 'green' }} />}
        />
      </div>
    );
  }
}

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

export default withStyles(styles)(SimpleSlider);

This looks something like this
44003423-f0bb4f9c-9e6b-11e8-9007-2406daf457f6

This is a clean version of #12485 which got messed up, I kept on trying for some hours. but I guess I messed that pr more. Lesson learnt at my end.

@adeelibr
Copy link
Contributor Author

@oliviertassinari I deleted the old PR, and created a new PR from scratch updated from upstream master, and I didn't run yarn docs:api but the build is still failing for CircleCI.

@eps1lon can you point me in the write direction?

@mbrookes
Copy link
Member

I didn't run yarn docs:api

You need to in order to update the slider API docs

@adeelibr
Copy link
Contributor Author

@mbrookes Thank you so so much for running 'yarn docs:api` apparently I am on a Windows machines and when I compile the markup build through it, it just gets messed up CRLF vs LF issue.

We had 2 long threads on this #12485 #12584

@mbrookes
Copy link
Member

No worries. BTW, no need to close one PR and open another - you can force push your fixed branch.

Please could you make the customization examples a little more practical? The first looks like a glitch, and the third doesn't make much sense. Why would a slider have a trash-can icon for it's thumb? 😄

No need for the second example - we already know what it looks like when not customized.

It also needs a heading "Custom thumb"? Not sure it warrants a description.

@adeelibr
Copy link
Contributor Author

I have added a better demo

slider v2

Along with the description, as instructed.

Regarding the part

BTW, no need to close one PR and open another - you can force push your fixed branch.

I tried doing it, tried for like 2 hours but I think I ended up messing the PR more, so created a new one. I'll spend some time tomorrow learning how to git reset, git revert and git reflog 💯

@eps1lon
Copy link
Member

eps1lon commented Aug 21, 2018

Interactions are currently not very smooth. The Icon bounces up and down on activation:
mui-slider-icon-bumpy
This also happens with the mui icon.

Chrome Version 68.0.3440.106 (Official Build) (64-bit)

@oliviertassinari oliviertassinari added the package: lab Specific to @mui/lab label Aug 21, 2018
@adeelibr
Copy link
Contributor Author

@eps1lon I have made changes to the thumb icon, is this the right approach? your suggestions.

@adeelibr
Copy link
Contributor Author

Regarding the CircleCI pipeline for ci/circleci: test_browser am I doing something wrong, I saw it's log but it didn't make much sense to me.

`${withIcon ? '' : classes.thumb}`,
commonClasses,
`${withIcon ? classes.thumbTransparent : ''}`,
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can leverage classNames implementation here by passing an object with the classnames as keys and a truthy value.

Why not apply classes.thumb always and classes.thumbIcon if an icon is passed and style accordingly. This would make more sense in my opinion. thumbTransparent just feels wrong. I'll always try to avoid naming my css classes after their look.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can leverage classNames implementation here by passing an object with the classnames as keys and a truthy value.

Done, I have made the changes. :)

Why not apply classes.thumb always and classes.thumbIcon if an icon is passed and style accordingly. This would make more sense in my opinion. thumbTransparent just feels wrong. I'll always try to avoid naming my css classes after their look.

Regarding why I am not using thumbIcon is because I am using it for the Thumbnail itself, so instead of thumbTransparent I used thumbIconWrapper

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I can not use the classes.thumb if there is a custom icon, because the recent issue you posted on active hovering state was because of this class.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would it look if you overwrite the conflicting styles? It might be confusing to users that this class is not always applied to the thumb.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood, i'll do something about it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have enabled classes.thumb even when it is used with a thumb icon.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I just wanted to say thank you for taking the time out of your busy schedule, to review my code. This really means a lot. Thank you.

@adeelibr
Copy link
Contributor Author

And now circleCI is failing again, because the docs needs to be updated using yarn docs:api.

Copy link
Member

@mbrookes mbrookes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks lime the position of the custom icon needs to be offset to the left by half its width. Also, there are no visible focus or click states.

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 Thumbnail Icon Logic Here */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you change references to Thumbnail to something less confusing? a thumbnail and a (Material) thumb are two different things.

Copy link
Contributor Author

@adeelibr adeelibr Aug 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I change the prop thumb to sliderIcon would that be better?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thumbIcon?

Copy link
Contributor Author

@adeelibr adeelibr Aug 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thumbIcon sounds nice. Should this be the external prop too. e.g, right now it is only thumb.

<Slider {...otherProps} thumb={} />

Should I change this to thumbIcon too, along with inside the code as well.

And in Slider.js file
Instead of Thumbnail use thumbIcon as ThumbIcon because I need to clone the element and pass some props to it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, I have made changes. Can you please review. Again thank you for taking your busy time out to review my code. This really means a lot.

thumb={
<img
alt="slider thumb icon"
src="/static/images/cards/live-from-space.jpg"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As previously mentioned, using a large image shrunk to the size of a thumb makes it look like a glitch (circular or not).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I remove this image example?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this image, then yes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, understood.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or if it's okay can I use a 50x50 image of a circle, to use here as thumb icon? Instead of removing it. I'll need to add a new file in the static/ folder for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up using another image, if that is not okay. Please do let me know. I'll just remove the image demo from the slider.


return (
<div className={classes.root}>
<Typography id="label">Slider Image</Typography>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Image thumb"

/>
}
/>
<Typography id="label">Slider Icon</Typography>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Icon thumb"

@adeelibr
Copy link
Contributor Author

I have updated the demo with a better image of a circle.
slider image

@mbrookes
Copy link
Member

Should I change this to thumbIcon too, along with inside the code as well.

Makes sense to me. @eps1lon?

Copy link
Member

@eps1lon eps1lon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one minor nitpick but this looks good now. Great work 👍 I can see focus states but no activated states. Maybe add some in the example?

I guess we do the ts definitions in another PR or is @adeelibr ok with adding a line in Slider.d.ts?

Edit:
To get CI working merge master to get #12611

packages/material-ui-lab/src/Slider/Slider.js Outdated Show resolved Hide resolved
@adeelibr
Copy link
Contributor Author

adeelibr commented Sep 8, 2018

@eps1lon I think the tests passed successfully this time.

@@ -122,6 +122,25 @@ export const styles = theme => {
height: 17,
},
},
/* Class applied to the thumb element if custom thumb icon provided` . */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seems to be a s stray back-tick and space before the full-stop.

@adeelibr
Copy link
Contributor Author

adeelibr commented Sep 9, 2018

@mbrookes since I updated the comment on style, the yarn docs:api command needs to be run again. I have an issue on windows. Can you please run the commands for this PR.

@adeelibr
Copy link
Contributor Author

@oliviertassinari this PR has no conflicts any more, can this be merged now?

import { withStyles } from '@material-ui/core/styles';
import Typography from '@material-ui/core/Typography';
import Slider from '@material-ui/lab/Slider';
import StarsIcon from '@material-ui/icons/Gamepad';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GamepadIcon?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops! Updating it :P

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated it to Lens icon, which is basically a filled circle.


return (
<div className={classes.root}>
<Typography id="label">Image thumb</Typography>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ids need to be unique.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added different id's

'&$activated': {
width: 22,
height: 22,
transition: 'none',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thumb should grow and shrink.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the thumb does grow and shrink.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4azq7c9svl

As you can see in the demo, the icon does grow and shrink.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had some bloated styling, which was redundant. I removed it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems to have fixed it in Firefox

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 withIcon = !!thumbIcon;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to convert thumbIcon to an explicit boolean?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you suggest me a better approach, i'd love to do it that way. (always learning 😅 )

Copy link
Member

@mbrookes mbrookes Sep 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If thumbIcon isn't provided, it will be undefined, so evaluate to false. You could still use !! for clarity if you prefer, but use it directly in the ternary and classNames, rather than assigning to a variable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed redundant !!

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/Lens';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would LensOutlined work so as so as to be distinct from the default?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mjlwiyfb1h

This is how that would look like?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wondered about that. Not ideal...

What about "adjust"? Otherwise "favorite" with a step slider as a sort of rating slider.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it have a default background color? e.g white?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With a default white backgroundColor it would look like this.

dvvcfnpye3

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That'll work (for the purpose of a demo)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this I have to change the thumbIconWrapper in my slider.js file

thumbIconWrapper: {
      backgroundColor: '#fff',
    },

Or should i override it with classes prop in the demo for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added it in the demo, is that okay? 😅 @mbrookes

@mbrookes
Copy link
Member

@eps1lon Could you have a last look please?

Copy link
Member

@eps1lon eps1lon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@mbrookes mbrookes merged commit 14de82e into mui:master Sep 13, 2018
@mbrookes
Copy link
Member

@adeelibr We got there in the end 😄 Thanks for working on it.

@adeelibr adeelibr deleted the slider-thumbnail-icon branch September 14, 2018 05:34
marcelpanse pushed a commit to marcelpanse/material-ui that referenced this pull request Oct 2, 2018
@oliviertassinari oliviertassinari added component: slider This is the name of the generic UI component, not the React module! and removed package: lab Specific to @mui/lab labels Jan 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: slider This is the name of the generic UI component, not the React module!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants