Created
January 4, 2021 03:08
-
-
Save alexalannunes/d3cf064e6684ffb09f24dcbc39d01e06 to your computer and use it in GitHub Desktop.
simulate fonts.google.com font selection url
This file contains hidden or 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
<!DOCTYPE html> | |
<html lang="pt"> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<title>Document</title> | |
<link rel="stylesheet" href="https://jakiestfu.github.io/Ripple.js/dist/ripple.min.css" /> | |
<style> | |
html, | |
body { | |
padding: 0; | |
margin: 0; | |
} | |
output { | |
display: flex; | |
flex-wrap: wrap; | |
justify-content: center; | |
max-width: 800px; | |
margin: 40px auto; | |
} | |
* { | |
box-sizing: border-box; | |
} | |
.list-item { | |
cursor: pointer; | |
padding: 100px; | |
width: calc(100% / 3); | |
border: 1px solid #cccccc; | |
} | |
.list-item.active { | |
border-color: red; | |
} | |
</style> | |
</head> | |
<body> | |
<output></output> | |
<script src="https://jakiestfu.github.io/Ripple.js/bower_components/jquery/dist/jquery.min.js"></script> | |
<script src="https://jakiestfu.github.io/Ripple.js/dist/ripple.min.js"></script> | |
<script> | |
const fonts = [ | |
{ | |
name: "Roboto", | |
}, | |
{ | |
name: "Nunito", | |
}, | |
{ | |
name: "Lucida Console", | |
}, | |
]; | |
const output = document.querySelector("output"); | |
const search = new URLSearchParams(location.search); | |
let fontsSelecteds = (search.get("font") && search.get("font").split(",")) || []; | |
console.log(fontsSelecteds); | |
const renderList = () => { | |
output.innerHTML = ""; | |
const html = fonts | |
.map( | |
(i, index) => | |
`<div class="list-item ${fontsSelecteds.includes(i.name) ? "active" : ""}" onclick="toggleFont('${i.name}')" data-color="limegreen" data-index="${index}"> | |
<span>${i.name}</span> | |
</div>` | |
) | |
.join(""); | |
output.innerHTML = html; | |
$.ripple(".list-item"); | |
}; | |
const toggleFont = (font) => { | |
const index = fontsSelecteds.findIndex((i) => i == font); | |
console.log(fontsSelecteds.join(",")); | |
if (index != -1) { | |
fontsSelecteds.splice(index, 1); | |
} else { | |
fontsSelecteds.push(font); | |
} | |
history.replaceState({}, "", "?font=" + fontsSelecteds.join(",")); | |
renderList(); | |
}; | |
renderList(); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment