Last active
July 3, 2024 19:01
-
-
Save OmarMtya/9ce68c563893d1c774f11a94ea73a31c to your computer and use it in GitHub Desktop.
Flowbite decorator to fix Angular routing problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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