Skip to content

Instantly share code, notes, and snippets.

@neenjaw
Created September 16, 2020 18:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neenjaw/803779a06d37ba41c9471fbf9439b5a4 to your computer and use it in GitHub Desktop.
Save neenjaw/803779a06d37ba41c9471fbf9439b5a4 to your computer and use it in GitHub Desktop.
Github contribution graph color changer
const activeColorBorderStyle = "2px solid rgb(255, 0, 0)";
const inactiveColorBorderStyle = "0";
const legend = document.querySelector("ul.legend");
const svg = document.querySelector("svg.js-calendar-graph-svg");
let colorChangeEnabled = false;
let selectedColorBox = null;
let selectedColor = null;
const legendClickHandler = (event) => {
const target = event.target;
if (target.parentElement != legend && target.tagName != "LI") {
return;
}
if (colorChangeEnabled && target.classList.contains("active-color")) {
colorChangeEnabled = false;
selectedColorBox.classList.remove("active-color");
target.style.border = inactiveColorBorderStyle;
return;
}
colorChangeEnabled = true;
if (selectedColorBox) {
selectedColorBox.classList.remove("active-color");
selectedColorBox.style.border = inactiveColorBorderStyle;
}
selectedColorBox = target;
selectedColorBox.classList.add("active-color");
selectedColorBox.style.border = activeColorBorderStyle;
selectedColor = selectedColorBox.style.backgroundColor;
};
legend.addEventListener("click", legendClickHandler);
const rectClickHandler = (event) => {
const target = event.target;
if (!colorChangeEnabled) {
return;
}
event.preventDefault();
event.stopPropagation();
if (target.tagName != "rect" || !target.classList.contains("day")) {
return;
}
target.style.fill = selectedColor;
};
svg.addEventListener("click", rectClickHandler);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment