-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathnavigation-header.tsx
97 lines (91 loc) · 2.87 KB
/
navigation-header.tsx
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
import clsx from 'clsx';
import { translate } from 'i18n-calypso';
import { ReactNode } from 'react';
import './navigation-header.scss';
// Type definitions for the props
interface BackLinkProps {
url?: string;
text?: string;
onBackClick?: ( e: React.MouseEvent< HTMLAnchorElement > ) => void;
}
interface HeaderProps extends React.HTMLAttributes< HTMLElement > {
title?: string;
titleLogo?: ReactNode;
backLinkProps?: BackLinkProps;
titleElement?: ReactNode;
headElement?: ReactNode;
rightSection?: ReactNode;
hasScreenOptionsTab?: boolean;
titleProps?: {
title?: string;
titleLogo?: ReactNode;
};
}
/**
* Header component that can be used in various contexts
* @param props - Component props
* @param props.className - Additional CSS class name for the component
* @param props.titleProps - Header title props
* @param props.backLinkProps - Object containing back link properties (backLink URL, backLinkText, and onBackClick handler)
* @param props.titleElement - Custom element to override default title rendering
* @param props.headElement - Custom element to override default head section rendering
* @param props.rightSection - Child elements to render in the right section
* @param props.hasScreenOptionsTab - Indicates whether the screen options tab should be added
* @returns The rendered NavigationHeader component
*/
const NavigationHeader: React.FC< HeaderProps > = ( {
className,
titleProps,
backLinkProps,
titleElement,
headElement = backLinkProps?.url && (
<a
className="calypso-navigation-header__back-link"
href={ backLinkProps?.url }
onClick={ ( e ) => {
if ( backLinkProps?.onBackClick ) {
e.preventDefault();
backLinkProps.onBackClick( e );
}
} }
>
← { backLinkProps?.text ?? translate( 'Back' ) }
</a>
),
rightSection,
hasScreenOptionsTab,
...rest
} ) => {
const defaultTitleElement = (
<h1 className="calypso-navigation-header__title">
{ titleProps?.titleLogo && (
<span className="calypso-navigation-header__title-logo" aria-hidden="true">
{ titleProps.titleLogo }
</span>
) }
{ titleProps?.title && titleProps?.titleLogo ? (
<span className="calypso-navigation-header__title-text">{ titleProps?.title }</span>
) : (
titleProps?.title
) }
</h1>
);
const finalTitleElement = titleElement ?? defaultTitleElement;
return (
<header
className={ clsx( 'calypso-navigation-header', className, {
'calypso-navigation-header__screen-options-tab': hasScreenOptionsTab,
} ) }
{ ...rest }
>
<div className="calypso-navigation-header__head">{ headElement }</div>
<div className="calypso-navigation-header__body">
<div className="calypso-navigation-header__left-section">{ finalTitleElement }</div>
{ rightSection && (
<div className="calypso-navigation-header__right-section">{ rightSection }</div>
) }
</div>
</header>
);
};
export default NavigationHeader;