You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.9 KiB
TypeScript

// Copyright (C) 2021 Intel Corporation
//
// SPDX-License-Identifier: MIT
import { useRef, useEffect, useState } from 'react';
// eslint-disable-next-line import/prefer-default-export
export function usePrevious<T>(value: T): T | undefined {
const ref = useRef<T>();
useEffect(() => {
ref.current = value;
});
return ref.current;
}
export interface ICardHeightHOC {
numberOfRows: number;
paddings: number;
containerClassName: string;
siblingClassNames: string[];
}
export function useCardHeightHOC(params: ICardHeightHOC): () => string {
const {
numberOfRows, paddings, containerClassName, siblingClassNames,
} = params;
return (): string => {
const [height, setHeight] = useState('auto');
useEffect(() => {
const resize = (): void => {
const container = window.document.getElementsByClassName(containerClassName)[0];
const siblings = siblingClassNames.map(
(classname: string): Element | undefined => window.document.getElementsByClassName(classname)[0],
);
if (container) {
const { clientHeight: containerHeight } = container;
const othersHeight = siblings.reduce<number>((acc: number, el: Element | undefined): number => {
if (el) {
return acc + el.clientHeight;
}
return acc;
}, 0);
const cardHeight = (containerHeight - (othersHeight + paddings)) / numberOfRows;
setHeight(`${Math.round(cardHeight)}px`);
}
};
resize();
window.addEventListener('resize', resize);
return () => {
window.removeEventListener('resize', resize);
};
}, []);
return height;
};
}