Giter Site home page Giter Site logo

dictionary-web-app's Introduction

Dictionary web app - Frontend Mentor Challenge

Table of contents

Overview

The challenge

Users should be able to search for words using the input field, see the Free Dictionary API's response for the searched word, see a form validation message when trying to submit a blank form, play the audio file for a word when it's available, switch between serif, sans serif, and monospace fonts, switch between light and dark themes, view the optimal layout for the interface depending on their device's screen size.

Links

My process

Built with

What I learned

I learned about the watchMedia method among with prefers-color-scheme media query, which allows developers to detect the user's preferred color scheme. This is a very useful feature for developers, as it allows them to provide a better user experience by automatically adapting the color scheme of the website to the user's preferences.

const [theme, setTheme] = useState("light");

useEffect(() => {
  const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
  const prefersDarkMode = mediaQuery.matches;

  if (prefersDarkMode) {
    setTheme("dark");
  }
}, []);

The variables fonts was something I had never done before, so I had to do some research on how to do it. I ended up using the font-variation-settings property, which allows me to change the font's weight and width.

:root {
  --font-weight: 400;
  --font-width: 100;
}

body {
  font-variation-settings: "wght" var(--font-weight), "wdth" var(--font-width);
}

I also created a custom hook for localStorage, which allows me to store the user's settings in the browser's local storage. This way, the user's settings are saved even if they close the browser.

import { useState } from "react";

export default function useLocalStorage(key, initialValue) {
  const [storedValue, setStoredValue] = useState(() => {
    const item = window.localStorage.getItem(key);
    return JSON.parse(item) ?? initialValue;
  });

  const saveToLocalStorage = (value) => {
    // Set to state
    setStoredValue(value);

    // Save to actual local storage
    window.localStorage.setItem(key, JSON.stringify(value));
  };

  return [storedValue, saveToLocalStorage, setStoredValue];
}

Continued development

I want to continue learning about more PostCSS add-ons, such as postcss-preset-env, which allows me to use the latest CSS features, such as font-variation-settings, without worrying about browser compatibility.

And want to create a project in React with TypeScript to take advantage of the type system and improve the code quality.

Useful resources

  • prefers-color-scheme - This article helped me understand how to detect the user's preferred color scheme.

  • font-variation-settings - This article helped me understand how to use the font-variation-settings property.

  • useLocalStorage - This article helped me understand how to create a custom hook for localStorage.

Author

dictionary-web-app's People

Contributors

874anthony avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.