Last active
March 16, 2023 08:16
-
-
Save lgk-bsw/790e35ebe5e42af65ed394a182a469fc to your computer and use it in GitHub Desktop.
The SVG code examples on the Bootstrap Icons docs use 16px for width and height. This script allows to use 1em instead.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Use this together with this browser extension (Chrome/Edge) and enable it for https://icons.getbootstrap.com: | |
// https://chrome.google.com/webstore/detail/custom-javascript-for-web/ddbjnfjiigjmcpcpkmhogomapikjbjdk | |
/** | |
* This code adds a switch checkbox on the Bootstrap Icons pages. | |
* For example on: https://icons.getbootstrap.com/icons/x-lg/ | |
* If enabled, the SVG code will be changed to use `1em` for width and height instead of `16`(px). | |
*/ | |
if (location.pathname.startsWith("/icons/")) { | |
function replaceSvgPxWithEm() { | |
for (const el of document.querySelectorAll(".language-html .s")) { | |
if (el.innerText === '"16"') { | |
el.innerText = '"1em"' | |
} | |
} | |
} | |
const fromStorage = localStorage.getItem("replaceSvgPxWithEm") | |
const div = document.createElement("div") | |
const input = document.createElement("input") | |
const label = document.createElement("label") | |
div.className = "form-check form-switch mt-4" | |
input.className = "form-check-input" | |
input.type = "checkbox" | |
input.role = "switch" | |
input.id = "replaceSvgPxWithEm-checkbox" | |
input.addEventListener("change", ({target}) => { | |
if (target.checked) { | |
localStorage.setItem("replaceSvgPxWithEm", "true") | |
} | |
else { | |
localStorage.removeItem("replaceSvgPxWithEm") | |
} | |
location.reload() | |
}) | |
div.appendChild(input) | |
label.className = "form-check-label" | |
label.htmlFor = input.id | |
label.innerText = "Change SVG code: replace 16px with 1em" | |
div.appendChild(label) | |
input.checked = fromStorage !== null | |
if (input.checked) { | |
replaceSvgPxWithEm() | |
} | |
document.querySelector("#icons-body main").prepend(div) | |
} |
Author
lgk-bsw
commented
Mar 16, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment