Skip to content

Commit

Permalink
remove autoshadowroot
Browse files Browse the repository at this point in the history
  • Loading branch information
keithamus committed May 4, 2022
1 parent 92e357a commit 9b0b963
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 94 deletions.
34 changes: 28 additions & 6 deletions docs/_guide/rendering.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ Remember to _always_ make your JavaScript progressively enhanced, where possible

By leveraging the native [`ShadowDOM`](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_shadow_DOM) feature, Catalyst components can render complex sub-trees, fully encapsulated from the rest of the page.

Catalyst will automatically look for elements that match the `template[data-shadowroot]` selector, within your controller. If it finds one as a direct-child of your controller, it will use that to create a shadowRoot.
[Actions]({{ site.baseurl }}/guide/actions) and [Targets]({{ site.baseurl }}/guide/targets) all work within an elements ShadowRoot.

Catalyst Controllers will search for a direct child of `template[data-shadowroot]` and load its contents as the `shadowRoot` of the element. [Actions]({{ site.baseurl }}/guide/actions) and [Targets]({{ site.baseurl }}/guide/targets) all work within an elements ShadowRoot.
You can also leverage the [declarative shadow DOM](https://web.dev/declarative-shadow-dom/) and render a template inline to your HTML, which will automatically be attached (this may require a polyfill for browsers which are yet to support this feature).

### Example

```html
<hello-world>
<template data-shadowroot>
<template shadowroot="open">
<p>
Hello <span data-target="hello-world.nameEl">World</span>
</p>
Expand All @@ -41,12 +41,34 @@ class HelloWorldElement extends HTMLElement {
}
```

Providing the `<template data-shadowroot>` element as a direct child of the `hello-world` element tells Catalyst to render the templates contents automatically, and so all `HelloWorldElements` with this template will be rendered with the contents.

{% capture callout %}
Remember that _all_ instances of your controller _must_ add the `<template data-shadowroot>` HTML. If an instance does not have the `<template data-shadowroot>` as a direct child, then the shadow DOM won't be rendered for it!
Remember that _all_ instances of your controller _must_ add the `<template shadowroot>` HTML. If an instance does not have the `<template data-shadowroot>` as a direct child, then the shadow DOM won't be rendered for it!
{% endcapture %}{% include callout.md %}


It is also possible to attach a shadowRoot to your element during the `connectedCallback`, like so:

```typescript
import { controller, target } from "@github/catalyst"

@controller
class HelloWorldElement extends HTMLElement {
@target nameEl: HTMLElement
get name() {
return this.nameEl.textContent
}
set name(value: string) {
this.nameEl.textContent = value
}

connectedCallback() {
this.attachShadow({ mode: 'open' }).innerHTML = `<p>
Hello <span data-target="hello-world.nameEl">World</span>
</p>`
}
}
```

### Updating a Template element using JS templates

Sometimes you wont have a template that is server rendered, and instead want to make a template using JS. Catalyst does not support this out of the box, but it is possible to use another library: `@github/jtml`. This library can be used to write declarative templates using JS. Let's re-work the above example using `@github/jtml`:
Expand Down
11 changes: 0 additions & 11 deletions src/auto-shadow-root.ts

This file was deleted.

77 changes: 0 additions & 77 deletions test/auto-shadow-root.ts

This file was deleted.

0 comments on commit 9b0b963

Please sign in to comment.