File size: 1,924 Bytes
eebc40f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
import React, { useState, useEffect } from 'react';
import Spinner from './Spinner';
/**
* Composant pour afficher un glyphe de police avec spinner
*/
const FontGlyph = ({
symbolId,
size = 16,
className = '',
style = {},
onLoad = null,
onError = null
}) => {
const [loading, setLoading] = useState(true);
const [error, setError] = useState(false);
useEffect(() => {
if (!symbolId) {
setLoading(false);
setError(true);
return;
}
setLoading(true);
setError(false);
// Vérifier si le symbole existe dans le sprite
const checkSymbol = () => {
const symbol = document.getElementById(symbolId);
if (symbol) {
setLoading(false);
setError(false);
if (onLoad) onLoad();
} else {
// Attendre un peu et réessayer
setTimeout(checkSymbol, 50);
}
};
checkSymbol();
}, [symbolId, onLoad, onError]);
if (loading) {
return (
<div
className={`font-glyph-loading ${className}`}
style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
width: size,
height: size,
...style
}}
>
<Spinner size={`${size * 0.6}px`} color="currentColor" />
</div>
);
}
if (error) {
return (
<div
className={`font-glyph-error ${className}`}
style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
width: size,
height: size,
color: '#999',
fontSize: `${size * 0.5}px`,
...style
}}
>
A
</div>
);
}
return (
<svg
className={className}
style={{
width: size,
height: size,
...style
}}
>
<use href={`#${symbolId}`} />
</svg>
);
};
export default FontGlyph;
|