diff --git a/.local.dic b/.local.dic index 2290e12f4a..2685e20e97 100644 --- a/.local.dic +++ b/.local.dic @@ -52,6 +52,8 @@ debounce declaratively DefinitelyTyped deps +destructor +destructors dev draggable dropdown diff --git a/guides/release/components/element-modifiers.md b/guides/release/components/element-modifiers.md new file mode 100644 index 0000000000..544cd12ccd --- /dev/null +++ b/guides/release/components/element-modifiers.md @@ -0,0 +1,400 @@ +Element modifiers are functions that run when an element is rendered, letting you attach behavior directly to that element. They are the bridge between Ember's declarative templates and the imperative, JavaScript side of the DOM: event listeners, focus management, measuring, third-party widget libraries, and other side effects that HTML alone can't express. + +A modifier appears inside the opening tag of an element, in double curly braces: + +```gjs +import autofocus from 'my-app/modifiers/autofocus'; + + +``` + +When Ember inserts the `` element into the DOM, it calls the modifier with that element. If the modifier sets anything up that needs to be undone later—like an event listener or an observer—it can provide cleanup that Ember runs automatically when the element is removed. + +The previous chapter, [Template Lifecycle, DOM, and Modifiers](../template-lifecycle-dom-and-modifiers/), explains _when_ to reach for a modifier instead of an attribute. This chapter covers modifiers themselves: the built-in `{{on}}` modifier, and how to write your own. + +## Rules of Modifiers + +A few things are true of every modifier: + +- Modifiers run **after** the element they are attached to has been created and inserted into the DOM. They are passed that element as their first argument. +- Modifiers do **not** run during server-side rendering, since there is no real DOM on the server to hand to them. +- Modifiers can run again when tracked state they used changes. Before a modifier re-runs, and when its element is removed, its cleanup function (if any) runs. +- The order in which multiple modifiers on the same element run is not guaranteed, so avoid writing modifiers that depend on the effects of other modifiers. + +Like components and helpers in `