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