--- 
title: Virtual Scroll
description: Efficient rendering of large lists by virtualizing viewport items
category: components
a1Full: true
group: Utilities

module: NxVirtualScrollModule
components:
  - name: NxVirtualViewportComponent
    selector: nx-virtual-viewport
    inputs:
      - name: itemHeight
        desc: "Estimated height in pixels for items before measurement (default: 36)"
        default: input<number>(36)
        type: "InputSignal<number>"
      - name: overscan
        desc: "Number of items to render above/below the visible area (default: 20)"
        default: input<number>(20)
        type: "InputSignal<number>"
    methods:
      - name: setForOf
        params:
          - name: forOf
            type: VirtualForOf
        desc: "Register the VirtualFor directive so viewport can call its methods. We register it instead of a contentChild query that it is available as early as possible and for edge cases when the directive would be wrapped in another component."
        returnType: void
      - name: scrollToIndex
        params:
          - name: index
            type: number
          - name: behavior
            type: ScrollBehavior
        desc: "Scroll to a specific index"
        returnType: void
      - name: scrollIntoView
        params:
          - name: index
            type: number
        desc: "Scroll an item into view using 'nearest' behavior (minimal scrolling)"
        returnType: void
      - name: getDomScrollTop
        params: 
        desc: "Get the current DOM scroll position directly (bypassing signal)"
        returnType: number
      - name: getDomViewportHeight
        params: 
        desc: "Get the current DOM viewport height directly (bypassing signal)"
        returnType: number
      - name: isAtBottom
        params: 
        desc: "Check if viewport is currently scrolled to the bottom (using DOM values)"
        returnType: boolean
      - name: updateSpacerHeight
        params:
          - name: height
            type: number
        desc: "Imperatively update the spacer height on the DOM. This bypasses the async signal → computed → template binding cycle, ensuring the browser has the correct scrollHeight before scrolling."
        returnType: void
      - name: setScrollTop
        params:
          - name: scrollTop
            type: number
        desc: "Set the scroll position directly"
        returnType: void
      - name: isScrollActive
        params: 
        desc: "Check if a smooth scroll animation is in progress"
        returnType: boolean
  - name: NxVirtualFor
    selector: [nxVirtualFor][nxVirtualForOf]
    desc: |
      Structural directive for rendering items in a virtual scroll viewport.
    inputs:
      - name: nxVirtualForOf
        desc: "The data source (array of items)"
        default: input.required<T[]>()
        type: "InputSignal<T[]>"
      - name: nxVirtualForTrackBy
        desc: "Property name or function to use for tracking items"
        default: input<TrackByAccessor<T>>('')
        type: "InputSignal<TrackByAccessor<T>>"
      - name: nxVirtualForGroupBy
        desc: "Returns the group of an item, or `null` for items without a group. The rendered items of each group (compared by `groupTrackBy`) are placed in one instance of `groupTemplate`. The items of a group must be next to each other, the data isn't reordered."
        default: input<((item: T) => G | null | undefined) | null>(null)
        type: "InputSignal<((item: T) => G | null | undefined) | null>"
      - name: nxVirtualForGroupTrackBy
        desc: "Property name or function that identifies a group. Defaults to the group itself. A group that is replaced by an object with the same key keeps its container and items."
        default: input<string | ((group: G) => unknown)>('')
        type: "InputSignal<string | ((group: G) => unknown)>"
      - name: nxVirtualForGroupTemplate
        desc: "Template that wraps the rendered items of each group, e.g. to give them a `role='group'`. Mark the place of the items with `nxVirtualGroupOutlet`. The wrapper must only contain the outlet and must not create a containing block, so the items are still positioned in the viewport."
        default: input<TemplateRef<NxVirtualForGroupContext<G>> | null>(null)
        type: "InputSignal<TemplateRef<NxVirtualForGroupContext<G>> | null>"
    methods:
      - name: getOffsetForIndex
        params:
          - name: index
            type: number
        desc: "Get the offset position for an item (called by viewport for scrollToIndex)"
        returnType: number
      - name: getHeightForIndex
        params:
          - name: index
            type: number
        desc: "Get the height for an item (called by viewport for scrollIntoView)"
        returnType: number
      - name: updateForScroll
        params:
          - name: scrollTop
            type: number
          - name: viewportHeight
            type: number
        desc: "Called by viewport when scroll position changes. Directly updates visible range and renders items."
        returnType: void
  - name: NxVirtualGroupOutlet
    selector: [nxVirtualGroupOutlet]
    desc: |
      Marks where the items of a group are rendered inside the group template of `nxVirtualFor`.

---
# Overview
The virtual scroll component provides efficient rendering of large lists by only rendering items visible in the viewport. The items can be of various and dynamic height.
