@@ -18,6 +18,7 @@ import {
1818 EnvironmentInjector ,
1919 inject ,
2020 RendererFactory2 ,
21+ Renderer2 ,
2122} from '@angular/core' ;
2223import { _IdGenerator } from '../a11y' ;
2324import { _CdkPrivateStyleLoader } from '../private' ;
@@ -29,6 +30,56 @@ import {OverlayRef} from './overlay-ref';
2930import { OverlayPositionBuilder } from './position/overlay-position-builder' ;
3031import { ScrollStrategyOptions } from './scroll/index' ;
3132
33+ /**
34+ * Creates an overlay.
35+ * @param injector Injector to use when resolving the overlay's dependencies.
36+ * @param config Configuration applied to the overlay.
37+ * @returns Reference to the created overlay.
38+ */
39+ export function createOverlayRef ( injector : Injector , config ?: OverlayConfig ) : OverlayRef {
40+ // This is done in the overlay container as well, but we have it here
41+ // since it's common to mock out the overlay container in tests.
42+ injector . get ( _CdkPrivateStyleLoader ) . load ( _CdkOverlayStyleLoader ) ;
43+
44+ const overlayContainer = injector . get ( OverlayContainer ) ;
45+ const doc = injector . get ( DOCUMENT ) ;
46+ const idGenerator = injector . get ( _IdGenerator ) ;
47+ const appRef = injector . get ( ApplicationRef ) ;
48+ const directionality = injector . get ( Directionality ) ;
49+
50+ const host = doc . createElement ( 'div' ) ;
51+ const pane = doc . createElement ( 'div' ) ;
52+
53+ pane . id = idGenerator . getId ( 'cdk-overlay-' ) ;
54+ pane . classList . add ( 'cdk-overlay-pane' ) ;
55+ host . appendChild ( pane ) ;
56+ overlayContainer . getContainerElement ( ) . appendChild ( host ) ;
57+
58+ const portalOutlet = new DomPortalOutlet ( pane , appRef , injector ) ;
59+ const overlayConfig = new OverlayConfig ( config ) ;
60+ const renderer =
61+ injector . get ( Renderer2 , null , { optional : true } ) ||
62+ injector . get ( RendererFactory2 ) . createRenderer ( null , null ) ;
63+
64+ overlayConfig . direction = overlayConfig . direction || directionality . value ;
65+
66+ return new OverlayRef (
67+ portalOutlet ,
68+ host ,
69+ pane ,
70+ overlayConfig ,
71+ injector . get ( NgZone ) ,
72+ injector . get ( OverlayKeyboardDispatcher ) ,
73+ doc ,
74+ injector . get ( Location ) ,
75+ injector . get ( OverlayOutsideClickDispatcher ) ,
76+ config ?. disableAnimations ??
77+ injector . get ( ANIMATION_MODULE_TYPE , null , { optional : true } ) === 'NoopAnimations' ,
78+ injector . get ( EnvironmentInjector ) ,
79+ renderer ,
80+ ) ;
81+ }
82+
3283/**
3384 * Service to create Overlays. Overlays are dynamically added pieces of floating UI, meant to be
3485 * used as a low-level building block for other components. Dialogs, tooltips, menus,
@@ -40,21 +91,8 @@ import {ScrollStrategyOptions} from './scroll/index';
4091@Injectable ( { providedIn : 'root' } )
4192export class Overlay {
4293 scrollStrategies = inject ( ScrollStrategyOptions ) ;
43- private _overlayContainer = inject ( OverlayContainer ) ;
4494 private _positionBuilder = inject ( OverlayPositionBuilder ) ;
45- private _keyboardDispatcher = inject ( OverlayKeyboardDispatcher ) ;
4695 private _injector = inject ( Injector ) ;
47- private _ngZone = inject ( NgZone ) ;
48- private _document = inject ( DOCUMENT ) ;
49- private _directionality = inject ( Directionality ) ;
50- private _location = inject ( Location ) ;
51- private _outsideClickDispatcher = inject ( OverlayOutsideClickDispatcher ) ;
52- private _animationsModuleType = inject ( ANIMATION_MODULE_TYPE , { optional : true } ) ;
53- private _idGenerator = inject ( _IdGenerator ) ;
54- private _renderer = inject ( RendererFactory2 ) . createRenderer ( null , null ) ;
55-
56- private _appRef : ApplicationRef ;
57- private _styleLoader = inject ( _CdkPrivateStyleLoader ) ;
5896
5997 constructor ( ...args : unknown [ ] ) ;
6098 constructor ( ) { }
@@ -65,31 +103,7 @@ export class Overlay {
65103 * @returns Reference to the created overlay.
66104 */
67105 create ( config ?: OverlayConfig ) : OverlayRef {
68- // This is done in the overlay container as well, but we have it here
69- // since it's common to mock out the overlay container in tests.
70- this . _styleLoader . load ( _CdkOverlayStyleLoader ) ;
71-
72- const host = this . _createHostElement ( ) ;
73- const pane = this . _createPaneElement ( host ) ;
74- const portalOutlet = this . _createPortalOutlet ( pane ) ;
75- const overlayConfig = new OverlayConfig ( config ) ;
76-
77- overlayConfig . direction = overlayConfig . direction || this . _directionality . value ;
78-
79- return new OverlayRef (
80- portalOutlet ,
81- host ,
82- pane ,
83- overlayConfig ,
84- this . _ngZone ,
85- this . _keyboardDispatcher ,
86- this . _document ,
87- this . _location ,
88- this . _outsideClickDispatcher ,
89- config ?. disableAnimations ?? this . _animationsModuleType === 'NoopAnimations' ,
90- this . _injector . get ( EnvironmentInjector ) ,
91- this . _renderer ,
92- ) ;
106+ return createOverlayRef ( this . _injector , config ) ;
93107 }
94108
95109 /**
@@ -100,44 +114,4 @@ export class Overlay {
100114 position ( ) : OverlayPositionBuilder {
101115 return this . _positionBuilder ;
102116 }
103-
104- /**
105- * Creates the DOM element for an overlay and appends it to the overlay container.
106- * @returns Newly-created pane element
107- */
108- private _createPaneElement ( host : HTMLElement ) : HTMLElement {
109- const pane = this . _document . createElement ( 'div' ) ;
110-
111- pane . id = this . _idGenerator . getId ( 'cdk-overlay-' ) ;
112- pane . classList . add ( 'cdk-overlay-pane' ) ;
113- host . appendChild ( pane ) ;
114-
115- return pane ;
116- }
117-
118- /**
119- * Creates the host element that wraps around an overlay
120- * and can be used for advanced positioning.
121- * @returns Newly-create host element.
122- */
123- private _createHostElement ( ) : HTMLElement {
124- const host = this . _document . createElement ( 'div' ) ;
125- this . _overlayContainer . getContainerElement ( ) . appendChild ( host ) ;
126- return host ;
127- }
128-
129- /**
130- * Create a DomPortalOutlet into which the overlay content can be loaded.
131- * @param pane The DOM element to turn into a portal outlet.
132- * @returns A portal outlet for the given DOM element.
133- */
134- private _createPortalOutlet ( pane : HTMLElement ) : DomPortalOutlet {
135- // We have to resolve the ApplicationRef later in order to allow people
136- // to use overlay-based providers during app initialization.
137- if ( ! this . _appRef ) {
138- this . _appRef = this . _injector . get < ApplicationRef > ( ApplicationRef ) ;
139- }
140-
141- return new DomPortalOutlet ( pane , this . _appRef , this . _injector ) ;
142- }
143117}
0 commit comments