Provides element modifiers that can be used to hook into specific portions of the rendering lifecycle.
The modifiers provided in this package are ideal for quickly migrating away from
classic Ember components to Glimmer components, because they largely allow you to
use the same lifecycle hook methods you've already written while attaching them to
these modifiers. For example, a didInsertElement
hook could be called by
{{did-insert this.didInsertElement}}
to ease your migration process.
However, we strongly encourage you to take this opportunity to rethink your
functionality rather than use these modifiers as a crutch. In many cases, classic
lifecycle hooks like didInsertElement
can be rewritten as custom modifiers that
internalize functionality manipulating or generating state from a DOM element.
Other times, you may find that a modifier is not the right fit for that logic at all,
in which case it's worth revisiting the design to find a better pattern.
Either way, we recommend using these modifiers with caution. They are very useful for quickly bridging the gap between classic components and Glimmer components. We recommend considering a custom modifier in most use-cases where you might want to reach for this package.
- Ember.js v3.20 or above
- Ember CLI v3.20 or above
- Node.js v12 or above
ember install @ember/render-modifiers
This sets the scroll position of an element, and updates it whenever the scroll position changes.
Before:
export default Component.extend({
classNames: ['scroll-container'],
didRender() {
this.element.scrollTop = this.scrollPosition;
},
});
After:
export default class Component.extend({
setScrollPosition(element, [scrollPosition]) {
element.scrollTop = scrollPosition;
}
})
This adds a CSS class to an alert element in a conditional whenever it renders to fade it in, which is a bit of an extra hoop. For CSS transitions to work, we need to append the element without the class, then add the class after it has been appended.
Before:
export default Component.extend({
didRender() {
let alert = this.element.querySelector('.alert');
if (alert) {
alert.classList.add('fade-in');
}
},
});
After:
export default Component.extend({
fadeIn(element) {
element.classList.add('fade-in');
},
});
One key thing to know about {{did-update}}
is it will not rerun whenever the
contents or attributes on the element change. For instance, {{did-update}}
will not rerun when @type
changes here:
If {{did-update}}
should rerun whenever a value changes, the value should be
passed as a parameter to the modifier. For instance, a textarea which wants to
resize itself to fit text whenever the text is modified could be setup like
this:
export default Component.extend({
resizeArea(element) {
element.style.height = `${element.scrollHeight}px`;
},
});
This is the type of rendering done by libraries like ember-leaflet
, which use
components to control the rendering of the library, but without any templates
themselves. The underlying library for this is here.
This is a simplified example of how you could accomplish this with Glimmer
components and element modifiers.
Node component:
// components/node.js
export default Component.extend({
init() {
super(...arguments);
this.children = new Set();
this.parent.registerChild(this);
}
willDestroy() {
super(...arguments);
this.parent.unregisterChild(this);
}
registerChild(child) {
this.children.add(child);
}
unregisterChild(child) {
this.children.delete(child);
}
didInsertNode(element) {
// library setup code goes here
this.children.forEach(c => c.didInsertNode(element));
}
willDestroyNode(element) {
// library teardown code goes here
this.children.forEach(c => c.willDestroyNode(element));
}
}
Root component:
// components/root.js
import NodeComponent from './node.js';
export default NodeComponent.extend();
Usage:
See the Contributing guide for details.
This project is licensed under the MIT License.