UNPKG

2.58 kBSource Map (JSON)View Raw
1{"version":3,"file":"index-0db71dc1.js","sources":["../src/providers/Root/Root.js"],"sourcesContent":["import React, { useCallback, useContext, useState } from 'react'\nimport PropTypes from 'prop-types'\n\nconst RootContext = React.createContext(null)\n\nfunction RootProvider({ children, ...props }) {\n const [element, setElement] = useState(null)\n\n const handleRef = useCallback(element => {\n if (element !== null) {\n setElement(element)\n }\n }, [])\n\n return (\n <RootContext.Provider value={element}>\n <div ref={handleRef} {...props}>\n {/*\n We don’t render the children tree until the element is present, at\n the second rendering.\n\n The reason why it is needed is because element references are\n assigned after the first rendering, and we don’t want to let\n `<Root />` consumers having to deal with the reference being `null`\n at the first rendering.\n\n This way, we can guarantee that if a consumer gets `null` rather\n than the element, it’s because <Root.Provider /> has to be defined\n at an upper level.\n */\n element ? children : null}\n </div>\n </RootContext.Provider>\n )\n}\n\nRootProvider.propTypes = {\n children: PropTypes.node,\n}\n\nfunction Root(props) {\n return <RootContext.Consumer {...props} />\n}\nRoot.Provider = RootProvider\n\nconst useRoot = () => useContext(RootContext)\n\nexport { Root, useRoot }\nexport default Root\n"],"names":["RootContext","React","createContext","RootProvider","children","props","useState","element","setElement","handleRef","useCallback","propTypes","PropTypes","node","Root","Provider","useRoot","useContext"],"mappings":";;;;;;;;;;;AAGA,IAAMA,WAAW,GAAGC,cAAK,CAACC,aAAN,CAAoB,IAApB,CAApB;;AAEA,SAASC,YAAT,OAA8C;AAAA,MAAtBC,QAAsB,QAAtBA,QAAsB;AAAA,MAATC,KAAS;;AAAA,kBACdC,cAAQ,CAAC,IAAD,CADM;AAAA;AAAA,MACrCC,OADqC;AAAA,MAC5BC,UAD4B;;AAG5C,MAAMC,SAAS,GAAGC,iBAAW,CAAC,UAAAH,OAAO,EAAI;AACvC,QAAIA,OAAO,KAAK,IAAhB,EAAsB;AACpBC,MAAAA,UAAU,CAACD,OAAD,CAAV;AACD;AACF,GAJ4B,EAI1B,EAJ0B,CAA7B;AAMA,sBACEN,6BAAC,WAAD,CAAa,QAAb;AAAsB,IAAA,KAAK,EAAEM;AAA7B,kBACEN;AAAK,IAAA,GAAG,EAAEQ;AAAV,KAAyBJ,KAAzB;AACG;;;;;;;;;;;AAaDE,EAAAA,OAAO,GAAGH,QAAH,GAAc,IAdvB,CADF,CADF;AAoBD;;AAEDD,YAAY,CAACQ,SAAb,GAAyB;AACvBP,EAAAA,QAAQ,EAAEQ,eAAS,CAACC;AADG,CAAzB;;AAIA,SAASC,IAAT,CAAcT,KAAd,EAAqB;AACnB,sBAAOJ,6BAAC,WAAD,CAAa,QAAb,EAA0BI,KAA1B,CAAP;AACD;;AACDS,IAAI,CAACC,QAAL,GAAgBZ,YAAhB;;IAEMa,OAAO,GAAG,SAAVA,OAAU;AAAA,SAAMC,gBAAU,CAACjB,WAAD,CAAhB;AAAA;;;;;"}
\No newline at end of file