Skip to content

Instantly share code, notes, and snippets.

@grapho
Last active November 4, 2015 22:25
Show Gist options
  • Select an option

  • Save grapho/0cb8efb437e6b9c4d954 to your computer and use it in GitHub Desktop.

Select an option

Save grapho/0cb8efb437e6b9c4d954 to your computer and use it in GitHub Desktop.
Dropdown Menu
import Ember from 'ember';
export default Ember.Controller.extend({
appName:'Dropdown Component',
menuSelectionOptions: [
Ember.Object.create({
label: "Case#: 123",
value: 1
}),
Ember.Object.create({
label: "Case#: 45699",
value: 2
}),
Ember.Object.create({
label: "Case#: 789",
value: 3
})
],
menuSelection: null,
});
<h1>{{appName}}</h1>
<div class="menu-bar">
{{dropdown-menu items=menuSelectionOptions selection=menuSelection select-item=(action (mut menuSelection))}}
</div>
import Ember from 'ember';
export default Ember.Component.extend({
tagName: 'dropdown-menu',
classNameBindings: ['hasFocus'],
attributeBindings: ['tabindex'],
tabindex: "-1",
hasFocus: false,
focusIn() {
this.set('hasFocus', true);
},
focusOut() {
this.set('hasFocus', false);
},
mouseEnter() {
this.set('hasFocus', true);
},
mouseLeave() {
this.set('hasFocus', false);
},
didInsertElement() {
this._super();
this._setScrolling();
},
_setScrolling() {
let menu = this.$('.menu_selection-container'),
menuScrollHeight = menu.prop('scrollHeight'),
menuHeight = this.get('menuHeight');
if (menuHeight && menuHeight < menuScrollHeight) {
menu.css('maxHeight', menuHeight);
menu.addClass('scrolling');
}
else {
menu.css('maxHeight', '')
menu.removeClass('scrolling');
}
},
actions: {
selectItem(item) {
this.sendAction('select-item', item);
this.set('hasFocus', false);
}
}
});
<div class="menu_display">
<div class="menu_selected-item">
<span class="menu_icon fa fa-caret-down">H</span>
{{selection.label}}
</div>
<span class="menu_caret fa fa-caret-down">H</span>
</div>
<div class="menu_selection-container">
<ul class="menu_selection-options" role="menu">
{{#each items as |item|}}
<li role="menuitemradio">
<button type="button" {{action "selectItem" item}}>{{item.label}}</button>
</li>
{{/each}}
</ul>
</div>
body {
margin: 12px 16px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
*, *:before, *:after {
box-sizing: border-box;
}
.menu-bar {
height: 40px;
border-radius: 4px;
background: linear-gradient(#fff, #eee);
box-shadow:
0 0 2px rgba(0, 0, 0, .2),
0 1px 2px rgba(0, 0, 0, .2);
}
dropdown-menu {
display: inline-block;
height: 100%;
position: relative;
outline: 0;
box-shadow: none;
}
dropdown-menu .menu_display {
height: 100%;
display: flex;
position: relative;
transition: box-shadow 150ms;
}
dropdown-menu .menu_display .menu_caret {
display: block;
height: 16px;
width: 16px;
line-height: 16px;
text-align: center;
position: absolute;
right: 8px;
top: calc(50% - 8px);
}
dropdown-menu.has-focus .menu_display {
height: calc(100% + 1px);
background-color: #fff;
border-bottom: 1px dotted rgba(0, 0, 0, 0);
box-shadow:
0 0 2px rgba(0, 0, 0, .2),
0 2px 4px rgba(0, 0, 0, .4);
}
dropdown-menu .menu_selected-item {
margin: auto 0;
position: relative;
}
dropdown-menu .menu_display .menu_selected-item .menu_icon {
display: block;
height: 16px;
width: 16px;
line-height: 16px;
text-align: center;
position: absolute;
left: -20px;
top: calc(50% - 8px);
}
dropdown-menu .menu_selection-container {
background-color: #fff;
overflow-y: hidden;
height: 0;
outline: 0;
position: relative;
transition: 150ms;
}
dropdown-menu .menu_selection-container.scrolling {
overflow-y: scroll;
}
dropdown-menu.has-focus .menu_selection-container {
height: auto;
box-shadow:
0 0 2px rgba(0, 0, 0, .2),
0 2px 4px rgba(0, 0, 0, .4);
}
dropdown-menu .menu_selection-options {
margin: 0;
padding: 0;
list-style: none;
}
dropdown-menu .menu_display,
dropdown-menu .menu_selection-options button {
padding: 8px 32px 8px 28px;
}
dropdown-menu .menu_selection-options button {
font-size: inherit;
display: block;
width: 100%;
text-align: left;
background: none;
border: 0;
outline: 0;
}
dropdown-menu .menu_selection-options button:hover,
dropdown-menu .menu_selection-options button:focus {
background-color: rgba(0, 0, 0, .02);
}
dropdown-menu .menu_selection-options button.active {
background-color: rgba(0, 0, 0, .6);
color: #eee;
}
{
"version": "0.4.16",
"EmberENV": {
"FEATURES": {}
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.1.0/ember.debug.js",
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/2.1.0/ember-data.js",
"ember-template-compiler": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.1.0/ember-template-compiler.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment