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] Individual MarkLabel customization #17023

Closed
2 tasks done
mstrugo opened this issue Aug 16, 2019 · 23 comments · Fixed by #17057
Closed
2 tasks done

[Slider] Individual MarkLabel customization #17023

mstrugo opened this issue Aug 16, 2019 · 23 comments · Fixed by #17057
Labels
component: slider This is the name of the generic UI component, not the React module! new feature New feature or request waiting for 👍 Waiting for upvotes

Comments

@mstrugo
Copy link
Contributor

mstrugo commented Aug 16, 2019

  • This is not a v0.x issue.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior ✋

In a RangeSlider with MarkLabels in Min and Max, it doesn't seem possible to style only one MarkLabel. With MuiSlider-markLabel theme override we could affect all mark labels but couldn't apply certain style to only one.
MarkLabels position is absolute, with a transform (translateX) and inline margin left.
image
If MarkLabel is overrided, I could have my Min MarkLabel OK, but I couldn't do the same with the Max one.
image
Also, we couldn't use css selectors because all the markup is maded with Span

Expected Behavior 👌

I expect to have something like this:
image
So I think I need a different class for each mark or a something to style only my Max (right) MarkLabel

Steps to Reproduce ⏯️

Tweak left MarkLabel overriding MUI Theme

MuiSlider: {
  markLabel: {
      transform: 'translateX(-12%)',
  },
},

Your Environment 💻

Tech Version
Language Typescript
Material-UI v4.2.1
React 16.8.6
Styles JSS
Browser Google Chrome

Thank you! 😄
P.S.: I love MUI and this is my first issue submitted... sorry for my bad english!

@oliviertassinari
Copy link
Member

oliviertassinari commented Aug 16, 2019

What do you think of adding data-index attributes to each mark like we do with the thumbs?

Then, you can target the right mark with CSS.

@oliviertassinari oliviertassinari added component: slider This is the name of the generic UI component, not the React module! new feature New feature or request labels Aug 16, 2019
@mstrugo
Copy link
Contributor Author

mstrugo commented Aug 16, 2019

Thanks @oliviertassinari for your quickly answer.
I think that could work. And we have to override like this?

MuiSlider: {
  markLabel: {
    color: graysDefault,
    '&[data-index="1"]': {
      left: '50%',
    },
  },
},

thanks!

@oliviertassinari

This comment has been minimized.

@oliviertassinari oliviertassinari added the good first issue Great for first contributions. Enable to learn the contribution process. label Aug 16, 2019
@mstrugo

This comment has been minimized.

@oliviertassinari

This comment has been minimized.

@pranshuchittora
Copy link
Contributor

I think we must consider #17034 before implementing the fix.

@mstrugo

This comment has been minimized.

@mstrugo
Copy link
Contributor Author

mstrugo commented Aug 19, 2019

@oliviertassinari pull request submitted :)

@oliviertassinari
Copy link
Member

@mstrugo Thanks, we will review it :)

@eps1lon
Copy link
Member

eps1lon commented Aug 20, 2019

Why would :nth-of-type not work?

@mstrugo
Copy link
Contributor Author

mstrugo commented Aug 20, 2019

@eps1lon
Why would :nth-of-type not work?

Because that css selector only works in markup elements instead of class names, and all of Slider components are Span in same tree level

@oliviertassinari
Copy link
Member

I should have thought about it in the first place, the label accept any node. You can style it so it has the required margin shift to match your app layout requirement. In the future, we can consider to have an option to handle it automatically. I think that we should wait for more upvotes.

@oliviertassinari
Copy link
Member

oliviertassinari commented Aug 20, 2019

Regarding a future potential API, one option is to have:

interface Mark {
  value: number;
  label?: React.ReactNode;
  labelAlign?: 'left' | 'center' | 'right';
}

It would also help when the marks are too close and the labels overlap.

@oliviertassinari oliviertassinari added waiting for 👍 Waiting for upvotes and removed good first issue Great for first contributions. Enable to learn the contribution process. labels Aug 20, 2019
@eps1lon
Copy link
Member

eps1lon commented Aug 20, 2019

@mstrugo Right, nth-of-type doesn't count among the parent selector.

It would still help to share the actual code and what you're trying to do. This would help a lot more than adding an API to make it easier to style every label differently.

@oliviertassinari oliviertassinari removed the waiting for 👍 Waiting for upvotes label Nov 30, 2019
@Louis345
Copy link

Louis345 commented Apr 4, 2020

What is the status of this issue? I'm having the same issue as the OP. Has this solution been implemented?

MuiSlider: {
  markLabel: {
    color: graysDefault,
    '&[data-index="1"]': {
      left: '50%',
    },
  },
},

@Lijun21
Copy link

Lijun21 commented Sep 14, 2020

Has this solution been implemented? It doesn't seem work.

MuiSlider: {
  markLabel: {
    color: graysDefault,
    '&[data-index="1"]': {
      left: '50%',
    },
  },
},

@oliviertassinari
Copy link
Member

oliviertassinari commented Sep 14, 2020

@Lijun21 It works, yes:

const Slider = styled(MuiSlider)({
  '& .MuiSlider-markLabel[data-index="0"]': {
    transform: "translateX(0%)"
  },
  '& .MuiSlider-markLabel[data-index="1"]': {
    transform: "translateX(-100%)"
  }
});

Capture d’écran 2020-09-14 à 23 02 56

https://codesandbox.io/s/material-demo-forked-ekd4f?file=/demo.js:121-327

@oliviertassinari oliviertassinari changed the title [Range Slider] Individual MarkLabel customization [Slider] Individual MarkLabel customization Sep 14, 2020
@enza252
Copy link

enza252 commented Oct 3, 2020

@oliviertassinari how would you propose the data-index value could be changed dynamically?

I'm wondering if there is a way to pass in a desired index to style, such as this:

    '& .MuiSlider-markLabel[data-index=`${indextostyle}`]': {
      color: palette.red.main
    }

@oliviertassinari
Copy link
Member

@enza252 You can do that with https://next.material-ui.com/components/slider-styled and emotion's styled() style helper. It let you defined styles dynamically, from the props.

@enza252
Copy link

enza252 commented Oct 3, 2020

@oliviertassinari thank you, that's a great point in the right direction - we currently export our Slider using withStyles, so I'll investigate

@samanmovaghar
Copy link

samanmovaghar commented Sep 6, 2021

I have found a better solution, so if you would like to add styling to the first index and last one dynamically, you can try like this

.MuiSlider-mark, MuiSlider-markActive {
    &[style="left: 0%;"], &[style="left: 100%;"]{
      your css here 
    }
  }

@oliviertassinari oliviertassinari added the waiting for 👍 Waiting for upvotes label Jun 10, 2022
@kenklin
Copy link

kenklin commented Aug 4, 2022

@enza252

@oliviertassinari how would you propose the data-index value could be changed dynamically?

I'm wondering if there is a way to pass in a desired index to style, such as this:

    '& .MuiSlider-markLabel[data-index=`${indextostyle}`]': {
      color: palette.red.main
    }

In ES2015 you can use computed property names, like below. This may not be your idea of dynamic, but rather computed (once).

[`& .MuiSlider-markLabel[data-index=${indextostyle}]`]: {
  color: palette.red.main
}

FYI - I've used this approach to specify 3 different styles of marks, whose data indices are computed at initial render, when I generate the marks. Like this:

[`& .MuiSlider-mark[data-index="${pitLaneDataIndex}"]`]: {
    backgroundColor: "white",
    height: MILESTONE_HEIGHT
},
[`& .MuiSlider-mark[data-index="${boxToPitOutDataIndex}"]`]: {
    backgroundColor: "lime",
    height: MILESTONE_HEIGHT
},
'& .MuiSlider-mark': {
    height: TICK_HEIGHT, // Default was too short
},

image

@Eliav2
Copy link

Eliav2 commented Jan 15, 2024

i was able to customize the mark completely using slots:

<Slider
  marks            
  slots={{ mark: CustomMark }}
  valueLabelDisplay="auto"
  ...
/>


type MarkProps = {
  "data-index": number
  className: string
  ownerState: {
    marks: boolean
    sx: {
      "& .MuiSlider-markLabel": {
        fontSize: string
      }
    }
    slots: {}
    size: string
    value: number
    max: number
    valueLabelDisplay: string
    isRtl: boolean
    min: number
    disabled: boolean
    disableSwap: boolean
    orientation: string
    color: string
    step: number
    track: string
    marked: boolean
    dragging: boolean
    focusedThumbIndex: number
  }
  markActive: boolean
  style: {
    right: string
  }
}

const CustomMark = (props:MarkProps) => {
  return (
    <div
      className={props.className}
      style={{
        ...props.style, // holds { right: <percentage> }
        position: "absolute",
        width: 5,
        height: 5,
        borderRadius: "50%",
        background: "red",
      }}
    ></div>
  );
};

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! new feature New feature or request waiting for 👍 Waiting for upvotes
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants