Skip to content

Instantly share code, notes, and snippets.

@AStoker
Last active July 23, 2017 12:38
Show Gist options
  • Save AStoker/c71b0c25d10b602762e92cd16afc576e to your computer and use it in GitHub Desktop.
Save AStoker/c71b0c25d10b602762e92cd16afc576e to your computer and use it in GitHub Desktop.
Toggle Switch Material
<template>
<require from="./switch"></require>
<span>${toggleState}</span><br />
<label for="toggle">Toggle: </label>
<switch id="toggle" value.bind="toggleState"></switch>
</template>
export class App {
toggleState = false;
toggle(){
this.toggleState = !this.toggleState;
}
}
<!doctype html>
<html>
<head>
<title>Aurelia</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body aurelia-app>
<h1>Loading...</h1>
<script src="https://jdanyow.github.io/rjs-bundle/node_modules/requirejs/require.js"></script>
<script src="https://jdanyow.github.io/rjs-bundle/config.js"></script>
<script src="https://jdanyow.github.io/rjs-bundle/bundles/aurelia.js"></script>
<script src="https://jdanyow.github.io/rjs-bundle/bundles/babel.js"></script>
<script>
require(['aurelia-bootstrapper']);
</script>
</body>
</html>
switch .switch-container{
position: relative;
border: none;
border-radius: 6px;
height: 12px;
width: 32px;
background-color: #BDBDBD;
padding: 0;
cursor: pointer;
transition: background-color .1s ease-in-out;
}
switch .switch-container.on{
background-color: #70BEB7;
}
switch .switch-container:focus{
outline: none;
}
switch .indicator{
position: absolute;
left: 0;
top: -3px;
height: 18px;
width: 18px;
margin: 0 -1px;
background-color: white;
border-radius: 50%;
box-shadow: 0px 1px 4px 0px rgba(0, 0, 0, 0.6);
transition: left .1s ease-in-out, background-color .1s ease-in-out;
}
switch .switch-container.on .indicator{
left: calc(100% - 16px);
background-color: #159588;
box-shadow: 0px 2px 5px 0px rgba(0, 0, 0, 0.4);
}
<template>
<require from="./switch.css"></require>
<button click.trigger="toggle()" class="switch-container ${state}">
<div class="indicator"></div>
</button>
</template>
import {bindable, bindingMode, computedFrom} from 'aurelia-framework';
export class SwitchCustomElement{
@bindable({ defaultBindingMode: bindingMode.twoWay }) value;
toggle(){
this.value = !!!this.value; //Double ! forces boolean, third ! toggles state
}
@computedFrom('value')
get state(){
if(this.value){
return 'on';
} else {
return 'off';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment