UNPKG

2.78 kBSource Map (JSON)View Raw
1{"version":3,"file":"contains-component.js","sources":["../src/utils/contains-component.js"],"sourcesContent":["import React, { useContext, useEffect, useMemo, useState } from 'react'\n\nfunction initContainsComponent() {\n const ContainsContext = React.createContext({ contains: false })\n\n return {\n // Wrap the parent component with this provider.\n /* eslint-disable react/prop-types */\n Provider({ children }) {\n const [count, setCount] = useState(0)\n\n const contextValue = useMemo(() => {\n return {\n updateCount(diff) {\n setCount(count => count + diff)\n },\n contains: count > 0,\n }\n }, [count])\n\n return (\n <ContainsContext.Provider value={contextValue}>\n {children}\n </ContainsContext.Provider>\n )\n },\n /* eslint-enable react/prop-types */\n\n // Call this from the parent component (returns a boolean)\n useContains() {\n return useContext(ContainsContext).contains\n },\n\n // Call this from the child component\n useRegister() {\n const { updateCount } = useContext(ContainsContext)\n\n useEffect(() => {\n if (!updateCount) {\n return\n }\n updateCount(1)\n return () => {\n updateCount(-1)\n }\n // We don't want this effect to ever re-run\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [])\n },\n }\n}\n\nexport { initContainsComponent }\n"],"names":["initContainsComponent","ContainsContext","React","createContext","contains","Provider","children","useState","count","setCount","contextValue","useMemo","updateCount","diff","useContains","useContext","useRegister","useEffect"],"mappings":";;;;;;;;;;;AAEA,SAASA,qBAAT,GAAiC;AAC/B,MAAMC,eAAe,GAAGC,cAAK,CAACC,aAAN,CAAoB;AAAEC,IAAAA,QAAQ,EAAE;AAAZ,GAApB,CAAxB;AAEA,SAAO;AACL;;AACA;AACAC,IAAAA,QAHK,0BAGkB;AAAA,UAAZC,QAAY,QAAZA,QAAY;;AAAA,sBACKC,cAAQ,CAAC,CAAD,CADb;AAAA;AAAA,UACdC,KADc;AAAA,UACPC,QADO;;AAGrB,UAAMC,YAAY,GAAGC,aAAO,CAAC,YAAM;AACjC,eAAO;AACLC,UAAAA,WADK,uBACOC,IADP,EACa;AAChBJ,YAAAA,QAAQ,CAAC,UAAAD,KAAK;AAAA,qBAAIA,KAAK,GAAGK,IAAZ;AAAA,aAAN,CAAR;AACD,WAHI;AAILT,UAAAA,QAAQ,EAAEI,KAAK,GAAG;AAJb,SAAP;AAMD,OAP2B,EAOzB,CAACA,KAAD,CAPyB,CAA5B;AASA,0BACEN,6BAAC,eAAD,CAAiB,QAAjB;AAA0B,QAAA,KAAK,EAAEQ;AAAjC,SACGJ,QADH,CADF;AAKD,KApBI;;AAqBL;AAEA;AACAQ,IAAAA,WAxBK,yBAwBS;AACZ,aAAOC,gBAAU,CAACd,eAAD,CAAV,CAA4BG,QAAnC;AACD,KA1BI;AA4BL;AACAY,IAAAA,WA7BK,yBA6BS;AAAA,wBACYD,gBAAU,CAACd,eAAD,CADtB;AAAA,UACJW,WADI,eACJA,WADI;;AAGZK,MAAAA,eAAS,CAAC,YAAM;AACd,YAAI,CAACL,WAAL,EAAkB;AAChB;AACD;;AACDA,QAAAA,WAAW,CAAC,CAAD,CAAX;AACA,eAAO,YAAM;AACXA,UAAAA,WAAW,CAAC,CAAC,CAAF,CAAX;AACD,SAFD,CALc;AASd;AACD,OAVQ,EAUN,EAVM,CAAT;AAWD;AA3CI,GAAP;AA6CD;;;;"}
\No newline at end of file