File size: 3,622 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import clsx from 'clsx';
import { Children, ReactNode, useLayoutEffect, useState } from 'react';
import Breadcrumb, { Item as BreadcrumbItem } from 'calypso/components/breadcrumb';
import useDetectWindowBoundary from 'calypso/lib/detect-window-boundary';

type Props = {
	showStickyContent?: boolean;
	children: ReactNode;
	className?: string;
	useColumnAlignment?: boolean;
};

export function LayoutHeaderTitle( { children }: Props ) {
	return <h1 className="hosting-dashboard-layout__header-title">{ children }</h1>;
}

export function LayoutHeaderSubtitle( { children }: Props ) {
	return <h2 className="hosting-dashboard-layout__header-subtitle">{ children }</h2>;
}

export function LayoutHeaderActions( { children, className, useColumnAlignment }: Props ) {
	const wrapperClass = clsx( className, 'hosting-dashboard-layout__header-actions', {
		'is-column-flex-align': useColumnAlignment,
	} );
	return <div className={ wrapperClass }>{ children }</div>;
}

export function LayoutHeaderBreadcrumb( {
	items,
	hideOnMobile,
}: {
	items: BreadcrumbItem[];
	hideOnMobile?: boolean;
} ) {
	return (
		<div
			className={ clsx( 'hosting-dashboard-layout__header-breadcrumb', {
				'is-hidden-on-mobile': hideOnMobile,
			} ) }
		>
			<Breadcrumb items={ items } />
		</div>
	);
}

export default function LayoutHeader( { showStickyContent, children, className }: Props ) {
	const headerBreadcrumb = Children.toArray( children ).find(
		// eslint-disable-next-line @typescript-eslint/no-explicit-any
		( child: any ) => child.type === LayoutHeaderBreadcrumb
	);

	const headerTitle = Children.toArray( children ).find(
		// eslint-disable-next-line @typescript-eslint/no-explicit-any
		( child: any ) => child.type === LayoutHeaderTitle
	);

	const headerSubtitle = Children.toArray( children ).find(
		// eslint-disable-next-line @typescript-eslint/no-explicit-any
		( child: any ) => child.type === LayoutHeaderSubtitle
	);

	const headerActions = Children.toArray( children ).find(
		// eslint-disable-next-line @typescript-eslint/no-explicit-any
		( child: any ) => child.type === LayoutHeaderActions
	);

	const [ divRef, hasCrossed ] = useDetectWindowBoundary();

	const outerDivProps = divRef ? { ref: divRef as React.RefObject< HTMLDivElement > } : {};
	const wrapperClass = clsx( className, 'hosting-dashboard-layout__viewport' );

	const [ minHeaderHeight, setMinHeaderHeight ] = useState( 0 );

	// To avoid shifting the layout when displaying sticky content,  we will need to
	// keep track of our Header height and set it as the minimum viewport height.
	useLayoutEffect(
		() => {
			const headerRef = outerDivProps?.ref?.current;

			const updateMinHeaderHeight = () => {
				setMinHeaderHeight( headerRef?.clientHeight ?? 0 );
			};

			window.addEventListener( 'resize', updateMinHeaderHeight );

			updateMinHeaderHeight();

			return () => {
				window.removeEventListener( 'resize', updateMinHeaderHeight );
			};
		},
		// eslint-disable-next-line react-hooks/exhaustive-deps
		[]
	);

	return (
		<div
			className={ wrapperClass }
			{ ...outerDivProps }
			style={ showStickyContent ? { minHeight: `${ minHeaderHeight }px` } : {} }
		>
			<div
				className={ clsx( {
					'hosting-dashboard-layout__sticky-header': showStickyContent && hasCrossed,
				} ) }
			>
				<div
					className={ clsx( 'hosting-dashboard-layout__header', {
						'has-actions': !! headerActions,
					} ) }
				>
					<div className="hosting-dashboard-layout__header-main">
						{ headerBreadcrumb }
						{ headerTitle }
						{ headerSubtitle }
					</div>

					{ headerActions }
				</div>
			</div>
		</div>
	);
}