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

Commit c901852

Browse files
authored
Merge pull request #128 from geotrev/js/modal-update
Modal trigger/api update
2 parents a9aa5e5 + 47a462a commit c901852

File tree

6 files changed

+43
-42
lines changed

6 files changed

+43
-42
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
)

js/test/modal.spec.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Undernet from "../src/index"
33
// This is the starting DOM.
44
// It is assigned to document.body.innerHTML before each test suite.
55
const dom = `
6-
<button href="#" data-modal-button="new-modal">Open modal</button>
6+
<button data-target="new-modal">Open modal</button>
77
88
<div className="modal-overlay" data-modal="new-modal">
99
<div className="modal-dialog" data-parent="new-modal" aria-labelledby="header-id">
@@ -86,7 +86,7 @@ describe("Modals", () => {
8686
document.body.innerHTML = dom
8787
Undernet.Modals.start()
8888
Undernet.Modals.stop()
89-
button = document.querySelector("[data-modal-button]")
89+
button = document.querySelector("[data-target]")
9090
modalOverlay = document.querySelector("[data-modal]")
9191
modalDialog = document.querySelector("[data-parent]")
9292
button.click()
@@ -109,7 +109,7 @@ describe("Modals", () => {
109109
beforeAll(() => {
110110
document.body.innerHTML = dom
111111
Undernet.Modals.start()
112-
button = document.querySelector("[data-modal-button]")
112+
button = document.querySelector("[data-target]")
113113
modalDialog = document.querySelector("[data-parent]")
114114
modalOverlay = document.querySelector("[data-modal]")
115115
button.click()
@@ -148,7 +148,7 @@ describe("Modals", () => {
148148
beforeAll(() => {
149149
document.body.innerHTML = dom
150150
Undernet.Modals.start()
151-
openButton = document.querySelector("[data-modal-button]")
151+
openButton = document.querySelector("[data-target]")
152152
closeButton = document.querySelector("[data-close]")
153153
modalOverlay = document.querySelector("[data-modal]")
154154
modalDialog = document.querySelector("[data-parent]")
@@ -175,7 +175,7 @@ describe("Modals", () => {
175175
})
176176
})
177177

178-
it("sets focus to [data-modal-button]", () => {
178+
it("sets focus to [data-target]", () => {
179179
expect(document.activeElement).toEqual(openButton)
180180
})
181181
})
@@ -187,7 +187,7 @@ describe("Modals", () => {
187187
beforeAll(() => {
188188
document.body.innerHTML = dom
189189
Undernet.Modals.start()
190-
button = document.querySelector("[data-modal-button]")
190+
button = document.querySelector("[data-target]")
191191
modalOverlay = document.querySelector("[data-modal]")
192192
button.click()
193193
modalOverlay.click()
@@ -205,7 +205,7 @@ describe("Modals", () => {
205205
beforeAll(() => {
206206
document.body.innerHTML = dom
207207
Undernet.Modals.start()
208-
button = document.querySelector("[data-modal-button]")
208+
button = document.querySelector("[data-target]")
209209
modalOverlay = document.querySelector("[data-modal]")
210210
})
211211

@@ -223,13 +223,13 @@ describe("Modals", () => {
223223
beforeAll(() => {
224224
document.body.innerHTML = dom
225225
Undernet.Modals.start()
226-
openButton = document.querySelector("[data-modal-button]")
226+
openButton = document.querySelector("[data-target]")
227227
closeButton = document.querySelector("[data-close]")
228228
openButton.click()
229229
closeButton.click()
230230
})
231231

232-
it("sets focus back to [data-modal-button]", () => {
232+
it("sets focus back to [data-target]", () => {
233233
expect(document.activeElement).toEqual(openButton)
234234
})
235235
})
@@ -241,7 +241,7 @@ describe("Modals", () => {
241241
beforeAll(() => {
242242
document.body.innerHTML = dom
243243
Undernet.Modals.start()
244-
openButton = document.querySelector("[data-modal-button]")
244+
openButton = document.querySelector("[data-target]")
245245
closeButton = document.querySelector("[data-close]")
246246
openButton.click()
247247
closeButton.click()
@@ -258,7 +258,7 @@ describe("Modals", () => {
258258
beforeAll(() => {
259259
document.body.innerHTML = dom
260260
Undernet.Modals.start()
261-
button = document.querySelector("[data-modal-button]")
261+
button = document.querySelector("[data-target]")
262262
button.click()
263263
})
264264

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` 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)