8000 Disable social links after being clicked by rmartinc · Pull Request #39031 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

Disable social links after being clicked - #39031

Merged
jonkoops merged 1 commit into
keycloak:mainfrom
rmartinc:issue-35278
May 6, 2025
Merged

jonkoops merged 1 commit into
keycloak:mainfrom
rmartinc:issue-35278

Conversation

@rmartinc
Copy link
Copy Markdown
Contributor

Closes #35278

@jonkoops This is a try to disable double click in the social links. I used the onclick to avoid the subsequent clicks and added a new property in the themes to add the patternfly disable class.

@rmartinc
Copy link
Copy Markdown
Contributor Author

Rebased to run the CI again.

@jonkoops
Copy link
Copy Markdown
Contributor

Instead of extending the authChecker.js script with more functionality, I think it would be better to put this in a dedicated script tag, as well as inlining the code of the script into the page itself. The code can be duplicated between the login themes, we will eventually drop the old version of the login theme entirely, so it is not really important to make it reusable.

Because elements could be removed or added to the page at any point by JavaScript (our theme does not, but a custom theme could), the event listener for clicks should be added on the root of the document, all events will eventually bubble up there.

I would also opt to use a more generic data attribute name to target the element, this allows the functionality to be used more generically, instead of being tied to a single context. We should also try to use data attributes to pass properties such as the disabled class, as this will make it easier to do CSP (Content Security Policy) later on.

<a
  href="..."
  data-once-link
  data-disabled-class="{properties.kcFormSocialAccountListButtonDisabledClass!}">
  ...
</a>
<script type="module">
  document.addEventListener("click", (event) => {
    const link = event.target.closest("a[data-once-link]");
  
    if (!link || !link.hasAttribute("href")) {
      return;
    }
    
    const { disabledClass } = link.dataset;
    
    if (disabledClass) {
      link.classList.add(disabledClass);
    }
    
    link.removeAttribute("href");
    link.setAttribute("role", "link");
    link.setAttribute("aria-disabled", "true");
  });
</script>

@rmartinc
rmartinc commented Apr 24, 2025 •
Copy link
Copy Markdown
Contributor Author

@jonkoops But if we remove the href in the onclick (which executes before any click), the link is not there even in the first click, right? I'm going to not remove the href and just preventDefault if the aria-disabled is true. Or do you prefer to manually assign location to the href the first time?

@jonkoops
Copy link
Copy Markdown
Contributor

But if we remove the href in the onclick (which executes before any click), the link is not there even in the first click, right?

You are correct. I thought we could still navigate away in that case, but it seems not. If we don't remove the href attribute, it would still be possible to navigate away by opening the link in a new tab (e.g. right-click through the context menu, or long-press on mobile). But perhaps that is acceptable, in that case we can simply call preventDefault() when the aria-disabled attribute is set.

document.addEventListener("click", (event) => {
  const link = event.target.closest("a[data-once-link]");

  if (!link || link.getAttribute("aria-disabled") === "true") {
    return;
  }

  const { disabledClass } = link.dataset;

  if (disabledClass) {
    link.classList.add(disabledClass);
  }

  link.setAttribute("aria-disabled", "true");
});

Closes keycloak#35278

Signed-off-by: rmartinc <rmartinc@redhat.com>
@rmartinc
Copy link
Copy Markdown
Contributor Author

@jonkoops Done! Let's see the CI...

@jonkoops jonkoops left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

@jonkoops
jonkoops merged commit 3969c40 into keycloak:main May 6, 2025
rmartinc added a commit to rmartinc/keycloak that referenced this pull request May 7, 2025
Closes keycloak#35278

Signed-off-by: rmartinc <rmartinc@redhat.com>
(cherry picked from commit 3969c40)
mposolda pushed a commit that referenced this pull request May 7, 2025
Closes #35278

Signed-off-by: rmartinc <rmartinc@redhat.com>
(cherry picked from commit 3969c40)
rmartinc added a commit to rmartinc/keycloak that referenced this pull request May 7, 2025
Closes keycloak#35278

Signed-off-by: rmartinc <rmartinc@redhat.com>
(cherry picked from commit 3969c40)
mposolda pushed a commit that referenced this pull request May 13, 2025
Closes #35278

Signed-off-by: rmartinc <rmartinc@redhat.com>
(cherry picked from commit 3969c40)
@kochen kochen mentioned this pull request May 14, 2025
2 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Double click on social provider link causes page has expired error

3 participants

0