Implement responsive design - convert sidebar to top navbar below 700px screen width #4
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.
This PR is a companion to my blog post Responsively converting a sidebar to a navbar with React and CSS.
Relevant state of codebase and design up until this point
Before this PR, there are only two types of pages in the site:
Both pages share the left-hand sidebar, which contains a headshot that links to the Home page, and three links for other places to find me on the web.
These are the existing React components that factor into the page-level design:
Commit 1: Prefactor
The first commit makes a simple but important change to prepare the codebase for an overhaul to page-level styling. Before this PR, the overall page layout logic (compose together a Menu and Content) was duplicated between
index
and[slug]
. This commit puts that composition, which represents a page, into a new component thatindex
and[slug]
both now use:scripts/Page.tsx
. All of the styling changes here affect that new component andMenu.tsx
, which only it uses.Commit 2: Mobile-first top navbar
The second commit removes almost all of the existing styles and implements a top navbar friendly to small screens. The Menu component now:
MenuIcon
is added to the top navbar to expand / collapse this nav menuThe main mechanism that leads to the mobile-friendly design is the expandable nav menu. The top nav has a clickable icon (sometimes called the "Hamburger icon") that will update React state when clicked, controlling the presence or absence of a css class
.menuOpen
on the nav menu container. Without.menuOpen
themax-height
of the nav menu is 0 (so it won't show up). With.menuOpen
themax-height
is set to be larger than the contents are expected. Along with thetransition
style in the CSS this will animate the opening and closing of the nav menu. The styles ingeneral.module.css
have been carefully annotated to explain how they achieve the top navbar design.Result on an iPhone X screen size (375 x 812)
Commit 3: Bring back large-screen-appropriate styles
The third commit makes the site look like it did before this PR for large screens (the second commit removed all large-screen specific styling considerations). It accomplishes this primarily with the use of the
@media
query ingeneral.module.css
, ensuring that the large-screen styles only get applied if the screen is detected to be wider than 700 pixels. Again, the styles have been carefully annotated.Result on larger screen size (980px wide)
Commit 4: Refactor and Document
The fourth commit captures our newfound understanding of the navbar, nav menu, and responsive design in names and comments in the codebase.
Menu.tsx
->NavBar.tsx
general.module.css
topage.module.css
scripts/NavBar.tsx
(formerlyMenu.tsx
) ->scripts/page/NavBar.tsx
scripts/Page.tsx
->scripts/page/Page.tsx
scripts/page/page.module.css