diff --git a/CHANGELOG.md b/CHANGELOG.md index cfef4401..0fcdbad5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -261,7 +261,12 @@ This comes from the fact that Owl 2 supports fragments (arbitrary content). Migration: if one need a reference to the root htmlelement of a template, it is suggested to simply add a `ref` on it, and access the reference as needed. -Documentation: [Refs](doc/reference/refs.md) +Another way to get access to a root node or html element is the hook `useRoots` + +Documentation: +- [Refs](doc/reference/refs.md) +- [useRoots](doc/reference/hooks.md#useroots) + ### 10. style/class on components are now regular props diff --git a/doc/readme.md b/doc/readme.md index 36fe630f..98cac833 100644 --- a/doc/readme.md +++ b/doc/readme.md @@ -38,6 +38,7 @@ Other hooks: - [`useRef`](reference/hooks.md#useref): get an object representing a reference (`t-ref`) - [`useChildSubEnv`](reference/hooks.md#usesubenv-and-usechildsubenv): extend the current env with additional information (for child components) - [`useSubEnv`](reference/hooks.md#usesubenv-and-usechildsubenv): extend the current env with additional information (for current component and child components) +- [`useRoots`](reference/hooks.md#useroots): returns an object that provides access to all root nodes or htmlelements Utility/helpers: diff --git a/doc/reference/hooks.md b/doc/reference/hooks.md index e86369e9..098eb594 100644 --- a/doc/reference/hooks.md +++ b/doc/reference/hooks.md @@ -13,6 +13,7 @@ - [`useComponent`](#usecomponent) - [`useEnv`](#useenv) - [`useEffect`](#useeffect) + - [`useRoots`](#useroots) - [Example: Mouse Position](#example-mouse-position) ## Overview @@ -288,6 +289,42 @@ class SomeComponent extends Component { } ``` +### `useRoots` + +`useRoots` is an alternative way to get a reference to the root notes or elements +of a component. It may be useful in some cases where `useRef` cannot be applied, +such as a higher order component, or a component with only text as content. + +The return value of `useRoots` is an object with the following key/values: + +- `node`: getter that evaluates to the first node of the component content (or null) +- `elem`: getter that evaluates to the first HTMLElement of the component content (or null) +- `nodes`: iterator that returns all content nodes +- `elems`: iterator that returns all HTMLElement nodes + +```js +class Child extends Component { + static template = xml`
some content
`; +} + +class Parent extends Component { + static template = xml` implements VNode bla paragraph bla paragraph