Skip to content

Commit 4ada43e

Browse files
committed
Use ContextRoot at document.html instead
1 parent 1d340fd commit 4ada43e

File tree

3 files changed

+33
-40
lines changed

3 files changed

+33
-40
lines changed

public/index.html

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ <h2 id="docdiff">CustomEvent</h2>
2323
<p>Project slug using <em>CustomEvent</em>: <span id="custom-event-project-slug"></span></p>
2424

2525
<h2 id="flyout">Flyout</h2>
26-
<readthedocs-context>
27-
<readthedocs-flyout></readthedocs-flyout>
28-
</readthedocs-context>
26+
<readthedocs-flyout></readthedocs-flyout>
2927

3028
<h2 id="docdiff">DocDiff</h2>
3129
<p>Visit <a href="docdiff.html">this page</a> to take a look at it.</p>
@@ -62,13 +60,11 @@ <h2>Link Previews</h2>
6260
</div>
6361

6462
<h2 id="notification">Notification</h2>
65-
<readthedocs-context>
66-
<readthedocs-notification class="raised toast"></readthedocs-notification>
67-
<readthedocs-notification class="inverted"></readthedocs-notification>
68-
<readthedocs-notification class="inverted raised"></readthedocs-notification>
69-
<readthedocs-notification class="inverted" style="--readthedocs-notification-background-color: purple;"></readthedocs-notification>
70-
<readthedocs-notification class="titled raised"></readthedocs-notification>
71-
<readthedocs-notification class="titled inverted raised"></readthedocs-notification>
72-
</readthedocs-context>
63+
<readthedocs-notification class="raised toast"></readthedocs-notification>
64+
<readthedocs-notification class="inverted"></readthedocs-notification>
65+
<readthedocs-notification class="inverted raised"></readthedocs-notification>
66+
<readthedocs-notification class="inverted" style="--readthedocs-notification-background-color: purple;"></readthedocs-notification>
67+
<readthedocs-notification class="titled raised"></readthedocs-notification>
68+
<readthedocs-notification class="titled inverted raised"></readthedocs-notification>
7369
</body>
7470
</html>

src/context.js

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
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";
56
import { EVENT_READTHEDOCS_ADDONS_DATA_READY } from "./events";
67

8+
export const contextRoot = new ContextRoot().attach(document.body);
79
export const configContext = createContext(Symbol("readthedocs-config"));
810

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();
1122

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+
});

src/flyout.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ export class FlyoutElement extends LitElement {
4444
this.floating = true;
4545
this.position = "bottom-right";
4646
this.readthedocsLogo = READTHEDOCS_LOGO;
47-
console.log("Flyout _config (from constructor() method)");
48-
console.log(this._config.value);
47+
console.log(
48+
"Flyout _config (from constructor() method)",
49+
this._config.value,
50+
);
4951
}
5052

5153
_close() {
@@ -306,8 +308,7 @@ export class FlyoutElement extends LitElement {
306308

307309
render() {
308310
// The element doesn't yet have our config, don't render it.
309-
console.log("Flyout config (from render() method )");
310-
console.log(this._config.value);
311+
console.log("Flyout config (from render() method )", this._config.value);
311312

312313
// Validate the context (`this._config.value`) on each update and return
313314
// nothing if it's invalid. ``nothing`` is a special Lit response type.

0 commit comments

Comments
 (0)