Skip to content

Commit 901aca2

Browse files
committed
Fix: don't access attributes inside of the constructor
1 parent d7c88a7 commit 901aca2

File tree

1 file changed

+62
-56
lines changed

1 file changed

+62
-56
lines changed

library/sheet.jsx

Lines changed: 62 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -129,37 +129,11 @@ export class SheetElement extends HTMLElement {
129129
*/
130130
returnValue = ""
131131

132+
#performedInitialization = false
133+
132134
constructor() {
133135
super()
134136

135-
this.role = "dialog"
136-
this.ariaModal = true
137-
this.addEventListener("submit", this.#eventListeners.onSubmit)
138-
139-
Object.defineProperties(this.options, {
140-
closeOnBackdropClick: {
141-
get: () =>
142-
!this.hasAttribute("ignore-backdrop-click"),
143-
set: value => Boolean(value)
144-
? this.removeAttribute("ignore-backdrop-click")
145-
: this.setAttribute("ignore-backdrop-click", true)
146-
},
147-
closeOnEscapeKey: {
148-
get: () =>
149-
!this.hasAttribute("ignore-escape-key"),
150-
set: value => Boolean(value)
151-
? this.removeAttribute("ignore-escape-key")
152-
: this.setAttribute("ignore-escape-key", true)
153-
},
154-
closeOnDraggingDown: {
155-
get: () =>
156-
!this.hasAttribute("ignore-dragging-down"),
157-
set: value => Boolean(value)
158-
? this.removeAttribute("ignore-dragging-down")
159-
: this.setAttribute("ignore-dragging-down", true)
160-
}
161-
})
162-
163137
const shadowRoot = this.attachShadow({
164138
mode: "open"
165139
})
@@ -204,6 +178,66 @@ export class SheetElement extends HTMLElement {
204178
)
205179
}
206180

181+
/**
182+
* Attaches event listeners to the window when the sheet is mounted
183+
* @ignore
184+
*/
185+
connectedCallback() {
186+
if (!(this.#performedInitialization)) {
187+
this.role = "dialog"
188+
this.ariaModal = true
189+
this.addEventListener("submit", this.#eventListeners.onSubmit)
190+
191+
Object.defineProperties(this.options, {
192+
closeOnBackdropClick: {
193+
get: () =>
194+
!this.hasAttribute("ignore-backdrop-click"),
195+
set: value => Boolean(value)
196+
? this.removeAttribute("ignore-backdrop-click")
197+
: this.setAttribute("ignore-backdrop-click", true)
198+
},
199+
closeOnEscapeKey: {
200+
get: () =>
201+
!this.hasAttribute("ignore-escape-key"),
202+
set: value => Boolean(value)
203+
? this.removeAttribute("ignore-escape-key")
204+
: this.setAttribute("ignore-escape-key", true)
205+
},
206+
closeOnDraggingDown: {
207+
get: () =>
208+
!this.hasAttribute("ignore-dragging-down"),
209+
set: value => Boolean(value)
210+
? this.removeAttribute("ignore-dragging-down")
211+
: this.setAttribute("ignore-dragging-down", true)
212+
}
213+
})
214+
215+
this.#performedInitialization = true
216+
}
217+
218+
window.addEventListener("keyup", this.#eventListeners.onKeyUp)
219+
220+
window.addEventListener("mousemove", this.#eventListeners.onDragMove)
221+
window.addEventListener("touchmove", this.#eventListeners.onDragMove)
222+
223+
window.addEventListener("mouseup", this.#eventListeners.onDragEnd)
224+
window.addEventListener("touchend", this.#eventListeners.onDragEnd)
225+
}
226+
227+
/**
228+
* Removes all the event listeners when the sheet is no longer mounted
229+
* @ignore
230+
*/
231+
disconnectedCallback() {
232+
window.removeEventListener("keyup", this.#eventListeners.onKeyUp)
233+
234+
window.removeEventListener("mousemove", this.#eventListeners.onDragMove)
235+
window.removeEventListener("touchmove", this.#eventListeners.onDragMove)
236+
237+
window.removeEventListener("mouseup", this.#eventListeners.onDragEnd)
238+
window.removeEventListener("touchend", this.#eventListeners.onDragEnd)
239+
}
240+
207241
/**
208242
* Open the sheet
209243
*/
@@ -414,34 +448,6 @@ export class SheetElement extends HTMLElement {
414448
this.#sheet.style.transform = ""
415449
this.#sheet.style.transition = ""
416450
}
417-
418-
/**
419-
* Attaches event listeners to the window when the sheet is mounted
420-
* @ignore
421-
*/
422-
connectedCallback() {
423-
window.addEventListener("keyup", this.#eventListeners.onKeyUp)
424-
425-
window.addEventListener("mousemove", this.#eventListeners.onDragMove)
426-
window.addEventListener("touchmove", this.#eventListeners.onDragMove)
427-
428-
window.addEventListener("mouseup", this.#eventListeners.onDragEnd)
429-
window.addEventListener("touchend", this.#eventListeners.onDragEnd)
430-
}
431-
432-
/**
433-
* Removes all the event listeners when the sheet is no longer mounted
434-
* @ignore
435-
*/
436-
disconnectedCallback() {
437-
window.removeEventListener("keyup", this.#eventListeners.onKeyUp)
438-
439-
window.removeEventListener("mousemove", this.#eventListeners.onDragMove)
440-
window.removeEventListener("touchmove", this.#eventListeners.onDragMove)
441-
442-
window.removeEventListener("mouseup", this.#eventListeners.onDragEnd)
443-
window.removeEventListener("touchend", this.#eventListeners.onDragEnd)
444-
}
445451
}
446452

447453
export default SheetElement

0 commit comments

Comments
 (0)