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][base] Improve logic for displaying value label #35479

Closed
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
894baea
remove valueLabelDisplay from SliderUnstyled
ZeeshanTamboli Dec 14, 2022
8f6a076
fix tests
ZeeshanTamboli Dec 14, 2022
beb8925
remove SliderValueLabelUnstyled component
ZeeshanTamboli Dec 16, 2022
4474ccb
show value label on hover
ZeeshanTamboli Dec 20, 2022
b1aeb3e
add TODO for v6
ZeeshanTamboli Dec 20, 2022
a12991c
use useSlider hook in Material-UI slider
ZeeshanTamboli Dec 21, 2022
04ad9aa
fix code
ZeeshanTamboli Dec 22, 2022
0a45706
Merge branch 'master' into issue/35398-remove-valueLabelDisplay-and-a…
ZeeshanTamboli Dec 22, 2022
3b85d6a
rename Slider value label component and migrate to TypeScript
ZeeshanTamboli Dec 22, 2022
1a77944
yarn proptypes
ZeeshanTamboli Dec 22, 2022
f5cd6c2
yarn docs:api
ZeeshanTamboli Dec 22, 2022
0063d08
yarn docs:typescript:formatted
ZeeshanTamboli Dec 22, 2022
052b617
yarn docs:typescript:formatted
ZeeshanTamboli Dec 22, 2022
c334e6f
destructure props
ZeeshanTamboli Dec 22, 2022
e6f40c6
fix argos by passing ownerState to root and track slots
ZeeshanTamboli Dec 22, 2022
47a9a9e
remove classes
ZeeshanTamboli Dec 23, 2022
62f2aec
Merge branch 'master' into issue/35398-remove-valueLabelDisplay-and-a…
ZeeshanTamboli Dec 23, 2022
bf03a98
Change structure of value label in SliderUnstyled
ZeeshanTamboli Dec 23, 2022
86b62fc
fix dem
ZeeshanTamboli Dec 23, 2022
289bd69
Update docs/data/base/components/slider/slider.md
ZeeshanTamboli Dec 30, 2022
b582d01
Update docs/data/base/components/slider/slider.md
ZeeshanTamboli Dec 30, 2022
aca075a
Update docs/data/base/components/slider/slider.md
ZeeshanTamboli Dec 30, 2022
3543f3b
yarn prettier
ZeeshanTamboli Dec 30, 2022
01e215d
remove unnecessary valueLabelProps className
ZeeshanTamboli Jan 3, 2023
76e27b4
Merge branch 'master' into issue/35398-remove-valueLabelDisplay-and-a…
ZeeshanTamboli Jan 3, 2023
1a0e1ff
rename SliderValueLabel component and export StyledSliderValueLabel a…
ZeeshanTamboli Jan 3, 2023
4de0741
remove not needed SliderValueLabelProps from SliderUnstyled types
ZeeshanTamboli Jan 3, 2023
886803e
export SliderValueLabelProps
ZeeshanTamboli Jan 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions docs/data/base/components/slider/DiscreteSlider.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import { styled, alpha, Box } from '@mui/system';
import SliderUnstyled, { sliderUnstyledClasses } from '@mui/base/SliderUnstyled';

Expand Down Expand Up @@ -109,7 +110,7 @@ const StyledSlider = styled(SliderUnstyled)(
background-color: #fff;
}

& .${sliderUnstyledClasses.valueLabel} {
& .valueLabel {
font-family: IBM Plex Sans;
font-size: 14px;
display: block;
Expand All @@ -121,6 +122,14 @@ const StyledSlider = styled(SliderUnstyled)(
`,
);

function SliderValueLabel({ children }) {
return <span className="valueLabel">{children}</span>;
}

SliderValueLabel.propTypes = {
children: PropTypes.element.isRequired,
};

function valuetext(value) {
return `${value}°C`;
}
Expand All @@ -132,11 +141,11 @@ export default function DiscreteSlider() {
aria-label="Temperature"
defaultValue={30}
getAriaValueText={valuetext}
valueLabelDisplay="auto"
step={10}
marks
min={10}
max={110}
slots={{ valueLabel: SliderValueLabel }}
/>
</Box>
);
Expand Down
12 changes: 10 additions & 2 deletions docs/data/base/components/slider/DiscreteSlider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ const StyledSlider = styled(SliderUnstyled)(
background-color: #fff;
}

& .${sliderUnstyledClasses.valueLabel} {
& .valueLabel {
font-family: IBM Plex Sans;
font-size: 14px;
display: block;
Expand All @@ -121,6 +121,14 @@ const StyledSlider = styled(SliderUnstyled)(
`,
);

interface SliderValueLabelProps {
children: React.ReactElement;
}

function SliderValueLabel({ children }: SliderValueLabelProps) {
return <span className="valueLabel">{children}</span>;
}

function valuetext(value: number) {
return `${value}°C`;
}
Expand All @@ -132,11 +140,11 @@ export default function DiscreteSlider() {
aria-label="Temperature"
defaultValue={30}
getAriaValueText={valuetext}
valueLabelDisplay="auto"
step={10}
marks
min={10}
max={110}
slots={{ valueLabel: SliderValueLabel }}
/>
</Box>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
aria-label="Temperature"
defaultValue={30}
getAriaValueText={valuetext}
valueLabelDisplay="auto"
step={10}
marks
min={10}
max={110}
slots={{ valueLabel: SliderValueLabel }}
/>
10 changes: 0 additions & 10 deletions docs/data/base/components/slider/DiscreteSliderMarks.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,6 @@ const StyledSlider = styled(SliderUnstyled)(
background-color: #fff;
}

& .${sliderUnstyledClasses.valueLabel} {
font-family: IBM Plex Sans;
font-size: 14px;
display: block;
position: relative;
top: -1.6em;
text-align: center;
transform: translateX(-50%);
}

& .${sliderUnstyledClasses.markLabel} {
font-family: IBM Plex Sans;
font-size: 12px;
Expand Down
10 changes: 0 additions & 10 deletions docs/data/base/components/slider/DiscreteSliderMarks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,6 @@ const StyledSlider = styled(SliderUnstyled)(
background-color: #fff;
}

& .${sliderUnstyledClasses.valueLabel} {
font-family: IBM Plex Sans;
font-size: 14px;
display: block;
position: relative;
top: -1.6em;
text-align: center;
transform: translateX(-50%);
}

& .${sliderUnstyledClasses.markLabel} {
font-family: IBM Plex Sans;
font-size: 12px;
Expand Down
10 changes: 0 additions & 10 deletions docs/data/base/components/slider/DiscreteSliderValues.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,6 @@ const StyledSlider = styled(SliderUnstyled)(
background-color: #fff;
}

& .${sliderUnstyledClasses.valueLabel} {
font-family: IBM Plex Sans;
font-size: 14px;
display: block;
position: relative;
top: -1.6em;
text-align: center;
transform: translateX(-50%);
}

& .${sliderUnstyledClasses.markLabel} {
font-family: IBM Plex Sans;
font-size: 12px;
Expand Down
10 changes: 0 additions & 10 deletions docs/data/base/components/slider/DiscreteSliderValues.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,6 @@ const StyledSlider = styled(SliderUnstyled)(
background-color: #fff;
}

& .${sliderUnstyledClasses.valueLabel} {
font-family: IBM Plex Sans;
font-size: 14px;
display: block;
position: relative;
top: -1.6em;
text-align: center;
transform: translateX(-50%);
}

& .${sliderUnstyledClasses.markLabel} {
font-family: IBM Plex Sans;
font-size: 12px;
Expand Down
10 changes: 0 additions & 10 deletions docs/data/base/components/slider/RangeSlider.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,6 @@ const StyledSlider = styled(SliderUnstyled)(
& .${sliderUnstyledClasses.markActive} {
background-color: #fff;
}

& .${sliderUnstyledClasses.valueLabel} {
font-family: IBM Plex Sans;
font-size: 14px;
display: block;
position: relative;
top: -1.6em;
text-align: center;
transform: translateX(-50%);
}
`,
);

Expand Down
10 changes: 0 additions & 10 deletions docs/data/base/components/slider/RangeSlider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,6 @@ const StyledSlider = styled(SliderUnstyled)(
& .${sliderUnstyledClasses.markActive} {
background-color: #fff;
}

& .${sliderUnstyledClasses.valueLabel} {
font-family: IBM Plex Sans;
font-size: 14px;
display: block;
position: relative;
top: -1.6em;
text-align: center;
transform: translateX(-50%);
}
`,
);

Expand Down
143 changes: 143 additions & 0 deletions docs/data/base/components/slider/UnstyledSliderValueLabel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import { styled, alpha, Box } from '@mui/system';
import SliderUnstyled, { sliderUnstyledClasses } from '@mui/base/SliderUnstyled';

const blue = {
100: '#DAECFF',
200: '#99CCF3',
400: '#3399FF',
300: '#66B2FF',
500: '#007FFF',
600: '#0072E5',
900: '#003A75',
};

const grey = {
50: '#f6f8fa',
100: '#eaeef2',
200: '#d0d7de',
300: '#afb8c1',
400: '#8c959f',
500: '#6e7781',
600: '#57606a',
700: '#424a53',
800: '#32383f',
900: '#24292f',
};

const StyledSlider = styled(SliderUnstyled)(
({ theme }) => `
color: ${theme.palette.mode === 'light' ? blue[500] : blue[300]};
height: 6px;
width: 100%;
padding: 16px 0;
display: inline-block;
position: relative;
cursor: pointer;
touch-action: none;
-webkit-tap-highlight-color: transparent;

&:hover {
opacity: 1;
}

&.${sliderUnstyledClasses.disabled} {
pointer-events: none;
cursor: default;
color: ${theme.palette.mode === 'light' ? grey[300] : grey[600]};
opacity: 0.5;
}

& .${sliderUnstyledClasses.rail} {
display: block;
position: absolute;
width: 100%;
height: 4px;
border-radius: 2px;
background-color: currentColor;
opacity: 0.4;
}

& .${sliderUnstyledClasses.track} {
display: block;
position: absolute;
height: 4px;
border-radius: 2px;
background-color: currentColor;
}

& .${sliderUnstyledClasses.thumb} {
position: absolute;
width: 16px;
height: 16px;
margin-left: -6px;
margin-top: -6px;
box-sizing: border-box;
border-radius: 50%;
outline: 0;
border: 3px solid currentColor;
background-color: #fff;

:hover,
&.${sliderUnstyledClasses.focusVisible} {
box-shadow: 0 0 0 0.25rem ${alpha(
theme.palette.mode === 'light' ? blue[400] : blue[300],
0.15,
)};
}

& .label {
background: unset;
background-color: ${theme.palette.mode === 'light' ? blue[500] : blue[300]};
width: 32px;
height: 32px;
padding: 0px;
visibility: hidden;
color: #fff;
border-radius: 50% 50% 50% 0;
position: absolute;
transform: translate(-35%, -140%) rotate(-45deg) scale(0);
transition: transform 0.3s ease;
}

:hover .label {
visibility: visible;
transform: translate(-35%, -140%) rotate(-45deg) scale(1);
}

:hover .value {
transform: rotate(45deg);
text-align: center;
line-height: 1.8
}

&.${sliderUnstyledClasses.active} {
box-shadow: 0 0 0 0.25rem ${alpha(
theme.palette.mode === 'light' ? blue[200] : blue[300],
0.3,
)};
}
}
`,
);

function SliderValueLabel({ children }) {
return (
<span className="label">
<div className="value">{children}</div>
</span>
);
}

SliderValueLabel.propTypes = {
children: PropTypes.element.isRequired,
};

export default function UnstyledSlider() {
return (
<Box sx={{ width: 300 }}>
<StyledSlider defaultValue={10} slots={{ valueLabel: SliderValueLabel }} />
</Box>
);
}