"Semantic Markup with HTML and ARIA" workshop - Main heading refactor idea with context #1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi, I am currently doing the "Semantic Markup with HTML and ARIA" workshop. In that workshop there is some talk about the implementation of the
h1
in the page.It's an interesting issue. I hadn't run into this problem myself in other projects because the nav bars they have are not as complex so I didn't feel the need to add headings in the nav.
One of the things that @marcysutton mentions in the workshop is that the portal solution for the
h1
is not perfect because it is a best practice for the h1 to be inside of a landmark and how in this case it would be good to have it inside theheader
landmark.This got me thinking about this other possible approach that ditches the portal and uses React context instead. It looses a bit of flexibility because the portal allows you to pass children but not sure if that is really needed. If the
h1
is just some text that is visually hidden and we don't want to add any other dynamic props or siblings this context approach should be more than enough.So here is the proposed solution with context api:
We have a context at the root of the App component that holds the value of the
mainHeading
which is just simple text.Then we expose a hook to all the pages that is
useMainHeading
Where they can set the heading for that page in particular.That hook is made so when the page unmounts it goes back to the default heading. So if someone forgets to set the mainHeading in a page, it won't still hold the value of the previous page, it will just have the default heading text.
Then the
h1
can be put inside the Header component that contains the header landmark and it gets its value from the context.NOTE:
I'm creating this PR as a draft just to show this as an idea