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

Use button instead of a for closing modals #446

Closed
Atmos4 opened this issue Feb 13, 2024 · 8 comments · Fixed by #453
Closed

Use button instead of a for closing modals #446

Atmos4 opened this issue Feb 13, 2024 · 8 comments · Fixed by #453

Comments

@Atmos4
Copy link

Atmos4 commented Feb 13, 2024

Describe the issue

The usage of button for closing modals makes a lot more sense than a.

  • from w3:

The button element should be used for any interaction that performs an action on the current page. The a element should be used for any interaction that navigates to another view.

  • from UX: Links only respond to Enter key presses. Buttons respond to both Enter and Space.
  • Links need to have an href attribute in order to be accessible. This forces the usage of href=#, which can confuse screen readers and will in fact perform the scroll animation unless the onclick handler returns false, which further increases verbosity.

Solutions

One solution would be to recommend button as a way to close modals, and properly style any button with class=close (or rel=prev even though it is not valid on buttons, and also a non-standard way to describe dialog closing actions).

Extending the previous solution for classless pico, we can use <form method=dialog> HTML element, which is a non-Javascript solution to closing dialogs. Alternatively if the button is already in a form, we can use <button formmethod=dialog>.

Here is a demo of the extended solution on CodePen.

If you agree on the issue, let me know which solution you like best and I can start working on a PR!

@Atmos4 Atmos4 changed the title Anchor tag to close modals is non standard Use button instead of a for closing modals Feb 13, 2024
@lucaslarroche
Copy link
Member

lucaslarroche commented Feb 13, 2024

@Atmos4,
Thank you. I appreciate your research. I agree. You made a good point.

But we must avoid breaking changes because we are no longer in beta or release-candidate.
So the current <a rel=prev></a> should still be functional.

I would prefer formmethod="dialog" (vs rel="prev").
I didn't know about HTMLDialogElement, so that's even better.

Sure, it would be great if you could open a PR.
Also, as following PRs, we would need to update the website and the examples.
It can come later.

Note, you also need to reset box-shadow

(EDITED)

@Atmos4
Copy link
Author

Atmos4 commented Feb 13, 2024

@lucaslarroche
I will work on a PR as soon I have some free time!

Out of curiosity, how much does bundle size matter to this project? If it is important, then I might just go with the first solution I described. It should be doable with :is(button, a):has([rel=prev]).

I edited the codepen to reflect what I described above, and simplify the CSS to work with .close class instead.

@lucaslarroche
Copy link
Member

lucaslarroche commented Feb 13, 2024

Semantically valid is more important than the bundle size.

:is(button, a):has([rel=prev])

Agreed

(EDITED)

@Atmos4
Copy link
Author

Atmos4 commented Feb 15, 2024

PR is ready, see #448

@lucaslarroche
Copy link
Member

@Atmos4, I edited my comments about targeting <button formmethod="dialog" />
I was confused with your example using it, but dialog is not a valid formmethod.

So you were right, .close, :is(a, button)[rel=prev] and :is(a, button)[rel=prev] are good!

@lucaslarroche lucaslarroche mentioned this issue Feb 16, 2024
@lucaslarroche lucaslarroche linked a pull request Feb 16, 2024 that will close this issue
@Atmos4
Copy link
Author

Atmos4 commented Feb 17, 2024

dialog is not a valid formmethod.

@lucaslarroche it is! Check out the MDN article and this codepen. The original codepen I posted above was also containing this solution.

@lucaslarroche
Copy link
Member

@lucaslarroche it is! Check out the MDN article and this codepen.

Great. I missed it when researching yesterday.

The original codepen I posted above was also containing this solution.

Yes. That's why I suggested at some point to target button[formmethod] to avoid using rel.

@Atmos4
Copy link
Author

Atmos4 commented Feb 18, 2024

@lucaslarroche The problem with using button[formmethod] as the way to style the icon is that there are other valid use cases for button[formmethod] without the icon, for example:

<dialog>
  <article>
    <!-- Modal content -->
    <footer>
      <form>
        <button formmethod=dialog>Cancel</button> <!-- this button should look normal! -->
      </form>
    </footer>
  </article>
</dialog>

By the way, is there a reason to have some part of the icon specific code coupled to the header? It could be interesting to allow usage of that icon anywhere inside the article, for example like this:

<dialog id=modal1>
  <article>
     <button rel=prev onclick="modal1.close()"></button> <!-- currently doesn't float:right -->
    <!-- Modal content -->
  </article>
</dialog>

I can create an issue/PR to decouple the code if you are interested. Maybe there are valid reasons to have it otherwise though!

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 a pull request may close this issue.

2 participants