Skip to content

Commit 04083bf

Browse files
committed
remove autoshadowroot
1 parent 20a9b39 commit 04083bf

File tree

7 files changed

+29
-127
lines changed

7 files changed

+29
-127
lines changed

docs/_guide/rendering.md

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ Remember to _always_ make your JavaScript progressively enhanced, where possible
1111

1212
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.
1313

14-
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.
14+
[Actions]({{ site.baseurl }}/guide/actions) and [Targets]({{ site.baseurl }}/guide/targets) all work within an elements ShadowRoot.
1515

16-
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.
16+
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).
1717

1818
### Example
1919

2020
```html
2121
<hello-world>
22-
<template data-shadowroot>
22+
<template shadowroot="open">
2323
<p>
2424
Hello <span data-target="hello-world.nameEl">World</span>
2525
</p>
@@ -41,12 +41,34 @@ class HelloWorldElement extends HTMLElement {
4141
}
4242
```
4343

44-
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.
45-
4644
{% capture callout %}
47-
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!
45+
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!
4846
{% endcapture %}{% include callout.md %}
4947

48+
49+
It is also possible to attach a shadowRoot to your element during the `connectedCallback`, like so:
50+
51+
```typescript
52+
import { controller, target } from "@github/catalyst"
53+
54+
@controller
55+
class HelloWorldElement extends HTMLElement {
56+
@target nameEl: HTMLElement
57+
get name() {
58+
return this.nameEl.textContent
59+
}
60+
set name(value: string) {
61+
this.nameEl.textContent = value
62+
}
63+
64+
connectedCallback() {
65+
this.attachShadow({ mode: 'open' }).innerHTML = `<p>
66+
Hello <span data-target="hello-world.nameEl">World</span>
67+
</p>`
68+
}
69+
}
70+
```
71+
5072
### Updating a Template element using JS templates
5173

5274
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`:

docs/_guide/your-first-component.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,14 @@ The `@controller` decorator ties together the various other decorators within Ca
4343
- Calls `defineObservedAttributes` with the class to add map any `@attr` decorators. See [attrs]({{ site.baseurl }}/guide/attrs) for more on this.
4444
- Injects the following code inside of the `connectedCallback()` function of your class:
4545
- `bind(this)`; ensures that as your element connects it picks up any `data-action` handlers. See [actions]({{ site.baseurl }}/guide/actions) for more on this.
46-
- `autoShadowRoot(this)`; ensures that your element loads any `data-shadowroot` templates. See [rendering]({{ site.baseurl }}/guide/rendering) for more on this.
4746
- `initializeAttrs(this)`; ensures that your element binds any `data-*` attributes to props. See [attrs]({{ site.baseurl }}/guide/attrs) for more on this.
4847

4948
You can do all of this manually; for example here's the above `HelloWorldElement`, written without the `@controller` annotation:
5049

5150
```js
52-
import {bind, autoShadowRoot, initializeAttrs, defineObservedAttributes} from '@github/catalyst'
51+
import {bind, initializeAttrs, defineObservedAttributes} from '@github/catalyst'
5352
class HelloWorldElement extends HTMLElement {
5453
connectedCallback() {
55-
autoShadowRoot(this)
5654
initializeAttrs(this)
5755
this.innerHTML = 'Hello World!'
5856
bind(this)

src/auto-shadow-root.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/core.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {register} from './register.js'
22
import {bind, bindShadow} from './bind.js'
3-
import {autoShadowRoot} from './auto-shadow-root.js'
43
import {defineObservedAttributes, initializeAttrs} from './attr.js'
54
import type {CustomElementClass} from './custom-element.js'
65

@@ -53,7 +52,6 @@ export class CatalystDelegate {
5352
connectedCallback(instance: HTMLElement, connectedCallback: () => void) {
5453
instance.toggleAttribute('data-catalyst', true)
5554
customElements.upgrade(instance)
56-
autoShadowRoot(instance)
5755
initializeAttrs(instance)
5856
bind(instance)
5957
connectedCallback?.call(instance)

src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@ export {findTarget, findTargets} from './findtarget.js'
44
export {target, targets} from './target.js'
55
export {controller} from './controller.js'
66
export {attr, initializeAttrs, defineObservedAttributes} from './attr.js'
7-
export {autoShadowRoot} from './auto-shadow-root.js'

test/auto-shadow-root.ts

Lines changed: 0 additions & 80 deletions
This file was deleted.

test/controller.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -65,30 +65,6 @@ describe('controller', () => {
6565
expect(instance.foo).to.have.callCount(1)
6666
})
6767

68-
it('binds auto shadowRoots', async () => {
69-
@controller
70-
class ControllerBindAutoShadowElement extends HTMLElement {
71-
foo() {
72-
return 'foo'
73-
}
74-
}
75-
instance = await fixture<ControllerBindAutoShadowElement>(html`
76-
<controller-bind-auto-shadow>
77-
<template data-shadowroot="open">
78-
<button data-action="click:controller-bind-auto-shadow#foo" />
79-
</template>
80-
</controller-bind-auto-shadow>
81-
`)
82-
replace(instance, 'foo', fake(instance.foo))
83-
84-
expect(instance.shadowRoot).to.exist
85-
expect(instance).to.have.property('shadowRoot').not.equal(null)
86-
expect(instance.shadowRoot!.children).to.have.lengthOf(1)
87-
instance.shadowRoot!.querySelector('button')!.click()
88-
89-
expect(instance.foo).to.have.callCount(1)
90-
})
91-
9268
it('upgrades child decendants when connected', async () => {
9369
@controller
9470
// eslint-disable-next-line @typescript-eslint/no-unused-vars

0 commit comments

Comments
 (0)