|
| 1 | +import React, {MouseEvent, useCallback, useState} from 'react'; |
| 2 | + |
| 3 | +import {block} from '../../../../utils'; |
| 4 | +import {HeaderData, NavigationLogo} from '../../../../models'; |
| 5 | +import {Col, Grid, Row} from '../../../../grid'; |
| 6 | +import OutsideClick from '../../../OutsideClick/OutsideClick'; |
| 7 | +import Control from '../../../Control/Control'; |
| 8 | +import Navigation from '../Navigation/Navigation'; |
| 9 | +import MobileNavigation from '../MobileNavigation/MobileNavigation'; |
| 10 | +import NavigationItem from '../NavigationItem/NavigationItem'; |
| 11 | +import Logo from '../Logo/Logo'; |
| 12 | + |
| 13 | +import {NavigationClose, NavigationOpen} from '../../../../icons'; |
| 14 | + |
| 15 | +import './Header.scss'; |
| 16 | + |
| 17 | +const b = block('header'); |
| 18 | + |
| 19 | +const ICON_SIZE = 36; |
| 20 | + |
| 21 | +export interface HeaderProps { |
| 22 | + logo: NavigationLogo; |
| 23 | + data: HeaderData; |
| 24 | +} |
| 25 | + |
| 26 | +interface MobileMenuButtonProps { |
| 27 | + isSidebarOpened: boolean; |
| 28 | + onSidebarOpenedChange: (arg: boolean) => void; |
| 29 | +} |
| 30 | + |
| 31 | +const MobileMenuButton: React.FC<MobileMenuButtonProps> = ({ |
| 32 | + isSidebarOpened, |
| 33 | + onSidebarOpenedChange, |
| 34 | +}) => { |
| 35 | + const iconProps = { |
| 36 | + icon: isSidebarOpened ? NavigationClose : NavigationOpen, |
| 37 | + iconSize: ICON_SIZE, |
| 38 | + }; |
| 39 | + |
| 40 | + return ( |
| 41 | + <Control |
| 42 | + className={b('mobile-menu-button')} |
| 43 | + onClick={(e: MouseEvent) => { |
| 44 | + e.stopPropagation(); |
| 45 | + onSidebarOpenedChange(!isSidebarOpened); |
| 46 | + }} |
| 47 | + size="l" |
| 48 | + {...iconProps} |
| 49 | + /> |
| 50 | + ); |
| 51 | +}; |
| 52 | + |
| 53 | +export const Header: React.FC<HeaderProps> = ({data, logo}) => { |
| 54 | + const {leftItems, rightItems} = data; |
| 55 | + const [isSidebarOpened, setIsSidebarOpened] = useState(false); |
| 56 | + const [activeItemIndex, setActiveItemIndex] = useState(-1); |
| 57 | + |
| 58 | + const onActiveItemChange = useCallback((index: number) => { |
| 59 | + setActiveItemIndex(index); |
| 60 | + }, []); |
| 61 | + |
| 62 | + const onSidebarOpenedChange = useCallback((isOpen: boolean) => { |
| 63 | + setIsSidebarOpened(isOpen); |
| 64 | + }, []); |
| 65 | + |
| 66 | + const hideSidebar = useCallback(() => { |
| 67 | + setIsSidebarOpened(false); |
| 68 | + }, []); |
| 69 | + |
| 70 | + return ( |
| 71 | + <Grid className={b()}> |
| 72 | + <Row> |
| 73 | + <Col> |
| 74 | + <header className={b('wrapper')}> |
| 75 | + {logo && ( |
| 76 | + <div className={b('left')}> |
| 77 | + <Logo {...logo} className={b('logo')} /> |
| 78 | + </div> |
| 79 | + )} |
| 80 | + <div className={b('navigation-container')}> |
| 81 | + <Navigation |
| 82 | + className={b('navigation')} |
| 83 | + links={leftItems} |
| 84 | + activeItemIndex={activeItemIndex} |
| 85 | + onActiveItemChange={onActiveItemChange} |
| 86 | + /> |
| 87 | + </div> |
| 88 | + <div className={b('right')}> |
| 89 | + <MobileMenuButton |
| 90 | + isSidebarOpened={isSidebarOpened} |
| 91 | + onSidebarOpenedChange={onSidebarOpenedChange} |
| 92 | + /> |
| 93 | + {rightItems && ( |
| 94 | + <div className={b('buttons')}> |
| 95 | + {rightItems.map((button) => ( |
| 96 | + <NavigationItem |
| 97 | + key={button.text} |
| 98 | + data={button} |
| 99 | + className={b('button')} |
| 100 | + /> |
| 101 | + ))} |
| 102 | + </div> |
| 103 | + )} |
| 104 | + </div> |
| 105 | + <OutsideClick onOutsideClick={() => onSidebarOpenedChange(false)}> |
| 106 | + <MobileNavigation |
| 107 | + topItems={leftItems} |
| 108 | + bottomItems={rightItems} |
| 109 | + isOpened={isSidebarOpened} |
| 110 | + activeItemIndex={activeItemIndex} |
| 111 | + onActiveItemChange={onActiveItemChange} |
| 112 | + onClose={hideSidebar} |
| 113 | + /> |
| 114 | + </OutsideClick> |
| 115 | + </header> |
| 116 | + </Col> |
| 117 | + </Row> |
| 118 | + </Grid> |
| 119 | + ); |
| 120 | +}; |
| 121 | + |
| 122 | +export default Header; |
0 commit comments