-
Notifications
You must be signed in to change notification settings - Fork 392
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(base): expose ScrollContainer as package export in addition …
…to part
- Loading branch information
Showing
16 changed files
with
123 additions
and
14 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
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
2 changes: 1 addition & 1 deletion
2
packages/@sanity/base/src/change-indicators/overlay/ConnectorsOverlay.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
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './formField' | ||
export * from './scroll' | ||
export * from './transitional' | ||
export * from './UserAvatar' | ||
export * from './zOffsets' |
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,10 @@ | ||
import React from 'react' | ||
import {ScrollContext} from './scrollContext' | ||
import {Subscriber} from 'nano-pubsub' | ||
|
||
export function useOnScroll(callback: Subscriber<Event>) { | ||
const parentContext = React.useContext(ScrollContext) | ||
React.useEffect(() => { | ||
return parentContext?.subscribe(callback) | ||
}, [callback]) | ||
} |
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,4 @@ | ||
export * from './hooks' | ||
export * from './scrollContainer' | ||
export * from './scrollMonitor' | ||
export * from './types' |
75 changes: 75 additions & 0 deletions
75
packages/@sanity/base/src/components/scroll/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,75 @@ | ||
import React from 'react' | ||
import createPubSub from 'nano-pubsub' | ||
import {ScrollContext} from './scrollContext' | ||
|
||
interface ScrollContainerProps<T extends React.ElementType> | ||
extends Omit<React.HTMLProps<T>, 'as' | 'onScroll'> { | ||
as?: React.ElementType | keyof JSX.IntrinsicElements | ||
onScroll?: (event: Event) => () => void | ||
} | ||
|
||
const noop = () => undefined | ||
|
||
/** | ||
* This provides a utility function for use within Sanity Studios to create scrollable containers | ||
* It also provides a way for components inside a scrollable container to track onScroll on their first parent scroll container | ||
* 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 const ScrollContainer = React.forwardRef(function ScrollContainer< | ||
T extends React.ElementType = 'div' | ||
>(props: ScrollContainerProps<T>, forwardedRef) { | ||
const {as = 'div', onScroll, ...rest} = props | ||
|
||
const selfRef = React.useRef<HTMLElement | null>(null) | ||
const parentContext = React.useContext(ScrollContext) | ||
const childContext = React.useMemo(() => createPubSub<Event>(), []) | ||
|
||
const handleScroll = React.useCallback( | ||
(event: Event) => { | ||
childContext.publish(event) | ||
}, | ||
[childContext] | ||
) | ||
|
||
React.useEffect(() => { | ||
if (onScroll) { | ||
// emit scroll events from children | ||
return childContext.subscribe(onScroll) | ||
} | ||
return noop | ||
}, [childContext, onScroll]) | ||
|
||
React.useEffect(() => { | ||
// let events bubble up | ||
if (parentContext) { | ||
return childContext.subscribe(parentContext.publish) | ||
} | ||
return noop | ||
}, [parentContext, childContext]) | ||
|
||
React.useEffect(() => { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
selfRef.current!.addEventListener('scroll', handleScroll, { | ||
passive: true, | ||
}) | ||
|
||
return () => { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
selfRef.current!.removeEventListener('scroll', handleScroll) | ||
} | ||
}, [handleScroll]) | ||
|
||
const setRef = (el: HTMLElement | null) => { | ||
selfRef.current = el | ||
if (typeof forwardedRef === 'function') forwardedRef(el) | ||
else if (forwardedRef && typeof forwardedRef === 'object') forwardedRef.current = el | ||
} | ||
|
||
return ( | ||
<ScrollContext.Provider value={childContext}> | ||
{React.createElement(as, {ref: setRef, ...rest})} | ||
</ScrollContext.Provider> | ||
) | ||
}) |
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,4 @@ | ||
import React from 'react' | ||
import {PubSub} from 'nano-pubsub' | ||
|
||
export const ScrollContext = React.createContext<null | PubSub<Event>>(null) |
13 changes: 13 additions & 0 deletions
13
packages/@sanity/base/src/components/scroll/scrollMonitor.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,13 @@ | ||
import React from 'react' | ||
import {useOnScroll} from './hooks' | ||
import {ScrollEventHandler} from './types' | ||
|
||
interface ScrollMonitorProps { | ||
onScroll: ScrollEventHandler | ||
children?: React.ReactNode | ||
} | ||
|
||
export function ScrollMonitor({onScroll, children}: ScrollMonitorProps) { | ||
useOnScroll(onScroll) | ||
return <>{children}</> | ||
} |
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,5 @@ | ||
export type ScrollEventHandler = (event: Event) => void | ||
|
||
export interface ScrollContextValue { | ||
onScroll?: ScrollEventHandler | ||
} |
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
3 changes: 1 addition & 2 deletions
3
packages/@sanity/desk-tool/src/panes/documentPane/documentPanel/documentPanel.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
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