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

[SliderUnstyled] Add useSlider hook and polish #30094

Merged
merged 23 commits into from Jan 11, 2022

Conversation

mnajdova
Copy link
Member

@mnajdova mnajdova commented Dec 7, 2021

Adds useSlider hook and updates the SliderUnstyled component's logic to be compatible with the other unstyled components. I don't expect there to be breaking changes in the @mui/material's Slider component. One change that I needed to make is to handle the component prop in the Slider component, as it differs to how it is implemented in the SliderUnstyled (the component prop just changes the tag, the components.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.

@mui-pr-bot
Copy link

mui-pr-bot commented Dec 7, 2021

Details of bundle changes

@material-ui/core: parsed: +0.03% , gzip: +0.08%
@material-ui/unstyled: parsed: +1.17% , gzip: +1.38%

Generated by 🚫 dangerJS against d556c51

@mnajdova mnajdova marked this pull request as ready for review December 8, 2021 12:14
@mnajdova mnajdova requested a review from a team December 8, 2021 12:14
@@ -457,25 +459,26 @@ const Slider = React.forwardRef(function Slider(inputProps, ref) {
...componentsProps,
root: {
...componentsProps.root,
...(shouldSpreadOwnerState(components.Root) && {
...(shouldSpreadAdditionalProps(components.Root) && {
as: component,
Copy link
Member Author

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[];
Copy link
Member

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.

Copy link
Member Author

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 },
Copy link
Member

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).

Copy link
Member Author

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

Copy link
Member Author

@mnajdova mnajdova Dec 17, 2021

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).

packages/mui-base/src/SliderUnstyled/useSlider.tsx Outdated Show resolved Hide resolved
packages/mui-base/src/SliderUnstyled/SliderUnstyled.js Outdated Show resolved Hide resolved
@oliviertassinari oliviertassinari added package: base-ui Specific to @mui/base component: slider This is the name of the generic UI component, not the React module! labels Dec 20, 2021
@@ -11,7 +11,6 @@
},
"default": "'primary'"
},
"component": { "type": { "name": "elementType" } },
Copy link
Member

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?

Copy link
Member Author

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()}
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Addressed with - afac9ca

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Jan 10, 2022
Copy link
Member

@michaldudak michaldudak left a 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.

@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Jan 11, 2022
@mnajdova mnajdova merged commit 98212a3 into mui:master Jan 11, 2022
wladimirguerra pushed a commit to wladimirguerra/material-ui that referenced this pull request Feb 2, 2022
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! package: base-ui Specific to @mui/base
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

5 participants