Skip to content

Commit b112568

Browse files
committed
fix(cdk/overlay): add DI token to opt out of popovers (angular#32306)
Adds a token that allows users to opt out of using popovers. (cherry picked from commit 0ca0c31)
1 parent cd14409 commit b112568

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

goldens/cdk/overlay/index.api.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,9 @@ export class Overlay {
378378
static ɵprov: i0.ɵɵInjectableDeclaration<Overlay>;
379379
}
380380

381+
// @public
382+
export const OVERLAY_DEFAULT_CONFIG: InjectionToken<OverlayDefaultConfig>;
383+
381384
// @public
382385
export class OverlayConfig {
383386
constructor(config?: OverlayConfig);
@@ -428,6 +431,12 @@ export class OverlayContainer implements OnDestroy {
428431
static ɵprov: i0.ɵɵInjectableDeclaration<OverlayContainer>;
429432
}
430433

434+
// @public
435+
export interface OverlayDefaultConfig {
436+
// (undocumented)
437+
usePopover?: boolean;
438+
}
439+
431440
// @public
432441
export class OverlayKeyboardDispatcher extends BaseOverlayDispatcher {
433442
add(overlayRef: OverlayRef): void;

src/cdk/overlay/overlay-directives.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
import {_getEventTarget} from '../platform';
3030
import {Subscription} from 'rxjs';
3131
import {takeWhile} from 'rxjs/operators';
32-
import {createOverlayRef} from './overlay';
32+
import {createOverlayRef, OVERLAY_DEFAULT_CONFIG} from './overlay';
3333
import {OverlayConfig} from './overlay-config';
3434
import {OverlayRef} from './overlay-ref';
3535
import {ConnectedOverlayPositionChange, ViewportMargin} from './position/connected-position';
@@ -253,7 +253,7 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
253253

254254
/** Whether the connected overlay should be rendered inside a popover element or the overlay container. */
255255
@Input({alias: 'cdkConnectedOverlayUsePopover'})
256-
usePopover: FlexibleOverlayPopoverLocation | null = 'global';
256+
usePopover: FlexibleOverlayPopoverLocation | null;
257257

258258
/** Whether the overlay should match the trigger's width. */
259259
@Input({alias: 'cdkConnectedOverlayMatchWidth', transform: booleanAttribute})
@@ -293,7 +293,9 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
293293
const templateRef = inject<TemplateRef<any>>(TemplateRef);
294294
const viewContainerRef = inject(ViewContainerRef);
295295
const defaultConfig = inject(CDK_CONNECTED_OVERLAY_DEFAULT_CONFIG, {optional: true});
296+
const globalConfig = inject(OVERLAY_DEFAULT_CONFIG, {optional: true});
296297

298+
this.usePopover = globalConfig?.usePopover === false ? null : 'global';
297299
this._templatePortal = new TemplatePortal(templateRef, viewContainerRef);
298300
this.scrollStrategy = this._scrollStrategyFactory();
299301

src/cdk/overlay/overlay.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
RendererFactory2,
2121
DOCUMENT,
2222
Renderer2,
23+
InjectionToken,
2324
} from '@angular/core';
2425
import {_IdGenerator} from '../a11y';
2526
import {_CdkPrivateStyleLoader} from '../private';
@@ -31,6 +32,16 @@ import {OverlayRef} from './overlay-ref';
3132
import {OverlayPositionBuilder} from './position/overlay-position-builder';
3233
import {ScrollStrategyOptions} from './scroll/index';
3334

35+
/** Object used to configure the default options for overlays. */
36+
export interface OverlayDefaultConfig {
37+
usePopover?: boolean;
38+
}
39+
40+
/** Injection token used to configure the default options for CDK overlays. */
41+
export const OVERLAY_DEFAULT_CONFIG = new InjectionToken<OverlayDefaultConfig>(
42+
'OVERLAY_DEFAULT_CONFIG',
43+
);
44+
3445
/**
3546
* Creates an overlay.
3647
* @param injector Injector to use when resolving the overlay's dependencies.
@@ -52,12 +63,15 @@ export function createOverlayRef(injector: Injector, config?: OverlayConfig): Ov
5263
injector.get(RendererFactory2).createRenderer(null, null);
5364

5465
const overlayConfig = new OverlayConfig(config);
66+
const defaultUsePopover =
67+
injector.get(OVERLAY_DEFAULT_CONFIG, null, {optional: true})?.usePopover ?? true;
68+
5569
overlayConfig.direction = overlayConfig.direction || directionality.value;
5670

5771
if (!('showPopover' in doc.body)) {
5872
overlayConfig.usePopover = false;
5973
} else {
60-
overlayConfig.usePopover = config?.usePopover ?? true;
74+
overlayConfig.usePopover = config?.usePopover ?? defaultUsePopover;
6175
}
6276

6377
const pane = doc.createElement('div');

src/cdk/overlay/public-api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export * from './position/connected-position';
1111
export * from './scroll/index';
1212
export * from './overlay-module';
1313
export * from './dispatchers/index';
14-
export {Overlay, createOverlayRef} from './overlay';
14+
export {Overlay, createOverlayRef, OverlayDefaultConfig, OVERLAY_DEFAULT_CONFIG} from './overlay';
1515
export {OverlayContainer} from './overlay-container';
1616
export {
1717
CdkOverlayOrigin,

0 commit comments

Comments
 (0)