:modal
Baseline 2022
Newly available
:modal
は CSS の擬似クラスで、操作が解除されるまで、それ以外の要素とのすべての操作を除外する状態にある要素と一致します。 :modal
擬似クラスを使用して、複数の要素を同時に選択することができますが、アクティブになり、入力を受け付けることができるのはそのうちの 1 つのみです。
ページの他の部分をユーザーが操作できないようにし、 :modal
擬似クラスによって選択される要素の例としては、例えば以下のようなものが含まれます。
dialog
要素が showModal()
API で開かれたとき。
requestFullscreen()
API で開かれたときに :fullscreen
擬似クラスで選択される要素。
この例では、「詳細を更新」ボタンがアクティブ化された際に開くモーダルダイアログにスタイル設定を行なっています。この例は、 <dialog>
要素の例を基に構築されています。
<!-- フォームを含む基本的なモーダルダイアログ -->
<dialog id="favDialog">
<form method="dialog">
<p>
<label
>好きな動物:
<select>
<option value="default">選択してください…</option>
<option>アルテミア</option>
<option>レッサーパンダ</option>
<option>クモザル</option>
</select>
</label>
</p>
<div>
<button value="cancel">キャンセル</button>
<button id="confirmBtn" value="default">確認</button>
</div>
</form>
</dialog>
<p>
<button id="updateDetails">詳細を更新</button>
</p>
<output></output>
CSS
:modal {
border: 5px solid red;
background-color: yellow;
box-shadow: 3px 3px 10px rgb(0 0 0 / 50%);
}
const updateButton = document.getElementById("updateDetails");
const favDialog = document.getElementById("favDialog");
const outputBox = document.querySelector("output");
const selectEl = favDialog.querySelector("select");
const confirmBtn = favDialog.querySelector("#confirmBtn");
// If a browser doesn't support the dialog, then hide the
// dialog contents by default.
if (typeof favDialog.showModal !== "function") {
favDialog.hidden = true;
// Your fallback script
}
// "Update details" button opens the <dialog> modally
updateButton.addEventListener("click", () => {
if (typeof favDialog.showModal === "function") {
favDialog.showModal();
} else {
outputBox.value = "このブラウザーはダイアログ API に対応していません。";
}
});
// "Favorite animal" input sets the value of the submit button
selectEl.addEventListener("change", (e) => {
confirmBtn.value = selectEl.value;
});
// "Confirm" button of form triggers "close" on dialog because of [method="dialog"]
favDialog.addEventListener("close", () => {
outputBox.value = `${
favDialog.returnValue
} button clicked - ${new Date().toString()}`;
});
BCD tables only load in the browser