You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Expose header navigation open/close state (with setter) #52
I'm not sure if I'm missing something obvious here (if I am, please point it out), I've been trying to close the menu (if it's open) if the route changes within my app. Since the HeaderContext provider doesn't cover my own components, I seem only to be able to access the menuOpen property and not use toggleMenu or setMenuToggle.
I believe this is a limitation of the context api in general - not being able to set a context that's not provided (although it seems odd to me that I can view things like menuOpen still).
Am I missing an obvious prop I can use/set with the header component that allows me to set the menuOpen state? If not, what would be the best way to achieve this currently?
Sorry for the slow response to this! There isn't currently a prop that allows you to manipulate the menuOpen state, apart from using the toggleMenu function from the context, though the context is designed to be internal rather than to be manipulated externally.
Would it be useful to have a menuOpen prop passed to the base Header element? This would be the "forced" value of opening the menu, and will get passed down to all of the children in the internal context.
Hmm I'm not sure, I guess it would then be possible to check for route changes where the Header component is used in an application and manipulate menuOpen that way? I'm just trying to think of the best way to do this as it would be best (I think) if the menu collapsed if a link is clicked outside of the header too. For example, if a user opens the menu and then clicks on a link on the page they are currently on (not the menu links), the menu should then disappear. Does that make sense and would that be possible using the way suggested above? Thank you for your help! 😄
It should be - the best way I would do it is to have some context up at the root level of your app that holds the menuOpen state, with a couple of setter methods to change that value. Then you could combine that with an event listener that looks for changes in the pathname so that you can automatically set that value when you navigate, which might be easier than adding it onto every link! Or you could even write a little custom hook that closes the menu when the view is unmounted, something like:
Returning another function from the useEffect callback, with an empty dependency array works the same in practice as the componentWillUnmount function.
Bear in mind that NHS.UK frontend handles the open/close toggle behaviour, e.g. it will automatically close when the viewport width increases and menu items no longer need to overflow
But if this happens the menuOpen context will still be updated
Activity
Tomdango commentedon Dec 3, 2020
Sorry for the slow response to this! There isn't currently a prop that allows you to manipulate the
menuOpenstate, apart from using thetoggleMenufunction from the context, though the context is designed to be internal rather than to be manipulated externally.Would it be useful to have a
menuOpenprop passed to the baseHeaderelement? This would be the "forced" value of opening the menu, and will get passed down to all of the children in the internal context.Let me know what you think!
charlesomer commentedon Dec 4, 2020
Hmm I'm not sure, I guess it would then be possible to check for route changes where the Header component is used in an application and manipulate
menuOpenthat way? I'm just trying to think of the best way to do this as it would be best (I think) if the menu collapsed if a link is clicked outside of the header too. For example, if a user opens the menu and then clicks on a link on the page they are currently on (not the menu links), the menu should then disappear. Does that make sense and would that be possible using the way suggested above? Thank you for your help! 😄Tomdango commentedon Dec 7, 2020
It should be - the best way I would do it is to have some context up at the root level of your app that holds the
menuOpenstate, with a couple of setter methods to change that value. Then you could combine that with an event listener that looks for changes in thepathnameso that you can automatically set that value when you navigate, which might be easier than adding it onto every link! Or you could even write a little custom hook that closes the menu when the view is unmounted, something like:Returning another function from the useEffect callback, with an empty dependency array works the same in practice as the
componentWillUnmountfunction.colinrotherham commentedon Sep 29, 2025
This is closed by #267
The context helper
toggleMenuwas replaced bysetMenuOpenalongside a newopenprop:Bear in mind that NHS.UK frontend handles the open/close toggle behaviour, e.g. it will automatically close when the viewport width increases and menu items no longer need to overflow
But if this happens the
menuOpencontext will still be updated[-]Access/set toggleMenu.[/-][+]Expose header navigation open/close state (with setter)[/+]