-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(StickyPlaceholder): update placeholder dimensions on children up…
…dates Fix #28
- Loading branch information
1 parent
e4467ce
commit 7210254
Showing
4 changed files
with
194 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import * as React from 'react'; | ||
import { IRect } from 'react-viewport-utils'; | ||
|
||
interface IElementResizeObserverProps { | ||
stickyRef: React.RefObject<any>; | ||
onUpdate: (rect: IRect) => void; | ||
} | ||
|
||
class ElementResizeObserver extends React.PureComponent< | ||
IElementResizeObserverProps | ||
> { | ||
private resizeObserver: ResizeObserver | null; | ||
constructor(props: IElementResizeObserverProps) { | ||
super(props); | ||
this.resizeObserver = null; | ||
} | ||
|
||
componentDidMount() { | ||
this.resetObserver(); | ||
this.installObserver(); | ||
} | ||
|
||
componentDidUpdate() { | ||
this.resetObserver(); | ||
this.installObserver(); | ||
} | ||
|
||
componentWillUnmount() { | ||
this.resetObserver(); | ||
} | ||
|
||
installObserver() { | ||
if (!this.props.stickyRef.current) { | ||
return; | ||
} | ||
if (typeof window.ResizeObserver !== 'undefined') { | ||
this.resizeObserver = new window.ResizeObserver(entries => { | ||
if (entries && entries[0] && entries[0].contentRect) { | ||
this.props.onUpdate(entries[0].contentRect); | ||
} | ||
}); | ||
this.resizeObserver!.observe(this.props.stickyRef.current); | ||
} | ||
} | ||
|
||
resetObserver() { | ||
if (this.resizeObserver) { | ||
this.resizeObserver.disconnect(); | ||
this.resizeObserver = null; | ||
} | ||
} | ||
|
||
render(): null { | ||
return null; | ||
} | ||
} | ||
|
||
export default ElementResizeObserver; |
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// @see https://gist.github.com/strothj/708afcf4f01dd04de8f49c92e88093c3 | ||
interface Window { | ||
ResizeObserver: ResizeObserver; | ||
} | ||
|
||
/** | ||
* The ResizeObserver interface is used to observe changes to Element's content | ||
* rect. | ||
* | ||
* It is modeled after MutationObserver and IntersectionObserver. | ||
*/ | ||
interface ResizeObserver { | ||
new (callback: ResizeObserverCallback): ResizeObserver; | ||
|
||
/** | ||
* Adds target to the list of observed elements. | ||
*/ | ||
observe: (target: Element) => void; | ||
|
||
/** | ||
* Removes target from the list of observed elements. | ||
*/ | ||
unobserve: (target: Element) => void; | ||
|
||
/** | ||
* Clears both the observationTargets and activeTargets lists. | ||
*/ | ||
disconnect: () => void; | ||
} | ||
|
||
/** | ||
* This callback delivers ResizeObserver's notifications. It is invoked by a | ||
* broadcast active observations algorithm. | ||
*/ | ||
interface ResizeObserverCallback { | ||
(entries: ResizeObserverEntry[], observer: ResizeObserver): void; | ||
} | ||
|
||
interface ResizeObserverEntry { | ||
/** | ||
* @param target The Element whose size has changed. | ||
*/ | ||
new (target: Element): ResizeObserverEntry; | ||
|
||
/** | ||
* The Element whose size has changed. | ||
*/ | ||
readonly target: Element; | ||
|
||
/** | ||
* Element's content rect when ResizeObserverCallback is invoked. | ||
*/ | ||
readonly contentRect: DOMRectReadOnly; | ||
} | ||
|
||
interface DOMRectReadOnly { | ||
readonly x: number; | ||
readonly y: number; | ||
readonly width: number; | ||
readonly height: number; | ||
readonly top: number; | ||
readonly right: number; | ||
readonly bottom: number; | ||
readonly left: number; | ||
|
||
toJSON: () => any; | ||
} |