Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Commit cf5d410

Browse files
author
george
committed
updates modal to use data-target instead of unnecessary data-modal-button
1 parent a9aa5e5 commit cf5d410

File tree

5 files changed

+32
-31
lines changed

5 files changed

+32
-31
lines changed

js/dist/modal.js

Lines changed: 7 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/dist/modal.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/src/modal.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ const KeyCodes = {
77
const Selectors = {
88
// unique
99
DATA_MODAL: "data-modal",
10-
DATA_MODAL_BUTTON: "data-modal-button",
1110
// common
11+
DATA_TARGET: "data-target",
1212
DATA_VISIBLE: "data-visible",
1313
DATA_CLOSE: "data-close",
1414
DATA_PARENT: "data-parent",
@@ -75,7 +75,6 @@ export default class Modal extends Utils {
7575
*/
7676
start() {
7777
this._modals = nodeListToArray(this._modalContainerAttr)
78-
this._modalButtons = nodeListToArray(`[${Selectors.DATA_MODAL_BUTTON}]`)
7978

8079
getFocusableElements(this._modalContainerAttr).forEach(element => {
8180
element.setAttribute(Selectors.TABINDEX, "-1")
@@ -84,11 +83,8 @@ export default class Modal extends Utils {
8483
if (this._modals.length) {
8584
this._modals.forEach(instance => {
8685
this._setupModal(instance)
87-
})
88-
}
89-
90-
if (this._modalButtons.length) {
91-
this._modalButtons.forEach(button => {
86+
const id = instance.getAttribute(Selectors.DATA_MODAL)
87+
const button = document.querySelector(`[${Selectors.DATA_TARGET}='${id}']`)
9288
button.addEventListener(Events.CLICK, this._render)
9389
})
9490
}
@@ -98,7 +94,9 @@ export default class Modal extends Utils {
9894
* Stop listening to modals
9995
*/
10096
stop() {
101-
this._modalButtons.forEach(button => {
97+
this._modals.forEach(instance => {
98+
const id = instance.getAttribute(Selectors.DATA_MODAL)
99+
const button = document.querySelector(`[${Selectors.DATA_TARGET}='${id}']`)
102100
button.removeEventListener(Events.CLICK, this._render)
103101
})
104102
}
@@ -112,7 +110,7 @@ export default class Modal extends Utils {
112110
_render(event) {
113111
event.preventDefault()
114112
this._activeModalButton = event.target
115-
this._activeModalId = this._activeModalButton.getAttribute(Selectors.DATA_MODAL_BUTTON)
113+
this._activeModalId = this._activeModalButton.getAttribute(Selectors.DATA_TARGET)
116114

117115
if (!this._activeModalId) {
118116
return console.error(Messages.NO_BUTTON_ID_ERROR)
@@ -121,8 +119,14 @@ export default class Modal extends Utils {
121119
this._activeModalOverlay = document.querySelector(
122120
`[${Selectors.DATA_MODAL}="${this._activeModalId}"]`
123121
)
122+
123+
// no modal overlay error
124+
124125
this._activeModalSelector = `[${Selectors.DATA_PARENT}='${this._activeModalId}']`
125126
this._activeModal = this._activeModalOverlay.querySelector(this._activeModalSelector)
127+
128+
// no modal error
129+
126130
this._activeModalCloseButtons = nodeListToArray(
127131
`${this._activeModalSelector} [${Selectors.DATA_CLOSE}]`
128132
)

scss/components/_modal.scss

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@
5353
}
5454

5555
header {
56-
display: flex;
57-
align-items: center;
58-
5956
h1,
6057
h2,
6158
h3,
@@ -70,10 +67,12 @@
7067

7168
header > button[data-close],
7269
header > a[data-close] {
73-
margin-left: auto;
7470
font-size: 32px;
7571
text-decoration: none;
7672
padding: 0 8px;
73+
position: absolute;
74+
right: 8px;
75+
top: 2px;
7776
}
7877

7978
header,

site/docs/modals.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Use modals to focus the user experience on a critical task or set of information
44

55
Check out this example modal:
66

7-
<button href="#" data-modal-button="new-modal">Open modal</button>
7+
<button data-target="new-modal">Open modal</button>
88

99
<div class="modal-overlay" data-modal="new-modal">
1010
<div class="modal-dialog" data-parent="new-modal" aria-labelledby="header-id">
@@ -31,7 +31,7 @@ Check out this example modal:
3131
</div>
3232

3333
```html
34-
<button href="#" data-modal-button="new-modal">Open modal</button>
34+
<button data-target="new-modal">Open modal</button>
3535
```
3636

3737
```html
@@ -67,7 +67,7 @@ You can have custom contents as well (hence subjective markup). Meaning the head
6767

6868
The modal will be able to handle long-content with ease, turning the overlay into a scrollable area.
6969

70-
<button href="#" data-modal-button="new-modal-3">Open modal</button>
70+
<button data-target="new-modal-3">Open modal</button>
7171

7272
<div class="modal-overlay" data-modal="new-modal-3">
7373
<div class="modal-dialog" data-parent="new-modal-3" aria-labelledby="header-id">
@@ -97,7 +97,7 @@ The modal will be able to handle long-content with ease, turning the overlay int
9797

9898
Adding the `is-centered` class onto the modal overlay will vertically center the modal dialog on the screen.
9999

100-
<button href="#" data-modal-button="new-modal-2">Open centered modal</button>
100+
<button data-target="new-modal-2">Open centered modal</button>
101101

102102
<div class="modal-overlay is-centered" data-modal="new-modal-2">
103103
<div class="modal-dialog" data-parent="new-modal-2" aria-labelledby="header-id-2">
@@ -107,7 +107,6 @@ Adding the `is-centered` class onto the modal overlay will vertically center the
107107
</h2>
108108
<a data-close href="#">
109109
<span aria-hidden="true">&times;</span>
110-
<span class"is-visually-hidden">close modal</span>
111110
</a>
112111
</header>
113112
<section>
@@ -125,7 +124,7 @@ Adding the `is-centered` class onto the modal overlay will vertically center the
125124
</div>
126125

127126
```html
128-
<button href="#" data-modal-button="new-modal-2">Open centered modal</button>
127+
<button data-target="new-modal-2">Open centered modal</button>
129128
```
130129

131130
```html
@@ -147,10 +146,10 @@ Two main pieces are required: an API call and correct HTML markup.
147146
For the modal button, it should have two main properties:
148147

149148
```html
150-
<button data-modal-button="new-modal">Press me</button>
149+
<button data-target="new-modal">Press me</button>
151150
```
152151

153-
- `data-modal-button`: an attribute containing a unique id pointing to the modal overlay's `data-modal-id` attribute.
152+
- `data-target`: an attribute containing a unique id pointing to the modal overlay's `data-modal-id` attribute.
154153

155154
#### Modal Attributes
156155

@@ -176,7 +175,7 @@ For the modal itself, you need a few more things.
176175
</div>
177176
```
178177

179-
- `data-modal`: an attribute matching your corresponding button's `data-modal-button` value. Add it to the modal overlay element. overlay.
178+
- `data-modal`: an attribute matching your corresponding button's `data-target` value. Add it to the modal overlay element. overlay.
180179
- `data-parent`: an attribute pointing to the modal wrapper. It should equal the value of the element with `data-modal`.
181180
- `data-close`: an attribute indicating a button or link will close the current modal dialog.
182181

0 commit comments

Comments
 (0)