Last active
July 19, 2016 07:10
-
-
Save jamesarosen/78920121b982e841a31c5bbbeb0b6650 to your computer and use it in GitHub Desktop.
my-popover
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 Ember from 'ember'; | |
export default Ember.Controller.extend({ | |
appName: 'Ember Twiddle' | |
}); |
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 Ember from 'ember'; | |
const { $, run } = Ember; | |
const HIDE_DELAY = 400; | |
export default Ember.Component.extend({ | |
classNames: 'my-popover', | |
isOpen: false, | |
click({ target }) { | |
// CRUFT: we cannot yield block params to {{else}} blocks, | |
// so we can't do | |
// | |
// {{#my-popover}} | |
// Open | |
// {{else as |close|}} | |
// <a {{action close}}>Close</a> | |
// {{/my-popover}} | |
// | |
// Thus, to tell the containing popover to close on click, | |
// an element within the popover marks itself with | |
// [data-my-popover-close]. | |
if ($(target).is('[data-my-popover-close]')) { | |
this.set('isOpen', false); | |
} | |
}, | |
didInsertElement() { | |
this.hideObserver = ({ target, data: hideDelay }) => { | |
if (this.isDestroyed) return; | |
const $this = this.$(); | |
if ($this == null) return; | |
const $target = $(target); | |
const keepOpen = $this.find(':focus').length > 0 || | |
$this.is($target).length > 0 || | |
$this.find($target).length > 0; | |
if (keepOpen) { | |
run.cancel(this.hideCallback); | |
this.hideCallback = null; | |
} else { | |
this.hideCallback = this.hideCallback || run.later(this, 'set', 'isOpen', false, hideDelay); | |
} | |
}; | |
$('body') | |
.on('click.my-popover', 0, this.hideObserver) | |
.on('focusin.my-popover', 0, this.hideObserver) | |
.on('mouseover.my-popover', HIDE_DELAY, this.hideObserver); | |
}, | |
willDestroyElement() { | |
$('body') | |
.off('click.my-popover', this.hideObserver) | |
.off('focusin.my-popover', this.hideObserver) | |
.off('mouseover.my-popover', this.hideObserver); | |
}, | |
actions: { | |
open() { | |
this.set('isOpen', true); | |
}, | |
close() { | |
this.set('isOpen', false); | |
} | |
} | |
}); |
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
body { | |
margin: 12px 16px; | |
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; | |
font-size: 12pt; | |
} | |
.my-popover__target { | |
cursor: pointer; | |
} | |
.my-popover__target:focus { | |
color: red; | |
text-decoration: underline; | |
} | |
.my-popover__content { | |
background: white; | |
border: 1px solid grey; | |
border-radius: 5px; | |
margin-top: 5px; | |
padding: 5px; | |
position: absolute; | |
z-index: 100; | |
} | |
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
{ | |
"version": "0.10.1", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": true, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js", | |
"ember": "2.6.0", | |
"ember-data": "2.6.1", | |
"ember-template-compiler": "2.6.0" | |
}, | |
"addons": {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment