refactor: Rebuild header components#1317
Conversation
| a, | ||
| button, | ||
| span { | ||
| button { |
There was a problem hiding this comment.
The span styles only existed for the (now altered) LanguagePicker
| &[open] { | ||
| &[data-open='true'] { |
There was a problem hiding this comment.
For TS, and we use data-open for every other menu too. Previously the hamburger menu used just open on the container div.
| if (x) out += x; | ||
| } | ||
| return out; | ||
| return out || null; |
There was a problem hiding this comment.
Just to avoid empty class attributes sitting around in prerendered HTML, more than anything.
| This guide walks through building a simple "ticking clock" component. More detailed information for each topic can be found in the dedicated pages under the Guide menu. | ||
|
|
||
| > :information*desk_person: You [don't \_have* to use ES2015 to use Preact](https://github.com/developit/preact-without-babel)... but you should. This guide assumes you have some sort of ES2015 build set up using babel and/or webpack/browserify/gulp/grunt/etc. If you don't, start with [preact-cli](https://github.com/preactjs/preact-cli) or a [CodePen Template](http://codepen.io/developit/pen/pgaROe?editors=0010). | ||
| > :information_desk_person: You [don't _have_ to use ES2015 to use Preact](https://github.com/developit/preact-without-babel)... but you should. This guide assumes you have some sort of ES2015 build set up using babel and/or webpack/browserify/gulp/grunt/etc. If you don't, start with [preact-cli](https://github.com/preactjs/preact-cli) or a [CodePen Template](http://codepen.io/developit/pen/pgaROe?editors=0010). |
There was a problem hiding this comment.
Sorry, unrelated but correcting a Prettier bug that screwed up the content.
Didn't want to bother w/ a separate PR for something so tiny.
| <Nav class={style.nav} routes={config.nav} current={url} /> | ||
| <MainNav /> | ||
| <Search /> | ||
| <div class={style.social}> | ||
| <ReleaseLink class={cx(style.socialItem, style.release)} /> | ||
| <SocialIcon | ||
| label="Browse the code on GitHub" | ||
| href="https://github.com/preactjs/preact" | ||
| viewbox="0 0 24 24" | ||
| id="github" | ||
| /> | ||
| <SocialIcon | ||
| label="Follow us on Twitter" | ||
| href="https://twitter.com/preactjs" | ||
| viewbox="0 0 34 27.646" | ||
| id="twitter" | ||
| /> | ||
| <SocialIcon | ||
| label="Follow us on Bluesky" | ||
| href="https://bsky.app/profile/preactjs.com" | ||
| viewbox="0 0 568 501" | ||
| id="bluesky" | ||
| /> | ||
| <SocialIcon | ||
| label="Chat with us on Slack" | ||
| href="http://chat.preactjs.com/" | ||
| viewbox="0 0 512 512" | ||
| id="slack" | ||
| /> | ||
| </div> | ||
| <div class={style.translation}> | ||
| <NavMenu language /> | ||
| </div> | ||
| <SocialLinks /> | ||
| <LanguagePicker /> |
There was a problem hiding this comment.
Broke this up as it was getting a bit big, though there's no complexity so equally I could merge everything back together too. Felt it looked marginally cleaner this way?
|
|
||
| /** | ||
| * Get the translated name of a path based upon the current language. | ||
| * @param {string} path | ||
| */ | ||
| export function useNavTranslation(path) { | ||
| const [lang] = useLanguage(); | ||
|
|
||
| for (const route in config.nav) { | ||
| if (config.nav[route].path === path) { | ||
| return getRouteName(config.nav[route], lang); | ||
| } else if (config.nav[route].routes) { | ||
| for (const subRoute of config.nav[route].routes) { | ||
| if (subRoute.path === path) { | ||
| return getRouteName(subRoute, lang); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * @param {{ name: Record<string, string> | string }} route | ||
| * @param {string} lang | ||
| * @return {string} | ||
| */ | ||
| export function getRouteName(route, lang) { | ||
| return typeof route.name === 'object' | ||
| ? route.name[lang] || route.name.en | ||
| : route.name; | ||
| } |
There was a problem hiding this comment.
More of a hold over mechanism, I hope to switch the nav translations into a keyed object & break them away from the route paths in the future.
62c3d90 to
0f7da0c
Compare
f98167a to
ce0c7fb
Compare
Forewarning, sorry, this is fairly big.
Refactors most of the header to make everything a bit more fit for purpose but there are no real functional changes. It corrects a few (IMO) minor grievances that have popped up whilst making other edits:
<NavMenu>for the "About" routes, we later added a language drop down. This overlapped somewhat with the drop-down menu, but not completely, so there's this awkward ternary in the component./brandinginconfig.navso that it was detected as a valid route (404 detection and whatnot), but that list makes up the header items, so we had to add a magic prop<NavLink>returns an actual link, sometimes it's just a button that triggers no navigation whatsoever. A bit weird.pathMatchesRoutefunction to apply the "selected" header style, but then have added additional conditionals outside of that because the function doesn't actually cover the cases we need.config.json's responsibilitiesconfig.jsonis a bit of a weird god-file, in that it controls templating (config.nav-> header links), route definitions (used in/lib/route-utils.jsfor 404 detection & prerendering), and internationalization, all at once.The responsibilities of
config.jsonis still a problem that still needs work, but this has at least gotten the templating out for the most part -- no where else are we really passing props via JSON. Ideally, I'll get routes split out from the internationalization in a sane way and then write a plugin to split the i18n file per language, should be about ~5kb of savings there. You'll only need 1 locale at a time of course.This ships a tiny bit less JS, but nothing stellar. ~1kb less.