Skip to content

Commit

Permalink
[components] Refactor scroll components
Browse files Browse the repository at this point in the history
  • Loading branch information
mariuslundgard authored and rexxars committed Oct 6, 2020
1 parent ac07157 commit e21e1cb
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 31 deletions.
35 changes: 4 additions & 31 deletions packages/@sanity/components/src/scroll/ScrollContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,6 @@
import React, {createElement, forwardRef, useCallback, useEffect, useContext, useRef} 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>
}
import React, {createElement, forwardRef, useCallback, useEffect, useRef} from 'react'
import {useScroll} from './hooks'
import {ScrollEventHandler} from './types'

interface ScrollContainerProps {
as?: React.ElementType | keyof JSX.IntrinsicElements
Expand All @@ -44,7 +17,7 @@ export const ScrollContainer = forwardRef(
(props: ScrollContainerProps & Omit<React.HTMLProps<HTMLElement>, 'onScroll'>, ref) => {
const {as = 'div', onScroll, ...restProps} = props
const rootRef = useRef<HTMLElement | null>(null)
const parentContext = useContext(Context)
const parentContext = useScroll()

const handleScroll = useCallback(
(event: Event) => {
Expand Down
6 changes: 6 additions & 0 deletions packages/@sanity/components/src/scroll/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {useContext} from 'react'
import {ScrollContext} from './scrollContext'

export function useScroll() {
return useContext(ScrollContext)
}
3 changes: 3 additions & 0 deletions packages/@sanity/components/src/scroll/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export * from './hooks'
export * from './ScrollContainer'
export * from './scrollMonitor'
export * from './types'
4 changes: 4 additions & 0 deletions packages/@sanity/components/src/scroll/scrollContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {createContext} from 'react'
import {ScrollContextValue} from './types'

export const ScrollContext = createContext<ScrollContextValue>({})
28 changes: 28 additions & 0 deletions packages/@sanity/components/src/scroll/scrollMonitor.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React, {useCallback} from 'react'
import {useScroll} from './hooks'
import {ScrollContext} from './scrollContext'
import {ScrollEventHandler} from './types'

interface ScrollMonitorProps {
onScroll: ScrollEventHandler
children?: React.ReactNode
}

export function ScrollMonitor({onScroll, children}: ScrollMonitorProps) {
const parentContext = useScroll()

const handleScroll = useCallback(
(event: Event) => {
onScroll(event)

if (parentContext.onScroll) {
parentContext.onScroll(event)
}
},
[parentContext, onScroll]
)

return (
<ScrollContext.Provider value={{onScroll: handleScroll}}>{children}</ScrollContext.Provider>
)
}
5 changes: 5 additions & 0 deletions packages/@sanity/components/src/scroll/types.ts
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
}

0 comments on commit e21e1cb

Please sign in to comment.