UNPKG

better-react-mathjax

Version:

Up-to-date component for using MathJax in latest React (using functional components and hooks API). Focuses on being versatile and making the use of MathJax in React a pleasant experience without flashes of non-typeset content, both with respect to initia

1 lines 3.71 kB
"use client";var __assign=this&&this.__assign||function(){return(__assign=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var i in t=arguments[n])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e}).apply(this,arguments)},__rest=this&&this.__rest||function(e,t){var n={};for(i in e)Object.prototype.hasOwnProperty.call(e,i)&&t.indexOf(i)<0&&(n[i]=e[i]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols)for(var r=0,i=Object.getOwnPropertySymbols(e);r<i.length;r++)t.indexOf(i[r])<0&&Object.prototype.propertyIsEnumerable.call(e,i[r])&&(n[i[r]]=e[i[r]]);return n};import React,{useContext,useEffect,useLayoutEffect,useRef}from"react";import{MathJaxBaseContext}from"../MathJaxContext";var typesettingFailed=function(e){return"Typesetting failed: ".concat(void 0!==e.message?e.message:JSON.stringify(e))},MathJax=function(e){function r(){var e;"every"===h&&m&&"post"===v&&null!==f.current&&(f.current.style.visibility=null!=(e=null==(e=p.style)?void 0:e.visibility)?e:"visible"),_.current||("first"===h&&null!==f.current&&(f.current.style.visibility="visible"),n&&n(),_.current=!0),o&&o(),b.current=!1}var t=e.inline,i=void 0!==t&&t,t=e.hideUntilTypeset,n=e.onInitTypeset,o=e.onTypeset,s=e.text,u=e.dynamic,a=e.typesettingOptions,l=e.renderMode,c=e.children,p=__rest(e,["inline","hideUntilTypeset","onInitTypeset","onTypeset","text","dynamic","typesettingOptions","renderMode","children"]),y=useRef(""),f=useRef(null),d=useContext(MathJaxBaseContext),h=null!=t?t:null==d?void 0:d.hideUntilTypeset,v=null!=l?l:null==d?void 0:d.renderMode,g=null!=a?a:null==d?void 0:d.typesettingOptions,m=!1!==u&&(u||"production"!==process.env.NODE_ENV),_=useRef(!1),b=useRef(!1);return!b.current&&null!==f.current&&m&&"every"===h&&"post"===v&&(f.current.style.visibility="hidden"),("undefined"!=typeof window?useLayoutEffect:useEffect)(function(){if((m||!_.current)&&null!==f.current){if(!d)throw Error("MathJax was not loaded, did you use the MathJax component outside of a MathJaxContext?");if("pre"===v){if(!("string"==typeof(e=s)&&0<e.length))throw Error("Render mode 'pre' requires text prop to be set and non-empty, which was currently \"".concat(s,'"'));if(!a||!a.fn)throw Error("Render mode 'pre' requires 'typesettingOptions' prop with 'fn' property to be set on MathJax element or in the MathJaxContext");if(2===d.version)throw Error("Render mode 'pre' only available with MathJax 3 and 4, and version 2 is currently in use")}"post"!==v&&s===y.current||b.current||(b.current=!0,3===d.version||4===d.version?d.promise.then(function(t){var n;"pre"===v?(n=function(e){y.current=s,t.startup.document.clear(),t.startup.document.updateDocument(),null!==f.current&&(f.current.innerHTML=e.outerHTML),r()},a.fn.endsWith("Promise")?t.startup.promise.then(function(){return t[g.fn](s,__assign(__assign({},(null==g?void 0:g.options)||{}),{display:!i}))}).then(n).catch(function(e){throw r(),Error(typesettingFailed(e))}):t.startup.promise.then(function(){var e=t[g.fn](s,__assign(__assign({},(null==g?void 0:g.options)||{}),{display:!i}));n(e)}).catch(function(e){throw r(),Error(typesettingFailed(e))})):t.startup.promise.then(function(){return t.typesetClear([f.current]),t.typesetPromise([f.current])}).then(r).catch(function(e){throw r(),Error(typesettingFailed(e))})}).catch(function(e){throw r(),Error(typesettingFailed(e))}):d.promise.then(function(e){e.Hub.Queue(["Typeset",e.Hub,f.current]),e.Hub.Queue(r)}).catch(function(e){throw r(),Error(typesettingFailed(e))}))}var e}),React.createElement("span",__assign({},p,{style:__assign(__assign({display:i?"inline":"block"},p.style),{visibility:h?"hidden":null==(e=p.style)?void 0:e.visibility}),ref:f}),c)};export default MathJax;