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

Javascript causing strange Custom Button behaviour #12893

Closed
dreamstar-enterprises opened this issue Jan 5, 2025 · 2 comments
Closed

Javascript causing strange Custom Button behaviour #12893

dreamstar-enterprises opened this issue Jan 5, 2025 · 2 comments
Labels
needs triage Issue needs to be triaged

Comments

@dreamstar-enterprises
Copy link

dreamstar-enterprises commented Jan 5, 2025

Astro Info

5.1.1

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

I have a custom button:

<Tag class={allClasses} {...rest}>

  <!--text-->
  <slot name="icon-left" />
  {text}
  <slot name="icon-right" />

  <!--image-->
  <span class="relative inline-flex items-center">
    <Chevron
      class={`ms-0.25 h-4 w-4 -rotate-90 stroke-[2.5] ${
        textColor.replace('text-', 'stroke-')
      } ${
        hoverTextColor.replace('text-', 'stroke-')
      } transition-all duration-100 group-hover:opacity-0`}
    />
    <Arrow
      class={`absolute start-0 ms-[4px] h-4 w-4 -rotate-90 stroke-[2.5] ${
        textColor.replace('text-', 'stroke-')
      } ${
        hoverTextColor.replace('text-', 'stroke-')
      } transition-all duration-100 opacity-0 group-hover:opacity-100`}
    />
  </span>
</Tag>

I have three instances of it on a page.

Here is one of them. Note this has a different id to the other 2 instances

      <Link
            id="back-home-link"
            href=""
            as="a"
            variant="solid"
            text="Back to Home"
            textColor="text-theme-selected-button-text"
            hoverTextColor="hover:text-white"
            hoverBgColor="hover:bg-rose-500"
            bgColor="bg-zinc-300"
            size="md"
          />

I have some javascript that does this:

  const elements = {
    backLink: document.getElementById(
      "back-home-link",
    ) as HTMLAnchorElement | null,
  };
  if (elements.backLink) {
    elements.backLink.textContent = t("auth.error.backHome");
  }

But doing this, for some reason causes the Arrow part to disappear on the ALL 3 buttons.

    <Arrow
      class={`absolute start-0 ms-[4px] h-4 w-4 -rotate-90 stroke-[2.5] ${
        textColor.replace('text-', 'stroke-')
      } ${
        hoverTextColor.replace('text-', 'stroke-')
      } transition-all duration-100 opacity-0 group-hover:opacity-100`}
    />

I know it is this one line causing the issue

elements.backLink.textContent = t("auth.error.backHome");

If I comment it out, the arrows on ALL 3 buttons reappear,

The html actually shows the Arrow svg to be there, on the other 2 buttons (and rightly not on the button I am targetting, since the JS replaces the textContent), but it says that the shadow root is closed, with nothing inside it

Screenshot 2025-01-05 at 01 25 29

Uncommenting that line of js, causes the shadow root on the other 2 buttons, to have arrow svg inside it.

Screenshot 2025-01-05 at 01 27 38

Not sure what might be going on?

What's the expected result?

This JS, should only affect the text content of the button being targetted, by id, not the other instances of the custom astro button

  const elements = {
    backLink: document.getElementById(
      "back-home-link",
    ) as HTMLAnchorElement | null,
  };
  if (elements.backLink) {
    elements.backLink.textContent = t("auth.error.backHome");
  }


### Link to Minimal Reproducible Example

seeabove minmal reproducible example

### Participation

- [ ] I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Jan 5, 2025
@dreamstar-enterprises
Copy link
Author

dreamstar-enterprises commented Jan 5, 2025

I modified the component and JS

  <!--text-->
  <slot name="icon-left" />
  <span class="link-text">{text}</span>
  <slot name="icon-right" />
const elements = {
   backLink: document
     .getElementById("back-home-link")
     ?.querySelector(".link-text"),
   backLinkAnchor: document.getElementById(
     "back-home-link",
   ) as HTMLAnchorElement | null,
 };

 if (elements.backLink) {
   elements.backLink.textContent = t("auth.error.backHome");
 }
 if (elements.backLinkAnchor) {
   elements.backLinkAnchor.href = `${import.meta.env.BASE_URL}/${locale}/`;
 }

It worked.

Strange behaviour before though

@martrapp
Copy link
Member

martrapp commented Jan 6, 2025

Hi @dreamstar-enterprises, happy you found a solution!
That was really a strange behavior.

@martrapp martrapp closed this as completed Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage Issue needs to be triaged
Projects
None yet
Development

No branches or pull requests

2 participants