diff --git a/docs-src/config.ts b/docs-src/config.ts index 02438ce9..08a8d875 100644 --- a/docs-src/config.ts +++ b/docs-src/config.ts @@ -1,9 +1,9 @@ import Home from './index.page' import Documentation from './documentation/index.page' import InstallationPage from './documentation/installation.page' -import EssentialTrainingPage from './documentation/essential-training.page' -import TutorialPage from './documentation/tutorial.page' -import ExamplesPage from './documentation/examples.page' +// import EssentialTrainingPage from './documentation/essential-training.page' +// import TutorialPage from './documentation/tutorial.page' +// import ExamplesPage from './documentation/examples.page' import CreatingAndRenderingPage from './documentation/creating-and-rendering.page' import TemplatingValuesPage from './documentation/template-values.page' import ReplacingContentPage from './documentation/replacing-content.page' diff --git a/docs-src/documentation/replacing-content.page.ts b/docs-src/documentation/replacing-content.page.ts index affc6795..6e21cda0 100644 --- a/docs-src/documentation/replacing-content.page.ts +++ b/docs-src/documentation/replacing-content.page.ts @@ -14,22 +14,24 @@ export default ({ page, nextPage, prevPage, docsMenu }: PageComponentProps) => html` ${Heading(page.name)}

- One special thing about templates is that you can treat it like - a DOM element for few things. One of these things is the ability - to replace one template with the another. + One special thing about Markup template is that it is an + instance object, which you can do a lot of things to. One of + these things is the ability to replace one template with the + another.

- Let's take for example an async scenario where you render a - loading indicator then need to replace this loading indicator - with the loaded content. + Let's take for example an async rendering scenario where you + render a loading indicator then need to replace this loading + indicator with the actual loaded content.

${CodeSnippet( 'const loadingIndicator = html`

loading...

`;\n' + '\n' + 'const fetchContent = () => {\n' + - ' setTimeout(() => {\n' + + ' setTimeout(() => { // simulate HTTP call\n' + ' const content = html`

loaded content

`;\n' + ' \n' + + ' // render content by telling it what to replace\n' + ' content.replace(loadingIndicator)\n' + ' }, 2500)\n' + '}\n' + @@ -51,14 +53,14 @@ export default ({ page, nextPage, prevPage, docsMenu }: PageComponentProps) => This means that you can have multiple tags, and it will just replace them all.

- ${Heading('Replacing a DOM Element', 'h3')} + ${Heading('Replacing HTML Element', 'h3')}

- You may also target a specific DOM Element. Here is the sample + You may also target a specific DOM Element. Here is the same example but with the loading indicator as an Element instead of - a template. + an HTML Template.

${CodeSnippet( - '// using a P Element instead\n' + + '// using a HTMLParagraphElement instance\n' + "const loadingIndicator = document.createElement('p');\n" + "loadingIndicator.textContent = 'Loading...'\n" + '\n' + @@ -76,7 +78,30 @@ export default ({ page, nextPage, prevPage, docsMenu }: PageComponentProps) => 'fetchContent();', 'typescript' )} -

This will have the same effect.

+ ${Heading('Replacing "Must known"', 'h3')} + ${DocPrevNextNav({ prev: prevPage, next: nextPage, diff --git a/src/html.ts b/src/html.ts index c3fb0946..bb6e8f4e 100644 --- a/src/html.ts +++ b/src/html.ts @@ -155,19 +155,19 @@ export class HtmlTemplate { this.#init(element) } - if (element.parentNode) { - const frag = doc.createDocumentFragment() - frag.append(...this.nodes) - element.parentNode.replaceChild(frag, element) + const frag = doc.createDocumentFragment() + frag.append(...this.nodes) + element.parentNode?.replaceChild(frag, element) - if (target instanceof HtmlTemplate) { - target.unmount() - } - } else { - return + // only need to unmount the template nodes + // if the target is not a template, it will be automatically removed + // when replaceChild is called above. + if (target instanceof HtmlTemplate) { + target.unmount() } - this.#renderTarget = element + this.#renderTarget = element.parentNode as HTMLElement + return }