2021-09-17 22:24:21 +00:00
|
|
|
// Copyright 2020-2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { Ref } from 'react';
|
|
|
|
import { useEffect, useState } from 'react';
|
2021-09-17 22:24:21 +00:00
|
|
|
import { first, last, noop } from 'lodash';
|
|
|
|
|
|
|
|
function getTop(element: Readonly<Element>): number {
|
|
|
|
return element.getBoundingClientRect().top;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isWrapped(element: Readonly<null | HTMLElement>): boolean {
|
|
|
|
if (!element) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { children } = element;
|
|
|
|
const firstChild = first(children);
|
|
|
|
const lastChild = last(children);
|
|
|
|
|
|
|
|
return Boolean(
|
|
|
|
firstChild &&
|
|
|
|
lastChild &&
|
|
|
|
firstChild !== lastChild &&
|
|
|
|
getTop(firstChild) !== getTop(lastChild)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A hook that returns a ref (to put on your element) and a boolean. The boolean will be
|
|
|
|
* `true` if the element's children have different `top`s, and `false` otherwise.
|
|
|
|
*/
|
|
|
|
export function useHasWrapped<T extends HTMLElement>(): [Ref<T>, boolean] {
|
|
|
|
const [element, setElement] = useState<null | T>(null);
|
|
|
|
|
|
|
|
const [hasWrapped, setHasWrapped] = useState(isWrapped(element));
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!element) {
|
|
|
|
return noop;
|
|
|
|
}
|
|
|
|
|
2021-11-05 13:25:25 +00:00
|
|
|
const observer = new ResizeObserver(() => {
|
2021-09-17 22:24:21 +00:00
|
|
|
setHasWrapped(isWrapped(element));
|
|
|
|
});
|
|
|
|
observer.observe(element);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
observer.disconnect();
|
|
|
|
};
|
|
|
|
}, [element]);
|
|
|
|
|
|
|
|
return [setElement, hasWrapped];
|
|
|
|
}
|