Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: auto init memory leak #441

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions src/plugins/combobox/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,18 @@ class HSComboBox extends HSBasePlugin<IComboBoxOptions> implements IComboBox {
}

static autoInit() {
if (!window.$hsComboBoxCollection) window.$hsComboBoxCollection = [];
if (!window.$hsComboBoxCollection) {
window.$hsComboBoxCollection = [];
window.addEventListener('click', (evt) => {
const evtTarget = evt.target;

HSComboBox.closeCurrentlyOpened(evtTarget as HTMLElement);
});

document.addEventListener('keydown', (evt) =>
HSComboBox.accessibility(evt),
);
}

document
.querySelectorAll('[data-hs-combo-box]:not(.--prevent-on-load-init)')
Expand All @@ -865,18 +876,6 @@ class HSComboBox extends HSBasePlugin<IComboBoxOptions> implements IComboBox {
new HSComboBox(el, options);
}
});

if (window.$hsComboBoxCollection) {
window.addEventListener('click', (evt) => {
const evtTarget = evt.target;

HSComboBox.closeCurrentlyOpened(evtTarget as HTMLElement);
});

document.addEventListener('keydown', (evt) =>
HSComboBox.accessibility(evt),
);
}
}

static close(target: HTMLElement | string) {
Expand Down
29 changes: 14 additions & 15 deletions src/plugins/dropdown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,20 +287,8 @@ class HSDropdown
}

static autoInit() {
if (!window.$hsDropdownCollection) window.$hsDropdownCollection = [];

document
.querySelectorAll('.hs-dropdown:not(.--prevent-on-load-init)')
.forEach((el: IHTMLElementPopper) => {
if (
!window.$hsDropdownCollection.find(
(elC) => (elC?.element?.el as HTMLElement) === el,
)
)
new HSDropdown(el);
});

if (window.$hsDropdownCollection) {
if (!window.$hsDropdownCollection) {
window.$hsDropdownCollection = [];
document.addEventListener('keydown', (evt) =>
HSDropdown.accessibility(evt),
);
Expand All @@ -318,7 +306,18 @@ class HSDropdown
HSDropdown.closeCurrentlyOpened(null, false);
}
});
}
}

document
.querySelectorAll('.hs-dropdown:not(.--prevent-on-load-init)')
.forEach((el: IHTMLElementPopper) => {
if (
!window.$hsDropdownCollection.find(
(elC) => (elC?.element?.el as HTMLElement) === el,
)
)
new HSDropdown(el);
});
}

static open(target: HTMLElement) {
Expand Down
13 changes: 6 additions & 7 deletions src/plugins/overlay/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,12 @@ class HSOverlay extends HSBasePlugin<{}> implements IOverlay {
}

static autoInit() {
if (!window.$hsOverlayCollection) window.$hsOverlayCollection = [];
if (!window.$hsOverlayCollection) {
window.$hsOverlayCollection = [];
document.addEventListener('keydown', (evt) =>
HSOverlay.accessibility(evt),
);
}

document
.querySelectorAll('[data-hs-overlay]:not(.--prevent-on-load-init)')
Expand All @@ -391,12 +396,6 @@ class HSOverlay extends HSBasePlugin<{}> implements IOverlay {
)
new HSOverlay(el);
});

if (window.$hsOverlayCollection) {
document.addEventListener('keydown', (evt) =>
HSOverlay.accessibility(evt),
);
}
}

static open(target: HTMLElement) {
Expand Down
25 changes: 12 additions & 13 deletions src/plugins/select/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1100,7 +1100,18 @@ class HSSelect extends HSBasePlugin<ISelectOptions> implements ISelect {
}

static autoInit() {
if (!window.$hsSelectCollection) window.$hsSelectCollection = [];
if (!window.$hsSelectCollection) {
window.$hsSelectCollection = [];
window.addEventListener('click', (evt) => {
const evtTarget = evt.target;

HSSelect.closeCurrentlyOpened(evtTarget as HTMLElement);
});

document.addEventListener('keydown', (evt) =>
HSSelect.accessibility(evt),
);
}

document
.querySelectorAll('[data-hs-select]:not(.--prevent-on-load-init)')
Expand All @@ -1116,18 +1127,6 @@ class HSSelect extends HSBasePlugin<ISelectOptions> implements ISelect {
new HSSelect(el, options);
}
});

if (window.$hsSelectCollection) {
window.addEventListener('click', (evt) => {
const evtTarget = evt.target;

HSSelect.closeCurrentlyOpened(evtTarget as HTMLElement);
});

document.addEventListener('keydown', (evt) =>
HSSelect.accessibility(evt),
);
}
}

static open(target: HTMLElement | string) {
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/tabs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ class HSTabs extends HSBasePlugin<{}> implements ITabs {
}

static autoInit() {
if (!window.$hsTabsCollection) window.$hsTabsCollection = [];
if (!window.$hsTabsCollection) {
window.$hsTabsCollection = [];
document.addEventListener('keydown', (evt) => HSTabs.accessibility(evt));
}

document
.querySelectorAll(
Expand All @@ -123,9 +126,6 @@ class HSTabs extends HSBasePlugin<{}> implements ITabs {
)
new HSTabs(el);
});

if (window.$hsTabsCollection)
document.addEventListener('keydown', (evt) => HSTabs.accessibility(evt));
}

static open(target: HTMLElement) {
Expand Down