Skip to content

Commit b557a8b

Browse files
authored
Merge pull request #1946 from ember-learn/mixonic/modifier-docs
2 parents c72ecd5 + 1031576 commit b557a8b

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed

guides/release/components/template-lifecycle-dom-and-modifiers.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,17 @@ The modifier that we're going to build will allow us to say:
293293

294294
Pretty nice, right?
295295

296-
To accomplish that, we'll create a modifier in `app/modifiers/autofocus.js`. First, install [`ember-modifier`](https://github.com/ember-modifier/ember-modifier) and then generate an `autofocus` modifier for your app:
296+
New Ember apps ship with a dependency on
297+
[ember-modifier](https://github.com/ember-modifier/ember-modifier), which
298+
provides a friendly API for writing your own element modifiers. This library is
299+
in turn based on a low level API named _modifier managers_. Managers are a
300+
framework-development level feature, and not something most developers need to
301+
interact with. You'll see in the following examples that the modifier API is
302+
imported from the `ember-modifier` package.
303+
304+
First generate the `autofocus` modifier for your application:
297305

298306
```bash
299-
ember install ember-modifier
300307
ember generate modifier autofocus
301308
```
302309

@@ -308,9 +315,10 @@ import { modifier } from "ember-modifier";
308315
export default modifier(element => element.focus());
309316
```
310317

311-
And that's it!
318+
And that's it! Now we can use our custom `{{autofocus}}` modifier throughout our application.
312319

313-
Now we can use our custom `{{autofocus}}` modifier throughout our application.
320+
Read more about the `ember-modifier` APIs at [ember-modifiers:
321+
Usage](https://github.com/ember-modifier/ember-modifier#usage).
314322

315323
## Communicating Between Elements in a Component
316324

@@ -390,7 +398,6 @@ export default class AudioPlayerComponent extends Component {
390398
That's it for the component: we're translating the user's interactions into _state_. Now we need to build a modifier to translate the state into the appropriate DOM method calls!
391399

392400
```bash
393-
ember install ember-modifier
394401
ember generate modifier play-when
395402
```
396403

@@ -448,12 +455,11 @@ document.addEventListener("click", event => {
448455

449456
The most important difference between this example and the cases we've seen so far is that we need to remove the `click` event handler from the document when this element is destroyed.
450457

451-
To accomplish this, we can use [`ember-modifier`](https://github.com/ember-modifier/ember-modifier) to create a `on-click-outside` modifier that sets up the event listener after the element is first inserted and removes the event listener when the element is removed.
458+
To accomplish this, we can use [`ember-modifier`](https://github.com/ember-modifier/ember-modifier) (which is already installed in newly generated Ember apps) to create a `on-click-outside` modifier that sets up the event listener after the element is first inserted and removes the event listener when the element is removed.
452459

453-
Run the following commands to install the addon and generate a new modifier:
460+
Generate the new modifier:
454461

455462
```bash
456-
ember install ember-modifier
457463
ember generate modifier on-click-outside
458464
```
459465

guides/release/upgrading/current-edition/glimmer-components.md

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -784,15 +784,17 @@ functionality that lifecycle hooks contained.
784784
785785
#### Writing your own modifiers
786786
787-
There are also community APIs available for writing your own modifiers, such as
788-
[ember-modifier](https://github.com/ember-modifier/ember-modifier).
789-
Ember itself has low level APIs known as _modifier managers_ which can be used
790-
to write these higher level APIs. In general, it's recommended to use a
791-
community addon to write modifiers, and _not_ to write your own modifier
792-
manager.
787+
New Ember apps ship with a dependency on
788+
[ember-modifier](https://github.com/ember-modifier/ember-modifier), which
789+
provides a friendly API for writing your own element modifiers. This library is
790+
in turn based on a low level API named _modifier managers_. Managers are a
791+
framework-development level feature, and not something most developers need to
792+
interact with.
793793
794-
Let's see what our first example would look like if we were to write it as a
795-
modifier using `ember-modifier`:
794+
Custom modifiers based on the `ember-modifier` API can be a more expressive
795+
interface for your logic, and can better encapsulate an implementation.
796+
797+
Let's write a modifier that implements adding an event listener.
796798

797799
```js {data-filename=app/modifiers/add-event-listener.js}
798800
import { modifier } from 'ember-modifier';
@@ -825,12 +827,16 @@ export default class ScrollComponent extends Component {
825827
</div>
826828
```
827829

828-
This modifier generalizes the functionality that the component implemented using
829-
lifecycle hooks before, so we can use this modifier whenever we need to in _any_
830-
component. This is a much better solution than manually managing event listeners
831-
every time we need one! At this point, the modifier is effectively the same as
832-
the `{{on}}` modifier as well, so we could get rid of it altogether and replace
833-
it with `on`:
830+
The new `add-event-listener` modifier presents a more expressive interface to
831+
the `hbs` template: There is only a single modifier to apply instead of two, the
832+
implementation always tears down after itself upon teardown of the target element,
833+
and the only JavaScript you have to write during re-user is the implementation
834+
of the business logic.
835+
836+
At this point, it is worth noting that the custom `{{add-event-listener}}`
837+
modifier is effectively a re-implementation of the Ember built-in `{{on}}`
838+
modifier (See the
839+
[documentation](https://api.emberjs.com/ember/5.1/classes/Ember.Templates.helpers/methods/on?anchor=on)). Using that built-in looks like:
834840

835841
```handlebars {data-filename=app/components/scroll-component.hbs}
836842
<div {{on "scroll" this.listener}}>

0 commit comments

Comments
 (0)