Skip to content

Commit 437ed2f

Browse files
committed
updates
1 parent 7a4785c commit 437ed2f

File tree

3 files changed

+105
-93
lines changed

3 files changed

+105
-93
lines changed

elements/pfe-modal/README.md

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Overview
44

5-
`pfe-modal` is a self-contained modal window that is hidden on page load and (when activated) restricts the user from interacting with content in the main window.
5+
`pfe-modal` is a self-contained modal window that is hidden on page load and (when activated) restricts the user from interacting with content in the main window.
66

77
## Usage
88
```html
@@ -19,30 +19,41 @@
1919
## Slots
2020

2121
### Trigger
22-
The only part visible on page load, the trigger opens the modal window. The trigger can be a button, a cta or a link. While it is part of the modal web component, it does not contain any intrinsic styles.
22+
The only part visible on page load, the trigger opens the modal window. The trigger can be a button, a cta or a link. While it is part of the modal web component, it does not contain any intrinsic styles.
2323

2424
### Header
25-
The header is an optional slot that appears at the top of the modal window. It should be a header tag (h2-h6).
25+
The header is an optional slot that appears at the top of the modal window. It should be a header tag (h2-h6).
2626

2727
### Default slot
2828
The default slot can contain any type of content. When the header is not present this unnamed slot appear at the top of the modal window (to the left of the close button). Otherwise it will appear beneath the header.
2929

30-
## Events
30+
## API
3131

32-
### openModal
33-
Fires when a user clicks on the trigger. openModal can be accessed from outside the web component by getting the modal that you want to fire and passing in the firing event: `document.querySelector("pfe-modal#custom-id").openModal(event).`
32+
### open
3433

35-
### closeModal
36-
Fires when either a user clicks on either the close button or the overlay. closeModal can be accessed from outside the web component by getting the modal that you want to fire and passing in the firing event: `document.querySelector("pfe-modal#custom-id").closeModal(event).`
34+
### close
3735

38-
### Dependencies
39-
Make sure you have [Polyserve][polyserve] and [Web Component Tester][web-component-tester] installed.
36+
### toggle
4037

41-
npm install -g polyserve web-component-tester
38+
## Events
4239

43-
## Dev
40+
### pfe-modal:open
41+
Fires when a user clicks on the trigger. open can be accessed from outside the web component by getting the modal that you want to fire and passing in the firing event: `document.querySelector("pfe-modal#custom-id").open(event).`
4442

45-
npm start
43+
```
44+
detail: {
45+
open: true
46+
}
47+
```
48+
49+
### pfe-modal:close
50+
Fires when either a user clicks on either the close button or the overlay. close can be accessed from outside the web component by getting the modal that you want to fire and passing in the firing event: `document.querySelector("pfe-modal#custom-id").close(event).`
51+
52+
```
53+
detail: {
54+
open: false
55+
}
56+
```
4657

4758
## Test
4859

@@ -54,13 +65,14 @@ Make sure you have [Polyserve][polyserve] and [Web Component Tester][web-compone
5465

5566
## Demo
5667

57-
Run `npm start` and Polyserve will start a server and open your default browser to the demo page of the element.
68+
From the PFElements root directory, run:
69+
70+
npm start
5871

5972
## Code style
6073

61-
Modal (and all PatternFly Elements) use [Prettier][prettier] to auto-format JS and JSON. The style rules get applied when you commit a change. If you choose to, you can [integrate your editor][prettier-ed] with Prettier to have the style rules applied on every save.
74+
Collapsible (and all PFElements) use [Prettier][prettier] to auto-format JS and JSON. The style rules get applied when you commit a change. If you choose to, you can [integrate your editor][prettier-ed] with Prettier to have the style rules applied on every save.
6275

6376
[prettier]: https://github.com/prettier/prettier/
6477
[prettier-ed]: https://github.com/prettier/prettier/#editor-integration
65-
[polyserve]: https://github.com/Polymer/polyserve
6678
[web-component-tester]: https://github.com/Polymer/web-component-tester

elements/pfe-modal/demo/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ <h2 slot="pfe-modal--header">Modal with a header with a super duper long title a
7171

7272
<script>
7373
document.querySelector("#custom-trigger").addEventListener("click", function (event) {
74-
document.querySelector("#custom-modal").openModal(event);
74+
document.querySelector("#custom-modal").open(event);
7575
});
7676
</script>
7777
</body>

elements/pfe-modal/src/pfe-modal.js

Lines changed: 76 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -29,58 +29,25 @@ class PfeModal extends PFElement {
2929
return PFElement.PfeTypes.Container;
3030
}
3131

32-
// static get observedAttributes() {
33-
// return [];
34-
// }
35-
36-
openModal(event) {
37-
event.preventDefault();
38-
39-
this.trigger = event ? event.target : window.event.srcElement;
40-
41-
this.dispatchEvent(
42-
new CustomEvent(`${this.tag}:open`, {
43-
detail: {
44-
open: true
45-
},
46-
bubbles: true
47-
})
48-
);
49-
}
50-
51-
closeModal(event) {
52-
event.preventDefault();
53-
54-
this.dispatchEvent(
55-
new CustomEvent(`${this.tag}:close`, {
56-
detail: {
57-
open: false
58-
},
59-
bubbles: true
60-
})
61-
);
62-
}
63-
6432
constructor() {
6533
super(PfeModal, { type: PfeModal.PfeType });
6634

6735
this.header_id = this.randomId;
68-
69-
this.open = false;
36+
this.isOpen = false;
7037

7138
this._keydownHandler = this._keydownHandler.bind(this);
72-
this._toggleModal = this._toggleModal.bind(this);
39+
this.toggle = this.toggle.bind(this);
7340

7441
// These fire custom events
75-
this.openModal = this.openModal.bind(this);
76-
this.closeModal = this.closeModal.bind(this);
42+
this.open = this.open.bind(this);
43+
this.close = this.close.bind(this);
7744

7845
this._modalWindow = this.shadowRoot.querySelector(`.${this.tag}__window`);
7946
this._modalCloseButton = this.shadowRoot.querySelector(`.${this.tag}__close`);
8047
this._overlay = this.shadowRoot.querySelector(`.${this.tag}__overlay`);
8148
this._container = this.shadowRoot.querySelector(`.${this.tag}__container`);
8249
this._outer = this.shadowRoot.querySelector(`.${this.tag}__outer`);
83-
50+
8451
this._observer = new MutationObserver(() => {
8552
this._mapSchemaToSlots(this.tag, this.slots);
8653
this._init();
@@ -92,28 +59,22 @@ class PfeModal extends PFElement {
9259

9360
this._init();
9461

95-
this.addEventListener(`${this.tag}:open`, this._toggleModal);
96-
this.addEventListener(`${this.tag}:close`, this._toggleModal);
97-
9862
this.addEventListener("keydown", this._keydownHandler);
9963
this._modalCloseButton.addEventListener("keydown", this._keydownHandler);
100-
this._modalCloseButton.addEventListener("click", this.closeModal);
101-
this._overlay.addEventListener("click", this.closeModal);
64+
this._modalCloseButton.addEventListener("click", this.close);
65+
this._overlay.addEventListener("click", this.close);
10266

10367
this._observer.observe(this, { childList: true });
10468
}
10569

10670
disconnectedCallback() {
107-
this.removeEventListener(`${this.tag}:open`, this._toggleModal);
108-
this.removeEventListener(`${this.tag}:close`, this._toggleModal);
109-
11071
this.removeEventListener("keydown", this._keydownHandler);
111-
this._modalCloseButton.removeEventListener("click", this.closeModal);
112-
this._modalCloseButton.removeEventListener("click", this.closeModal);
113-
this._overlay.removeEventListener("click", this.closeModal);
72+
this._modalCloseButton.removeEventListener("click", this.close);
73+
this._modalCloseButton.removeEventListener("click", this.close);
74+
this._overlay.removeEventListener("click", this.close);
11475

11576
if (this.trigger) {
116-
this.trigger.removeEventListener("click", this.openModal);
77+
this.trigger.removeEventListener("click", this.open);
11778
}
11879

11980
this._observer.disconnect();
@@ -129,7 +90,7 @@ class PfeModal extends PFElement {
12990
this.body = [...this.querySelectorAll(`*:not([slot])`)];
13091

13192
if (this.trigger) {
132-
this.trigger.addEventListener("click", this.openModal);
93+
this.trigger.addEventListener("click", this.open);
13394
}
13495

13596
if (this.header) {
@@ -161,41 +122,80 @@ class PfeModal extends PFElement {
161122
case "Escape":
162123
case "Esc":
163124
case 27:
164-
this.closeModal(event);
125+
this.close(event);
165126
return;
166127
case "Enter":
167128
case 13:
168129
if (target === this.trigger) {
169-
this.openModal(event);
130+
this.open(event);
170131
}
171132
return;
172133
}
173134
}
174135

175-
_toggleModal(event) {
176-
if(event && event.detail) {
177-
if(event.detail.open) {
178-
this.open = true;
179-
// Reveal the container and overlay
180-
this._modalWindow.removeAttribute("hidden");
181-
this._overlay.removeAttribute("hidden");
182-
this._outer.removeAttribute("hidden");
183-
// This prevents background scroll
184-
document.body.style.overflow = "hidden";
185-
// Set the focus to the container
186-
this._modalWindow.focus();
187-
} else {
188-
this.open = false;
189-
// Hide the container and overlay
190-
this._modalWindow.setAttribute("hidden", true);
191-
this._overlay.setAttribute("hidden", true);
192-
this._outer.setAttribute("hidden", true);
193-
// Return scrollability
194-
document.body.style.overflow = "auto";
195-
// Move focus back to the trigger element
196-
this.trigger.focus();
197-
}
136+
toggle(event) {
137+
this.isOpen ? this.close(event) : this.open(event);
138+
}
139+
140+
open(event) {
141+
if (event) {
142+
event.preventDefault();
143+
this.trigger = event ? event.target : window.event.srcElement;
144+
}
145+
146+
const detail = {
147+
open: true
148+
};
149+
150+
if (event && this.trigger) {
151+
detail.trigger = this.trigger;
152+
}
153+
154+
this.isOpen = true;
155+
// Reveal the container and overlay
156+
this._modalWindow.removeAttribute("hidden");
157+
this._overlay.removeAttribute("hidden");
158+
this._outer.removeAttribute("hidden");
159+
// This prevents background scroll
160+
document.body.style.overflow = "hidden";
161+
// Set the focus to the container
162+
this._modalWindow.focus();
163+
164+
this.dispatchEvent(
165+
new CustomEvent(`${this.tag}:open`, {
166+
detail: detail,
167+
bubbles: true
168+
})
169+
);
170+
}
171+
172+
close(event) {
173+
if (event) {
174+
event.preventDefault();
198175
}
176+
177+
this.isOpen = false;
178+
// Hide the container and overlay
179+
this._modalWindow.setAttribute("hidden", true);
180+
this._overlay.setAttribute("hidden", true);
181+
this._outer.setAttribute("hidden", true);
182+
// Return scrollability
183+
document.body.style.overflow = "auto";
184+
185+
if (this.trigger) {
186+
// Move focus back to the trigger element
187+
this.trigger.focus();
188+
this.trigger = null;
189+
}
190+
191+
this.dispatchEvent(
192+
new CustomEvent(`${this.tag}:close`, {
193+
detail: {
194+
open: false
195+
},
196+
bubbles: true
197+
})
198+
);
199199
}
200200
}
201201

0 commit comments

Comments
 (0)