HTML Elements
This chapter introduces some recently adopted standard tags.
<dialog>
Basic Usage
The <dialog>
tag represents a dialog box that can be closed.
1 | <dialog> |
The above code shows a simple dialog box.
By default, the dialog is hidden and does not display on the webpage. To make the dialog visible, you must add the open
attribute.
1 | <dialog open> |
The above code will display a box on the webpage with the content “Hello world.”
Inside a <dialog>
element, you can place other HTML elements.
1 | <dialog open> |
In this example, the dialog contains an input field and a submit button.
Note that in the example above, setting the form’s method
attribute to “dialog” means that when the submit button is clicked, the dialog will disappear. However, the form will not be submitted to the server; instead, the browser will set the form element’s returnValue
property to the value attribute of the Submit button (in this case, “foo”).
JavaScript API
The <dialog>
element provides a JavaScript API with two methods: Dialog.showModal()
and Dialog.close()
, used to open and close the dialog.
1 | const modal = document.querySelector('dialog'); |
You can provide a close button that triggers the Dialog.close()
method to close the dialog.
The Dialog.close()
method can accept a string parameter to pass information. The returnValue
property of the <dialog>
interface can read this string. Otherwise, returnValue
defaults to the value attribute of the submit button.
1 | modal.close('Accepted'); |
When Dialog.showModal()
is called, a transparent overlay appears to prevent user interaction with content outside the dialog. CSS provides the ::backdrop
pseudo-class to style this overlay, allowing you to make it visible.
1 | dialog { |
The code above styles the <dialog>
element and changes the dialog’s overlay to a semi-transparent gray.
The <dialog>
element also has a Dialog.show()
method that opens the dialog without the overlay, allowing users to interact with the content outside the dialog.
Events
The <dialog>
element has two events that can be listened to:
close
: Triggered when the dialog is closed.cancel
: Triggered when the dialog is closed by pressing the Esc key.
To close the dialog when the user clicks the overlay, you can use the following code:
1 | modal.addEventListener('click', (event) => { |
<details>
and <summary>
Basic Usage
The <details>
tag is used to create a collapsible section, which the browser will display in a collapsed state. By default, the content inside the <details>
tag will be collapsed, showing only a summary with a triangle indicator.
1 | <details> |
In the browser, the above code will show as:
1 | ▶ Details |
Clicking on “Details” will expand the text to show:
1 | ▼ Details |
Clicking again will collapse the text.
The open
attribute on the <details>
tag allows you to open it by default:
1 | <details open> |
In this case, the section will be expanded when the page loads.
The <summary>
tag is used to customize the title of the collapsible content:
1 | <details> |
This will display as:
1 | ▶ This is the title |
Clicking on the title will expand the section to show:
1 | ▼ This is the title |
You can use CSS to change the default arrow using summary::-webkit-details-marker
:
1 | summary::-webkit-details-marker { |
Alternatively, you can hide the default arrow and use a different indicator:
1 | summary::-webkit-details-marker { |
JavaScript API
You can check if a <details>
element is open or closed using the open
property:
1 | const details = document.querySelector('details'); |
The toggle
event is fired whenever the <details>
element is opened or closed:
1 | details.addEventListener('toggle', event => { |
Reference Links
- Meet the New Dialog Element, Keith J. Grant
- The dialog element: The way to create tomorrow’s modal windows, Abhishek Jakhar
- Details/Summary is the Easiest Way Ever to Make an Accordion, Chris Coyier
Link to original article: