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

[ScrollArea] Read DirectionProvider and use logical positioning CSS props #1194

Merged
merged 3 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Member

Choose a reason for hiding this comment

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

I am curious, what's the benefit of adding the direction in a context, when each subcomponent that need it can read the DirectionProvider's context?

Copy link
Member Author

Choose a reason for hiding this comment

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

what's the benefit of adding the direction in a context

It doesn't need to be in the root context anymore, I've updated this ~

...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 [scrollingX, setScrollingX] = React.useState(false);
const [scrollingY, setScrollingY] = React.useState(false);
Expand Down Expand Up @@ -47,15 +44,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(scrollYTimeoutRef.current);
Expand Down Expand Up @@ -193,7 +181,6 @@ export function useScrollAreaRoot(params: useScrollAreaRoot.Parameters) {
const getRootProps = React.useCallback(
(externalProps = {}) =>
mergeReactProps<'div'>(externalProps, {
dir,
onPointerEnter: handlePointerEnterOrMove,
onPointerMove: handlePointerEnterOrMove,
onPointerDown({ pointerType }) {
Expand All @@ -208,7 +195,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 @@ -266,8 +253,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
Loading