-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
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
[base-ui][useSlider] Suggestions for refactoring the useSlider hook #39365
Comments
There's already a pretty flexible clamp implementation including a
|
Thanks for your input! Corrections and improvements are always welcome.
We only use non-strict equality comparison when testing for
Nice catch! Could you submit a PR that corrects it?
Feel free to create a PR that moves them to the @mui/utils package. |
Thank you for the clarification One could make use of: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing which is equally short and adjustable to various styles: (but your solution of enforcing it via eslint is exemplary) const value = getValue() // could retrun any
if (value ?? true) {
console.log(":poop:");
}
if (value ?? 1) {
console.log(":poop:");
}
if (value ?? !0) {
console.log(":poop:");
} if (undefined ?? true) {
console.log("undefined")
}
if (null ?? true) {
console.log("null")
}
if (false ?? true) {
console.log("false")
}
if (0 ?? true) {
console.log("0")
}
Sure, I can look into that, since this is straight forward
Sadly not any time soon, if this is still open around end of year I might revisit this. |
Created PR for useSlider refactoring |
|
Duplicates
Latest version
Steps to reproduce 🕹
Link to live example: https://github.com/mui/material-ui/blob/master/packages/mui-base/src/useSlider/useSlider.ts
Steps:
Current behavior 😯
The implementation of useSlider seems overengineered and has several potential issues and code smells.
==
&!=
where a strict variant would do the same and add stability. (i.e. here, here or here)clamp
,valueToPercent
and others that should probably go into a shared utils file (if not already available) to prevent code duplication.Addition to 3. & 4.
Passive event listeners should be used when user interaction happens in fast steps (like scrolling, moving pointers, resizing etc.) which could result in jank.
removeEventListener
doesn't even allow{passive: true}
so this is actually invalid code: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/removeEventListener#parametersExpected behavior 🤔
I expect the code to meet common/best practice and allow developers to understand the behavior. Usually this is achieved by "separation of concerns".
I also expect issues like these to be caught by linters or code-reviews.
Context 🔦
I stumbled upon this while trying to debug #39362.
I soon realized that it is close to impossible to get through this code (for me).
Not a single useful comment explaining the decisions or reason for certain steps in the process (except for a nice elaboration on the iOS bug which has been resolved several Major versions ago)
Your environment 🌎
npx @mui/envinfo
The text was updated successfully, but these errors were encountered: