|
1 | | -import { html, nothing, render, LitElement } from "lit"; |
2 | | -import { ContextProvider } from "@lit/context"; |
3 | | -import { createContext } from "@lit/context"; |
4 | | -import { getReadTheDocsConfig } from "./readthedocs-config.js"; |
| 1 | +import { |
| 2 | + ContextProvider, |
| 3 | + ContextRoot, |
| 4 | + createContext, |
| 5 | +} from "@lit/context"; |
5 | 6 | import { EVENT_READTHEDOCS_ADDONS_DATA_READY } from "./events"; |
6 | 7 |
|
| 8 | +export const contextRoot = new ContextRoot().attach(document.body); |
7 | 9 | export const configContext = createContext(Symbol("readthedocs-config")); |
8 | 10 |
|
9 | | -export class AddonsApp extends LitElement { |
10 | | - config = new ContextProvider(this, { context: configContext }); |
| 11 | +/** |
| 12 | + * Because `config` provider is not attached to a ReactiveElement, and is |
| 13 | + * instead connected to `document.html`, we have to call `hostConnected()` |
| 14 | + * manually. See: |
| 15 | + * |
| 16 | + * https://github.com/lit/lit/blob/935697d47e62ed75e3157423400163a8371c62fc/packages/context/src/lib/controllers/context-provider.ts#L55-L58 |
| 17 | + **/ |
| 18 | +const config = new ContextProvider(document.documentElement, { |
| 19 | + context: configContext, |
| 20 | +}); |
| 21 | +config.hostConnected(); |
11 | 22 |
|
12 | | - connectedCallback() { |
13 | | - super.connectedCallback(); |
14 | | - document.addEventListener( |
15 | | - EVENT_READTHEDOCS_ADDONS_DATA_READY, |
16 | | - this._handleAddonsDataReady, |
17 | | - ); |
18 | | - } |
19 | | - |
20 | | - createRenderRoot() { |
21 | | - return this; |
22 | | - } |
23 | | - |
24 | | - _handleAddonsDataReady = (event) => { |
25 | | - console.log("_handleAddonsDataReady"); |
26 | | - this.config.setValue(event.detail.data()); |
27 | | - }; |
28 | | -} |
29 | | - |
30 | | -customElements.define("readthedocs-context", AddonsApp); |
| 23 | +document.addEventListener(EVENT_READTHEDOCS_ADDONS_DATA_READY, (event) => { |
| 24 | + console.log("Event:", EVENT_READTHEDOCS_ADDONS_DATA_READY); |
| 25 | + config.setValue(event.detail.data()); |
| 26 | +}); |
0 commit comments