Skip to content
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

Working Turbo Frames called from the Shadow Dom #1021

Open
stedekay opened this issue Oct 6, 2023 · 2 comments · May be fixed by #1022
Open

Working Turbo Frames called from the Shadow Dom #1021

stedekay opened this issue Oct 6, 2023 · 2 comments · May be fixed by #1022

Comments

@stedekay
Copy link

stedekay commented Oct 6, 2023

I am trying to create a Button Component (with Lit) with a data-turbo-frame attribute. The component looks like this:

import {html, LitElement} from "lit";

export class NavButton extends LitElement {
    static get properties() {
        return {
            path: {type: String},
            label: {type: String},
        }
    }

    render() {
        return html`
            <a href="${this.path}" data-turbo-frame="body">
                ${this.label}
            </a>
        `;
    }

    connectedCallback() {
        super.connectedCallback();
        window.addEventListener('turbo:visit', this.handleVisit);
    }

    disconnectedCallback() {
        window.removeEventListener('turbo:visit', this.handleVisit);
        super.disconnectedCallback();
    }

    handleVisit = (event) => {
        const anchorElement = this.shadowRoot.querySelector('a');

        if (event.detail.url.endsWith(this.path)) {
            anchorElement.style.fontWeight = 'bold';
        } else {
            anchorElement.style.fontWeight = 'normal';
        }
    }
}

customElements.define('nav-button', NavButton);

The basic layout is the following:

<nav>
    <nav-button path="/desktop" label="Desktop"></nav-button>
    <nav-button path="/about" label="About"></nav-button>
</nav>
<div>
    <turbo-frame id="body" data-turbo-action="advance">
    </turbo-frame>
</div>

It isn't working as expected. The whole page gets rerendered instead of the frame. I assume the reason is the shadow dom. When I disable the shadow dom with

createRenderRoot() {
    return this;
}

in the NavButton element everything works as expected.

I am not sure if this is a bug or I messed up. Maybe someone can help me out.

@yuki24
Copy link
Contributor

yuki24 commented Oct 7, 2023

@stedekay What version of Turbo are you using? I believe this has been fixed by #802.

@stedekay
Copy link
Author

stedekay commented Oct 7, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants