# Overlays in scroll containers

All Element components that open CDK overlays (like typeahead, popovers, ...) automatically configure the overlays to reposition on scroll.

**Important:** If Element components with overlays are placed within a container that can scroll, apply the `cdkScrollable` directive to it. By default, the CDK only listens to scroll events on the document.

## Customizing the scroll strategy

By default, all overlays by Element components will reposition themselves on scroll. You can change this globally by overriding the default configuration of CDK Overlays using the [OVERLAY\_DEFAULT\_CONFIG](<https://material.angular.dev/cdk/overlay/api#OVERLAY_DEFAULT_CONFIG>) injection token.

In addition, you can set the input of the respective Element component.

## Available scroll strategies

Read the [related documentation of the CDK](<https://material.angular.dev/cdk/overlay/overview#scroll-strategies>) to find a list of available strategies.

## Code

Add `cdkScrollable` to the element that actually scrolls. The directive registers the element with Angular CDK's `ScrollDispatcher`, allowing overlay scroll strategies to react to its scroll events.

To create a custom strategy, use either the `Overlay` or the `ScrollStrategyOptions` from the CDK. Always create a new instance for each overlay.

```
import { Overlay, ScrollStrategyOptions } from '@angular/cdk/overlay';
import { inject } from '@angular/core';

class MyComponent {
  // Using the Overlay
  scrollStrategy = inject(Overlay).scrollStrategies.reposition();
  // Using the ScrollStrategyOptions
  scrollStrategy = inject(ScrollStrategyOptions).reposition();
}
```

The following example uses the close strategy for a popover inside a non-root scrolling container. The typeahead uses the default reposition strategy.

#### Example

Component:

```typescript
/**
 * Copyright (c) Siemens 2016 - 2026
 * SPDX-License-Identifier: MIT
 */
import { Overlay } from '@angular/cdk/overlay';
import { CdkScrollable } from '@angular/cdk/scrolling';
import { Component, inject } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { SiFormItemComponent } from '@siemens/element-ng/form';
import { SiPopoverDirective } from '@siemens/element-ng/popover';
import { SiTypeaheadDirective } from '@siemens/element-ng/typeahead';

@Component({
  selector: 'app-sample',
  imports: [
    CdkScrollable,
    FormsModule,
    SiPopoverDirective,
    SiTypeaheadDirective,
    SiFormItemComponent
  ],
  templateUrl: './overlay-scroll-strategy.html'
})
export class SampleComponent {
  private readonly overlay = inject(Overlay);

  readonly popoverScrollStrategy = this.overlay.scrollStrategies.close();

  readonly countries = ['Canada', 'France', 'Germany', 'India', 'Japan', 'United Kingdom'];
  selectedCountry = '';
}
```

Template:

```html
<div class="p-6">
  <p class="mb-4">
    Open each overlay, then scroll this container. The typeahead dropdown repositions, while the
    popover closes.
  </p>

  <div
    class="border rounded-2 overflow-auto p-6"
    style="block-size: 360px"
    cdkScrollable
    tabindex="0"
    aria-label="Scrollable overlay examples"
  >
    <p class="d-flex align-items-center">This container is scrollable. </p>

    <div class="d-flex flex-column align-items-start gap-6">
      <si-form-item label="Country (overlay will reposition)">
        <!-- This is using the default strategy: reposition -->
        <input
          id="country"
          type="text"
          class="form-control"
          placeholder="Select a country"
          [siTypeahead]="countries"
          [typeaheadMinLength]="0"
          [(ngModel)]="selectedCountry"
        />
      </si-form-item>

      <button
        type="button"
        class="btn btn-secondary"
        siPopoverTitle="Nested scrolling"
        siPopover="This popover closes when its container scrolls."
        [siPopoverScrollStrategy]="popoverScrollStrategy"
      >
        Show popover
      </button>
    </div>

    <div class="d-flex align-items-end text-secondary" style="block-size: 460px">
      End of scroll container
    </div>
  </div>
</div>
```
