{"version":3,"file":"createB2CComponent.mjs","sources":["../../../../../../../../../web/src/ui/react/bindings/createB2CComponent.tsx"],"sourcesContent":["import { StytchProjectConfigurationInput } from '@stytch/core/public';\nimport { injectCssIntoNode, injectGlobalStyle } from '@stytch/internal-style-injector';\nimport React, { ComponentType, useLayoutEffect, useRef, useState } from 'react';\nimport { createPortal } from 'react-dom';\n\nimport { StytchClient } from '../../../StytchClient';\nimport { PresentationConfig, StytchLoginConfig } from '../../../types';\nimport { useIsMounted__INTERNAL, useStytch } from '../b2c/StytchContext';\nimport { noProviderError, serverRenderError } from '../utils/errors';\nimport { invariant } from '../utils/invariant';\n\n// Mirrors createB2BComponent and createWebComponent\nexport function createB2CComponent<\n  Props extends {\n    client: StytchClient<TProjectConfiguration>;\n    config?: StytchLoginConfig;\n    presentation?: PresentationConfig;\n  },\n  TProjectConfiguration extends StytchProjectConfigurationInput = Stytch.DefaultProjectConfiguration,\n>(name: string, Component: ComponentType<Props>) {\n  const Wrapped: ComponentType<Omit<Props, 'client'>> = (props) => {\n    serverRenderError(name);\n    invariant(useIsMounted__INTERNAL(), noProviderError(`<${name} />`));\n\n    const client = useStytch<TProjectConfiguration>();\n\n    const shadow = props.presentation?.options?.enableShadowDOM ?? props.config?.enableShadowDOM;\n    const root = useRef<HTMLDivElement>(null);\n    const [mountPoint, setMountPoint] = useState<HTMLDivElement>();\n\n    useLayoutEffect(() => {\n      if (shadow) {\n        if (!root.current || root.current.shadowRoot) return;\n        const shadowRoot = root.current.attachShadow({ mode: 'open' });\n        const mountPoint = document.createElement('div');\n\n        shadowRoot.appendChild(injectCssIntoNode());\n        shadowRoot.appendChild(mountPoint);\n\n        setMountPoint(mountPoint);\n      } else {\n        injectGlobalStyle();\n      }\n    }, [shadow]);\n\n    const element = <Component {...(props as Props)} client={client} />;\n    if (shadow) {\n      return <div ref={root}>{mountPoint && createPortal(element, mountPoint)}</div>;\n    } else {\n      return element;\n    }\n  };\n\n  Wrapped.displayName = name;\n  return Wrapped;\n}\n"],"names":["createB2CComponent","name","Component","Wrapped","props","serverRenderError","invariant","useIsMounted__INTERNAL","noProviderError","client","useStytch","shadow","presentation","options","enableShadowDOM","config","root","useRef","mountPoint","setMountPoint","useState","useLayoutEffect","current","shadowRoot","attachShadow","mode","document","createElement","appendChild","injectCssIntoNode","injectGlobalStyle","element","React","div","ref","createPortal","displayName"],"mappings":";;;;;;;AAWA;AACO,SAASA,kBAAAA,CAOdC,IAAY,EAAEC,SAA+B,EAAA;AAC7C,IAAA,MAAMC,UAAgD,CAACC,KAAAA,GAAAA;QACrDC,iBAAAA,CAAkBJ,IAAAA,CAAAA;AAClBK,QAAAA,SAAAA,CAAUC,0BAA0BC,eAAAA,CAAgB,CAAC,CAAC,EAAEP,IAAAA,CAAK,GAAG,CAAC,CAAA,CAAA;AAEjE,QAAA,MAAMQ,MAAAA,GAASC,SAAAA,EAAAA;QAEf,MAAMC,MAAAA,GAASP,MAAMQ,YAAY,EAAEC,SAASC,eAAAA,IAAmBV,KAAAA,CAAMW,MAAM,EAAED,eAAAA;AAC7E,QAAA,MAAME,OAAOC,MAAAA,CAAuB,IAAA,CAAA;QACpC,MAAM,CAACC,UAAAA,EAAYC,aAAAA,CAAc,GAAGC,QAAAA,EAAAA;QAEpCC,eAAAA,CAAgB,IAAA;AACd,YAAA,IAAIV,MAAAA,EAAQ;gBACV,IAAI,CAACK,KAAKM,OAAO,IAAIN,KAAKM,OAAO,CAACC,UAAU,EAAE;AAC9C,gBAAA,MAAMA,UAAAA,GAAaP,IAAAA,CAAKM,OAAO,CAACE,YAAY,CAAC;oBAAEC,IAAAA,EAAM;AAAO,iBAAA,CAAA;gBAC5D,MAAMP,UAAAA,GAAaQ,QAAAA,CAASC,aAAa,CAAC,KAAA,CAAA;AAE1CJ,gBAAAA,UAAAA,CAAWK,WAAW,CAACC,iBAAAA,EAAAA,CAAAA;AACvBN,gBAAAA,UAAAA,CAAWK,WAAW,CAACV,UAAAA,CAAAA;gBAEvBC,aAAAA,CAAcD,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