Skip to content

Instantly share code, notes, and snippets.

@OmarMtya
Last active July 3, 2024 19:01
Show Gist options
  • Save OmarMtya/9ce68c563893d1c774f11a94ea73a31c to your computer and use it in GitHub Desktop.
Save OmarMtya/9ce68c563893d1c774f11a94ea73a31c to your computer and use it in GitHub Desktop.
Flowbite decorator to fix Angular routing problem
import { initFlowbite } from "flowbite";
import { Subject, concatMap, delay, of } from "rxjs";
let flowbiteQueue = new Subject<any>();
flowbiteQueue.pipe(
concatMap(item => of(item).pipe(delay(100)))
).subscribe((x) => {
x();
})
export function Flowbite() {
return function (target: any) {
const originalOnInit = target.prototype.ngOnInit;
target.prototype.ngOnInit = function () {
if (originalOnInit) {
originalOnInit.apply(this);
}
InitFlowbiteFix();
};
};
}
export function InitFlowbiteFix () {
flowbiteQueue.next(() => {
const elements = document.querySelectorAll('*');
const flowbiteElements = [];
const initializedElements = Array.from(document.querySelectorAll('[flowbite-initialized]'));
for (let i = 0; i < elements.length; i++) {
const element = elements[i];
const attributes = element.attributes;
for (let j = 0; j < attributes.length; j++) {
const attribute = attributes[j];
if (attribute.name.startsWith('data-')) {
// add to the flowbiteElements array if it doesn't exist
if (!flowbiteElements.includes(element) && !initializedElements.find(x => x.isEqualNode(element))) {
flowbiteElements.push(element);
}
}
}
}
// add an attribute to the element to indicate that it has been initialized
for (let i = 0; i < flowbiteElements.length; i++) {
const element = flowbiteElements[i];
element.setAttribute('flowbite-initialized', '');
}
initFlowbite();
flowbiteElements.forEach(element => {
const attributes: { name: string; value: string }[] = Array.from(element.attributes);
const dataAttributes = attributes.filter(attribute => attribute.name.startsWith('data-'));
dataAttributes.forEach(attribute => {
element.setAttribute(attribute.name.replace('data-', 'fb-'), attribute.value);
element.removeAttribute(attribute.name);
});
});
});
}
@juanpabloguerra16
Copy link

juanpabloguerra16 commented Jun 26, 2024

La mejor idea sería que hagas un solo request HTTP por fuera, que sería el contenedor, y hacer un ngFor de los elementos que quieras repetir, pero que este for loop sea un componente personalizado con el decorador en el componente. Así cada componente ejecuta la función de initFlowbiteFix y se arregla el problema cada que se redibujan los elementos.

@OmarMtya
Aqui te dejo un proyecto de prueba para que puedas replicar el error. En este caso nisiquera estoy haciendo una llamada HTTP. Simplemente al condicionar un nodo usando ngif ya no funciona luego flowbite. Intente agregar tu decorador y tampoco. Dejame saber que consigues.

https://github.com/juanpabloguerra16/flowbite-angular-app

@juanpabloguerra16
Copy link

@OmarMtya no se si pudiste ver mi mensaje? Saludos

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment