ember-cli-textarea-autosize
Version:
An Ember addon that provides a textarea component that adjusts its height according to the supplied text. Included are also several mixins that can be used to ensure `autofocus` works properly, focused text inputs have their text selected, and ctrl+enter
42 lines (37 loc) • 1.23 kB
JavaScript
import autosize from 'autosize';
import { get } from '@ember/object';
import Mixin from '@ember/object/mixin';
import { isPresent } from '@ember/utils';
/**
* A mixin for attaching autosize to a component's element. Is also capable of reading the `min-height` and
* `max-height` property to initialize the extents of the component's height.
* @see http://www.jacklmoore.com/autosize/
*/
export default Mixin.create({
/**
* Once this textarea is being destroyed let's help clean up the DOM by removing the autosize binding.
* @see http://www.jacklmoore.com/autosize/
*/
willDestroyElement() {
autosize.destroy(this.element);
},
/**
* Once this textarea is inserted in the DOM initialize on autosize.
* @see https://github.com/jackmoore/autosize
*/
didInsertElement() {
autosize(this.element);
this._setCss('min-height');
this._setCss('max-height');
},
/**
* Set the css property `propertyName` with the component property `propertyName`, e.g. `min-height`.
* @param propertyName the css property.
* @private
*/
_setCss(propertyName) {
if (isPresent(get(this, propertyName))) {
this.element.style[propertyName] = get(this, propertyName);
}
},
});