2023-05-03 19:09:13 +02:00
|
|
|
import React, { useRef, useEffect, useMemo, useState } from 'react';
|
2023-03-23 16:24:23 +01:00
|
|
|
import classnames from 'classnames';
|
|
|
|
import {
|
2023-08-02 02:36:55 +02:00
|
|
|
BlockSize,
|
|
|
|
Display,
|
2023-03-23 16:24:23 +01:00
|
|
|
JustifyContent,
|
|
|
|
} from '../../../helpers/constants/design-system';
|
2023-08-02 02:36:55 +02:00
|
|
|
import { Box } from '..';
|
2023-03-23 16:24:23 +01:00
|
|
|
|
2023-08-02 02:36:55 +02:00
|
|
|
import type { PolymorphicRef, BoxProps } from '../box';
|
2023-03-23 16:24:23 +01:00
|
|
|
|
2023-08-02 02:36:55 +02:00
|
|
|
import { HeaderBaseProps, HeaderBaseComponent } from './header-base.types';
|
2023-03-23 16:24:23 +01:00
|
|
|
|
2023-08-02 02:36:55 +02:00
|
|
|
export const HeaderBase: HeaderBaseComponent = React.forwardRef(
|
|
|
|
<C extends React.ElementType = 'div'>(
|
|
|
|
{
|
|
|
|
startAccessory,
|
|
|
|
endAccessory,
|
|
|
|
className = '',
|
|
|
|
children,
|
|
|
|
childrenWrapperProps,
|
|
|
|
startAccessoryWrapperProps,
|
|
|
|
endAccessoryWrapperProps,
|
|
|
|
...props
|
|
|
|
}: HeaderBaseProps<C>,
|
|
|
|
ref?: PolymorphicRef<C>,
|
|
|
|
) => {
|
|
|
|
const startAccessoryRef = useRef<HTMLDivElement>(null);
|
|
|
|
const endAccessoryRef = useRef<HTMLDivElement>(null);
|
|
|
|
const [accessoryMinWidth, setAccessoryMinWidth] = useState<number>();
|
2023-03-23 16:24:23 +01:00
|
|
|
|
2023-08-02 02:36:55 +02:00
|
|
|
useEffect(() => {
|
|
|
|
function handleResize() {
|
|
|
|
if (startAccessoryRef.current && endAccessoryRef.current) {
|
|
|
|
const accMinWidth = Math.max(
|
|
|
|
startAccessoryRef.current.scrollWidth,
|
|
|
|
endAccessoryRef.current.scrollWidth,
|
|
|
|
);
|
|
|
|
setAccessoryMinWidth(accMinWidth);
|
|
|
|
} else if (startAccessoryRef.current && !endAccessoryRef.current) {
|
|
|
|
setAccessoryMinWidth(startAccessoryRef.current.scrollWidth);
|
|
|
|
} else if (!startAccessoryRef.current && endAccessoryRef.current) {
|
|
|
|
setAccessoryMinWidth(endAccessoryRef.current.scrollWidth);
|
|
|
|
} else {
|
|
|
|
setAccessoryMinWidth(0);
|
|
|
|
}
|
|
|
|
}
|
2023-03-23 16:24:23 +01:00
|
|
|
|
2023-08-02 02:36:55 +02:00
|
|
|
handleResize();
|
|
|
|
window.addEventListener('resize', handleResize);
|
2023-03-23 16:24:23 +01:00
|
|
|
|
2023-08-02 02:36:55 +02:00
|
|
|
return () => {
|
|
|
|
window.removeEventListener('resize', handleResize);
|
2023-03-23 16:24:23 +01:00
|
|
|
};
|
2023-08-02 02:36:55 +02:00
|
|
|
}, [startAccessoryRef, endAccessoryRef, children]);
|
|
|
|
|
|
|
|
const getTitleStyles = useMemo(() => {
|
|
|
|
if (startAccessory && !endAccessory) {
|
|
|
|
return {
|
|
|
|
marginRight: `${accessoryMinWidth}px`,
|
|
|
|
};
|
|
|
|
} else if (!startAccessory && endAccessory) {
|
|
|
|
return {
|
|
|
|
marginLeft: `${accessoryMinWidth}px`,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}, [accessoryMinWidth, startAccessory, endAccessory]);
|
2023-03-23 16:24:23 +01:00
|
|
|
|
2023-08-02 02:36:55 +02:00
|
|
|
return (
|
|
|
|
<Box
|
|
|
|
className={classnames('mm-header-base', className)}
|
|
|
|
ref={ref}
|
|
|
|
display={Display.Flex}
|
|
|
|
justifyContent={JustifyContent.spaceBetween}
|
|
|
|
{...(props as BoxProps<C>)}
|
|
|
|
>
|
|
|
|
{startAccessory && (
|
|
|
|
<Box
|
|
|
|
ref={startAccessoryRef}
|
|
|
|
style={
|
|
|
|
children
|
|
|
|
? {
|
|
|
|
minWidth: `${accessoryMinWidth}px`,
|
|
|
|
}
|
|
|
|
: undefined
|
|
|
|
}
|
|
|
|
{...(startAccessoryWrapperProps as BoxProps<'div'>)}
|
|
|
|
>
|
|
|
|
{startAccessory}
|
|
|
|
</Box>
|
|
|
|
)}
|
|
|
|
{children && (
|
|
|
|
<Box
|
|
|
|
width={BlockSize.Full}
|
|
|
|
style={getTitleStyles}
|
|
|
|
{...(childrenWrapperProps as BoxProps<'div'>)}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</Box>
|
|
|
|
)}
|
|
|
|
{endAccessory && (
|
|
|
|
<Box
|
|
|
|
display={Display.Flex}
|
|
|
|
justifyContent={JustifyContent.flexEnd}
|
|
|
|
ref={endAccessoryRef}
|
|
|
|
style={
|
|
|
|
children
|
|
|
|
? {
|
|
|
|
minWidth: `${accessoryMinWidth}px`,
|
|
|
|
}
|
|
|
|
: undefined
|
|
|
|
}
|
|
|
|
{...(endAccessoryWrapperProps as BoxProps<'div'>)}
|
|
|
|
>
|
|
|
|
{endAccessory}
|
|
|
|
</Box>
|
|
|
|
)}
|
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|