Created
February 26, 2017 07:05
-
-
Save jiverson/5c74fee6882f67a33ec639fc07a9b762 to your computer and use it in GitHub Desktop.
JS Bin Rxjs Toggle Button // source https://jsbin.com/jiyosu
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="Rxjs Toggle Button"> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
<script src="https://unpkg.com/@reactivex/[email protected]/dist/global/Rx.js"></script> | |
<style id="jsbin-css"> | |
.panel { | |
margin-top: 1em; | |
color: #fff; | |
font-family: sans-serif; | |
display: block; | |
padding: 1em; | |
background: red; | |
} | |
</style> | |
</head> | |
<body> | |
<button class="toggleButton">Toggle panel</button> | |
<div class="panel">Hi, I'm a toggleable panel</div> | |
<script id="jsbin-javascript"> | |
'use strict'; | |
var toggleButton = document.querySelector('.toggleButton'); | |
var toggleablePanel = document.querySelector('.panel'); | |
var buttonInitialState = false; | |
var clicks = Rx.Observable.fromEvent(toggleButton, 'click'); | |
var toggleState = function toggleState(currentState) { | |
return !currentState; | |
}; | |
var toggle = clicks.scan(toggleState, buttonInitialState).startWith(buttonInitialState); | |
toggle.subscribe(function (show) { | |
toggleablePanel.style.display = show ? 'block' : 'none'; | |
}); | |
</script> | |
<script id="jsbin-source-css" type="text/css">.panel { | |
margin-top: 1em; | |
color: #fff; | |
font-family: sans-serif; | |
display: block; | |
padding: 1em; | |
background: red; | |
}</script> | |
<script id="jsbin-source-javascript" type="text/javascript">const toggleButton = document.querySelector('.toggleButton'); | |
const toggleablePanel = document.querySelector('.panel'); | |
const buttonInitialState = false; | |
const clicks = Rx.Observable.fromEvent(toggleButton, 'click'); | |
const toggleState = currentState => !currentState; | |
const toggle = clicks | |
.scan(toggleState,buttonInitialState) | |
.startWith(buttonInitialState); | |
toggle.subscribe ((show) => { | |
toggleablePanel.style.display = show ? 'block' : 'none'; | |
});</script></body> | |
</html> |
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
.panel { | |
margin-top: 1em; | |
color: #fff; | |
font-family: sans-serif; | |
display: block; | |
padding: 1em; | |
background: red; | |
} |
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
'use strict'; | |
var toggleButton = document.querySelector('.toggleButton'); | |
var toggleablePanel = document.querySelector('.panel'); | |
var buttonInitialState = false; | |
var clicks = Rx.Observable.fromEvent(toggleButton, 'click'); | |
var toggleState = function toggleState(currentState) { | |
return !currentState; | |
}; | |
var toggle = clicks.scan(toggleState, buttonInitialState).startWith(buttonInitialState); | |
toggle.subscribe(function (show) { | |
toggleablePanel.style.display = show ? 'block' : 'none'; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment