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] Modernize implementation #583

Merged
merged 17 commits into from
Oct 15, 2024

Conversation

mj12albert
Copy link
Member

@mj12albert mj12albert commented Sep 5, 2024

Summary

  1. Replaced useCompound with Composite

Previously the id attribute of all the input elements were passed to useCompound as metadata to generate the for attribute of the output element.

However the refs provided by Composite can't be used during the render of Output for this, so the ids have to be held in a new state

  1. Converted types to namespaces
  2. Fix [slider] Vertical slider incorrectly exposed as horizontal in the accessibility tree in Chrome<124 #635
  3. Hooks/context are no longer exported for now - it's no longer possible to (easily) implement a Material Design slider as it would require custom subcomponents
  4. Matches latest export convention in [core] Improve DX of importing Base UI components #700

@mj12albert mj12albert added the component: slider This is the name of the generic UI component, not the React module! label Sep 5, 2024
@mui-bot
Copy link

mui-bot commented Sep 5, 2024

Netlify deploy preview

https://deploy-preview-583--base-ui.netlify.app/

Generated by 🚫 dangerJS against 129bdec

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Sep 5, 2024
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Sep 5, 2024
@mj12albert mj12albert force-pushed the refactor/slider-composite branch 4 times, most recently from 753b46c to 04e16b3 Compare September 10, 2024 16:34
@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Sep 11, 2024
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Sep 11, 2024
@mj12albert mj12albert marked this pull request as ready for review September 13, 2024 09:51
@mj12albert mj12albert marked this pull request as draft September 20, 2024 04:30
@mj12albert mj12albert changed the title [Slider] Replace useCompound with Composite [Slider] Modernize implementation Sep 20, 2024
@michaldudak michaldudak mentioned this pull request Oct 1, 2024
15 tasks
@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Oct 3, 2024
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Oct 9, 2024
@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Oct 10, 2024
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Oct 10, 2024
@mj12albert mj12albert marked this pull request as ready for review October 11, 2024 12:00
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.

It would be great if @atomiks could review the usage of the Composite component

@@ -1,7 +1,7 @@
'use client';
import * as React from 'react';
import * as Slider from '@base_ui/react/Slider';
import { useSliderContext } from '@base_ui/react/Slider';
import { useSliderContext } from '../../../packages/mui-base/src/Slider/Root/SliderContext';
Copy link
Member

Choose a reason for hiding this comment

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

If there is a legitimate use case for the context, we should export it from the package.

Copy link
Member Author

Choose a reason for hiding this comment

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

We discussed this and decided to revisit exporting Context(s) after the alpha phase

<Slider.Control className={classes.control}>
<Slider.Track className={classes.track}>
<Slider.Indicator className={classes.indicator} />
<Slider.Thumb className={classes.thumb} inputId=":slider-thumb-input:" />
Copy link
Member

Choose a reason for hiding this comment

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

The inputId prop is not documented

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated!

/**
* @ignore - internal component.
*/
export const SliderContext = React.createContext<SliderRoot.Context | undefined>(undefined);
Copy link
Member

Choose a reason for hiding this comment

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

Context type should be defined in this file (and be called SliderContext)

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated ~ I've also renamed it to SliderRootContext/useSliderRootContext to match other components

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, that's actually what I meant 😅

@mj12albert mj12albert force-pushed the refactor/slider-composite branch 3 times, most recently from 491175e to f16af45 Compare October 13, 2024 06:39
@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Oct 14, 2024
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Oct 14, 2024
Comment on lines +192 to +215

// Map with index (DOM position) as the key and the id attribute of each thumb <input> element as the value
const [inputIdMap, setInputMap] = React.useState(() => new Map<number, string>());

const deregisterInputId = React.useCallback((index: number) => {
setInputMap((prevMap) => {
const nextMap = new Map(prevMap);
nextMap.delete(index);
return nextMap;
});
}, []);

const registerInputId = React.useCallback(
(index: number, inputId: string | undefined) => {
if (index > -1 && inputId !== undefined) {
setInputMap((prevMap) => new Map(prevMap).set(index, inputId));
}

return {
deregister: deregisterInputId,
};
},
[deregisterInputId],
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible to modify the Composite source to allow for metadata instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

Should be! How about doing this when refactoring Tabs to use Composite? Tabs uses metadata more extensively, and I wanted to get this PR merged ASAP to unblock the ESM work @michaldudak is doing 🙏

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems ok to me as of now

@mj12albert mj12albert merged commit 680f5b3 into mui:master Oct 15, 2024
18 checks passed
@mj12albert mj12albert deleted the refactor/slider-composite branch October 15, 2024 09:57
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!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[slider] Vertical slider incorrectly exposed as horizontal in the accessibility tree in Chrome<124
4 participants