Skip to content

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

Closed

Description

@qlfy17

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!

Activity

  1. Scipion commented on Oct 29, 2022

    @Scipion
    Contributor

    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.

  2. qlfy17 commented on Oct 30, 2022

    @qlfy17
    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

  3. locked and limited conversation to collaborators on Oct 31, 2022
  4. converted this issue into a discussion #296 on Oct 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions