@@ -10,6 +10,13 @@ import {
1010} from './mdjsViewerSharedStates.js' ;
1111import { addResizeHandler } from './resizeHandler.js' ;
1212
13+ /**
14+ * @typedef {{values: unknown[], strings:string[],processor:Function} } TemplateResult1
15+ * @typedef {import('lit').TemplateResult } TemplateResult2
16+ * @typedef {{templateFactory:any; eventContext: EventTarget } } RenderOptions1
17+ * @typedef {import('lit').RenderOptions } RenderOptions2
18+ */
19+
1320/**
1421 * @param {string } input
1522 * @param {'js'|'css' } type
@@ -72,6 +79,46 @@ export class MdJsPreview extends ScopedElementsMixin(LitElement) {
7279 } ;
7380 }
7481
82+ /**
83+ * By default, the render of lit2 is provided, which is compatible with TemplateResults of lit2.
84+ * However, in contexts that need to run multiple versions of lit, it should be possible to
85+ * provide a specific render function, like renderHybrid, that internally checks, based on the
86+ * TemplateResult, whether the render function of lit 1 or 2 should called.
87+ * Overriding the render function would look like:
88+ *
89+ * @protected
90+ * @param {TemplateResult1|TemplateResult2|LitHtmlStoryFn } html Any value renderable by NodePart - typically a TemplateResult
91+ * created by evaluating a template tag like `html` or `svg`.
92+ * @param {HTMLElement } container A DOM parent to render to. The entire contents are either
93+ * replaced, or efficiently updated if the same result type was previous
94+ * rendered there.
95+ * @param {Partial<RenderOptions2> } [options] RenderOptions for the entire render tree rendered to this
96+ * container. Render options must *not* change between renders to the same
97+ * container, as those changes will not effect previously rendered DOM.
98+ *
99+ * @example
100+ * ```js
101+ * import { MdJsPreview } from '@mdjs/mdjs-preview';
102+ * import { render as render2 } from 'lit';
103+ * import { isTemplateResult as isTemplateResult2 } from 'lit/directive-helpers.js';
104+ * import { render as render1 } from 'lit-html';
105+ *
106+ * export class HybridLitMdjsPreview extends MdJsPreview {
107+ * renderStory(html, container, options) {
108+ * if (isTemplateResult2(html)) {
109+ * render2(html, container, options);
110+ * } else {
111+ * render1(html, container, options);
112+ * }
113+ * }
114+ * }
115+ * customElements.define('mdjs-preview', HybridLitMdjsPreview);
116+ * ```
117+ */
118+ renderStory ( html , container , options ) {
119+ render ( html , container , options ) ;
120+ }
121+
75122 constructor ( ) {
76123 super ( ) ;
77124 /** @type {LitHtmlStoryFn } */
@@ -257,7 +304,10 @@ export class MdJsPreview extends ScopedElementsMixin(LitElement) {
257304 }
258305
259306 if ( this . lightDomRenderTarget && changeProps . has ( 'story' ) ) {
260- render ( this . story ( { shadowRoot : this } ) , this . lightDomRenderTarget ) ;
307+ this . renderStory (
308+ /** @type {LitHtmlStoryFn } */ ( this . story ( { shadowRoot : this } ) ) ,
309+ this . lightDomRenderTarget ,
310+ ) ;
261311 }
262312
263313 if ( changeProps . has ( 'platform' ) || changeProps . has ( 'size' ) ) {
0 commit comments