ghost-portal/packages/react-hooks/src/useElementPosition.ts
MiZiet b6c827a908
Remove sui popup (#5924)
* Add custom popup

* Render popup window with react portal

* Handle scroll

* Handle pointer scroll

* Refactor types

* Add useOutsideClick

* Restore button styles

* Update types

* Add tests

* Set multiple refs

* Cleanup

* Cleanup

* Move offset const to the utils

* Add useCallback in useScroll

* Remove empty lines in useElementPosition

* Change styled components to functional

* Make sure popup window stays in view

* Highlight trigger button when popup is open

* Add uesPopupWindow hook

* Make triangle larger

* Use full trigger height

* Apply new lint rules

Co-authored-by: Jaco Greeff <jacogr@gmail.com>
2021-08-31 09:35:33 +02:00

36 lines
951 B
TypeScript

// Copyright 2017-2021 @polkadot/react-hooks authors & contributors
// SPDX-License-Identifier: Apache-2.0
import React, { useEffect, useState } from 'react';
import { useScroll } from '@polkadot/react-hooks/useScroll';
import { useWindowSize } from '@polkadot/react-hooks/useWindowSize';
export interface ElementPosition {
x: number,
y: number,
width: number,
height: number,
}
export function useElementPosition (ref: React.MutableRefObject<HTMLElement | undefined | null>): ElementPosition | undefined {
const [elementPosition, setElementPosition] = useState<ElementPosition>();
const windowSize = useWindowSize();
const scrollY = useScroll();
useEffect(() => {
if (ref && ref.current) {
const { height, width, x, y } = ref.current?.getBoundingClientRect();
setElementPosition({
height,
width,
x,
y
});
}
}, [ref, scrollY, windowSize]);
return elementPosition;
}