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
[SliderUnstyled] Add useSlider hook and polish #30094
Conversation
@material-ui/core: parsed: +0.03% , gzip: +0.08% |
@@ -457,25 +459,26 @@ const Slider = React.forwardRef(function Slider(inputProps, ref) { | |||
...componentsProps, | |||
root: { | |||
...componentsProps.root, | |||
...(shouldSpreadOwnerState(components.Root) && { | |||
...(shouldSpreadAdditionalProps(components.Root) && { | |||
as: component, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was previously handled in the SliderUnstyled
. The logic was moved here now.
scale?: (value: number) => number; | ||
step?: number | null; | ||
tabIndex?: number; | ||
value?: number | number[]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically, the value is number | [number, number]
as arrays of size != 2 don't make sense in this context. But I'm afraid this could be a breaking change.
In general not being able to experiment and introduce breaking changes on Slider and Badge may be a bit limiting. We'll have to discuss it in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually no, I believe there can be more values, for example, if more than two thumbs are used. See https://www.w3.org/TR/wai-aria-practices/#slidertwothumb.
In general not being able to experiment and introduce breaking changes on Slider and Badge may be a bit limiting. We'll have to discuss it in the future.
Agree, let's see how far we can get, and if there are blockers we can create duplicates components (not ideal, but I don't have any other idea)
|
||
const getTrackProps = () => { | ||
return { | ||
style: { ...trackStyle, ...componentsProps.track?.style }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about instead of setting style directly in the hook we return the offset and leap values and let the caller set the styles? This way the hook would truly be "unstyled" (except for setting the styles of the hidden input, but I suppose that one is ok).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, let me try to refactor and see where we'll land
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would create a problem I think. I would expect that the unstyled version should handle this. If we let this be a responsibility of the users of the hook/unstyled component, we would need to document how each developer should set it, as it is related to the functionality of the component.
I will however move it to the SliderUnstyled
as it is already setting style
on some places for the thumbs
, valueLabel
etc, so that at least those will be in one place (in the component).
@@ -11,7 +11,6 @@ | |||
}, | |||
"default": "'primary'" | |||
}, | |||
"component": { "type": { "name": "elementType" } }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The component
prop should remain in the docs, shouldn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's removed because the prop is available in the unstyled component. Will double check
const classes = useUtilityClasses(ownerState); | ||
|
||
return ( | ||
<Root | ||
ref={handleRef} | ||
onMouseDown={handleMouseDown} | ||
{...getRootProps()} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pass in the external event handlers, so that ours onMouseDown
is not overridden by the caller accidentally.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is handled in the hook itself, it was removed from here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant that if someone adds an onMouseDown
prop on SliderUnstyled
, it'll shadow our internal handler defined in useSlider
. To prevent this, pass in other
to getRootProps
.
Then, ideally, the createHandleMouseDown
function in useSlider
should check if defaultPrevented
was set and skip its execution if it was.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is destructured from the props, and the hook already handles this. Let me refactor to explicitly send it in the hook.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed with - afac9ca
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Note that #30362 has been completed, so you'll have to merge these changes.
Aside from that, it looks great.
Adds
useSlider
hook and updates theSliderUnstyled
component's logic to be compatible with the other unstyled components. I don't expect there to be breaking changes in the@mui/material
'sSlider
component. One change that I needed to make is to handle thecomponent
prop in theSlider
component, as it differs to how it is implemented in theSliderUnstyled
(thecomponent
prop just changes the tag, thecomponents.Root
replaces the whole root element, for example the styles won't be applied).In a follow up PR I plan to address the TODOs that came up while converting the code to the hook which is written in
TypeScript
.