|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +import type { ReactNode } from 'react'; |
| 11 | +import React, { useMemo } from 'react'; |
| 12 | +import { useEuiTheme } from '@elastic/eui'; |
| 13 | +import { css } from '@emotion/react'; |
| 14 | +import { COLLAPSED_WIDTH, EXPANDED_WIDTH } from '@kbn/ui-side-navigation'; |
| 15 | +import { useSideNavWidth } from '@kbn/core-chrome-browser-hooks'; |
| 16 | + |
| 17 | +const GLOBAL_HEADER_HEIGHT_PX = 48; |
| 18 | + |
| 19 | +const logoSlot = css({ |
| 20 | + display: 'flex', |
| 21 | + alignItems: 'center', |
| 22 | + justifyContent: 'center', |
| 23 | + width: 'var(--logo-width)', |
| 24 | + height: GLOBAL_HEADER_HEIGHT_PX, |
| 25 | + flexShrink: 0, |
| 26 | +}); |
| 27 | + |
| 28 | +export interface ChromeNextGlobalHeaderShellProps { |
| 29 | + logo?: ReactNode; |
| 30 | + switcher?: ReactNode; |
| 31 | + search?: ReactNode; |
| 32 | + help?: ReactNode; |
| 33 | + actions?: ReactNode; |
| 34 | + userMenu?: ReactNode; |
| 35 | +} |
| 36 | + |
| 37 | +const useGlobalHeaderStyles = () => { |
| 38 | + const { euiTheme } = useEuiTheme(); |
| 39 | + |
| 40 | + return useMemo(() => { |
| 41 | + const root = css` |
| 42 | + display: flex; |
| 43 | + align-items: center; |
| 44 | + height: ${GLOBAL_HEADER_HEIGHT_PX}px; |
| 45 | + box-sizing: border-box; |
| 46 | + padding: 0 ${euiTheme.size.s} 0 0; |
| 47 | + background: ${euiTheme.colors.backgroundTransparent}; |
| 48 | + `; |
| 49 | + |
| 50 | + const leftGroup = css` |
| 51 | + display: flex; |
| 52 | + align-items: center; |
| 53 | + flex-shrink: 0; |
| 54 | + `; |
| 55 | + |
| 56 | + const switcherSlot = css` |
| 57 | + display: flex; |
| 58 | + align-items: center; |
| 59 | + gap: ${euiTheme.size.xs}; |
| 60 | + margin-inline-end: ${euiTheme.size.xs}; |
| 61 | + `; |
| 62 | + |
| 63 | + const spacer = css` |
| 64 | + flex: 1 1 auto; |
| 65 | + min-width: 0; |
| 66 | + `; |
| 67 | + |
| 68 | + const rightGroup = css` |
| 69 | + display: flex; |
| 70 | + align-items: center; |
| 71 | + flex-shrink: 0; |
| 72 | + gap: ${euiTheme.size.s}; |
| 73 | + `; |
| 74 | + |
| 75 | + const searchSlot = css` |
| 76 | + display: flex; |
| 77 | + align-items: center; |
| 78 | + flex-shrink: 0; |
| 79 | + `; |
| 80 | + |
| 81 | + const actionsSlot = css` |
| 82 | + display: flex; |
| 83 | + align-items: center; |
| 84 | + gap: ${euiTheme.size.s}; |
| 85 | + `; |
| 86 | + |
| 87 | + const helpSlot = css` |
| 88 | + display: flex; |
| 89 | + align-items: center; |
| 90 | + `; |
| 91 | + |
| 92 | + const userMenuSlot = css` |
| 93 | + display: flex; |
| 94 | + align-items: center; |
| 95 | + `; |
| 96 | + |
| 97 | + const separator = css` |
| 98 | + width: 1px; |
| 99 | + height: 24px; |
| 100 | + flex-shrink: 0; |
| 101 | + background: ${euiTheme.colors.borderBaseSubdued}; |
| 102 | + `; |
| 103 | + |
| 104 | + return { |
| 105 | + root, |
| 106 | + leftGroup, |
| 107 | + switcherSlot, |
| 108 | + spacer, |
| 109 | + rightGroup, |
| 110 | + searchSlot, |
| 111 | + actionsSlot, |
| 112 | + helpSlot, |
| 113 | + userMenuSlot, |
| 114 | + separator, |
| 115 | + }; |
| 116 | + }, [euiTheme]); |
| 117 | +}; |
| 118 | + |
| 119 | +export const ChromeNextGlobalHeaderShell = React.memo<ChromeNextGlobalHeaderShellProps>( |
| 120 | + ({ logo, switcher, search, help, actions, userMenu }) => { |
| 121 | + const sideNavWidth = useSideNavWidth(); |
| 122 | + const styles = useGlobalHeaderStyles(); |
| 123 | + const logoWidth = sideNavWidth <= COLLAPSED_WIDTH ? COLLAPSED_WIDTH : EXPANDED_WIDTH; |
| 124 | + |
| 125 | + return ( |
| 126 | + <header css={styles.root} data-test-subj="chromeNextGlobalHeader"> |
| 127 | + <div css={styles.leftGroup}> |
| 128 | + <div css={logoSlot} style={{ '--logo-width': `${logoWidth}px` } as React.CSSProperties}> |
| 129 | + {logo} |
| 130 | + </div> |
| 131 | + {switcher && ( |
| 132 | + <div css={styles.switcherSlot} data-test-subj="chromeNextGlobalHeaderSwitcher"> |
| 133 | + {switcher} |
| 134 | + </div> |
| 135 | + )} |
| 136 | + </div> |
| 137 | + <div css={styles.separator} /> |
| 138 | + <div css={styles.spacer} /> |
| 139 | + <div css={styles.separator} /> |
| 140 | + <div css={styles.rightGroup}> |
| 141 | + {search && ( |
| 142 | + <div css={styles.searchSlot} data-test-subj="chromeNextGlobalHeaderSearch"> |
| 143 | + {search} |
| 144 | + </div> |
| 145 | + )} |
| 146 | + {help && ( |
| 147 | + <div css={styles.helpSlot} data-test-subj="chromeNextGlobalHeaderHelp"> |
| 148 | + {help} |
| 149 | + </div> |
| 150 | + )} |
| 151 | + {actions && ( |
| 152 | + <div css={styles.actionsSlot} data-test-subj="chromeNextGlobalHeaderActions"> |
| 153 | + {actions} |
| 154 | + </div> |
| 155 | + )} |
| 156 | + {userMenu && ( |
| 157 | + <div css={styles.userMenuSlot} data-test-subj="chromeNextGlobalHeaderUserMenu"> |
| 158 | + {userMenu} |
| 159 | + </div> |
| 160 | + )} |
| 161 | + </div> |
| 162 | + </header> |
| 163 | + ); |
| 164 | + } |
| 165 | +); |
| 166 | + |
| 167 | +ChromeNextGlobalHeaderShell.displayName = 'ChromeNextGlobalHeaderShell'; |
0 commit comments