Skip to content

Commit

Permalink
ScrollArea reads DirectionProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
mj12albert committed Dec 20, 2024
1 parent 4941275 commit 4a754f5
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 41 deletions.
4 changes: 2 additions & 2 deletions packages/react/src/scroll-area/corner/ScrollAreaCorner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const ScrollAreaCorner = React.forwardRef(function ScrollAreaCorner(
) {
const { render, className, ...otherProps } = props;

const { dir, cornerRef, cornerSize, hiddenState } = useScrollAreaRootContext();
const { cornerRef, cornerSize, hiddenState } = useScrollAreaRootContext();

const mergedRef = useForkRef(cornerRef, forwardedRef);

Expand All @@ -34,7 +34,7 @@ const ScrollAreaCorner = React.forwardRef(function ScrollAreaCorner(
style: {
position: 'absolute',
bottom: 0,
[dir === 'rtl' ? 'left' : 'right']: 0,
insetInlineEnd: 0,
width: cornerSize.width,
height: cornerSize.height,
},
Expand Down
15 changes: 7 additions & 8 deletions packages/react/src/scroll-area/root/ScrollAreaRoot.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import { useDirection } from '../../direction-provider/DirectionContext';
import type { BaseUIComponentProps } from '../../utils/types';
import { useComponentRenderer } from '../../utils/useComponentRenderer';
import { ScrollAreaRootContext } from './ScrollAreaRootContext';
Expand All @@ -18,9 +19,11 @@ const ScrollAreaRoot = React.forwardRef(function ScrollAreaRoot(
props: ScrollAreaRoot.Props,
forwardedRef: React.ForwardedRef<HTMLDivElement>,
) {
const { render, className, dir, ...otherProps } = props;
const { render, className, ...otherProps } = props;

const scrollAreaRoot = useScrollAreaRoot({ dir });
const direction = useDirection();

const scrollAreaRoot = useScrollAreaRoot();

const { rootId } = scrollAreaRoot;

Expand All @@ -35,10 +38,10 @@ const ScrollAreaRoot = React.forwardRef(function ScrollAreaRoot(

const contextValue = React.useMemo(
() => ({
dir,
direction,
...scrollAreaRoot,
}),
[dir, scrollAreaRoot],
[direction, scrollAreaRoot],
);

const viewportId = `[data-id="${rootId}-viewport"]`;
Expand Down Expand Up @@ -83,10 +86,6 @@ ScrollAreaRoot.propTypes /* remove-proptypes */ = {
* returns a class based on the component’s state.
*/
className: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
/**
* @ignore
*/
dir: PropTypes.string,
/**
* Allows you to replace the component’s HTML element
* with a different tag, or compose it with another component.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';

export interface ScrollAreaRootContext {
dir: string | undefined;
direction: string | undefined;
cornerSize: { width: number; height: number };
setCornerSize: React.Dispatch<React.SetStateAction<{ width: number; height: number }>>;
thumbSize: { width: number; height: number };
Expand Down
23 changes: 3 additions & 20 deletions packages/react/src/scroll-area/root/useScrollAreaRoot.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as React from 'react';
import { useEventCallback } from '../../utils/useEventCallback';
import { useEnhancedEffect } from '../../utils/useEnhancedEffect';
import { mergeReactProps } from '../../utils/mergeReactProps';
import { useBaseUiId } from '../../utils/useBaseUiId';
import { SCROLL_TIMEOUT } from '../constants';
Expand All @@ -12,9 +11,7 @@ interface Size {
height: number;
}

export function useScrollAreaRoot(params: useScrollAreaRoot.Parameters) {
const { dir: dirParam } = params;

export function useScrollAreaRoot() {
const [hovering, setHovering] = React.useState(false);
const [scrolling, setScrolling] = React.useState(false);
const [cornerSize, setCornerSize] = React.useState<Size>({ width: 0, height: 0 });
Expand Down Expand Up @@ -44,15 +41,6 @@ export function useScrollAreaRoot(params: useScrollAreaRoot.Parameters) {
cornerHidden: false,
});

const [autoDir, setAutoDir] = React.useState(dirParam);
const dir = dirParam ?? autoDir;

useEnhancedEffect(() => {
if (dirParam === undefined && viewportRef.current) {
setAutoDir(getComputedStyle(viewportRef.current).direction);
}
}, [dirParam]);

React.useEffect(() => {
return () => {
window.clearTimeout(timeoutRef.current);
Expand Down Expand Up @@ -170,7 +158,6 @@ export function useScrollAreaRoot(params: useScrollAreaRoot.Parameters) {
const getRootProps = React.useCallback(
(externalProps = {}) =>
mergeReactProps<'div'>(externalProps, {
dir,
onPointerEnter: handlePointerEnterOrMove,
onPointerMove: handlePointerEnterOrMove,
onPointerDown({ pointerType }) {
Expand All @@ -185,7 +172,7 @@ export function useScrollAreaRoot(params: useScrollAreaRoot.Parameters) {
[ScrollAreaRootCssVars.scrollAreaCornerWidth as string]: `${cornerSize.width}px`,
},
}),
[cornerSize, dir, handlePointerEnterOrMove],
[cornerSize, handlePointerEnterOrMove],
);

return React.useMemo(
Expand Down Expand Up @@ -238,8 +225,4 @@ export function useScrollAreaRoot(params: useScrollAreaRoot.Parameters) {
);
}

export namespace useScrollAreaRoot {
export interface Parameters {
dir: string | undefined;
}
}
export namespace useScrollAreaRoot {}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function useScrollAreaScrollbar(params: useScrollAreaScrollbar.Parameters
const { orientation } = params;

const {
dir,
direction,
scrollbarYRef,
scrollbarXRef,
viewportRef,
Expand Down Expand Up @@ -130,7 +130,7 @@ export function useScrollAreaScrollbar(params: useScrollAreaScrollbar.Parameters
const scrollRatioX = clickX / maxThumbOffsetX;

let newScrollLeft: number;
if (dir === 'rtl') {
if (direction === 'rtl') {
// In RTL, invert the scroll direction
newScrollLeft = (1 - scrollRatioX) * (scrollableContentWidth - viewportWidth);

Expand All @@ -154,13 +154,12 @@ export function useScrollAreaScrollbar(params: useScrollAreaScrollbar.Parameters
...(orientation === 'vertical' && {
top: 0,
bottom: `var(${ScrollAreaRootCssVars.scrollAreaCornerHeight})`,
[dir === 'rtl' ? 'left' : 'right']: 0,
insetInlineEnd: 0,
[ScrollAreaScrollbarCssVars.scrollAreaThumbHeight as string]: `${thumbSize.height}px`,
}),
...(orientation === 'horizontal' && {
[dir === 'rtl' ? 'right' : 'left']: 0,
[dir === 'rtl' ? 'left' : 'right']:
`var(${ScrollAreaRootCssVars.scrollAreaCornerWidth})`,
insetInlineStart: 0,
insetInlineEnd: `var(${ScrollAreaRootCssVars.scrollAreaCornerWidth})`,
bottom: 0,
[ScrollAreaScrollbarCssVars.scrollAreaThumbWidth as string]: `${thumbSize.width}px`,
}),
Expand All @@ -170,7 +169,7 @@ export function useScrollAreaScrollbar(params: useScrollAreaScrollbar.Parameters
rootId,
handlePointerUp,
orientation,
dir,
direction,
thumbSize.height,
thumbSize.width,
viewportRef,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function useScrollAreaViewport(params: useScrollAreaViewport.Parameters)
thumbYRef,
thumbXRef,
cornerRef,
dir,
direction,
setCornerSize,
setThumbSize,
rootId,
Expand Down Expand Up @@ -102,7 +102,7 @@ export function useScrollAreaViewport(params: useScrollAreaViewport.Parameters)
// In Safari, don't allow it to go negative or too far as `scrollLeft` considers the rubber
// band effect.
const thumbOffsetX =
dir === 'rtl'
direction === 'rtl'
? clamp(scrollRatioX * maxThumbOffsetX, -maxThumbOffsetX, 0)
: clamp(scrollRatioX * maxThumbOffsetX, 0, maxThumbOffsetX);

Expand Down Expand Up @@ -146,7 +146,7 @@ export function useScrollAreaViewport(params: useScrollAreaViewport.Parameters)

useEnhancedEffect(() => {
computeThumb();
}, [computeThumb, hiddenState, dir]);
}, [computeThumb, hiddenState, direction]);

useEnhancedEffect(() => {
// `onMouseEnter` doesn't fire upon load, so we need to check if the viewport is already
Expand Down

0 comments on commit 4a754f5

Please sign in to comment.