Last active
August 12, 2019 21:09
-
-
Save bpmckee/c85a87bbcce1602372c38be29089ce60 to your computer and use it in GitHub Desktop.
A11y training
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
| <h1>Golf Scorecard</h1> | |
| <p class="score"> | |
| You are at par | |
| </p> | |
| <div class="button-container"> | |
| <button data-offset="-1">▼</button> | |
| <span class="tooltip">Lower score</span> | |
| </div> | |
| <div class="button-container"> | |
| <div class="button" data-offset="1">▲</div> | |
| <span class="tooltip">Increase score</span> | |
| </div> | |
| <footer> | |
| Triggering the button shouldn't have scrolled the page! | |
| </footer> |
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
| import 'dart:html'; | |
| final buttons = querySelectorAll('.button,button'); | |
| final customButton = querySelector('.button'); | |
| final scoreEl = querySelector('.score'); | |
| var score = 0; | |
| void main() { | |
| buttons.onClick.listen(updateScore); | |
| buttons.onMouseUp.listen(blur); | |
| customButton.onKeyUp.listen(handleOnKeyUp); | |
| customButton.onKeyDown.listen(handleOnKeyDown); | |
| } | |
| void handleOnKeyUp(KeyboardEvent event) { | |
| var key = event.which ?? event.keyCode; | |
| if (key == null) return; | |
| /* Add code here */ | |
| } | |
| void handleOnKeyDown(KeyboardEvent event) { | |
| var key = event.which ?? event.keyCode; | |
| if (key == null) return; | |
| /* Add code here */ | |
| } | |
| void updateScore(Event event) { | |
| score += offset(event.target); | |
| scoreEl.text = scoreText(); | |
| } | |
| int offset(HtmlElement element) => | |
| int.parse(element.attributes['data-offset']); | |
| String scoreText() { | |
| if (score > 0) return 'You are $score above par'; | |
| if (score == 0) return 'You are at par'; | |
| return 'You are ${score.abs()} below par'; | |
| } | |
| void blur(MouseEvent event) { | |
| (event.target as HtmlElement).blur(); | |
| } |
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
| /* These are the only styles that | |
| * affect the button color contrast */ | |
| button, | |
| .button { | |
| background: #1A73E8; | |
| color: #202124; | |
| font-size: 14px; | |
| font-weight: 500; | |
| } | |
| /* These styles add the tooltip on hover */ | |
| button:hover + .tooltip, | |
| .button:hover + .tooltip { | |
| animation: 1s ease 0s normal forwards 1 fadein; | |
| opacity: 1; | |
| } | |
| /* Windows high contrast */ | |
| @media (-ms-high-contrast: active) { | |
| body button, body .button { | |
| /* Add styles here */ | |
| } | |
| } | |
| @media (-ms-high-contrast: black-on-white) { | |
| body button, body .button { | |
| /* Add styles here */ | |
| } | |
| } | |
| /* Ignore the rest of these styles */ | |
| body { | |
| background: #202124; | |
| color: #FFF; | |
| font-family: "Google Sans",Roboto,Arial,sans-serif; | |
| font-size: 14px; | |
| text-align: center; | |
| } | |
| button, | |
| .button { | |
| height: 36px; | |
| min-width: 36px; | |
| padding: 0 23px; | |
| margin: 16px 4px; | |
| display: inline-flex; | |
| align-items: center; | |
| justify-content: center; | |
| border: 0; | |
| border-radius: 4px; | |
| font-family: inherit; | |
| box-sizing: border-box; | |
| } | |
| button:hover, | |
| .button:hover { | |
| background: #D2E3FC; | |
| cursor: pointer; | |
| user-select: none; | |
| } | |
| button:focus, | |
| .button:focus { | |
| background: #D2E3FC; | |
| outline: 0; | |
| box-shadow: 0 0 0 5px rgba(255,255,255, 0.3); | |
| } | |
| p { | |
| font-size: 18px; | |
| } | |
| .score { | |
| color: #AECBFA; | |
| color: #FDE293; | |
| } | |
| .button-container { | |
| display: inline-block; | |
| position: relative; | |
| } | |
| .tooltip { | |
| position: absolute; | |
| pointer-events: none; | |
| background: #FFF; | |
| color: black; | |
| padding: 4px 8px; | |
| border-radius: 4px; | |
| top: -5px; | |
| left: 50%; | |
| transform: translate(-50%, -50%); | |
| white-space: nowrap; | |
| opacity: 0; | |
| } | |
| .tooltip:after { | |
| content: ''; | |
| position: absolute; | |
| top: 100%; | |
| left: 50%; | |
| margin-left: -8px; | |
| width: 0; | |
| height: 0; | |
| border: 8px solid transparent; | |
| border-top-color: #FFF; | |
| } | |
| @keyframes fadein{ | |
| 0% { opacity:0; } | |
| 80% { opacity:0; } | |
| 100% { opacity:1; } | |
| } | |
| footer { | |
| background: #F28B82; | |
| color: #202124; | |
| font-size: 24px; | |
| display: block; | |
| padding: 16px 32px; | |
| margin: 550px auto 32px; | |
| border-radius: 8px; | |
| max-width: 80%; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment