From 4ada43edfaa856cf86c96baf43d5910f7afea4b9 Mon Sep 17 00:00:00 2001 From: Anthony Johnson Date: Wed, 15 Jan 2025 13:57:05 -0800 Subject: [PATCH] Use ContextRoot at document.html instead --- public/index.html | 18 +++++++----------- src/context.js | 46 +++++++++++++++++++++------------------------- src/flyout.js | 9 +++++---- 3 files changed, 33 insertions(+), 40 deletions(-) diff --git a/public/index.html b/public/index.html index caf18bd9..68016500 100644 --- a/public/index.html +++ b/public/index.html @@ -23,9 +23,7 @@

CustomEvent

Project slug using CustomEvent:

Flyout

- - - +

DocDiff

Visit this page to take a look at it.

@@ -62,13 +60,11 @@

Link Previews

Notification

- - - - - - - - + + + + + + diff --git a/src/context.js b/src/context.js index abab9bcb..587ce754 100644 --- a/src/context.js +++ b/src/context.js @@ -1,30 +1,26 @@ -import { html, nothing, render, LitElement } from "lit"; -import { ContextProvider } from "@lit/context"; -import { createContext } from "@lit/context"; -import { getReadTheDocsConfig } from "./readthedocs-config.js"; +import { + ContextProvider, + ContextRoot, + createContext, +} from "@lit/context"; import { EVENT_READTHEDOCS_ADDONS_DATA_READY } from "./events"; +export const contextRoot = new ContextRoot().attach(document.body); export const configContext = createContext(Symbol("readthedocs-config")); -export class AddonsApp extends LitElement { - config = new ContextProvider(this, { context: configContext }); +/** + * Because `config` provider is not attached to a ReactiveElement, and is + * instead connected to `document.html`, we have to call `hostConnected()` + * manually. See: + * + * https://github.com/lit/lit/blob/935697d47e62ed75e3157423400163a8371c62fc/packages/context/src/lib/controllers/context-provider.ts#L55-L58 + **/ +const config = new ContextProvider(document.documentElement, { + context: configContext, +}); +config.hostConnected(); - connectedCallback() { - super.connectedCallback(); - document.addEventListener( - EVENT_READTHEDOCS_ADDONS_DATA_READY, - this._handleAddonsDataReady, - ); - } - - createRenderRoot() { - return this; - } - - _handleAddonsDataReady = (event) => { - console.log("_handleAddonsDataReady"); - this.config.setValue(event.detail.data()); - }; -} - -customElements.define("readthedocs-context", AddonsApp); +document.addEventListener(EVENT_READTHEDOCS_ADDONS_DATA_READY, (event) => { + console.log("Event:", EVENT_READTHEDOCS_ADDONS_DATA_READY); + config.setValue(event.detail.data()); +}); diff --git a/src/flyout.js b/src/flyout.js index e90ed1f4..eb6fbaaf 100644 --- a/src/flyout.js +++ b/src/flyout.js @@ -44,8 +44,10 @@ export class FlyoutElement extends LitElement { this.floating = true; this.position = "bottom-right"; this.readthedocsLogo = READTHEDOCS_LOGO; - console.log("Flyout _config (from constructor() method)"); - console.log(this._config.value); + console.log( + "Flyout _config (from constructor() method)", + this._config.value, + ); } _close() { @@ -306,8 +308,7 @@ export class FlyoutElement extends LitElement { render() { // The element doesn't yet have our config, don't render it. - console.log("Flyout config (from render() method )"); - console.log(this._config.value); + console.log("Flyout config (from render() method )", this._config.value); // Validate the context (`this._config.value`) on each update and return // nothing if it's invalid. ``nothing`` is a special Lit response type.