feishin/src/renderer/components/page-header/index.tsx

140 lines
3.7 KiB
TypeScript
Raw Normal View History

2022-12-31 17:50:05 -08:00
import { Flex, FlexProps } from '@mantine/core';
2023-01-02 17:03:33 -08:00
import { AnimatePresence, motion, Variants } from 'framer-motion';
2023-09-15 20:42:38 -07:00
import { ReactNode, useRef } from 'react';
2022-12-19 15:59:14 -08:00
import styled from 'styled-components';
2023-01-02 18:54:48 -08:00
import { useShouldPadTitlebar, useTheme } from '/@/renderer/hooks';
2023-03-30 06:44:33 -07:00
import { useWindowSettings } from '/@/renderer/store/settings.store';
import { Platform } from '/@/renderer/types';
2022-12-19 15:59:14 -08:00
2023-01-02 17:03:33 -08:00
const Container = styled(motion(Flex))<{
2023-07-01 19:10:05 -07:00
height?: string;
position?: string;
2023-01-02 17:03:33 -08:00
}>`
2023-07-01 19:10:05 -07:00
position: ${(props) => props.position || 'relative'};
z-index: 200;
2023-07-01 19:10:05 -07:00
width: 100%;
height: ${(props) => props.height || '65px'};
background: var(--titlebar-bg);
2022-12-19 15:59:14 -08:00
`;
const Header = styled(motion.div)<{
2023-07-01 19:10:05 -07:00
$isDraggable?: boolean;
$isHidden?: boolean;
$padRight?: boolean;
}>`
2023-07-01 19:10:05 -07:00
position: relative;
z-index: 15;
width: 100%;
height: 100%;
margin-right: ${(props) => (props.$padRight ? '140px' : '1rem')};
pointer-events: ${(props) => (props.$isHidden ? 'none' : 'auto')};
2023-09-15 20:42:38 -07:00
user-select: ${(props) => (props.$isHidden ? 'none' : 'auto')};
2023-07-01 19:10:05 -07:00
-webkit-app-region: ${(props) => props.$isDraggable && 'drag'};
2022-12-19 15:59:14 -08:00
2023-07-01 19:10:05 -07:00
button {
-webkit-app-region: no-drag;
}
2022-12-21 01:27:29 -08:00
2023-07-01 19:10:05 -07:00
input {
-webkit-app-region: no-drag;
}
2022-12-19 15:59:14 -08:00
`;
2022-12-31 17:50:05 -08:00
const BackgroundImage = styled.div<{ background: string }>`
2023-07-01 19:10:05 -07:00
position: absolute;
top: 0;
z-index: 1;
width: 100%;
height: 100%;
background: ${(props) => props.background || 'var(--titlebar-bg)'};
2022-12-31 17:50:05 -08:00
`;
2022-12-29 18:50:57 -08:00
2023-01-02 18:54:48 -08:00
const BackgroundImageOverlay = styled.div<{ theme: 'light' | 'dark' }>`
2023-07-01 19:10:05 -07:00
position: absolute;
top: 0;
left: 0;
z-index: 10;
width: 100%;
height: 100%;
background: ${(props) =>
props.theme === 'light'
? 'linear-gradient(rgba(255, 255, 255, 25%), rgba(255, 255, 255, 25%))'
: 'linear-gradient(rgba(0, 0, 0, 50%), rgba(0, 0, 0, 50%))'};
2022-12-31 17:50:05 -08:00
`;
2022-12-29 18:50:57 -08:00
2023-01-02 17:03:33 -08:00
export interface PageHeaderProps
2023-07-01 19:10:05 -07:00
extends Omit<FlexProps, 'onAnimationStart' | 'onDragStart' | 'onDragEnd' | 'onDrag'> {
animated?: boolean;
2023-07-01 19:10:05 -07:00
backgroundColor?: string;
2023-09-15 20:42:38 -07:00
children?: ReactNode;
2023-07-01 19:10:05 -07:00
height?: string;
isHidden?: boolean;
position?: string;
2022-12-19 15:59:14 -08:00
}
2023-01-02 17:03:33 -08:00
const TitleWrapper = styled(motion.div)`
2023-07-01 19:10:05 -07:00
position: absolute;
display: flex;
width: 100%;
height: 100%;
2023-01-02 17:03:33 -08:00
`;
const variants: Variants = {
animate: {
opacity: 1,
transition: {
duration: 0.3,
ease: 'easeIn',
},
},
2023-07-01 19:10:05 -07:00
exit: { opacity: 0 },
initial: { opacity: 0 },
2023-01-02 17:03:33 -08:00
};
2022-12-29 18:50:57 -08:00
export const PageHeader = ({
animated,
2023-07-01 19:10:05 -07:00
position,
height,
backgroundColor,
isHidden,
children,
...props
2022-12-29 18:50:57 -08:00
}: PageHeaderProps) => {
2023-07-01 19:10:05 -07:00
const ref = useRef(null);
const padRight = useShouldPadTitlebar();
const { windowBarStyle } = useWindowSettings();
const theme = useTheme();
2022-12-19 15:59:14 -08:00
2023-07-01 19:10:05 -07:00
return (
<Container
ref={ref}
height={height}
position={position}
{...props}
>
<Header
$isDraggable={windowBarStyle === Platform.WEB}
$isHidden={isHidden}
$padRight={padRight}
2023-01-02 17:03:33 -08:00
>
<AnimatePresence initial={animated ?? false}>
<TitleWrapper
animate="animate"
exit="exit"
initial="initial"
variants={variants}
>
{children}
</TitleWrapper>
2023-07-01 19:10:05 -07:00
</AnimatePresence>
</Header>
{backgroundColor && (
<>
<BackgroundImage background={backgroundColor || 'var(--titlebar-bg)'} />
2023-09-15 20:42:38 -07:00
<BackgroundImageOverlay theme={theme as 'light' | 'dark'} />
2023-07-01 19:10:05 -07:00
</>
)}
</Container>
);
2022-12-19 15:59:14 -08:00
};