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

Idea: Investigate HTML Dialog Element for Modals #2

Open
sbussard opened this issue Jul 18, 2023 · 1 comment
Open

Idea: Investigate HTML Dialog Element for Modals #2

sbussard opened this issue Jul 18, 2023 · 1 comment

Comments

@sbussard
Copy link

Benefits:

  • Less styling and markup required
  • Semantic Support
  • Less react implementation overhead
  • More fine grained control
  • Automatic esc to close

Read more:
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dialog

@sbussard
Copy link
Author

The approach I found valuable

Each component gets its own modals, and

  1. remove default styling for dialog
dialog {
  padding: 0;
  border: none;
  outline: none;
  background-color: transparent;
}
  1. add custom styling
.SomeComponent {
  &-modal {
    padding: 2rem;
    border-radius: 1rem;
    background-color: var(--theme-bg-alt);

    &::backdrop {
      backdrop-filter: blur(1rem);
      background-color: var(--black);
    }
  }
}
  1. useRef to access the dom
const modalRef = useRef<HTMLDialogElement>(null);
<dialog ref={modalRef} ... >
  1. add a click handler to close when user clicks the backdrop
function handleModalClick(event: MouseEvent<HTMLDialogElement>) {
  if (event.target === event.currentTarget) {
    event.currentTarget.close();
  }
}
<dialog onClick={handleModalClick} ... >
  1. trigger modal programmatically
function handleSomethingClicked() {
  modalRef.current?.showModal();
}

There are two options for what to do from here

  1. Use the return value of the modal (I'm not compelled)
  2. Add a button inside the modal and use regular click handlers to do whatever action needs 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

No branches or pull requests

1 participant