Adding SVG Icons to my French Wordle

michelc

Michel

Posted on January 19, 2022

Adding SVG Icons to my French Wordle

Just after I modified my French Wordle game to use accessible colors, I took a little time to add SVG icons.

My first "icons"

Initially, I only used simple Ascii or Unicode characters to represent the game actions or the keyboard keys:

  • "?" for the Help menu
  • "⚙" for the Settings menu
  • "↵" for Enter key
  • "⌫" for Backspace key

It's simple, it works, and considering that I've rarely had the opportunity to use SVG icons, this was the most practical solution to quickly create LeMOT.

Original icons

It was decent, but it didn't look the same depending on what you're playing on: Windows PC, phone, iPhone, ... And then I had to tweak some CSS to give a "button" look to my menu icons.

.menu {
  background-color: #ddd;
  color: #fff;
  border-radius: 50%;
  font-size: 24px;
  width: 35px;
  height: 35px;
  text-align: center;
  cursor: pointer;
  user-select: none;
}

.menu:hover {
  background-color: #bbb;
}
Enter fullscreen mode Exit fullscreen mode

Switching to SVG icons

Regardless, I had planned from the beginning to look at SVG icons to improve LeMOT. As I had a number of SVG icon sets bookmarks, I looked for one that would be the best fit.

Though in the end these are not the icons I selected, I listed them here because they are all very good. But I had some difficulties finding icons to represent the Enter and Backspace keys. Also, just at that time, Josh Wardle had added a new icon in the menu to view stats.

Also, because I was impatient, I had trouble finding explanations for how to color or resize them.

Luckily, I finally tilted and thought again about Bootstrap icons that I sometimes use as icon fonts! And there we are, everything I needed, plus they were easily customizable with a bit of CSS.

To set the color:

svg {
  fill: green;
}
Enter fullscreen mode Exit fullscreen mode

To resize them:

svg {
  height: 1.75em;
  width: 1.75em;
}
Enter fullscreen mode Exit fullscreen mode

The icing on the cake, using the "em" unit allows them to be sized proportionally to the font size.

SVG icons

Problem with clicking on SVG icons

As it was late and I hadn't tested enough (or not at all?), it bugged :(

On the phones, it was no longer possible to go back and correct a letter or to validate the word entered! All of this because my code was still relying on the Unicode characters "↵" and "⌫" to detect which key had been used.

  if (touche === "") {
    validerLigne();
  } else if (touche === "") {
    retourArriere();
  }
Enter fullscreen mode Exit fullscreen mode

Or something like that...

Never mind, I just had to add a data-attribute on the 2 keys and problem solved!

Unfortunately, when you click on an SVG, the event.target given by the "click" event doesn't necessarily correspond to the DOM element "button" that represents the key. It can be the "SVG" element or even the "PATH" element depending on the exact place where you clicked...

Note: Most certainly on a phone it will always fall on the "button" (unless you have a super thin finger), but I don't want to leave anything to chance.

Fortunately, this is a well-known problem that I had already encountered in my previous run-ins with event delegation. In his post "Detecting click events on SVGs with vanilla JS event delegation", Chris Ferdinandi gives two solutions to solve the problem.

Of course, I took the simpler solution and added a pointer-events: none; to my CSS file.

And that's why it's now possible to play French Wordle on a phone and with pretty icons.


This post was originally published on blog.pagesd.info.

💖 💪 🙅 🚩
michelc
Michel

Posted on January 19, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Array.every() et new Set() sauvent LeMOT
javascript Array.every() et new Set() sauvent LeMOT

January 18, 2023

Adding SVG Icons to my French Wordle
javascript Adding SVG Icons to my French Wordle

January 19, 2022

Mode sombre pour Wordle en français
javascript Mode sombre pour Wordle en français

February 1, 2022