Skip to content

Commit

Permalink
Tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
atomiks committed Nov 3, 2024
1 parent 1635971 commit 150792a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const ScrollAreaScrollbar = styled(ScrollArea.Scrollbar)`
background 0.2s,
visibility 0.2s;
opacity: 0;
display: flex;
&:hover {
background: rgb(0 0 0 / 0.1);
Expand All @@ -86,6 +87,7 @@ const ScrollAreaScrollbar = styled(ScrollArea.Scrollbar)`
}
&[data-orientation='horizontal'] {
flex-direction: column;
height: 10px;
}
Expand All @@ -104,8 +106,7 @@ const ScrollAreaScrollbar = styled(ScrollArea.Scrollbar)`
const ScrollAreaThumb = styled(ScrollArea.Thumb)`
background: rgb(0 0 0 / 0.5);
border-radius: 20px;
height: var(--scroll-area-thumb-height, 6px);
width: var(--scroll-area-thumb-width, 6px);
flex: 1;
&::before {
content: '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const ScrollAreaScrollbar = styled(ScrollArea.Scrollbar)`
background 0.2s,
visibility 0.2s;
opacity: 0;
display: flex;
&:hover {
background: rgb(0 0 0 / 0.1);
Expand All @@ -86,6 +87,7 @@ const ScrollAreaScrollbar = styled(ScrollArea.Scrollbar)`
}
&[data-orientation='horizontal'] {
flex-direction: column;
height: 10px;
}
Expand All @@ -104,8 +106,7 @@ const ScrollAreaScrollbar = styled(ScrollArea.Scrollbar)`
const ScrollAreaThumb = styled(ScrollArea.Thumb)`
background: rgb(0 0 0 / 0.5);
border-radius: 20px;
height: var(--scroll-area-thumb-height, 6px);
width: var(--scroll-area-thumb-width, 6px);
flex: 1;
&::before {
content: '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export function useScrollAreaScrollbar(params: useScrollAreaScrollbar.Parameters
handlePointerDown,
handlePointerUp,
rootId,
type,
thumbSize,
touchModality,
} = useScrollAreaRootContext();
Expand All @@ -24,6 +23,10 @@ export function useScrollAreaScrollbar(params: useScrollAreaScrollbar.Parameters
const viewportEl = viewportRef.current;
const scrollbarEl = orientation === 'vertical' ? scrollbarYRef.current : scrollbarXRef.current;

if (!scrollbarEl) {
return undefined;
}

function handleWheel(event: WheelEvent) {
if (!viewportEl || !scrollbarEl || event.ctrlKey) {
return;
Expand Down Expand Up @@ -60,10 +63,10 @@ export function useScrollAreaScrollbar(params: useScrollAreaScrollbar.Parameters
}
}

scrollbarEl?.addEventListener('wheel', handleWheel, { passive: false });
scrollbarEl.addEventListener('wheel', handleWheel, { passive: false });

return () => {
scrollbarEl?.removeEventListener('wheel', handleWheel);
scrollbarEl.removeEventListener('wheel', handleWheel);
};
}, [orientation, scrollbarXRef, scrollbarYRef, viewportRef]);

Expand Down Expand Up @@ -129,9 +132,7 @@ export function useScrollAreaScrollbar(params: useScrollAreaScrollbar.Parameters
onPointerUp: handlePointerUp,
style: {
position: 'absolute',
...(type === 'inset'
? { touchAction: 'none' }
: { pointerEvents: touchModality ? 'none' : 'auto' }),
...(touchModality && { pointerEvents: 'none' }),
...(orientation === 'vertical' && {
top: 0,
bottom: 'var(--scroll-area-corner-height)',
Expand All @@ -149,12 +150,10 @@ export function useScrollAreaScrollbar(params: useScrollAreaScrollbar.Parameters
[
rootId,
handlePointerUp,
type,
touchModality,
orientation,
dir,
thumbSize.height,
thumbSize.width,
thumbSize,
viewportRef,
thumbYRef,
scrollbarYRef,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useEventCallback } from '../../utils/useEventCallback';
import { useEnhancedEffect } from '../../utils/useEnhancedEffect';
import { mergeReactProps } from '../../utils/mergeReactProps';
import { ownerWindow } from '../../utils/owner';
import { SCROLL_TIMEOUT } from '../constants';
import { MIN_THUMB_SIZE, SCROLL_TIMEOUT } from '../constants';

export function useScrollAreaViewport(params: useScrollAreaViewport.Parameters) {
const { children } = params;
Expand Down Expand Up @@ -112,13 +112,16 @@ export function useScrollAreaViewport(params: useScrollAreaViewport.Parameters)
? 0
: (viewportHeight / scrollableContentHeight) * viewportHeight;

if (prevSize.height === height && prevSize.width === width) {
const clampedWidth = Math.max(MIN_THUMB_SIZE, width);
const clampedHeight = Math.max(MIN_THUMB_SIZE, height);

if (prevSize.height === clampedHeight && prevSize.width === clampedWidth) {
return prevSize;
}

return {
width: scrollbarXHidden ? 0 : (viewportWidth / scrollableContentWidth) * viewportWidth,
height: scrollbarYHidden ? 0 : (viewportHeight / scrollableContentHeight) * viewportHeight,
width: clampedWidth,
height: clampedHeight,
};
});

Expand Down
1 change: 1 addition & 0 deletions packages/mui-base/src/ScrollArea/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const SCROLL_TIMEOUT = 500;
export const MIN_THUMB_SIZE = 16;

0 comments on commit 150792a

Please sign in to comment.