Skip to content

Commit

Permalink
refactor(instances): destroy the previous instance when overriding
Browse files Browse the repository at this point in the history
  • Loading branch information
zoltanszogyenyi committed Oct 31, 2023
1 parent 7def36e commit 9cbdb65
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 43 deletions.
44 changes: 15 additions & 29 deletions src/components/drawer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,35 +273,21 @@ export function initDrawers() {
'data-drawer-edge-offset'
);

if (
!instances.instanceExists(
'Drawer',
$drawerEl.getAttribute('id')
)
) {
console.log(
`drawer ${$drawerEl.getAttribute('id')} initialised`
);
new Drawer($drawerEl, {
placement: placement ? placement : Default.placement,
bodyScrolling: bodyScrolling
? bodyScrolling === 'true'
? true
: false
: Default.bodyScrolling,
backdrop: backdrop
? backdrop === 'true'
? true
: false
: Default.backdrop,
edge: edge
? edge === 'true'
? true
: false
: Default.edge,
edgeOffset: edgeOffset ? edgeOffset : Default.edgeOffset,
} as DrawerOptions);
}
new Drawer($drawerEl, {
placement: placement ? placement : Default.placement,
bodyScrolling: bodyScrolling
? bodyScrolling === 'true'
? true
: false
: Default.bodyScrolling,
backdrop: backdrop
? backdrop === 'true'
? true
: false
: Default.backdrop,
edge: edge ? (edge === 'true' ? true : false) : Default.edge,
edgeOffset: edgeOffset ? edgeOffset : Default.edgeOffset,
} as DrawerOptions);
} else {
console.error(
`Drawer with id ${drawerId} not found. Are you sure that the data-drawer-target attribute points to the correct drawer id?`
Expand Down
20 changes: 7 additions & 13 deletions src/components/modal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,18 +233,13 @@ export function initModals() {
const placement = $modalEl.getAttribute('data-modal-placement');
const backdrop = $modalEl.getAttribute('data-modal-backdrop');

if (
!instances.instanceExists('Modal', $modalEl.getAttribute('id'))
) {
console.log(`modal ${$modalEl.getAttribute('id')} initialised`);
new Modal(
$modalEl as HTMLElement,
{
placement: placement ? placement : Default.placement,
backdrop: backdrop ? backdrop : Default.backdrop,
} as ModalOptions
);
}
new Modal(
$modalEl as HTMLElement,
{
placement: placement ? placement : Default.placement,
backdrop: backdrop ? backdrop : Default.backdrop,
} as ModalOptions
);
} else {
console.error(
`Modal with id ${modalId} does not exist. Are you sure that the data-modal-target attribute points to the correct modal id?.`
Expand All @@ -270,7 +265,6 @@ export function initModals() {
$modalEl.getAttribute('id')
);
} else {
console.log(`modal ${$modalEl.getAttribute('id')} initialised`);
modal = new Modal(
$modalEl as HTMLElement,
{
Expand Down
9 changes: 8 additions & 1 deletion src/dom/instances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ class Instances {
return;
}

if (forceOverride && this._instances[component][id]) {
this._instances[component][id].destroy();
this._instances[component][id ? id : this._generateRandomId()] =
instance;
return;
}

this._instances[component][id ? id : this._generateRandomId()] =
instance;
}
Expand Down Expand Up @@ -139,7 +146,7 @@ class Instances {
}

if (!this._instances[component][id]) {
console.warn(`Flowbite: Instance with ID ${id} already exists.`);
console.warn(`Flowbite: Instance with ID ${id} does not exist.`);
return false;
}

Expand Down

0 comments on commit 9cbdb65

Please sign in to comment.