Skip to content

Instantly share code, notes, and snippets.

@snewcomer
Created March 30, 2020 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snewcomer/f66cefe63dff9e3c5520874dd4dfaf01 to your computer and use it in GitHub Desktop.
Save snewcomer/f66cefe63dff9e3c5520874dd4dfaf01 to your computer and use it in GitHub Desktop.
Range Slider
import Component from '@glimmer/component';
import { action } from '@ember/object';
export default class extends Component {
get handleStyles() {
const fraction = (this.args.currentValue / this.args.max) * 100;
return `left: ${fraction}%`;
}
@action
onClick(event) {
const { clientX } = event;
const { width, x } = event.target.getBoundingClientRect();
this.args.onChange(((clientX - x) / width) * 100);
}
}
import Controller from '@ember/controller';
import { action } from '@ember/object';
import { tracked } from '@glimmer/tracking';
export default class ApplicationController extends Controller {
@tracked
currentValue = 30;
@action
onChange(value) {
this.currentValue = value;
}
}
body {
margin: 12px 16px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
.range-slider {
height: 100px;
background-color: lightgrey;
display: flex;
flex-direction: column;
justify-content: center;
padding: 10px;
}
.range-slider--interactive {
position: relative;
display: flex;
justify-content: center;
height: 10px;
background-color: grey;
}
.range-slider--handle {
position: absolute;
left: 40%;
height: 10px;
width: 10px;
border-radius: 50%;
background-color: red;
margin-left: -5px;
}
<h1>Current Range Value: {{this.currentValue}}</h1>
<RangeSlider
@min={{0}}
@max={{100}}
@currentValue={{this.currentValue}}
@onChange={{this.onChange}} />
<br>
<br>
{{outlet}}
<br>
<br>
<div class="range-slider">
<div
class="range-slider--interactive"
{{on "click" this.onClick}}>
<div class="range-slider--handle" style={{this.handleStyles}}></div>
</div>
</div>
{
"version": "0.17.0",
"EmberENV": {
"FEATURES": {},
"_TEMPLATE_ONLY_GLIMMER_COMPONENTS": false,
"_APPLICATION_TEMPLATE_WRAPPER": true,
"_JQUERY_INTEGRATION": true
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "3.17.0",
"ember-template-compiler": "3.17.0",
"ember-testing": "3.17.0"
},
"addons": {
"@glimmer/component": "1.0.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment