material-ui icon indicating copy to clipboard operation
material-ui copied to clipboard

[docs][material-ui] Slider in color customization playground twitches when sliding

Open yassinmars opened this issue 10 months ago • 5 comments

Steps to reproduce

Link to live example: (required)

https://github.com/mui/material-ui/assets/110468377/af4c2657-662e-4537-acc7-13a9f04e840b

Steps: 1.Go to Settings 2.Click on "Edit documentation colors" 3.Slide the toggle from left to right. 4.Observe the behavior as it passes the 100 and 50 marks and the marks after that.

Current behavior

When sliding the Slider component's toggle from left to right, a noticeable twitch occurs around the 100 and 50 marks. The marks for A700 and A400 are not displayed when the slider moves from left to right. These issues do not occur when sliding from right to left.

Expected behavior

The Slider should move smoothly without any twitching at any point along the track. All marks, including A700 and A400, should be visible when sliding from left to right.

Context

I am attempting to provide a seamless user experience when using the slider. This twitching effect is causing confusion and a less satisfactory user interaction.

Your environment

npx @mui/envinfo
  Don't forget to mention which browser you used.
  Output from `npx @mui/envinfo` goes here.

Search keywords: Slider, twitch, A700, A400, marks, animation issue

yassinmars avatar Apr 25 '24 00:04 yassinmars

Thanks for reporting this - I think this could be an issue with the playground and not the Slider component itself, as it doesn't repro in the Slider demos here

The component for the playground is here - would you be interested in investigating this further? @yassinmars

mj12albert avatar Apr 29 '24 13:04 mj12albert

Thank you for your response and guidance @mj12albert - yes absolutely i would like to investigate the issue further, i have already located the code for the playground and I'm currently looking through it to pinpoint the exact cause of the twitching issue. I'll share my findings as soon as I have something concrete.

yassinmars avatar Apr 29 '24 18:04 yassinmars

@mj12albert, I believe I've identified potential areas that could be causing the twitching issue with the Slider component. Here are my key observations:

-State Management in handleChangeShade: This function updates several state variables simultaneously ([name]Shade, [name], and [name]Input). The frequency and scope of these updates might be causing the component to rerender inefficiently, leading to the twitching observed, particularly near the 100 and 50 marks. Here's code for reference:

image

-Dynamic Styling Calculations: The Slider's sx property involves dynamic calculations that might be affecting its rendering performance. Reducing complexity in these style calculations could potentially improve performance. Here’s the relevant code:

image

I'm currently exploring optimizations in the handleChangeShade function to reduce the number of state updates and simplify the dynamic style calculations. These changes might help in achieving smoother interactions with the Slider.

yassinmars avatar May 01 '24 19:05 yassinmars

@mj12albert, I've implemented several optimizations based on the initial observation to address the issue. Here are the changes and the solutions i came up with:

  1. Optimized State Management in handleChangeShade:

I refactored the handleChangeShade function to minimize the number of state updates and potential re-renders. By consolidating the state updates into a single setState call, we reduce the overhead and improve the slider's responsiveness. Here's the revised code snippet:

image

  1. Simplified and Improved Slider Styling:

To further enhance performance, I simplified the Slider's dynamic styling by moving the style calculations outside of the component render method. This approach minimizes recalculations and leverages CSS for better performance. The updated styling I applied is as follows:

image

  1. Integrated Changes within colorPicker Function:

I ensured that these changes were integrated smoothly within the colorPicker function, keeping in mind the overall component structure and existing functionality. The focus was on ensuring that the modifications did not introduce any new issues or regressions. Here's how the modified section looks now:

image

yassinmars avatar May 02 '24 17:05 yassinmars

@yassinmars Thanks for looking into this - would you like you make a PR so we can try out the changes?

mj12albert avatar May 13 '24 07:05 mj12albert

This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue. Now, if you have additional information related to this issue or things that could help future readers, feel free to leave a comment.

[!NOTE] We value your feedback @yassinmars! How was your experience with our support team? If you could spare a moment, we'd love to hear your thoughts in this brief Support Satisfaction survey. Your insights help us improve!

github-actions[bot] avatar Sep 14 '24 16:09 github-actions[bot]