UNPKG

@payfit/unity-icons

Version:

51 lines (50 loc) • 1.37 kB
import { injectSprite as e, isSpriteInDocument as t } from "../../utils.js"; import { createContext as n, useContext as r, useEffect as i, useState as a } from "react"; import { Fragment as o, jsx as s } from "react/jsx-runtime"; //#region src/components/icons-provider/UnityIconsProvider.tsx var c = n(null); function l({ children: n, onError: r, fallback: l }) { let [u, d] = a({ isLoaded: !1, error: null, isLoading: !0 }); return i(() => { if (t()) { d({ isLoaded: !0, error: null, isLoading: !1 }); return; } try { d((e) => ({ ...e, isLoading: !0 })), e(), d({ isLoaded: !0, error: null, isLoading: !1 }); } catch (e) { let t = e instanceof Error ? e : /* @__PURE__ */ Error("Unknown error"); d({ isLoaded: !1, error: t, isLoading: !1 }), r?.(t); } }, [r]), u.error === null ? u.isLoading && l !== void 0 ? /* @__PURE__ */ s(o, { children: l }) : /* @__PURE__ */ s(c.Provider, { value: u, children: n }) : l === void 0 ? /* @__PURE__ */ s("div", { children: "Failed to load Unity icons" }) : /* @__PURE__ */ s(o, { children: l }); } l.displayName = "UnityIconsProvider"; function u() { let e = r(c); if (!e) throw Error("useIconsContext must be used within a UnityIconsProvider"); return e; } //#endregion export { l as UnityIconsProvider, u as useIconsContext };