-
Notifications
You must be signed in to change notification settings - Fork 337
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 #223 from Kanaries/feat-causal-auto_vis_tab-ks-1130
[Refactor] causal store
- Loading branch information
Showing
86 changed files
with
3,896 additions
and
4,345 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
69 changes: 69 additions & 0 deletions
69
packages/rath-client/src/hooks/use-bounding-client-rect.ts
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,69 @@ | ||
import { RefObject, useEffect, useRef, useState } from "react"; | ||
|
||
|
||
export type BoundingClientRectAttributes = { | ||
/** @default true */ | ||
-readonly [key in keyof Omit<DOMRect, 'toJSON'>]?: boolean; | ||
}; | ||
|
||
/** | ||
* Updates on certain keys of DOMRect changes, detected using `ResizeObserver`. | ||
* DISCUSS: use `IntersectionObserver` with ref elements to implements position changes. | ||
*/ | ||
const useBoundingClientRect = < | ||
T extends BoundingClientRectAttributes = { -readonly [key in keyof Omit<DOMRect, 'toJSON'>]: true }, | ||
M extends { -readonly [key in keyof Omit<DOMRect, 'toJSON'>]: T[key] extends true ? key : never } = { -readonly [key in keyof Omit<DOMRect, 'toJSON'>]: T[key] extends true ? key : never }, | ||
E extends Exclude<M[keyof M], never> & keyof DOMRect = Exclude<M[keyof M], never> & keyof DOMRect, | ||
R extends { readonly [key in E]?: DOMRect[key] } = { readonly [key in E]?: DOMRect[key] }, | ||
>( | ||
ref: RefObject<HTMLElement>, | ||
/** @default {height:true,width:true,x:true,y:true,bottom:true,left:true,right:true,top:true} */ | ||
attributes: T = { | ||
height: true, | ||
width: true, | ||
x: true, | ||
y: true, | ||
bottom: true, | ||
left: true, | ||
right: true, | ||
top: true, | ||
} as T, | ||
): R => { | ||
const compareKeysRef = useRef<(keyof BoundingClientRectAttributes)[]>([]); | ||
compareKeysRef.current = (["height", "width", "x", "y", "bottom", "left", "right", "top"] as const).filter(key => { | ||
return attributes[key] === true; | ||
}); | ||
|
||
const [box, setBox] = useState<R>({} as R); | ||
|
||
const prevRectRef = useRef<DOMRect>(); | ||
const shouldReportRef = useRef<(next: DOMRect) => boolean>(() => true); | ||
shouldReportRef.current = (next: DOMRect): boolean => { | ||
return !prevRectRef.current || compareKeysRef.current.some(k => next[k] !== prevRectRef.current![k]); | ||
}; | ||
|
||
useEffect(() => { | ||
const { current: element } = ref; | ||
|
||
if (element) { | ||
const cb = () => { | ||
const rect = element.getBoundingClientRect(); | ||
if (shouldReportRef.current(rect)) { | ||
setBox(Object.fromEntries(compareKeysRef.current.map(key => [key, rect[key]])) as R); | ||
} | ||
prevRectRef.current = rect; | ||
}; | ||
const ro = new ResizeObserver(cb); | ||
ro.observe(element); | ||
cb(); | ||
return () => { | ||
ro.disconnect(); | ||
}; | ||
} | ||
}, [ref]); | ||
|
||
return box; | ||
}; | ||
|
||
|
||
export default useBoundingClientRect; |
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
Oops, something went wrong.
b2aebd0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
rath – ./
rath-kanaries.vercel.app
rath-steel.vercel.app
rath.kanaries.net
rath-git-master-kanaries.vercel.app