-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MenuRenderer, OverflowMenu: Provide improved scroll affordance (#1661)
- Loading branch information
1 parent
6a118bc
commit ca2e854
Showing
7 changed files
with
242 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
'braid-design-system': patch | ||
--- | ||
|
||
--- | ||
updated: | ||
- MenuRenderer | ||
- OverflowMenu | ||
--- | ||
|
||
**MenuRenderer, OverflowMenu:** Provide improved scroll affordance | ||
|
||
Introduce scroll affordance to menus, providing a visual cue that there are more items overflowing vertically. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...ges/braid-design-system/src/lib/components/private/ScrollContainer/ScrollContainer.css.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { | ||
createVar, | ||
fallbackVar, | ||
style, | ||
styleVariants, | ||
} from '@vanilla-extract/css'; | ||
|
||
export const container = style({ | ||
WebkitOverflowScrolling: 'touch', | ||
maskComposite: 'intersect', | ||
}); | ||
|
||
export const hideScrollbar = style({ | ||
scrollbarWidth: 'none', | ||
msOverflowStyle: 'none', | ||
'::-webkit-scrollbar': { | ||
width: 0, | ||
height: 0, | ||
}, | ||
}); | ||
|
||
const scrollOverlaySize = createVar(); | ||
export const fadeSize = styleVariants({ | ||
small: { | ||
vars: { | ||
[scrollOverlaySize]: '40px', | ||
}, | ||
}, | ||
medium: { | ||
vars: { | ||
[scrollOverlaySize]: '60px', | ||
}, | ||
}, | ||
large: { | ||
vars: { | ||
[scrollOverlaySize]: '80px', | ||
}, | ||
}, | ||
}); | ||
|
||
export const direction = styleVariants({ | ||
horizontal: { | ||
overflowX: 'auto', | ||
overflowY: 'hidden', | ||
}, | ||
vertical: { | ||
overflowX: 'hidden', | ||
overflowY: 'auto', | ||
}, | ||
all: { | ||
overflow: 'auto', | ||
}, | ||
}); | ||
|
||
const left = createVar(); | ||
const right = createVar(); | ||
const top = createVar(); | ||
const bottom = createVar(); | ||
export const mask = style({ | ||
maskImage: [ | ||
`linear-gradient(to bottom, transparent 0, black ${fallbackVar(top, '0')})`, | ||
`linear-gradient(to right, transparent 0, black ${fallbackVar(left, '0')})`, | ||
`linear-gradient(to left, transparent 0, black ${fallbackVar(right, '0')})`, | ||
`linear-gradient(to top, transparent 0, black ${fallbackVar(bottom, '0')})`, | ||
].join(','), | ||
}); | ||
|
||
export const maskLeft = style({ | ||
vars: { | ||
[left]: scrollOverlaySize, | ||
}, | ||
}); | ||
|
||
export const maskRight = style({ | ||
vars: { | ||
[right]: scrollOverlaySize, | ||
}, | ||
}); | ||
|
||
export const maskTop = style({ | ||
vars: { | ||
[top]: scrollOverlaySize, | ||
}, | ||
}); | ||
|
||
export const maskBottom = style({ | ||
vars: { | ||
[bottom]: scrollOverlaySize, | ||
}, | ||
}); |
88 changes: 88 additions & 0 deletions
88
packages/braid-design-system/src/lib/components/private/ScrollContainer/ScrollContainer.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import React, { type ReactNode, useRef, useCallback } from 'react'; | ||
import { useIsomorphicLayoutEffect } from '../../../hooks/useIsomorphicLayoutEffect'; | ||
import { Box } from '../../Box/Box'; | ||
import buildDataAttributes, { | ||
type DataAttributeMap, | ||
} from '../buildDataAttributes'; | ||
import { throttle } from 'throttle-debounce'; | ||
|
||
import * as styles from './ScrollContainer.css'; | ||
|
||
const maskOverflow = ( | ||
element: HTMLElement, | ||
direction: keyof typeof styles.direction, | ||
) => { | ||
const atTop = element.scrollTop === 0; | ||
const atBottom = | ||
element.scrollHeight - element.offsetHeight - element.scrollTop < 1; | ||
const atLeft = element.scrollLeft === 0; | ||
const atRight = | ||
element.scrollWidth - element.offsetWidth - element.scrollLeft < 1; | ||
|
||
if (direction === 'vertical' || direction === 'all') { | ||
element.classList[atTop ? 'remove' : 'add'](styles.maskTop); | ||
element.classList[atBottom ? 'remove' : 'add'](styles.maskBottom); | ||
} | ||
if (direction === 'horizontal' || direction === 'all') { | ||
element.classList[atLeft ? 'remove' : 'add'](styles.maskLeft); | ||
element.classList[atRight ? 'remove' : 'add'](styles.maskRight); | ||
} | ||
}; | ||
|
||
interface ScrollContainerProps { | ||
children: ReactNode; | ||
direction?: keyof typeof styles.direction; | ||
fadeSize?: keyof typeof styles.fadeSize; | ||
hideScrollbar?: boolean; | ||
data?: DataAttributeMap; | ||
} | ||
|
||
export const ScrollContainer = ({ | ||
children, | ||
direction = 'horizontal', | ||
fadeSize = 'medium', | ||
hideScrollbar = false, | ||
data, | ||
...restProps | ||
}: ScrollContainerProps) => { | ||
const containerRef = useRef<HTMLDivElement>(null); | ||
|
||
// This must be called within a `useLayoutEffect` because `.scrollLeft`, `.scrollWidth` and `.offsetWidth` force a reflow | ||
// https://gist.github.com/paulirish/5d52fb081b3570c81e3a | ||
const updateMask = throttle( | ||
100, | ||
useCallback(() => { | ||
if (containerRef.current) { | ||
maskOverflow(containerRef.current, direction); | ||
} | ||
}, [containerRef, direction]), | ||
); | ||
|
||
useIsomorphicLayoutEffect(() => { | ||
if (containerRef.current) { | ||
updateMask(); | ||
} | ||
|
||
window.addEventListener('resize', updateMask); | ||
return () => window.removeEventListener('resize', updateMask); | ||
}, [updateMask]); | ||
|
||
return ( | ||
<Box | ||
ref={containerRef} | ||
onScroll={updateMask} | ||
position="relative" | ||
height="full" | ||
className={[ | ||
styles.container, | ||
styles.mask, | ||
hideScrollbar ? styles.hideScrollbar : null, | ||
styles.fadeSize[fadeSize], | ||
styles.direction[direction], | ||
]} | ||
{...buildDataAttributes({ data, validateRestProps: restProps })} | ||
> | ||
{children} | ||
</Box> | ||
); | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.