HTML dialog element demonstration session

ranwahle 11 views 19 slides Sep 04, 2024
Slide 1
Slide 1 of 19
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19

About This Presentation

Demonstration of HTML dialog element.

Slides with link to demo code.


Slide Content

Let’s dialog Ran Wahle

About Me Ran Wahle Full Stack developer at Tikal

A little story

Dialog without dialog Dialog content Container that hides everything else

HTML < div id = "my-dialog-container" > < div id = "my-dialog" > < p > Hello dialog </ p > < button type = "button" > Close </ button > </ div > </ div > < button id = "open-dialog" > Open dialog </ button >

CSS #my-dialog { display : none ; position : fixed ; padding : 1rem ; top : 50% ; left : 50% ; background-color : white ; border-style : dashed ; &[open] { display : block ; } } :has ( #my-dialog [ open ]) { background-color : black ; position : fixed ; top : ; right : ; width : 100% ; height : 300vh ; z-index : 1000 ; overflow-y : hidden ; }

javascript document . querySelector ( '#open-dialog' ). addEventListener ( 'click' , () => { const dialog = document . querySelector ( '#my-dialog' ); dialog . setAttribute ( 'open' , true ); dialog . querySelector ( 'button' ). addEventListener ( 'click' , () => { dialog . removeAttribute ( 'open' ); }); });

Old school dialog Demo

Dialog element < dialog > < form method = "dialog" > < p > Hello dialog </ p > < button type = "submit" > Close </ button > </ dialog >

Open attribute < dialog open > < form method = "dialog" > < p > Hello dialog </ p > < button type = "submit" > Close </ button > </form> </ dialog >

Demo Open attribute

Main method showModal close

Events close

Demo showModal

Return value document . querySelector ( 'button#cancel' ). addEventListener ( 'click' , () => { dialog . returnValue = "Cancel" dialog . close (); }); dialog . addEventListener ( 'close' , () => { document . querySelector ( 'output' ). textContent = dialog . returnValue ; });

Demo Return value

Let’s wrap We have seen the new dialog element, got familiar with its properties and methods. UI-wize there is no big difference between dialog and a div with some CSS games, but we have some functionality of method and events that supports us as programmers

My code repo https://github.com/ranwahle/dialog-demo

Thank you! Ran Wahle [email protected] [email protected] @ranwahle