1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-26 20:39:08 +01:00
metamask-extension/ui/components/component-library/header-base/header-base.tsx
Garrett Bear ed5b78d61b
Feat/add header base component (#18043)
* add header base component

* fix resizing issue

* add center

* add demo

* header base using flexbox

* fix button issue

* header base clean up

* update tests

* add readme description

* add docs

* update snapshot

* add more to readme

* convert to TS

* fix file name

* fix types and colors

* fix classname error

* fix boxprops import

* fix boxprops

* prop fix

* fix errors

* Update ui/components/component-library/header-base/header-base.stories.tsx

Co-authored-by: George Marshall <george.marshall@consensys.net>

* Update ui/components/component-library/header-base/header-base.types.ts

Co-authored-by: George Marshall <george.marshall@consensys.net>

* Update ui/components/component-library/header-base/header-base.types.ts

Co-authored-by: George Marshall <george.marshall@consensys.net>

* headerbase fixes

* fix export

* remove Math.max

* change order for index on storybook to prep build

* revert back to order

* remove type from export

* add type to export

* change export of headerbase function

* export update

* revert back to normal

* add type to export

* Removing interface export from index

---------

Co-authored-by: George Marshall <george.marshall@consensys.net>
2023-03-23 08:24:23 -07:00

118 lines
3.2 KiB
TypeScript

import React, { useRef, useLayoutEffect, useMemo, useState } from 'react';
import classnames from 'classnames';
import {
BLOCK_SIZES,
DISPLAY,
JustifyContent,
} from '../../../helpers/constants/design-system';
import Box from '../../ui/box';
import { HeaderBaseProps } from './header-base.types';
export const HeaderBase: React.FC<HeaderBaseProps> = ({
startAccessory,
endAccessory,
className = '',
children,
childrenWrapperProps,
startAccessoryWrapperProps,
endAccessoryWrapperProps,
...props
}) => {
const startAccessoryRef = useRef<HTMLDivElement>(null);
const endAccessoryRef = useRef<HTMLDivElement>(null);
const [accessoryMinWidth, setAccessoryMinWidth] = useState<number>();
useLayoutEffect(() => {
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);
}
}
handleResize();
window.addEventListener('resize', handleResize);
return () => {
window.removeEventListener('resize', handleResize);
};
}, [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]);
return (
<Box
className={classnames('mm-header-base', className)}
display={DISPLAY.FLEX}
justifyContent={JustifyContent.spaceBetween}
{...props}
>
{startAccessory && (
<Box
className="mm-header-base__start-accessory"
ref={startAccessoryRef}
style={
children
? {
minWidth: `${accessoryMinWidth}px`,
}
: undefined
}
{...startAccessoryWrapperProps}
>
{startAccessory}
</Box>
)}
{children && (
<Box
className="mm-header-base__children"
width={BLOCK_SIZES.FULL}
style={getTitleStyles}
{...childrenWrapperProps}
>
{children}
</Box>
)}
{endAccessory && (
<Box
display={DISPLAY.FLEX}
justifyContent={JustifyContent.flexEnd}
className="mm-header-base__end-accessory"
ref={endAccessoryRef}
style={
children
? {
minWidth: `${accessoryMinWidth}px`,
}
: undefined
}
{...endAccessoryWrapperProps}
>
{endAccessory}
</Box>
)}
</Box>
);
};