-
-
Notifications
You must be signed in to change notification settings - Fork 407
Modifiers #353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Modifiers #353
Changes from 6 commits
fa68f50
e32f332
1330b4c
807aa2d
7c63603
4e6d89f
929499c
99d057b
28928e8
16f580d
7ff2c2a
4c31657
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,242 @@ | ||
- Start Date: (fill me in with today's date, YYYY-MM-DD) | ||
- RFC PR: (leave this empty) | ||
- Ember Issue: (leave this empty) | ||
|
||
# Element Modifiers | ||
|
||
## Summary | ||
|
||
This RFC introduces the concept of user defined element modifiers. Unlike a component, there is no template/layout for an element modifier. Unlike a helper, an element modifier does not return a value. | ||
|
||
Below is an example of the element modifier syntax: | ||
|
||
```hbs | ||
<button {{add-event-listener 'click' (action 'save')}}>Save</button> | ||
``` | ||
|
||
This RFC supercedes the [original element modifiers RFC](https://github.com/emberjs/rfcs/pull/112) and is intended to replace the [`this.bounds` RFC](https://github.com/emberjs/rfcs/pull/351). | ||
|
||
## Motivation | ||
|
||
Classic component instances have a `this.element` property which provides you a single DOM node as defined by `tagName`. The children of this node will be the DOM representation of what you wrote in your template. Templates are typically referred to having `innerHTML` semantics in classic components since there is a single wrapping element that is the parent of the template. These semantics allow for components to encapsulate some 3rd party JavaScript library or do some fine grain DOM manipulation. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe s/classic components/Ember.Component/ ? |
||
|
||
Glimmer components have `outerHTML` semantics, meaning what you see in the template is what you get in the DOM, there is no `tagName` that wraps the template. While this drastically simplifies the API for creating new components it makes reliable access to the a component's DOM structure very difficult to do. As [pointed out](https://github.com/emberjs/rfcs/pull/351#issuecomment-412123046) in the [Bounds RFC](https://github.com/emberjs/rfcs/pull/351) the stability of the nodes in the `Bounds` object creates way too many footguns. So a formalized way for accessing DOM in the Glimmer component world is still needed. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/Glimmer components/custom components/ (we don't have "glimmer components" yet 😛, but "custom components" are included in 3.4.0). |
||
|
||
Element modifiers allow for stable access of the DOM node they are installed on. This allows for programatic assess to DOM in Glimmer templates and also offers a more targeted construct for cases where classic components were being used. The introduction of this API will likely result in the proliferation of one or several popular addons for managing element event listeners, style and animation. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
## Detailed design | ||
|
||
### Invocation | ||
|
||
An element modifier is invoked in "element space". This is the space between `<` and `>` opening an HTML tag. For example: | ||
|
||
```hbs | ||
<button {{flummux}}></button> | ||
<span {{whipperwill 'carrot'}}><i>Some DOM</i></span> | ||
<b {{crum bing='whoop'}} zip="bango">Hm...</b> | ||
``` | ||
|
||
Element modifiers may be invoked with params or hash arguments. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you mean they can accept positional and/or named arguments, right? As written this infers only positional or named are allowed at once (and that you can't use both). |
||
|
||
### Definition and lookup | ||
|
||
A basic element modifier is defined with the type of `modifier`. For example these paths would be global element modifiers in an application: | ||
|
||
``` | ||
Classic paths: | ||
|
||
app/modifiers/flummux.js | ||
app/modifiers/whipperwill.js | ||
|
||
MU paths: | ||
|
||
src/ui/components/flummux/modifier.js | ||
src/ui/routes/posts/-components/whipperwill/modifier.js | ||
``` | ||
|
||
In Module Unification, modifiers live within the generalized collection type "components" [as specified](https://github.com/dgeb/rfcs/blob/module-unification/text/0000-module-unification.md#components). Modifiers, like component and helpers, are eligible for local lookup. For example: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this link should probably not reference |
||
|
||
``` | ||
MU paths: | ||
|
||
src/ui/routes/posts/-components/post-editor/flummux/modifier.js | ||
``` | ||
|
||
The element modifier class is a default export from these files. For example: | ||
|
||
```js | ||
import Modifier from '@ember/modifier'; | ||
|
||
export default class extends Modifier {} | ||
``` | ||
|
||
### Lifecycle Hooks | ||
|
||
During rendering and teardown of a target element, any attached element modifiers will execute a series of hooks. These hooks are: | ||
|
||
* `didInsertElement` | ||
* `didUpdate` | ||
* `willDestroyElement` | ||
|
||
It is important to note that in server-side rendering environments none of the lifecycle events are called. | ||
|
||
#### `didInsertElement` semantics | ||
|
||
`didInsertElement` is called when the element is in the DOM and it's children are attached. The element is available on `this.element` of the modifier instance. Unlike classic components, the `didInsertElement` hook receives the positional and named params as arguments in the same way helpers do. This hook is only called once. | ||
|
||
This hook has the following timing semantics: | ||
|
||
**Always** | ||
- called **after** all children modifiers `didInsertElement` hook are called | ||
- called **after** DOM insertion | ||
|
||
**May or May Not** | ||
- be called in the same tick as DOM insertion | ||
- have the sibling nodes fully initialized in DOM | ||
|
||
Below is an example of how this hook could be used: | ||
|
||
```js | ||
import Modifier from '@ember/modifier'; | ||
|
||
export default class extends Modifier { | ||
this.listenerOptions = undefined; | ||
didInsertElement([ eventType, callback ], eventOptions) { | ||
this.listenerOptions = [ | ||
eventType, | ||
callBack, | ||
eventOptions | ||
]; | ||
this.element.addEventListener(eventType, callback, eventOptions); | ||
} | ||
} | ||
``` | ||
|
||
#### `didUpdate` semantics | ||
|
||
`didUpdate` is called whenever any of the parameters used by the modifier are updated. `didUpdate` has the same signature as `didInsertElement`. | ||
|
||
This hook has the following timing semantics: | ||
|
||
**Always** | ||
- called **after** the arguments to the modifier have changed | ||
|
||
**Never** | ||
- called if the arguments to the modifier are constants | ||
|
||
Below is an example of how this hook could be used: | ||
|
||
```js | ||
import Modifier from '@ember/modifier'; | ||
|
||
export default class extends Modifier { | ||
this.listenerOptions = undefined; | ||
didInsertElement([ eventType, callback ], eventOptions) { | ||
this.listenerOptions = [ | ||
eventType, | ||
callBack, | ||
eventOptions | ||
]; | ||
this.element.addEventListener(eventType, callback, eventOptions); | ||
}, | ||
|
||
didUpdate([ eventType, callback ], eventOptions) { | ||
this.element.removeEventListener(...this.listenerOptions); | ||
this.element.addEventListener(eventType, callback, eventOptions); | ||
this.listenerOptions = [eventType, callback, eventOptions]; | ||
} | ||
} | ||
``` | ||
|
||
#### `willDestroyElement` semantics | ||
|
||
`willDestroyElement` is called during the destruction of a template. It receives no arguments. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The wording here seems odd. It doesn't seem that "destruction of a template" is actually the limit of {{#if someCondition}}
<div {{flummux foo bar}}></div>
{{/if}} When |
||
|
||
Below is an example of how this hook could be used: | ||
|
||
```js | ||
import Modifier from '@ember/modifier'; | ||
|
||
export default class extends Modifier { | ||
this.listenerOptions = undefined; | ||
didInsertElement([ eventType, callback ], eventOptions) { | ||
this.listenerOptions = [ | ||
eventType, | ||
callBack, | ||
eventOptions | ||
]; | ||
this.element.addEventListener(eventType, callback, eventOptions); | ||
}, | ||
|
||
didUpdate([ eventType, callback ], eventOptions) { | ||
this.element.removeEventListener(...this.listenerOptions); | ||
this.element.addEventListener(eventType, callback, eventOptions); | ||
this.listenerOptions = [eventType, callback, eventOptions]; | ||
} | ||
|
||
willDestroyElement() { | ||
this.element.removeEventListener(...this.listenerOptions); | ||
} | ||
} | ||
``` | ||
|
||
This hook has the following timing semantics: | ||
|
||
**Always** | ||
- called **after** all children modifier's `willDestroyElement` hook is called | ||
This comment was marked as resolved.
Sorry, something went wrong.
This comment was marked as resolved.
Sorry, something went wrong. |
||
|
||
**May or May Not** | ||
- be called in the same tick as DOM removal | ||
|
||
## How we teach this | ||
|
||
While user defined element modifiers are a new concept, Ember has had the [`{{action}}` modifier](https://www.emberjs.com/api/ember/release/classes/Ember.Templates.helpers/methods/action?anchor=action) since 1.0.0. We would need to update the existing documentation around `{{action}}` to refer to it as a modifier. In terms of documenting the modifier base class, I think we can largely repurpose existing documentation around [component's `didInsertElement`](https://guides.emberjs.com/release/components/the-component-lifecycle/#toc_integrating-with-third-party-libraries-with-didinsertelement) hook. | ||
|
||
In terms of guides, I believe we should add a section to the "Templates" section to outline how to write modifiers. This would be similar to the ["Writing Helpers"](https://guides.emberjs.com/release/templates/writing-helpers/) guide. | ||
|
||
## Drawbacks | ||
|
||
The drawbacks of adding element modifiers largely deal with explaining when to use a classic component for encapsulating some DOM manipulation versus when to use an element modifier. That being said it is likely that the recommendation would be to use element modifiers if you are just manually modifying the DOM. | ||
|
||
This API also doesn't attempt to create a corollary of `this.element` for Glimmer Components and instead offers a different API for altering DOM nodes directly. This expands the surface area of Ember's API. | ||
|
||
## Alternatives | ||
|
||
The alternative to this is to create a "ref"-like API that is available in [other client side frameworks](https://reactjs.org/docs/refs-and-the-dom.html). This may look like the following: | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
|
||
```hbs | ||
<section> | ||
<h1 {{ref "heading"}}>Hello!</h1> | ||
<p>How are you?</p> | ||
</section> | ||
``` | ||
|
||
```js | ||
import Component from '@glimmer/component'; | ||
|
||
export class extends Component { | ||
headingNode = null; | ||
heading(headingElement) { | ||
if (headingElement) { | ||
this.headingElement = headingElement; | ||
headingElement.addEventListener('click', this.click); | ||
} else { | ||
this.headingElement.removeEventListener('click', this.click); | ||
this.headingElement = null; | ||
} | ||
} | ||
|
||
click(evt) { | ||
evt.preventDefault(); | ||
alert('click'); | ||
} | ||
} | ||
``` | ||
|
||
This API would be implemented as a framework level modifier and would call hooks on the backing class with the element the `{{ref}}` was installed on. When the element is being removed the method would be called with `null`. It's the component author's responsibility to manage the DOM node. | ||
|
||
This RFC does not close the door on a `{{ref}}`-like API, but rather exposes the primitives needed to create it. | ||
|
||
## Unresolved questions | ||
|
||
TBD? |
Uh oh!
There was an error while loading. Please reload this page.