Skip to content

Commit

Permalink
Remove both-edges
Browse files Browse the repository at this point in the history
  • Loading branch information
atomiks committed Oct 31, 2024
1 parent 3355f9f commit 1635971
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 51 deletions.
5 changes: 1 addition & 4 deletions docs/data/api/scroll-area-root.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
"props": {
"className": { "type": { "name": "union", "description": "func<br>&#124;&nbsp;string" } },
"gutter": {
"type": {
"name": "enum",
"description": "'both-edges'<br>&#124;&nbsp;'none'<br>&#124;&nbsp;'stable'"
},
"type": { "name": "enum", "description": "'none'<br>&#124;&nbsp;'stable'" },
"default": "'stable'"
},
"render": { "type": { "name": "union", "description": "element<br>&#124;&nbsp;func" } },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ const ScrollAreaViewport = styled(ScrollArea.Viewport)`
`;

const ScrollAreaScrollbar = styled(ScrollArea.Scrollbar)`
width: 10px;
padding: 2px;
visibility: hidden;
background: transparent;
Expand All @@ -82,8 +81,11 @@ const ScrollAreaScrollbar = styled(ScrollArea.Scrollbar)`
background: rgb(0 0 0 / 0.1);
}
&[data-orientation='vertical'] {
width: 10px;
}
&[data-orientation='horizontal'] {
width: 100%;
height: 10px;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ const ScrollAreaViewport = styled(ScrollArea.Viewport)`
`;

const ScrollAreaScrollbar = styled(ScrollArea.Scrollbar)`
width: 10px;
padding: 2px;
visibility: hidden;
background: transparent;
Expand All @@ -82,8 +81,11 @@ const ScrollAreaScrollbar = styled(ScrollArea.Scrollbar)`
background: rgb(0 0 0 / 0.1);
}
&[data-orientation='vertical'] {
width: 10px;
}
&[data-orientation='horizontal'] {
width: 100%;
height: 10px;
}
Expand Down
5 changes: 1 addition & 4 deletions docs/data/components/scroll-area/scroll-area.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,11 @@ The scrollbars can be either `overlay` (default) or `inset`. The `overlay` type

When using `inset` scrollbars, the `gutter` prop can be used to keep a gutter present when the scrollbar is hidden. This is useful to prevent layout shift when the scrollbar is shown or hidden based on the content.

Possible values match the CSS `scrollbar-gutter` property:

- `stable` (default; padding added to account for vertical scrollbar)
- `both-edges` (symmetrical padding added for vertical scrollbar)
- `none` (layout shift allowed)

```jsx
<ScrollArea.Root gutter="both-edges">
<ScrollArea.Root gutter="none">
```

## Styling
Expand Down
26 changes: 0 additions & 26 deletions packages/mui-base/src/ScrollArea/Root/ScrollAreaRoot.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,32 +130,6 @@ describe('<ScrollArea.Root />', () => {
});

describe('prop: gutter', () => {
it('should adjust inset padding for gutter: both-edges', async () => {
await render(
<ScrollArea.Root
type="inset"
gutter="both-edges"
style={{ width: VIEWPORT_SIZE, height: VIEWPORT_SIZE }}
>
<ScrollArea.Viewport data-testid="viewport" style={{ width: '100%', height: '100%' }} />
<ScrollArea.Scrollbar
orientation="vertical"
style={{ width: SCROLLBAR_WIDTH, height: '100%' }}
/>
<ScrollArea.Scrollbar
orientation="horizontal"
style={{ height: SCROLLBAR_HEIGHT, width: '100%' }}
/>
</ScrollArea.Root>,
);

const contentWrapper = screen.getByTestId('viewport').firstElementChild!;
const style = getComputedStyle(contentWrapper);

expect(style.paddingLeft).to.equal(`${SCROLLBAR_WIDTH}px`);
expect(style.paddingRight).to.equal(`${SCROLLBAR_WIDTH}px`);
});

it('should not add inset padding for gutter: none', async () => {
await render(
<ScrollArea.Root
Expand Down
4 changes: 2 additions & 2 deletions packages/mui-base/src/ScrollArea/Root/ScrollAreaRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ namespace ScrollAreaRoot {
* shifts when the scrollbar is hidden/shown.
* @default 'stable'
*/
gutter?: 'none' | 'stable' | 'both-edges';
gutter?: 'none' | 'stable';
}

export interface OwnerState {}
Expand Down Expand Up @@ -106,7 +106,7 @@ ScrollAreaRoot.propTypes /* remove-proptypes */ = {
* shifts when the scrollbar is hidden/shown.
* @default 'stable'
*/
gutter: PropTypes.oneOf(['both-edges', 'none', 'stable']),
gutter: PropTypes.oneOf(['none', 'stable']),
/**
* A function to customize rendering of the component.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import * as React from 'react';
export interface ScrollAreaRootContext {
dir: string | undefined;
type: 'overlay' | 'inset';
gutter?: 'stable' | 'both-edges' | 'none';
gutter?: 'stable' | 'none';
cornerSize: { width: number; height: number };
setCornerSize: React.Dispatch<React.SetStateAction<{ width: number; height: number }>>;
thumbSize: { width: number; height: number };
setThumbSize: React.Dispatch<React.SetStateAction<{ width: number; height: number }>>;
touchModality: boolean;
hovering: boolean;
setHovering: React.Dispatch<React.SetStateAction<boolean>>;
scrolling: boolean;
Expand Down
12 changes: 11 additions & 1 deletion packages/mui-base/src/ScrollArea/Root/useScrollAreaRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function useScrollAreaRoot(params: useScrollAreaRoot.Parameters) {
const [scrolling, setScrolling] = React.useState(false);
const [cornerSize, setCornerSize] = React.useState<Size>({ width: 0, height: 0 });
const [thumbSize, setThumbSize] = React.useState<Size>({ width: 0, height: 0 });
const [touchModality, setTouchModality] = React.useState(false);

const rootId = useId();

Expand Down Expand Up @@ -130,10 +131,17 @@ export function useScrollAreaRoot(params: useScrollAreaRoot.Parameters) {
mergeReactProps<'div'>(externalProps, {
dir,
onPointerEnter({ pointerType }) {
if (pointerType !== 'touch') {
const isTouch = pointerType === 'touch';

setTouchModality(isTouch);

if (!isTouch) {
setHovering(true);
}
},
onPointerDown({ pointerType }) {
setTouchModality(pointerType === 'touch');
},
onPointerLeave() {
setHovering(false);
},
Expand All @@ -156,6 +164,7 @@ export function useScrollAreaRoot(params: useScrollAreaRoot.Parameters) {
setCornerSize,
thumbSize,
setThumbSize,
touchModality,
cornerRef,
scrolling,
setScrolling,
Expand All @@ -177,6 +186,7 @@ export function useScrollAreaRoot(params: useScrollAreaRoot.Parameters) {
handlePointerUp,
cornerSize,
thumbSize,
touchModality,
cornerRef,
scrolling,
hovering,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function useScrollAreaScrollbar(params: useScrollAreaScrollbar.Parameters
rootId,
type,
thumbSize,
touchModality,
} = useScrollAreaRootContext();

React.useEffect(() => {
Expand Down Expand Up @@ -128,7 +129,9 @@ export function useScrollAreaScrollbar(params: useScrollAreaScrollbar.Parameters
onPointerUp: handlePointerUp,
style: {
position: 'absolute',
...(type === 'inset' && { touchAction: 'none' }),
...(type === 'inset'
? { touchAction: 'none' }
: { pointerEvents: touchModality ? 'none' : 'auto' }),
...(orientation === 'vertical' && {
top: 0,
bottom: 'var(--scroll-area-corner-height)',
Expand All @@ -147,9 +150,11 @@ export function useScrollAreaScrollbar(params: useScrollAreaScrollbar.Parameters
rootId,
handlePointerUp,
type,
touchModality,
orientation,
dir,
thumbSize,
thumbSize.height,
thumbSize.width,
viewportRef,
thumbYRef,
scrollbarYRef,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,17 +205,13 @@ export function useScrollAreaViewport(params: useScrollAreaViewport.Parameters)
if (!hiddenState.scrollbarYHidden) {
styles[dir === 'rtl' ? 'paddingLeft' : 'paddingRight'] = paddingX;
}

if (!hiddenState.scrollbarXHidden) {
styles.paddingBottom = paddingY;
}

if (hiddenState.scrollbarYHidden) {
if (gutter === 'stable') {
styles[dir === 'rtl' ? 'paddingLeft' : 'paddingRight'] = paddingX;
} else if (gutter === 'both-edges') {
styles.paddingLeft = paddingX;
styles.paddingRight = paddingX;
}
if (hiddenState.scrollbarYHidden && gutter === 'stable') {
styles[dir === 'rtl' ? 'paddingLeft' : 'paddingRight'] = paddingX;
}
}

Expand Down

0 comments on commit 1635971

Please sign in to comment.