Skip to content

Instantly share code, notes, and snippets.

@danklammer
Created February 12, 2020 15:05
Show Gist options
  • Save danklammer/0e6c8578b432074206613ae01fa96a09 to your computer and use it in GitHub Desktop.
Save danklammer/0e6c8578b432074206613ae01fa96a09 to your computer and use it in GitHub Desktop.
NPS Score Selector
<div class="screen">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 360 92" class="droop">
<path fill="#0070B9" d="M0 0v82.6a1745.7 1745.7 0 00360 0V0H0z"/>
<path fill="#FFFFFF" d="M317 7v10h-10V7h10zm13 0a5 5 0 110 10 5 5 0 010-10zm23 0l-6 10-6-10h12z"/>
<text fill="#FFFFFF" font-size="20" font-weight="400" letter-spacing=".25">
<tspan x="72" y="54">Share Feedback</tspan>
</text>
<path fill="#FFFFFF" d="M28.7 38.5c.4.4.4 1 0 1.4l-5 5.1H35a1 1 0 01.1 2H23.5l5.1 5c.4.4.4 1 0 1.4a1 1 0 01-1.3.1h-.1l-6.8-6.8a1 1 0 010-1.3v-.1l6.8-6.8a1 1 0 011.4 0z"/>
</svg>
<div class="content">
<h1>How likely are you to recommend this app to a friend or colleague?</h1>
<div class="slider">
<div class="emoji-container">
<span class="emoji hide" data-range-show="0">😱</span>
<span class="emoji hide" data-range-show="1">😨</span>
<span class="emoji hide" data-range-show="2">😞</span>
<span class="emoji hide" data-range-show="3">😔</span>
<span class="emoji hide" data-range-show="4">😟</span>
<span class="emoji dim" data-range-show="5">🙁</span>
<span class="emoji hide" data-range-show="6">😕</span>
<span class="emoji hide" data-range-show="7">😐</span>
<span class="emoji hide" data-range-show="8">🙂</span>
<span class="emoji hide" data-range-show="9">😃</span>
<span class="emoji hide" data-range-show="10">😍</span>
</div>
<div class="slider-container">
<div class="ticks">
<span class="tick">0</span>
<span class="tick">1</span>
<span class="tick">2</span>
<span class="tick">3</span>
<span class="tick">4</span>
<span class="tick">5</span>
<span class="tick">6</span>
<span class="tick">7</span>
<span class="tick">8</span>
<span class="tick">9</span>
<span class="tick">10</span>
</div>
<input class="slider" id="rangeInput" type="range" min="0" max="10" step="1" onInput="updateHighlight(this.value)" />
<div class="slider-highlight" name="amount" id="amount" for="rangeInput"></div>
<div class="captions">
<span>Not likely</span>
<span>Very likely</span>
</div>
</div>
</div>
<a href="#" class="btn disabled">
Next
</a>
</div>
</div>
/* global consts */
const SELECTOR = {
range: '.slider',
emoji: '.emoji',
}
const ELEMENT = {
range: document.querySelector(SELECTOR.range),
emojis: document.querySelectorAll(SELECTOR.emoji),
}
const DATA_ATTR = 'data-range-show';
const BTN = document.querySelector('.btn');
const HIGHLIGHT = document.querySelector('.slider-highlight');
/* end global consts */
/* helper functions */
const hideElements = (...elementKeys) => {
elementKeys.forEach(key => {
ELEMENT[key].forEach(element => {
element.classList.add('hide');
element.classList.remove('dim');
})
})
}
const showElements = (data_attr, selectorKeys) => {
selectorKeys
.map(key => `${SELECTOR[key]}[${DATA_ATTR}="${data_attr}"]`)
.map(selector => document.querySelector(selector))
.forEach(element => element.classList.remove('hide'))
}
/* end helper functions */
/* event listeners */
const handleRangeUpdate = ({ target: { value: elementToShow } }) => {
hideElements('emojis');
showElements(elementToShow, ['emoji']);
BTN.classList.remove('disabled');
}
ELEMENT.range.addEventListener('input', handleRangeUpdate)
/* end event listeners */
function updateHighlight(val) {
HIGHLIGHT.setAttribute('data-width', val);
}
* { box-sizing: border-box }
img { width: 100vw; height: auto; }
body {
font-family: "Roboto", sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol, Noto Color Emoji;
margin: 0;
background: #f9f9f9;
}
.screen {
margin: 0 auto;
text-align: center;
background: #f9f9f9;
}
@media (min-width: 480px) {
body {
display: flex;
align-items: center;
justify-content: center;
height: 97vh;
background: #ddd;
}
.screen {
width: 360px;
height: 640px;
}
}
.content { padding: 16px; }
h1 {
font-weight: 500;
font-size: 20px;
color: rgba(0,0,0,0.87);
letter-spacing: 0.25px;
margin: 8px 0 0;
text-align: left;
}
.hide {
display: none;
}
.slider { user-select: none }
.emoji-container {
font-size: 48px;
height: 95px;
overflow: hidden;
padding: 16px;
}
.ticks {
display: flex;
justify-content: space-between;
font-size: 14px;
letter-spacing: -0.5px;
text-align: center;
padding: 0 3px 0 0;
}
.tick {
width: 20px;
display: block;
}
.tick:after {
content: " ";
display: block;
margin: 8px auto -7px;
width: 1px;
height: 6px;
background: #ddd;
border-radius: 2px;
}
.slider-highlight {
height: 3px;
width: 0;
background: #0070B9;
border-radius: 5px;
margin-top: -15px;
z-index: 1;
position: relative;
pointer-events: none;
}
.slider-highlight[data-width="1"] { width: 10%; }
.slider-highlight[data-width="2"] { width: 20%; }
.slider-highlight[data-width="3"] { width: 30%; }
.slider-highlight[data-width="4"] { width: 40% }
.slider-highlight[data-width="5"] { width: 50% }
.slider-highlight[data-width="6"] { width: 60% }
.slider-highlight[data-width="7"] { width: 70% }
.slider-highlight[data-width="8"] { width: 80% }
.slider-highlight[data-width="9"] { width: 90% }
.slider-highlight[data-width="10"] { width: 100% }
.captions {
margin: 20px 0 8px;
font-size: 14px;
color: rgba(0,0,0,0.87);
display: flex;
justify-content: space-between;
}
.btn {
margin-top: 24px;
font-size: 14px;
letter-spacing: 0.5px;
line-height: 16px;
line-height: 0;
max-width: 268px;
text-decoration: none;
color: white;
display: inline-block;
width: 90%;
background: #0070B9;
padding: 25px;
border-radius: 25px;
transition: all 0.3s ease;
}
.btn.disabled {
background: rgba(0,0,0,0.15);
color: rgba(0,0,0,0.30);
}
.dim { opacity: 0.3; }
/*
https://github.com/darlanrod/input-range-scss/blob/master/example/inputrange.css
Only use Webkit for this demo
*/
[type=range] {
-webkit-appearance: none;
background: transparent;
margin: 12px 0;
width: 100%;
}
[type=range]:focus {
outline: 0;
}
[type=range]:focus::-webkit-slider-runnable-track {
background: #d3d3d3;
}
[type=range]::-webkit-slider-runnable-track {
cursor: default;
height: 3px;
transition: all 0.2s ease;
width: 100%;
background: #d3d3d3;
border-radius: 5px;
}
[type=range]::-webkit-slider-thumb {
background: #0070B9;
border: 4px solid #fff;
box-shadow: 0 0 0 1px #D3D3D3;
border-radius: 24px;
box-sizing: border-box;
cursor: default;
height: 24px;
width: 24px;
-webkit-appearance: none;
margin-top: -10px;
transition: .2s ease;
z-index: 2;
position: relative;
}
input[type=range]::-webkit-slider-thumb:active {
transform: scale(1.75);
}
[type=range]:disabled::-webkit-slider-thumb, [type=range]:disabled::-webkit-slider-runnable-track {
cursor: not-allowed;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment