Last active
December 30, 2016 16:56
-
-
Save jpdevries/a94c03e1b0eb99a350163afd9398aa1d to your computer and use it in GitHub Desktop.
Font Size Preference Widget
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
<form id="font-size-widget" class="widget"> | |
<h2 id="font-size"><noscript>Please enable JavaScript to </noscript>Choose a Font Size</h2> | |
<fieldset> | |
<legend>Font Size</legend> | |
<div> | |
<input type="radio" name="fontsize" id="fontsize__normal" value="normal" checked disabled> | |
<label for="fontsize__normal">Normal</label> | |
</div> | |
<div> <input type="radio" name="fontsize" id="fontsize__medium" value="medium" disabled> | |
<label for="fontsize__medium">Medium</label></div> | |
<div> | |
<input type="radio" name="fontsize" id="fontsize__large" value="large" disabled> | |
<label for="fontsize__large">Large</label> | |
</div> | |
</fieldset> | |
</form> |
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
document.getElementById('font-size-widget').addEventListener('change', function(event) { | |
document.body.setAttribute('data-font-size', event.target.value); | |
localStorage.setItem('fontSize', event.target.value); | |
}); | |
try { | |
if(localStorage.getItem('fontSize')) { | |
const checkedInput = document.querySelector(`input[value="${localStorage.getItem('fontSize')}"]`); | |
checkedInput.checked = true; | |
document.body.setAttribute('data-font-size', checkedInput.value); | |
} | |
} catch(e) {} | |
const disabledInputs = document.querySelectorAll('input[disabled]'); | |
for(let i = 0; i < disabledInputs.length; i++) { | |
disabledInputs[i].removeAttribute('disabled'); | |
} |
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
body { | |
font-size: var(--font-size, 100%); | |
} | |
body[data-font-size="medium"] { | |
--font-size: 120%; | |
} | |
body[data-font-size="large"] { | |
--font-size: 140%; | |
} | |
body { | |
text-align: center; | |
padding-top:10vh; | |
} | |
fieldset { | |
text-align: left; | |
} | |
.widget { | |
max-width: 18em; | |
margin: 0 auto; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://codepen.io/jpdevries/pen/ObKKyP