Skip to content

Project of integrate with the API REST Countries to pull country data and display and toggle the color scheme between light and dark mode

License

Notifications You must be signed in to change notification settings

Cdrn19/rest-countries-api-with-color-theme-switcher

Repository files navigation

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

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

Links

My process

Built with

  • Semantic HTML5 markup
  • CSS custom properties
  • Flexbox
  • Responsive Desing
  • ECMAScript 6
  • React - JS library
  • Webpack - Static module bundler
  • SASS - Processor for styles

What I learned

In the continuous development of this project, many objectives were achieved that left great lessons.

The first of these learnings is the handling of data attributes and JavaScript:

  const handleClick = () => {
    const changeMode = theme === "light" ? "dark" : "light";
    modeTheme(changeMode);
  };
  document.body.dataset.theme = theme;

Which modifies the body tag to set the CSS properties:

<body data-theme="light">
[data-theme="light"] {
    --shadow: hsl(0, 0%, 52%);
    --bg-header: hsl(0, 0%, 100%);
    --background: hsl(0, 0%, 98%);
    --text: hsl(200, 15%, 8%)
}

[data-theme="dark"] {
    --shadow: hsl(200, 15%, 8%);
    --bg-header: hsl(209, 23%, 22%);
    --background: hsl(207, 26%, 17%);
    --text: hsl(0, 0%, 100%);
    --filter: invert(94%) sepia(100%) saturate(0%) hue-rotate(226deg) brightness(108%) contrast(101%);
}

Enabling properties for changing the color of the theme chosen by the user.

I also learned to use regular values ​​or regex to modify the name of country borders.

In the CountrySelect.jsx component we can find the section:

<div className="countrySelect__detail--button">

Where a mapping of its borders is carried out:

{Borders.length === 0 ? (
  <button>Not Borders</button>
) : (
  Borders?.map((border, i) => (
    <Link key={i} to={"/Country/" + border.name.toLowerCase()}>
      <button>
        {border.name.replace(/\s\(([^()][^()]*)\)/g, "")}
      </button>
    </Link>
  ))
)}

Using regular values ​​like /\s\(([^()][^()]*)\)/g to replace unnecessary values ​​which in this case would be the inner content between parentheses " ( unnecessary content )".

Continued development

I will continue to constantly learn the benefits that the React.js library can provide and make future implementations to improve the performance of my applications.

Also learn new ways to consume API REST to avoid redundant requests that delay layout and performance.

Useful resources

  • ESlint - Find and fix problems in JavaScript code.
  • Prettier - code formatter.

Author

Acknowledgments

I want to give thanks to Frontend Mentor website for making these projects and its community.

About

Project of integrate with the API REST Countries to pull country data and display and toggle the color scheme between light and dark mode

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published