TheMcnafaha / fe-rest-country

Home Page:https://fe-rest-country.vercel.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Frontend Mentor - REST Countries API with color theme switcher solution

This is a solution to the REST Countries API with color theme switcher challenge on Frontend Mentor. Frontend Mentor challenges help you improve your coding skills by building realistic projects.

Table of contents

Note: Delete this note and update the table of contents based on what sections you keep.

Overview

The challenge

Users should be able to:

  • See all countries from the API on the homepage
  • Search for a country using an input field
  • Filter countries by region
  • Click on a country to see more detailed information on a separate page
  • Click through to the border countries on the detail page
  • Toggle the color scheme between light and dark mode (optional)

Screenshot

Preview of project

Links

My process

Built with

  • Semantic HTML5 markup
  • CSS Flexbox
  • CSS Grid
  • Mobile-first workflow
  • Qwik - JS framework
  • Tailwind - CSS framework

What I learned

This challenge tested my knowledge of Qwik City.

Wanting to do a MPA, I had to render all the countries the API gave me on the server before I sent any HTML to the client. RouteLoaders makes this surprisingly easy by giving you a hook that returns your fetch response, solving the hardest part of this challenge.

I also learned that themes with MPAs need to have state so that your page-to-page navigation doesn't lose the user picked theme. This felt very "spaghetti" to do in Qwik because you lose syntax higlighting here:

if (
  window.matchMedia("(prefers-color-scheme: dark)").matches &&
  localStorage.getItem("theme") === undefined
) {
  document.documentElement.classList.add("dark");
  localStorage.setItem("theme", "dark");
} else {
  if (localStorage.getItem("theme") === "dark") {
    document.documentElement.classList.add("dark");
  } else {
    document.documentElement.classList.remove("dark");
  }
}
})();
window.addEventListener('load', function() {
        }
        );
};

I do like how Tailwind solves themes though; you just need to add a "dark" class and it will handle the rest.

Continued development

My current solution SSRs everything but the images. This means that the user most render the images, so on slow connections they will see the default grey background. This is true despite the fact that I use SVGs, which are text-friendly files. In theory, I could send the SVGs text to the client so that they don't need to eat the call to the CDN.

My current solution also has a suprising amount of blocking time. I'd like to drop that down, but I would need to find the cause of first.

Useful resources

  • Qwik Documentation - Their docs and built-in AI help saved me hours.
  • Tailwind Documentaion - Same idea as the one above, but styling. I needed to handle themes swithching, couldn't have done it without it.

Author

  • Frontend Mentor - @TheMcnafaha

About

https://fe-rest-country.vercel.app


Languages

Language:TypeScript 89.0%Language:JavaScript 9.5%Language:CSS 1.4%Language:Shell 0.0%