-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
132 additions
and
129 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,54 @@ | ||
'use client'; | ||
|
||
import { FC, ReactNode, useEffect, useRef, useState } from 'react'; | ||
|
||
interface InspectableBoxProps { | ||
className?: string; | ||
children: (ctx: PositionContext) => ReactNode; | ||
} | ||
|
||
interface PositionContext { | ||
width: number; | ||
height: number; | ||
x: number; | ||
y: number; | ||
} | ||
|
||
const InspectableBox: FC<InspectableBoxProps> = ({ className, children }) => { | ||
const ref = useRef<HTMLDivElement>(null); | ||
|
||
const [positionContext, setPositionContext] = useState<PositionContext | null>(null); | ||
|
||
useEffect(() => { | ||
if (!ref.current) { | ||
return; | ||
} | ||
|
||
const observer = new ResizeObserver((entires) => { | ||
if (!ref.current) { | ||
return; | ||
} | ||
|
||
for (const entry of entires) { | ||
setPositionContext({ | ||
height: entry.contentRect.height, | ||
width: entry.contentRect.width, | ||
x: ref.current.offsetLeft, | ||
y: ref.current.offsetTop, | ||
}); | ||
} | ||
}); | ||
|
||
observer.observe(ref.current); | ||
|
||
return () => observer.disconnect(); | ||
}, []); | ||
|
||
return ( | ||
<div ref={ref} className={className}> | ||
{positionContext && children(positionContext)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default InspectableBox; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { RefObject, useEffect, useState } from 'react'; | ||
|
||
interface PositionContext { | ||
width: number; | ||
height: number; | ||
x: number; | ||
y: number; | ||
} | ||
|
||
const useDomInspect = (ref: RefObject<HTMLElement>) => { | ||
const [positionContext, setPositionContext] = useState<PositionContext>({ | ||
height: 0, | ||
width: 0, | ||
x: 0, | ||
y: 0, | ||
}); | ||
|
||
useEffect(() => { | ||
if (!ref.current) { | ||
return; | ||
} | ||
|
||
const observer = new ResizeObserver((entries) => { | ||
if (!ref.current) { | ||
return; | ||
} | ||
|
||
for (const entry of entries) { | ||
setPositionContext({ | ||
height: entry.contentRect.height, | ||
width: entry.contentRect.width, | ||
x: ref.current.offsetLeft, | ||
y: ref.current.offsetTop, | ||
}); | ||
} | ||
}); | ||
observer.observe(ref.current); | ||
return () => { | ||
observer.disconnect(); | ||
}; | ||
}, [ref]); | ||
|
||
return positionContext; | ||
}; | ||
|
||
export default useDomInspect; |