MCPcopy Create free account
hub / github.com/adobe/react-spectrum / Fonts

Function Fonts

packages/@react-spectrum/s2/src/Fonts.tsx:43–106  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

41};
42
43export function Fonts(): ReactNode {
44 let {locale: localeString} = useLocale();
45 let locale = useMemo(() => new Intl.Locale(localeString), [localeString]);
46 let languageAndRegion = locale.language + (locale.region ? '-' + locale.region : '');
47 let languageAndScript = locale.language + (locale.script ? '-' + locale.script : '');
48
49 // Load script tag for CJK font
50 let typekitId =
51 scripts[locale.baseName] ||
52 scripts[languageAndRegion] ||
53 scripts[languageAndScript] ||
54 scripts[locale.language];
55 let script = typekitId ? `https://use.typekit.net/${typekitId}.js` : null;
56
57 let scriptRef = useRef<HTMLScriptElement | null>(null);
58 useEffect(() => {
59 let scriptEl = scriptRef.current;
60
61 // In older React versions, we must manually insert scripts into the <head>.
62 // Scripts rendered by React are never executed.
63 if (script && parseInt(ReactVersion, 10) < 19) {
64 scriptEl = Array.from(document.scripts).find(s => s.src === script) || null;
65 if (!scriptEl) {
66 scriptEl = document.createElement('script');
67 scriptEl.async = true;
68 scriptEl.src = script;
69 document.head.appendChild(scriptEl);
70 }
71 }
72
73 if (scriptEl) {
74 scriptEl.onload = () => {
75 if (
76 typeof window['Typekit'] !== 'undefined' &&
77 typeof window['Typekit'].load === 'function'
78 ) {
79 window['Typekit'].load();
80 }
81 };
82 }
83 }, [scriptRef, script]);
84
85 // When using React 19, async scripts are automatically deduped and hoisted into the <head>.
86 // This also works during SSR.
87 if (script && parseInt(ReactVersion, 10) >= 19) {
88 return <script async src={script} key={script} ref={scriptRef} />;
89 }
90
91 // Preload fonts referenced by CSS file using React 19's preloading mechanism (when available).
92 // This will insert a <link rel="preload"> tag in the <head>, which is useful during SSR to start
93 // font loading before the CSS downloads.
94 let preloadUrls = preloads[locale.language] || preloads.en;
95 if (preloadUrls && typeof ReactDOM['preload'] === 'function') {
96 for (let url of preloadUrls) {
97 ReactDOM.preload(url, {
98 as: 'font',
99 type: 'font/woff2',
100 crossOrigin: ''

Callers

nothing calls this directly

Calls 4

useLocaleFunction · 0.90
createElementMethod · 0.80
appendChildMethod · 0.80
loadMethod · 0.80

Tested by

no test coverage detected