Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I render html both in component element and out component element? #294

Closed
taleoflong opened this issue Oct 29, 2022 · 2 comments
Closed

Comments

@taleoflong
Copy link

taleoflong commented Oct 29, 2022

First,define the primary-button component,like this:

@controller
class PrimaryButtonElement extends HTMLElement {
  connectedCallback(){
       const itemTemplate = () => {
           return html`
          <button>
              ???
          </button>
          `
        }

        render(itemTemplate(), this)
   }
}

Then,use the component like this,

<primary-button>new issue</primary-button>

The question is how to render the component's content in the controller?
Thanks!

@Scipion
Copy link
Contributor

Scipion commented Oct 29, 2022

I'm not sure of the question. Do you mean you want to wrap what the children of the tag into your button? if so you can get the initial content with this.innerHTML as a string and do it like so:

@controller
export class PrimaryButtonElement extends HTMLElement {
  connectedCallback(){
        const initialContent = this.innerHTML;
        const itemTemplate = (content:string) => {
           return html`<button>${content}???</button>`
        }

        this.innerHTML=''
        render(itemTemplate(initialContent), this);
   }
}

You will have to get rid of it and re-render it again.
it will render:

<primary-button data-catalyst="">
    <button>new issue???</button>
</primary-button>

I hope it helps.

@taleoflong
Copy link
Author

I'm not sure of the question. Do you mean you want to wrap what the children of the tag into your button? if so you can get the initial content with this.innerHTML as a string and do it like so:

@controller
export class PrimaryButtonElement extends HTMLElement {
  connectedCallback(){
        const initialContent = this.innerHTML;
        const itemTemplate = (content:string) => {
           return html`<button>${content}???</button>`
        }

        this.innerHTML=''
        render(itemTemplate(initialContent), this);
   }
}

You will have to get rid of it and re-render it again. it will render:

<primary-button data-catalyst="">
    <button>new issue???</button>
</primary-button>

I hope it helps.

thanks a lot

@github github locked and limited conversation to collaborators Oct 31, 2022
@keithamus keithamus converted this issue into discussion #296 Oct 31, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants