Skip to content

Commit 86d0952

Browse files
agjohnsonhumitos
authored andcommitted
Use ContextRoot at document.html instead (#499)
Additional elements shouldn't be needed in our structure. This is how we can use ContextRoot with `document.html` as the context root for providers/consumers of the config context.
1 parent c305707 commit 86d0952

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

public/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ <h1 id="documentation-addons">Documentation Addons</h1>
2222
<h2 id="docdiff">CustomEvent</h2>
2323
<p>Project slug using <em>CustomEvent</em>: <span id="custom-event-project-slug"></span></p>
2424

25+
<h2 id="flyout">Flyout</h2>
26+
<readthedocs-flyout></readthedocs-flyout>
27+
2528
<h2 id="docdiff">DocDiff</h2>
2629
<p>Visit <a href="docdiff.html">this page</a> to take a look at it.</p>
2730

src/context.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {
2+
ContextProvider,
3+
ContextRoot,
4+
createContext,
5+
} from "@lit/context";
6+
import { EVENT_READTHEDOCS_ADDONS_DATA_READY } from "./events";
7+
8+
export const contextRoot = new ContextRoot().attach(document.body);
9+
export const configContext = createContext(Symbol("readthedocs-config"));
10+
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();
22+
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

Comments
 (0)