Skip to content

Instantly share code, notes, and snippets.

@mattmccray
Last active August 26, 2015 19:56
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 mattmccray/48970722ff3221da4e2b to your computer and use it in GitHub Desktop.
Save mattmccray/48970722ff3221da4e2b to your computer and use it in GitHub Desktop.
Tooltip Manager with Mobservable (simplified)
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