import { PortalContext, DOMContextSnapshot, ZIndexLayer, ContextTrackingConfig } from './types'; /** * Options for creating a portal context. */ export interface PortalContextOptions { /** Target portal root element or selector */ target?: Element | string; /** Z-index layer for the portal */ layer?: ZIndexLayer; /** Whether to bridge events back to source */ bridgeEvents?: boolean; /** Parent portal context (for nesting) */ parentPortal?: PortalContext | null; } /** * Callback for portal lifecycle events. */ export type PortalLifecycleCallback = (portal: PortalContext) => void; /** * Event handler for bridged events. */ export type BridgedEventHandler = (event: Event, sourceContext: DOMContextSnapshot) => void; /** * Manages portal contexts and context bridging across portal boundaries. * * @remarks * Singleton class that handles the creation and management of portal contexts, * ensuring that DOM context is preserved when components render through portals. * * @example * ```typescript * const manager = PortalContextManager.getInstance(); * * // Create a portal context from a source element * const context = manager.createPortalContext(sourceElement, { * target: document.getElementById('modal-root'), * layer: 'modal', * bridgeEvents: true, * }); * * // Use the context in your portal * // When done: * manager.destroyPortalContext(context.portalId); * ``` */ export declare class PortalContextManager { private static instance; /** Map of portal IDs to their contexts */ private readonly portalContexts; /** Map of portal IDs to their source elements */ private readonly sourceElements; /** Map of portal IDs to lifecycle callbacks */ private readonly lifecycleCallbacks; /** Map of portal IDs to bridged event handlers */ private readonly eventHandlers; /** Configuration */ private readonly config; /** Whether we're in SSR mode */ private readonly isSSR; /** Portal ID counter */ private portalIdCounter; /** * Creates a new PortalContextManager instance. */ private constructor(); /** * Gets the singleton instance. */ static getInstance(config?: Partial): PortalContextManager; /** * Resets the singleton instance. */ static resetInstance(): void; /** * Creates a new portal context. * * @param sourceElement - The element where the portal is invoked * @param options - Portal options * @returns Created PortalContext */ createPortalContext(sourceElement: Element, options?: PortalContextOptions): PortalContext; /** * Destroys a portal context. * * @param portalId - ID of the portal to destroy */ destroyPortalContext(portalId: string): void; /** * Destroys all portal contexts. */ destroyAll(): void; /** * Gets a portal context by ID. * * @param portalId - Portal ID * @returns PortalContext or undefined */ getPortalContext(portalId: string): PortalContext | undefined; /** * Gets the portal context for an element (if it's inside a portal). * * @param element - Element to check * @returns PortalContext or null */ getContextForElement(element: Element): PortalContext | null; /** * Gets the source context for a portal. * * @param portalId - Portal ID * @returns Source context snapshot or null */ getSourceContext(portalId: string): DOMContextSnapshot | null; /** * Gets the source element for a portal. * * @param portalId - Portal ID * @returns Source element or null */ getSourceElement(portalId: string): Element | null; /** * Checks if an element is inside a portal. * * @param element - Element to check * @returns Whether element is inside a portal */ isInPortal(element: Element): boolean; /** * Gets the portal hierarchy for an element. * * @param element - Element to check * @returns Array of portal contexts from innermost to outermost */ getPortalHierarchy(element: Element): PortalContext[]; /** * Registers a lifecycle callback for a portal. * * @param portalId - Portal ID * @param callback - Lifecycle callback * @returns Unregister function */ onLifecycle(portalId: string, callback: PortalLifecycleCallback): () => void; /** * Registers an event handler for bridged events. * * @param portalId - Portal ID * @param handler - Event handler * @returns Unregister function */ onBridgedEvent(portalId: string, handler: BridgedEventHandler): () => void; /** * Updates the source context snapshot for a portal. * * @param portalId - Portal ID */ refreshSourceContext(portalId: string): void; /** * Generates a unique portal ID. */ private generatePortalId; /** * Resolves the portal root element. */ private resolvePortalRoot; /** * Captures a snapshot of the current DOM context. */ private captureContextSnapshot; /** * Sets up event bridging for a portal. */ private setupEventBridging; /** * Cleans up event bridging for a portal. */ private cleanupEventBridging; /** * Notifies lifecycle callbacks. */ private notifyLifecycle; } /** * Gets the portal context manager instance. */ export declare function getPortalContextManager(): PortalContextManager; /** * Creates a portal context from a source element. * * @param sourceElement - Source element * @param options - Portal options * @returns Created portal context */ export declare function createPortalContext(sourceElement: Element, options?: PortalContextOptions): PortalContext; /** * Destroys a portal context. * * @param portalId - Portal ID to destroy */ export declare function destroyPortalContext(portalId: string): void; /** * Gets the portal context for an element. * * @param element - Element to check * @returns Portal context or null */ export declare function getPortalContextForElement(element: Element): PortalContext | null; /** * Checks if an element is inside a portal. * * @param element - Element to check * @returns Whether inside a portal */ export declare function isInPortal(element: Element): boolean; /** * Gets the complete portal hierarchy for an element. * * @param element - Element to check * @returns Array of portal contexts */ export declare function getPortalHierarchy(element: Element): PortalContext[];