|
1 | 1 | 'use client'; |
2 | | -import React, { FC, HTMLProps, useContext, useState, useEffect, useMemo, useRef } from 'react'; |
| 2 | +import React, { |
| 3 | + ComponentPropsWithoutRef, |
| 4 | + useState, |
| 5 | + useEffect, |
| 6 | + useMemo, |
| 7 | + useRef, |
| 8 | + Children, |
| 9 | +} from 'react'; |
3 | 10 | import classNames from 'classnames'; |
4 | | -import NHSLogo, { NHSLogoNavProps } from './components/NHSLogo'; |
5 | | -import OrganisationalLogo, { OrganisationalLogoProps } from './components/OrganisationalLogo'; |
| 11 | +import { Container } from '@components/layout'; |
| 12 | +import { childIsOfComponentType } from '@util/types/TypeGuards'; |
6 | 13 | import HeaderContext, { IHeaderContext } from './HeaderContext'; |
| 14 | +import Account from './components/Account'; |
| 15 | +import AccountItem from './components/AccountItem'; |
| 16 | +import Logo from './components/Logo'; |
| 17 | +import Navigation from './components/Navigation'; |
| 18 | +import NavigationItem from './components/NavigationItem'; |
7 | 19 | import Search from './components/Search'; |
8 | | -import Nav from './components/Nav'; |
9 | | -import NavItem from './components/NavItem'; |
10 | | -import NavDropdownMenu from './components/NavDropdownMenu'; |
11 | | -import { Container } from '@components/layout'; |
12 | | -import Content from './components/Content'; |
13 | | -import TransactionalServiceName from './components/TransactionalServiceName'; |
| 20 | +import ServiceName from './components/ServiceName'; |
14 | 21 | import { Header } from 'nhsuk-frontend'; |
15 | 22 |
|
16 | | -const HeaderLogo: FC<OrganisationalLogoProps & NHSLogoNavProps> = (props) => { |
17 | | - const { orgName } = useContext<IHeaderContext>(HeaderContext); |
18 | | - if (orgName) { |
19 | | - return <OrganisationalLogo {...props} />; |
20 | | - } |
21 | | - return <NHSLogo {...props} />; |
22 | | -}; |
23 | | - |
24 | | -const HeaderContainer: FC<HTMLProps<HTMLDivElement>> = ({ className, ...rest }) => ( |
25 | | - <Container className={classNames('nhsuk-header__container', className)} {...rest} /> |
26 | | -); |
27 | | - |
28 | | -interface HeaderProps extends HTMLProps<HTMLDivElement> { |
29 | | - transactional?: boolean; |
30 | | - orgName?: string; |
31 | | - orgSplit?: string; |
32 | | - orgDescriptor?: string; |
33 | | - serviceName?: string; |
| 23 | +interface HeaderProps extends ComponentPropsWithoutRef<'div'> { |
| 24 | + containerClasses?: string; |
| 25 | + logo?: IHeaderContext['logoProps']; |
| 26 | + service?: IHeaderContext['serviceProps']; |
| 27 | + organisation?: IHeaderContext['organisationProps']; |
34 | 28 | white?: boolean; |
35 | 29 | } |
36 | 30 |
|
37 | 31 | const HeaderComponent = ({ |
38 | 32 | className, |
| 33 | + containerClasses, |
39 | 34 | children, |
40 | | - transactional, |
41 | | - orgName, |
42 | | - orgSplit, |
43 | | - orgDescriptor, |
44 | | - role = 'banner', |
45 | | - serviceName, |
| 35 | + logo, |
| 36 | + service, |
| 37 | + organisation, |
46 | 38 | white, |
47 | 39 | ...rest |
48 | 40 | }: HeaderProps) => { |
49 | 41 | const moduleRef = useRef<HTMLDivElement>(null); |
50 | 42 |
|
51 | | - const [hasMenuToggle, setHasMenuToggle] = useState(false); |
52 | | - const [hasSearch, setHasSearch] = useState(false); |
53 | | - const [hasServiceName, setHasServiceName] = useState(false); |
| 43 | + const [logoProps, setLogoProps] = useState(logo); |
| 44 | + const [serviceProps, setServiceProps] = useState(service); |
| 45 | + const [organisationProps, setOrganisationProps] = useState(organisation); |
54 | 46 | const [instance, setInstance] = useState<Header>(); |
55 | 47 | const [menuOpen, setMenuOpen] = useState(false); |
56 | 48 |
|
57 | 49 | useEffect(() => { |
58 | | - if (!moduleRef.current || instance) { |
| 50 | + if (!logo) { |
59 | 51 | return; |
60 | 52 | } |
61 | 53 |
|
62 | | - setInstance(new Header(moduleRef.current)); |
63 | | - }, [moduleRef, instance]); |
| 54 | + setLogoProps(logo); |
| 55 | + return () => setLogoProps(undefined); |
| 56 | + }, [logo]); |
| 57 | + |
| 58 | + useEffect(() => { |
| 59 | + if (!service) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + setServiceProps(service); |
| 64 | + return () => setServiceProps(undefined); |
| 65 | + }, [service]); |
64 | 66 |
|
65 | | - const setMenuToggle = (toggle: boolean): void => { |
66 | | - setHasMenuToggle(toggle); |
67 | | - }; |
| 67 | + useEffect(() => { |
| 68 | + if (!organisation) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + setOrganisationProps(organisation); |
| 73 | + return () => setOrganisationProps(undefined); |
| 74 | + }, [organisation]); |
| 75 | + |
| 76 | + useEffect(() => { |
| 77 | + if (!moduleRef.current || instance) { |
| 78 | + if (!instance) { |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + // Sync menu open state |
| 83 | + if (menuOpen && !instance.menuIsOpen) { |
| 84 | + instance.openMenu(); |
| 85 | + } |
68 | 86 |
|
69 | | - const setSearch = (toggle: boolean): void => { |
70 | | - setHasSearch(toggle); |
71 | | - }; |
| 87 | + // Sync menu close state |
| 88 | + if (!menuOpen && instance.menuIsOpen) { |
| 89 | + instance.closeMenu(); |
| 90 | + } |
72 | 91 |
|
73 | | - const toggleMenu = (): void => { |
74 | | - setMenuOpen(!menuOpen); |
75 | | - }; |
| 92 | + return; |
| 93 | + } |
76 | 94 |
|
77 | | - const setServiceName = (toggle: boolean): void => { |
78 | | - setHasServiceName(toggle); |
79 | | - }; |
| 95 | + setInstance(new Header(moduleRef.current)); |
| 96 | + }, [moduleRef, instance, menuOpen]); |
80 | 97 |
|
81 | 98 | const contextValue: IHeaderContext = useMemo(() => { |
82 | 99 | return { |
83 | | - orgName, |
84 | | - orgSplit, |
85 | | - orgDescriptor, |
86 | | - serviceName, |
87 | | - hasSearch, |
88 | | - hasMenuToggle, |
89 | | - hasServiceName, |
90 | | - setMenuToggle, |
91 | | - setSearch, |
92 | | - setServiceName, |
93 | | - toggleMenu, |
| 100 | + logoProps, |
| 101 | + serviceProps, |
| 102 | + organisationProps, |
94 | 103 | menuOpen, |
95 | | - transactional: transactional ?? false, |
| 104 | + setMenuOpen, |
| 105 | + setLogoProps, |
| 106 | + setServiceProps, |
| 107 | + setOrganisationProps, |
96 | 108 | }; |
97 | | - }, [ |
98 | | - orgName, |
99 | | - orgSplit, |
100 | | - orgDescriptor, |
101 | | - serviceName, |
102 | | - hasSearch, |
103 | | - hasMenuToggle, |
104 | | - hasServiceName, |
105 | | - setMenuToggle, |
106 | | - setSearch, |
107 | | - setServiceName, |
108 | | - toggleMenu, |
109 | | - menuOpen, |
110 | | - transactional, |
111 | | - ]); |
| 109 | + }, [logoProps, serviceProps, organisationProps]); |
| 110 | + |
| 111 | + const items = Children.toArray(children); |
| 112 | + const [childLogo] = items.filter((child) => childIsOfComponentType(child, Logo)); |
| 113 | + const [childSearch] = items.filter((child) => childIsOfComponentType(child, Search)); |
| 114 | + const [childNavigation] = items.filter((child) => childIsOfComponentType(child, Navigation)); |
| 115 | + const [childAccount] = items.filter((child) => childIsOfComponentType(child, Account)); |
112 | 116 |
|
113 | 117 | return ( |
114 | 118 | <header |
115 | 119 | className={classNames( |
116 | 120 | 'nhsuk-header', |
117 | | - { 'nhsuk-header__transactional': transactional }, |
118 | | - { 'nhsuk-header--organisation': orgName }, |
119 | | - { 'nhsuk-header--white': white }, |
| 121 | + { 'nhsuk-header--organisation': !!organisationProps }, |
| 122 | + { 'nhsuk-header--white': !!white }, |
120 | 123 | className, |
121 | 124 | )} |
122 | | - role={role} |
| 125 | + data-module="nhsuk-header" |
| 126 | + role="banner" |
123 | 127 | ref={moduleRef} |
124 | 128 | {...rest} |
125 | 129 | > |
126 | | - <HeaderContext.Provider value={contextValue}>{children}</HeaderContext.Provider> |
| 130 | + <HeaderContext.Provider value={contextValue}> |
| 131 | + <Container className={classNames('nhsuk-header__container', containerClasses)}> |
| 132 | + <ServiceName {...serviceProps}>{childLogo}</ServiceName> |
| 133 | + {childSearch} |
| 134 | + {childAccount} |
| 135 | + </Container> |
| 136 | + {childNavigation} |
| 137 | + </HeaderContext.Provider> |
127 | 138 | </header> |
128 | 139 | ); |
129 | 140 | }; |
130 | 141 |
|
131 | | -HeaderComponent.Logo = HeaderLogo; |
| 142 | +HeaderComponent.Account = Account; |
| 143 | +HeaderComponent.AccountItem = AccountItem; |
| 144 | +HeaderComponent.Logo = Logo; |
132 | 145 | HeaderComponent.Search = Search; |
133 | | -HeaderComponent.Nav = Nav; |
134 | | -HeaderComponent.NavItem = NavItem; |
135 | | -HeaderComponent.NavDropdownMenu = NavDropdownMenu; |
136 | | -HeaderComponent.Container = HeaderContainer; |
137 | | -HeaderComponent.Content = Content; |
138 | | -HeaderComponent.ServiceName = TransactionalServiceName; |
| 146 | +HeaderComponent.Navigation = Navigation; |
| 147 | +HeaderComponent.NavigationItem = NavigationItem; |
139 | 148 |
|
140 | 149 | export default HeaderComponent; |
0 commit comments