-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[base, desk-tool, etc] Add scroll container for emulating scroll even…
…t bubbling
- Loading branch information
Showing
16 changed files
with
92 additions
and
84 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 @@ | ||
export * from './lib/components/ScrollContainer' |
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,70 @@ | ||
import React from 'react' | ||
|
||
type ScrollEventHandler = (event: Event) => void | ||
interface ScrollContextValue { | ||
onScroll?: ScrollEventHandler | ||
} | ||
|
||
export const Context = React.createContext<ScrollContextValue>({}) | ||
|
||
export function ScrollMonitor({ | ||
onScroll, | ||
children | ||
}: { | ||
onScroll: ScrollEventHandler | ||
children?: React.ReactNode | ||
}) { | ||
const parentContext = React.useContext(Context) | ||
const handleScroll = React.useCallback( | ||
(event: Event) => { | ||
onScroll(event) | ||
if (parentContext.onScroll) { | ||
parentContext.onScroll(event) | ||
} | ||
}, | ||
[parentContext, onScroll] | ||
) | ||
|
||
return <Context.Provider value={{onScroll: handleScroll}}>{children}</Context.Provider> | ||
} | ||
|
||
/** | ||
* This provides a utility function for use within Sanity Studios to create scrollable containers | ||
* Note: this is used by different studio utilities to track positions of elements on screen | ||
* Note: It will call any given `onScroll` callback with a Native DOM Event, and not a React Synthetic event | ||
* Note: It will not make sure the element is actually scrollable, this still needs to be done with css as usual | ||
*/ | ||
export function ScrollContainer<T extends React.ElementType = 'div'>({ | ||
onScroll, | ||
as = 'div', | ||
...props | ||
}: React.ComponentProps<T> & { | ||
onScroll?: ScrollEventHandler | ||
as?: T | ||
}) { | ||
const containerRef = React.useRef<HTMLDivElement>(null) | ||
const parentContext = React.useContext(Context) | ||
|
||
const handleScroll = React.useCallback( | ||
(event: Event) => { | ||
if (onScroll) { | ||
onScroll(event) | ||
} | ||
if (parentContext.onScroll) { | ||
parentContext.onScroll(event) | ||
} | ||
}, | ||
[onScroll] | ||
) | ||
|
||
React.useEffect(() => { | ||
containerRef.current!.addEventListener('scroll', handleScroll, { | ||
passive: true | ||
}) | ||
return () => { | ||
containerRef.current!.removeEventListener('scroll', handleScroll) | ||
} | ||
}, [onScroll]) | ||
|
||
return React.createElement(as, {ref: containerRef, ...props}) | ||
} |
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
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
4 changes: 0 additions & 4 deletions
4
packages/@sanity/components/src/utilities/ScrollContainer.css
This file was deleted.
Oops, something went wrong.
64 changes: 0 additions & 64 deletions
64
packages/@sanity/components/src/utilities/ScrollContainer.tsx
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,4 +62,5 @@ | |
min-height: 0; | ||
padding: var(--large-padding) var(--medium-padding); | ||
overflow: auto; | ||
position: relative; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ | |
|
||
.documentScroller { | ||
height: 100%; | ||
position: relative; | ||
|
||
@media (--screen-medium) { | ||
height: 100%; | ||
|
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