Skip to content

Commit

Permalink
WIP ssr improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mj12albert committed Jun 18, 2024
1 parent 14de73a commit f6f37a2
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 6 deletions.
1 change: 0 additions & 1 deletion docs/data/base/components/slider/slider.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ To let users set the start and end of a range on a Slider, provide an array of v
<Slider.Track/>
</Slider.Control>
</Slider.Root>
```

{{"demo": "RangeSlider.js"}}

Expand Down
1 change: 1 addition & 0 deletions docs/pages/base-ui/api/use-slider-thumb.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
}
},
"id": { "type": { "name": "string", "description": "string" } },
"index": { "type": { "name": "number", "description": "number" } },
"name": { "type": { "name": "string", "description": "string" } },
"onBlur": {
"type": { "name": "React.FocusEventHandler", "description": "React.FocusEventHandler" }
Expand Down
6 changes: 3 additions & 3 deletions docs/pages/experiments/slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ export default function App() {
<Slider.Control className="MySlider-control">
<Slider.Track className="MySlider-track">
<Slider.Indicator className="MySlider-indicator" />
<Slider.Thumb className="MySlider-thumb one" />
<Slider.Thumb className="MySlider-thumb two" />
<Slider.Thumb className="MySlider-thumb three" />
<Slider.Thumb className="MySlider-thumb" />
<Slider.Thumb className="MySlider-thumb" />
<Slider.Thumb className="MySlider-thumb" />
</Slider.Track>
</Slider.Control>
</Slider.Root>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"getAriaValueText": {
"description": "Accepts a function which returns a string value that provides a user-friendly name for the current value of the slider. This is important for screen reader users."
},
"index": { "description": "A unique index for each thumb to assist SSR (optional)" },
"largeStep": {
"description": "The large step value of the slider when incrementing or decrementing while the shift key is held, or when using Page-Up or Page-Down keys. Snaps to multiples of this value."
},
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-base/src/Slider/SliderThumb/SliderThumb.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ const SliderThumb = React.forwardRef(function SliderThumb(
});

const styleHooks = React.useMemo(
() => getStyleHookProps({ disabled, dragging: activeIndex === index }),
() => getStyleHookProps({ disabled, dragging: index !== -1 && activeIndex === index }),
[activeIndex, disabled, index],
);

Expand Down
15 changes: 14 additions & 1 deletion packages/mui-base/src/Slider/SliderThumb/useSliderThumb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ function getNewValue(
}

function getDefaultAriaValueText(values: readonly number[], index: number): string | undefined {
if (index < 0) {
return undefined;
}

if (values.length === 2) {
if (index === 0) {
return `start range ${values[index]}`;
}

return `end range ${values[index]}`;
}

return undefined;
}
/**
Expand Down Expand Up @@ -87,12 +92,20 @@ export function useSliderThumb(parameters: UseSliderThumbParameters) {

const thumbValue = sliderValues[index];

const percent = percentageValues[index];
// for SSR, don't wait for the index if there's only one thumb
const percent = percentageValues.length === 1 ? percentageValues[0] : percentageValues[index];

const isRtl = direction === 'rtl';

const getThumbStyle = React.useCallback(() => {
const isVertical = orientation === 'vertical';

if (!percent) {
return {
zIndex: -1,
};
}

return {
position: 'absolute',
[{
Expand Down

0 comments on commit f6f37a2

Please sign in to comment.