gamja-old/components/dialog.js
Simon Ser 0d36e8a3fc Turn dialog close button into a <button>
This is semantically more correct.
2021-06-22 16:02:55 +02:00

61 lines
1.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { html, Component, createRef } from "../lib/index.js";
export default class Dialog extends Component {
body = createRef();
constructor(props) {
super(props);
this.handleCloseClick = this.handleCloseClick.bind(this);
this.handleBackdropClick = this.handleBackdropClick.bind(this);
this.handleKeyDown = this.handleKeyDown.bind(this);
}
dismiss() {
this.props.onDismiss();
}
handleCloseClick(event) {
event.preventDefault();
this.dismiss();
}
handleBackdropClick(event) {
if (event.target.className == "dialog") {
this.dismiss();
}
}
handleKeyDown(event) {
if (event.key == "Escape") {
this.dismiss();
}
}
componentDidMount() {
window.addEventListener("keydown", this.handleKeyDown);
let autofocus = this.body.current.querySelector("input[autofocus]");
if (autofocus) {
autofocus.focus();
}
}
componentWillUnmount() {
window.removeEventListener("keydown", this.handleKeyDown);
}
render() {
return html`
<div class="dialog" onClick=${this.handleBackdropClick}>
<div class="dialog-body" ref=${this.body}>
<div class="dialog-header">
<h2>${this.props.title}</h2>
<button class="dialog-close" onClick=${this.handleCloseClick}>×</button>
</div>
${this.props.children}
</div>
</div>
`;
}
}