|
| 1 | +import { combine } from '@atlaskit/pragmatic-drag-and-drop/combine'; |
| 2 | +import { autoScrollForElements } from '@atlaskit/pragmatic-drag-and-drop-auto-scroll/element'; |
| 3 | +import { autoScrollForExternal } from '@atlaskit/pragmatic-drag-and-drop-auto-scroll/external'; |
1 | 4 | import type { ChakraProps } from '@invoke-ai/ui-library'; |
2 | 5 | import { Box, Flex } from '@invoke-ai/ui-library'; |
3 | 6 | import { getOverlayScrollbarsParams } from 'common/components/OverlayScrollbars/constants'; |
| 7 | +import type { OverlayScrollbarsComponentRef } from 'overlayscrollbars-react'; |
4 | 8 | import { OverlayScrollbarsComponent } from 'overlayscrollbars-react'; |
5 | 9 | import type { CSSProperties, PropsWithChildren } from 'react'; |
6 | | -import { memo, useMemo } from 'react'; |
| 10 | +import { memo, useEffect, useMemo, useState } from 'react'; |
7 | 11 |
|
8 | 12 | type Props = PropsWithChildren & { |
9 | 13 | maxHeight?: ChakraProps['maxHeight']; |
10 | 14 | overflowX?: 'hidden' | 'scroll'; |
11 | 15 | overflowY?: 'hidden' | 'scroll'; |
12 | 16 | }; |
13 | 17 |
|
14 | | -const styles: CSSProperties = { height: '100%', width: '100%' }; |
| 18 | +const styles: CSSProperties = { position: 'absolute', top: 0, left: 0, right: 0, bottom: 0 }; |
15 | 19 |
|
16 | 20 | const ScrollableContent = ({ children, maxHeight, overflowX = 'hidden', overflowY = 'scroll' }: Props) => { |
17 | 21 | const overlayscrollbarsOptions = useMemo( |
18 | 22 | () => getOverlayScrollbarsParams(overflowX, overflowY).options, |
19 | 23 | [overflowX, overflowY] |
20 | 24 | ); |
| 25 | + const [os, osRef] = useState<OverlayScrollbarsComponentRef | null>(null); |
| 26 | + useEffect(() => { |
| 27 | + const osInstance = os?.osInstance(); |
| 28 | + |
| 29 | + if (!osInstance) { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + const element = osInstance.elements().viewport; |
| 34 | + |
| 35 | + // `pragmatic-drag-and-drop-auto-scroll` requires the element to have `overflow-y: scroll` or `overflow-y: auto` |
| 36 | + // else it logs an ugly warning. In our case, using a custom scrollbar library, it will be 'hidden' by default. |
| 37 | + // To prevent the erroneous warning, we temporarily set the overflow-y to 'scroll' and then revert it back. |
| 38 | + const overflowY = element.style.overflowY; // starts 'hidden' |
| 39 | + element.style.setProperty('overflow-y', 'scroll', 'important'); |
| 40 | + const cleanup = combine(autoScrollForElements({ element }), autoScrollForExternal({ element })); |
| 41 | + element.style.setProperty('overflow-y', overflowY); |
| 42 | + |
| 43 | + return cleanup; |
| 44 | + }, [os]); |
| 45 | + |
21 | 46 | return ( |
22 | 47 | <Flex w="full" h="full" maxHeight={maxHeight} position="relative"> |
23 | 48 | <Box position="absolute" top={0} left={0} right={0} bottom={0}> |
24 | | - <OverlayScrollbarsComponent defer style={styles} options={overlayscrollbarsOptions}> |
| 49 | + <OverlayScrollbarsComponent ref={osRef} style={styles} options={overlayscrollbarsOptions}> |
25 | 50 | {children} |
26 | 51 | </OverlayScrollbarsComponent> |
27 | 52 | </Box> |
|
0 commit comments