Last active
August 26, 2015 19:56
-
-
Save mattmccray/48970722ff3221da4e2b to your computer and use it in GitHub Desktop.
Tooltip Manager with Mobservable (simplified)
This file contains 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 {makeReactive, sideEffect, asReference, transaction} from 'mobservable' | |
function TooltipManager() { | |
const DOM_ROOT = document.getElementById('tooltip_root') | |
const LEAVE_DELAY = 400 | |
let state = makeReactive({ | |
hoverCount: 0, | |
tooltip: asReference(null), | |
position: asReference({}) | |
}) | |
sideEffect( _ => { | |
// console.debug('hoverCount:', state.hoverCount) | |
if( state.hoverCount > 0 ) { | |
React.render( | |
<Tooltip {...state.tooltip} position={state.position} />, | |
DOM_ROOT | |
) | |
} | |
else { | |
React.unmountComponentAtNode( DOM_ROOT ) | |
} | |
}) | |
return function attachTooltipDOMHandlers( options={} ) { | |
if(! options.content ) throw new Error("Tooltips need content!") | |
return { | |
onMouseEnter(e) { | |
transaction( _ => { | |
state.hoverCount += 1 | |
state.tooltip = options | |
state.position = RectTools.getRect( e.currentTarget || e.target ) | |
}) | |
options.onMouseEnter && options.onMouseEnter(e) | |
}, | |
onMouseLeave(e) { | |
setTimeout( _ => { | |
state.hoverCount -= 1 | |
}, LEAVE_DELAY) | |
options.onMouseLeave && options.onMouseLeave(e) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment