-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29546 from pasyukevich/feature/migrate-with-view-…
…top-offset [No QA] [TS migration] Migrate 'withViewportOffsetTop.js' HOC to TypeScript
- Loading branch information
Showing
4 changed files
with
46 additions
and
65 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import React, {useEffect, forwardRef, useState, ComponentType, RefAttributes, ForwardedRef} from 'react'; | ||
import getComponentDisplayName from '../libs/getComponentDisplayName'; | ||
import addViewportResizeListener from '../libs/VisualViewport'; | ||
|
||
type ViewportOffsetTopProps = { | ||
// viewportOffsetTop returns the offset of the top edge of the visual viewport from the | ||
// top edge of the layout viewport in CSS pixels, when the visual viewport is resized. | ||
viewportOffsetTop: number; | ||
}; | ||
|
||
export default function withViewportOffsetTop<TProps extends ViewportOffsetTopProps, TRef>(WrappedComponent: ComponentType<TProps & RefAttributes<TRef>>) { | ||
function WithViewportOffsetTop(props: Omit<TProps, keyof ViewportOffsetTopProps>, ref: ForwardedRef<TRef>) { | ||
const [viewportOffsetTop, setViewportOffsetTop] = useState(0); | ||
|
||
useEffect(() => { | ||
const updateDimensions = (event: Event) => { | ||
const targetOffsetTop = (event.target instanceof VisualViewport && event.target.offsetTop) || 0; | ||
setViewportOffsetTop(targetOffsetTop); | ||
}; | ||
|
||
const removeViewportResizeListener = addViewportResizeListener(updateDimensions); | ||
|
||
return () => { | ||
removeViewportResizeListener(); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<WrappedComponent | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...(props as TProps)} | ||
ref={ref} | ||
viewportOffsetTop={viewportOffsetTop} | ||
/> | ||
); | ||
} | ||
|
||
WithViewportOffsetTop.displayName = `WithViewportOffsetTop(${getComponentDisplayName(WrappedComponent)})`; | ||
|
||
return forwardRef(WithViewportOffsetTop); | ||
} |
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