|
| 1 | +// CREATE THIS FILE: client/src/components/FixedLucideIcon.tsx |
| 2 | +import React, { useEffect, useRef } from 'react'; |
| 3 | +import { LucideProps } from 'lucide-react'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Wrapper component that fixes Lucide React SVG className issues |
| 7 | + * Use this instead of importing Lucide icons directly |
| 8 | + */ |
| 9 | +export function FixedLucideIcon({ |
| 10 | + children, |
| 11 | + className, |
| 12 | + ...props |
| 13 | +}: { |
| 14 | + children: React.ReactElement, |
| 15 | + className?: string |
| 16 | +} & LucideProps) { |
| 17 | + const ref = useRef<HTMLElement>(null); |
| 18 | + |
| 19 | + useEffect(() => { |
| 20 | + if (ref.current) { |
| 21 | + // Fix the SVG and all its children |
| 22 | + const svgElements = ref.current.querySelectorAll('svg, svg *'); |
| 23 | + svgElements.forEach((el: Element) => { |
| 24 | + const htmlEl = el as HTMLElement; |
| 25 | + if (htmlEl.className && typeof htmlEl.className === 'object' && 'baseVal' in htmlEl.className) { |
| 26 | + const newClassName = (htmlEl.className as any).baseVal || ''; |
| 27 | + htmlEl.setAttribute('class', newClassName); |
| 28 | + } |
| 29 | + }); |
| 30 | + } |
| 31 | + }); |
| 32 | + |
| 33 | + // Clone the child element and add our ref |
| 34 | + const childWithRef = React.cloneElement(children, { |
| 35 | + ref, |
| 36 | + className: className || children.props.className, |
| 37 | + ...props |
| 38 | + }); |
| 39 | + |
| 40 | + return childWithRef; |
| 41 | +} |
| 42 | + |
| 43 | +// Alternative: Create a hook for existing components |
| 44 | +export function useLucideIconFix() { |
| 45 | + useEffect(() => { |
| 46 | + const fixSVGs = () => { |
| 47 | + const svgElements = document.querySelectorAll('svg.lucide, svg[class*="lucide"]'); |
| 48 | + svgElements.forEach((el: Element) => { |
| 49 | + const htmlEl = el as HTMLElement; |
| 50 | + if (htmlEl.className && typeof htmlEl.className === 'object' && 'baseVal' in htmlEl.className) { |
| 51 | + const newClassName = (htmlEl.className as any).baseVal || ''; |
| 52 | + htmlEl.setAttribute('class', newClassName); |
| 53 | + } |
| 54 | + |
| 55 | + // Also fix children |
| 56 | + const children = htmlEl.querySelectorAll('*'); |
| 57 | + children.forEach((child: Element) => { |
| 58 | + const childEl = child as HTMLElement; |
| 59 | + if (childEl.className && typeof childEl.className === 'object' && 'baseVal' in childEl.className) { |
| 60 | + const newClassName = (childEl.className as any).baseVal || ''; |
| 61 | + childEl.setAttribute('class', newClassName); |
| 62 | + } |
| 63 | + }); |
| 64 | + }); |
| 65 | + }; |
| 66 | + |
| 67 | + // Fix immediately |
| 68 | + fixSVGs(); |
| 69 | + |
| 70 | + // Fix after any potential re-renders |
| 71 | + const timeoutId = setTimeout(fixSVGs, 100); |
| 72 | + |
| 73 | + return () => clearTimeout(timeoutId); |
| 74 | + }); |
| 75 | +} |
0 commit comments