skip to content

With the Angular CDK overlay, how do the noop, reposition, block and close scroll strategies differ, and which suits a cell editor inside a scrolling spreadsheet grid?

level: seniorimportance: should knowfreq 33%

answer

  1. what happens when the page moves
  2. noop is the config default
  3. block only freezes the document
  4. close has a pixel threshold
  5. which scroll events are heard

basics

~20 s

Noop ignores scrolling, reposition re-runs the position strategy so the overlay follows its anchor, block freezes document scrolling, and close detaches the overlay. A grid's cell editor usually wants reposition or close; block does not stop the grid's own scrolling.

solid answer

~40 s

A scroll strategy decides what a CDK overlay does when something scrolls while it is open. `OverlayConfig` defaults to noop, so a code-built editor stays at its viewport coordinates while its cell scrolls away. `createRepositionScrollStrategy(injector, {scrollThrottle, autoClose})` re-applies the position on each scroll; `autoClose` detaches once the overlay has scrolled completely out of the viewport. `createBlockScrollStrategy(injector)` adds `cdk-global-scrollblock` to `<html>` and pins its offset, but only when the document overflows the viewport, and it never stops an inner `overflow: auto` container. `createCloseScrollStrategy(injector, {threshold})` detaches on scroll, ignoring scrolls inside the overlay. For the cell editor, reposition keeps it on the cell and close suits commit-on-scroll. Scroll events come from the document plus containers registered with the CDK scroll dispatcher, so mark the grid's scroll container `cdkScrollable`.

code

ts · 12 lines
ts
import {Injector} from '@angular/core';
import {
  createOverlayRef, createCloseScrollStrategy, FlexibleConnectedPositionStrategy,
} from '@angular/cdk/overlay';

export function openCommitOnScrollEditor(injector: Injector, position: FlexibleConnectedPositionStrategy) {
  // Repositions for the first 40px of page scroll, then detaches the editor.
  return createOverlayRef(injector, {
    positionStrategy: position,
    scrollStrategy: createCloseScrollStrategy(injector, {threshold: 40}),
  });
}

go deeper

for a junior

Recall the four names and the one-line behaviour of each: ignore, follow, freeze the page, close.

for a middle

Explain the defaults, noop for a config and reposition for the directive, and what each strategy's options do, including the close threshold and reposition's autoClose.

for a senior

Diagnose the grid cases: block not stopping the container, strategies deaf to an unregistered scroll container, autoClose ignoring the grid's viewport, and the cost of repositioning on every scroll.

for a principal

Set one convention per overlay kind across the product, such as follow for anchored editors and block for modal confirmations, and encode it in shared factories rather than per-call choices.

## What a scroll strategy is A CDK overlay's pane is positioned against the **viewport**, not inside the element it is anchored to. When the user scrolls, the anchor moves and the pane does not, unless something reacts. That something is the overlay's **scroll strategy**, set through `OverlayConfig.scrollStrategy` or `cdkConnectedOverlayScrollStrategy`. Each strategy has a small life cycle: `attach` when the overlay is created, `enable` when content is attached, `disable` when it is detached or disposed. ## The four built-in strategies | Strategy | Factory in v22 | Behaviour | Options | |---|---|---|---| | noop | `createNoopScrollStrategy()` | does nothing; default for `OverlayConfig` | none | | reposition | `createRepositionScrollStrategy(injector, config?)` | re-applies the position strategy on scroll; default for `cdkConnectedOverlay` | `scrollThrottle` (ms), `autoClose` | | block | `createBlockScrollStrategy(injector)` | freezes page-level scrolling while open | none | | close | `createCloseScrollStrategy(injector, config?)` | detaches the overlay on scroll | `threshold` (px) | The `Overlay` service exposes the same set as `overlay.scrollStrategies.noop()`, `.reposition()`, `.block()` and `.close()`. ## The details that bite **Noop is the silent default.** A code-built overlay with no `scrollStrategy` keeps its coordinates, so the editor floats over the wrong row after a scroll. `cdkConnectedOverlay` avoids this because it defaults to reposition. **Reposition costs work on every scroll.** Each scroll event re-runs the position strategy; the CDK guide notes the performance impact. `scrollThrottle` limits how often it runs. `autoClose` detaches the overlay once it has scrolled entirely out of view, but the source checks only the viewport, not ancestor scroll containers, so an editor whose cell scrolls out of the grid's own viewport can stay open while still on screen. **Block is page-level only.** Enabling it records the scroll position, sets negative `top`/`left` on `<html>` and adds the `cdk-global-scrollblock` class. It does this only if the document is taller or wider than the viewport, and only once even with several blocking overlays. It does nothing to an element with its own `overflow: auto`, so the grid keeps scrolling under a "blocking" editor. **Close ignores the overlay's own scrolling.** Scroll events from containers inside the pane are filtered out, so scrolling a long list in the filter panel does not close it. With a `threshold` above 1, it repositions until the page has scrolled more than that many pixels from where it started, then detaches. The distance is read from the page's vertical scroll position, so scrolling that happens only inside the grid's own container never reaches the threshold and only repositions. ## Which scroll events are heard Reposition and close listen to the CDK `ScrollDispatcher`. It listens for `scroll` on the document, and scroll events do not bubble from inner elements, so a container's scrolling is heard only if the container is registered with the dispatcher, which the `cdkScrollable` directive does. A grid that scrolls inside its own container must carry it, or neither strategy reacts when only the grid scrolls. ## Choosing for the spreadsheet grid 1. **Cell editor:** reposition keeps the editor glued to its cell while the user scrolls to check other rows. Many spreadsheets instead commit and close on scroll, which is close. Either way, not noop. 2. **Column-filter panel:** reposition with `autoClose: true` follows the header and drops the panel when it leaves the viewport. 3. **Modal confirmation such as "delete 500 rows?":** block, together with a backdrop, so the page behind cannot move. To also hide the editor when its cell scrolls out of the grid's viewport, give the position strategy the grid's container through `withScrollableContainers([...])`; its `positionChanges` then report `isOriginClipped` and `isOriginOutsideView` in `scrollableViewProperties`, and you can detach in response. ## A quick decision checklist - Is the overlay anchored to something that can scroll? Then noop is almost never right. - Should the user keep scrolling while it is open? Reposition or close; otherwise block. - Does the anchor live inside its own scroll container? Register that container with the scroll dispatcher. - Is repositioning visibly janky on long pages? Add `scrollThrottle`, or switch to close. ## Rules for strategy instances - A strategy instance belongs to one overlay; attaching it to a second throws `Scroll strategy has already been attached.` in development mode. - `overlayRef.updateScrollStrategy(newStrategy)` swaps the strategy on a live overlay. - A custom strategy implements the `ScrollStrategy` interface (`attach`, `enable`, `disable`) and usually injects `ScrollDispatcher`.

  • The filter panel contains its own scrolling list of values. Will the close strategy shut the panel when the user scrolls that list?
    No. The close strategy filters out scroll events whose scrollable lives inside the overlay element, so scrolling within the panel is ignored. Only scrolling outside the overlay, on the page or on a registered container, detaches it.
  • Two editors are opened with the same block strategy instance. What happens?
    Attaching the instance to the second overlay throws `Scroll strategy has already been attached.` in development mode, because a strategy keeps a reference to one overlay. Create a fresh strategy per overlay, for example inside the function that opens it.

  • Overlays follow their anchor on scroll by default when built from code.
  • The block strategy stops every scrollable element on the page.
  • The close strategy closes the panel when its own inner list scrolls.
  • Reposition with autoClose checks every ancestor scroll container.
  • One scroll strategy instance can be shared across all overlays.

Assumes Angular CDK 22; the createRepositionScrollStrategy, createBlockScrollStrategy and createCloseScrollStrategy functions arrived in 20, and overlay.scrollStrategies on the Overlay service still offers the same four.