(injector: Injector, config?: OverlayConfig)
| 50 | * @returns Reference to the created overlay. |
| 51 | */ |
| 52 | export function createOverlayRef(injector: Injector, config?: OverlayConfig): OverlayRef { |
| 53 | // This is done in the overlay container as well, but we have it here |
| 54 | // since it's common to mock out the overlay container in tests. |
| 55 | injector.get(_CdkPrivateStyleLoader).load(_CdkOverlayStyleLoader); |
| 56 | |
| 57 | const overlayContainer = injector.get(OverlayContainer); |
| 58 | const doc = injector.get(DOCUMENT); |
| 59 | const idGenerator = injector.get(_IdGenerator); |
| 60 | const appRef = injector.get(ApplicationRef); |
| 61 | const directionality = injector.get(Directionality); |
| 62 | const renderer = |
| 63 | injector.get(Renderer2, null, {optional: true}) || |
| 64 | injector.get(RendererFactory2).createRenderer(null, null); |
| 65 | |
| 66 | const overlayConfig = new OverlayConfig(config); |
| 67 | const defaultUsePopover = |
| 68 | injector.get(OVERLAY_DEFAULT_CONFIG, null, {optional: true})?.usePopover ?? true; |
| 69 | |
| 70 | overlayConfig.direction = overlayConfig.direction || directionality.value; |
| 71 | |
| 72 | if (!('showPopover' in doc.body)) { |
| 73 | overlayConfig.usePopover = false; |
| 74 | } else { |
| 75 | overlayConfig.usePopover = config?.usePopover ?? defaultUsePopover; |
| 76 | } |
| 77 | |
| 78 | const pane = doc.createElement('div'); |
| 79 | const host = doc.createElement('div'); |
| 80 | pane.id = idGenerator.getId('cdk-overlay-'); |
| 81 | pane.classList.add('cdk-overlay-pane'); |
| 82 | host.appendChild(pane); |
| 83 | |
| 84 | if (overlayConfig.usePopover) { |
| 85 | host.setAttribute('popover', 'manual'); |
| 86 | host.classList.add('cdk-overlay-popover'); |
| 87 | } |
| 88 | |
| 89 | const customInsertionPoint = overlayConfig.usePopover |
| 90 | ? overlayConfig.positionStrategy?.getPopoverInsertionPoint?.() |
| 91 | : null; |
| 92 | |
| 93 | if (isElement(customInsertionPoint)) { |
| 94 | customInsertionPoint.after(host); |
| 95 | } else if (customInsertionPoint?.type === 'parent') { |
| 96 | customInsertionPoint.element.appendChild(host); |
| 97 | } else { |
| 98 | overlayContainer.getContainerElement().appendChild(host); |
| 99 | } |
| 100 | |
| 101 | return new OverlayRef( |
| 102 | new DomPortalOutlet(pane, appRef, injector), |
| 103 | host, |
| 104 | pane, |
| 105 | overlayConfig, |
| 106 | injector.get(NgZone), |
| 107 | injector.get(OverlayKeyboardDispatcher), |
| 108 | doc, |
| 109 | injector.get(Location), |
searching dependent graphs…