open-consul/ui-v2/app/components/tab-nav/index.js
John Cowen d1cbc3f032 ui: Tab Improvements (animations/branding) (#7772)
* ui: Adds a tab selection animation to our app tabs

1. Replace all mentions of `magenta` with a themeable CSS property.
2. Add an easy way to inline style DOM nodes
3. Use CSS properties to add tab animation

* Fix up rendering test

* Avoid DOM noodling as much as possible
2020-05-12 17:14:43 +00:00

45 lines
1.2 KiB
JavaScript

import Component from '@ember/component';
import { setProperties, set } from '@ember/object';
import { inject as service } from '@ember/service';
import { schedule } from '@ember/runloop';
const ENTER = 13;
const SELECTED = 'li.selected';
export default Component.extend({
name: 'tab',
tagName: '',
dom: service('dom'),
isAnimatable: false,
init: function() {
this._super(...arguments);
this.guid = this.dom.guid(this);
},
didInsertElement: function() {
this._super(...arguments);
this.$nav = this.dom.element(`#${this.guid}`);
this.select(this.dom.element(SELECTED, this.$nav));
set(this, 'isAnimatable', true);
},
didUpdateAttrs: function() {
schedule('afterRender', () => this.select(this.dom.element(SELECTED, this.$nav)));
},
select: function($el) {
if (!$el) {
return;
}
setProperties(this, {
selectedWidth: $el.offsetWidth,
selectedLeft: $el.offsetLeft,
selectedHeight: $el.offsetHeight,
selectedTop: $el.offsetTop,
});
},
actions: {
keydown: function(e) {
if (e.keyCode === ENTER) {
e.target.dispatchEvent(new MouseEvent('click'));
}
},
},
});