Skip to content

Commit f47acd9

Browse files
committed
Replace leanmodal2 with native dialog element
Adds a lot of good features that make it way more accessible like preventing outside elements from being focused, which can be difficult to do otherwise. Removes dependency on leanmodel2 so quicker page loads. TODO before ready: - Fallback gracefully in browsers that dont support it - Test in real screen readers
1 parent 67b1892 commit f47acd9

File tree

4 files changed

+82
-87
lines changed

4 files changed

+82
-87
lines changed

_scripts/lib/modal.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

_scripts/widgets/download-modal.js

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,37 @@
33
* Code for opening the download overlay widget on the homepage
44
*/
55

6-
import modal from '~/lib/modal'
6+
function detectDialogSupport () {
7+
return typeof document.createElement('dialog').showModal === 'function'
8+
}
9+
10+
// Some browsers have styling for the native dialog element but they dont actually support it.
11+
function swapDialogToDiv ($dialog) {
12+
$dialog.outerHTML = $dialog.outerHTML.replace('<dialog', '<div').replace('</dialog>', '</div>')
13+
}
14+
15+
function moveToAnchor (id) {
16+
const originalURL = location.href
17+
window.location.href = window.location.href.split('#')[0] + '#' + id
18+
// Remove hash from URL after jumping
19+
window.history.replaceState(null, null, originalURL)
20+
}
721

822
export function openDownloadOverlay () {
9-
modal.then(($) => {
10-
const $openModal = $('.open-modal')
11-
console.log('Open the download overlay!')
12-
$openModal.leanModal({
13-
// Add this class to download buttons to make them close it.
14-
closeButton: '.close-modal',
15-
disableCloseOnOverlayClick: true
16-
})
17-
// This is what actually opens the modal overlay.
18-
$openModal.click()
19-
})
23+
const modalId = 'download-modal'
24+
const $downloadModal = document.getElementById(modalId)
25+
if (!$downloadModal) {
26+
return
27+
}
28+
const supportsDialog = detectDialogSupport()
29+
if (!supportsDialog) {
30+
swapDialogToDiv($downloadModal)
31+
moveToAnchor(modalId)
32+
return
33+
}
34+
$downloadModal.showModal()
35+
const $closeButton = $downloadModal.querySelector('.js-close-button')
36+
$closeButton.addEventListener('click', () => {
37+
$downloadModal.close()
38+
}, { once: true })
2039
}

_styles/main.css

Lines changed: 45 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,47 +1018,19 @@ input:focus::placeholder {
10181018
* Modal Dialogs *
10191019
****************/
10201020

1021-
.modal {
1022-
background-color: #f5f5f5;
1023-
border-radius: 4px;
1024-
box-shadow:
1025-
inset 1px 0 0 0 rgba(255, 255, 255, 0.2),
1026-
inset -1px 0 0 0 rgba(255, 255, 255, 0.2),
1027-
inset 0 1px 0 0 #fff,
1028-
0 0 0 1px rgba(0, 0, 0, 0.2),
1029-
0 10px 20px rgba(0, 0, 0, 0.19),
1030-
0 6px 6px rgba(0, 0, 0, 0.23);
1031-
max-height: 100vh;
1032-
max-width: 100vw;
1033-
padding: 12px;
1034-
z-index: 11000;
1035-
display: none;
1036-
text-align: center;
1037-
}
1038-
1039-
.modal p {
1040-
text-align: center;
1041-
margin: 0 6px;
1042-
max-width: 388px;
1043-
}
1044-
1045-
.modal .row.actions {
1046-
margin-top: 24px;
1047-
padding: 0;
1048-
}
1049-
1050-
.modal .row.actions .column {
1051-
margin: 6px !important;
1052-
}
1053-
1054-
@media only screen and (max-width: 425px) {
1055-
.dialog {
1056-
width: calc(100vw - 32px);
1057-
}
1058-
}
1021+
/*
1022+
The use of the [open] selector is to distinguish between
1023+
browsers that support the <dialog> element and therefore <dialog open>
1024+
So that for browsers that dont we can have a sensible inline style that can be anchored to.
1025+
*/
10591026

10601027
.dialog {
1028+
padding: 24px;
1029+
text-align: center;
10611030
background-color: #f5f5f5;
1031+
}
1032+
.dialog[open] {
1033+
border: none;
10621034
border-radius: 3px;
10631035
box-shadow:
10641036
inset 1px 0 0 0 rgba(255, 255, 255, 0.2),
@@ -1068,41 +1040,61 @@ input:focus::placeholder {
10681040
0 8px 20px 1px rgba(0, 0, 0, 0.14),
10691041
0 3px 28px 2px rgba(0, 0, 0, 0.12),
10701042
0 5px 10px -3px rgba(0, 0, 0, 0.4);
1071-
margin: 0 auto;
10721043
padding: 12px;
10731044
padding-top: 24px;
1045+
text-align: initial;
1046+
}
1047+
1048+
@media only screen and (max-width: 425px) {
1049+
.dialog[open] {
1050+
width: calc(100vw - 32px);
1051+
}
10741052
}
10751053

1076-
.dialog img {
1054+
.dialog[open]::backdrop {
1055+
background: rgba(0, 0, 0, 0.7);
1056+
}
1057+
1058+
.dialog[open] .dialog-title {
1059+
font-weight: 700;
1060+
font-size: 1em;
1061+
text-align: left;
1062+
line-height: 24px;
1063+
margin: 0;
1064+
}
1065+
1066+
.dialog[open] img {
10771067
display: inline-block;
10781068
margin-right: 12px;
10791069
user-select: none;
10801070
vertical-align: top;
10811071
}
10821072

1083-
.dialog .content-area {
1073+
.dialog[open] .content-area {
10841074
display: inline-block;
10851075
width: calc(100% - 48px - 18px); /* Subtract icon width and right margin */
10861076
}
10871077

10881078
.dialog p {
1079+
margin: 0 auto;
1080+
text-align: left;
10891081
line-height: 24px;
1090-
margin: 6px 0 0;
10911082
max-width: 380px;
1092-
text-align: left;
10931083
}
10941084

1095-
.dialog p.primary {
1096-
font-weight: 700;
1097-
margin-top: 0;
1085+
.dialog[open] p {
1086+
margin: 6px 0 0;
10981087
}
10991088

11001089
.dialog .action-area {
1090+
margin-top: 12px;
1091+
}
1092+
1093+
.dialog[open] .action-area {
11011094
display: flex;
11021095
flex-flow: row;
11031096
flex-wrap: wrap;
11041097
justify-content: flex-end;
1105-
margin-top: 12px;
11061098
}
11071099

11081100
.dialog .button {
@@ -1121,9 +1113,12 @@ input:focus::placeholder {
11211113
margin-left: 6px;
11221114
}
11231115

1124-
i.fa.fa-close.close-modal {
1125-
cursor: pointer;
1126-
float: left;
1116+
/* If the dialog is inline hide the close button */
1117+
.dialog .close-modal {
1118+
display: none;
1119+
}
1120+
.dialog[open] .close-modal {
1121+
display: block;
11271122
}
11281123

11291124
/*********

index.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -467,21 +467,21 @@
467467

468468
<span id="translate-download" style="display:none;" hidden>Download elementary OS</span>
469469
<span id="translate-purchase" style="display:none;" hidden>Purchase elementary OS</span>
470-
<div id="download-modal" class="dialog modal">
471-
<img alt="Download elementary OS icon" src="images/icons/apps/48/system-os-installer.svg">
470+
<dialog id="download-modal" class="dialog" aria-labelledby="download-modal-title">
471+
<img src="images/icons/apps/48/system-os-installer.svg" alt=""/>
472472
<div class="content-area">
473-
<p class="primary">Choose a Download</p>
473+
<h2 id="download-modal-title" class="dialog-title">Choose a Download</h2>
474474
<p>Download from a localized server or by magnet link. For help and more info, see the <a class="read-more" href="docs/installation" target="_blank" rel="noopener">installation guide</a></p>
475475
</div>
476476
<div class="action-area">
477-
<a class="button clickable close-modal">Close</a>
477+
<button class="js-close-button button clickable close-modal">Close</button>
478478
<div class="linked">
479479
<a class="button suggested-action download-link http" href="<?php echo $download_link.$config['release_filename']; ?>">Download</a>
480480
<a class="button suggested-action download-link magnet" title="Torrent Magnet Link" href="<?php echo 'magnet:?xt=urn:btih:'.$config['release_magnet'].'&dn='.$config['release_filename']; ?>&tr=https%3A%2F%2Fashrise.com%3A443%2Fphoenix%2Fannounce&tr=udp%3A%2F%2Fopen.demonii.com%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80%2Fannounce&ws=http%3A<?php echo urlencode($download_link.$config['release_filename']); ?>"><i class="fa fa-magnet"></i></a>
481481
</div>
482482
</div>
483-
</div>
484-
<a style="display:none;" class="open-modal" href="#download-modal"></a>
483+
</dialog>
484+
485485
<!--[if lt IE 10]><script type="text/javascript" src="https://cdn.jsdelivr.net/gh/eligrey/classList.js@1.1.20170427/classList.min.js"></script><![endif]-->
486486
<?php
487487
include $template['footer'];

0 commit comments

Comments
 (0)