Components

Virtual list

Ten thousand rows that scroll as thirty, since only the rows in view are drawn.

Previews are drawn in the theme you made.

Edit it
scheme
direction

Ten thousand rows #

    import { Bundle } from 'foldkit-bundle'
    import { Bundles, Recipes, VirtualList, VirtualListSlots } from 'foldkit-mixins-ui'
    import { example } from '../../gallery/example.js'
    
    const FILES = Array.from(
      { length: 10_000 },
      (_, index) => `report-${String(index + 1).padStart(5, '0')}.pdf`,
    )
    
    // Ten thousand rows, of which only those in view are drawn, with spacers for
    // the rest. The scroll state is the Bundle's Model (upstream's); the rows are
    // the application's, passed to the view.
    const App = Bundle.compose({}).pipe(
      Bundle.withChild('files', Bundles.VirtualList, {
        args: { id: 'basic-files', rowHeightPx: 36 },
        onOut: Bundle.ignore,
      }),
    )
    
    export default example({
      title: 'Ten thousand rows',
      slots: VirtualListSlots,
      recipe: Recipes.VirtualList,
      state: App.placements.runtime({ Model: App.Model, initial: {} }),
      view: ({ style, model, onMessage, h }) =>
        h.div(
          [h.Style({ inlineSize: 'min(24rem, 100%)' })],
          [
            VirtualList.view(
              {
                model: model.files,
                items: FILES,
                itemToKey: file => file,
                itemToView: file => h.span([], [file]),
                onMessage: message => onMessage(App.Message.GotFilesMessage({ message })),
                style,
              },
              h,
            ),
          ],
        ),
    })

    In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

    Rows of their own heights #

      import { Bundle } from 'foldkit-bundle'
      import { Bundles, Recipes, VirtualList, VirtualListSlots } from 'foldkit-mixins-ui'
      import { example } from '../../gallery/example.js'
      
      const NOTES = Array.from({ length: 2_000 }, (_, index) => ({
        id: `note-${index}`,
        text: 'A note that runs on. '.repeat((index % 5) + 1).trim(),
      }))
      
      // Rows of their own heights: `dynamicRowHeights` measures each row as it is
      // drawn, starting from `rowHeightPx` as the estimate. `VirtualList.overlayBars`
      // draws a thin bar over the list in place of the browser's.
      const App = Bundle.compose({}).pipe(
        Bundle.withChild('notes', Bundles.VirtualList, {
          args: { id: 'heights-notes', rowHeightPx: 48 },
          onOut: Bundle.ignore,
        }),
      )
      
      export default example({
        title: 'Rows of their own heights',
        slots: VirtualListSlots,
        recipe: Recipes.VirtualList,
        state: App.placements.runtime({ Model: App.Model, initial: {} }),
        view: ({ style, model, onMessage, h }) =>
          h.div(
            [h.Style({ inlineSize: 'min(24rem, 100%)' })],
            [
              VirtualList.view(
                {
                  model: model.notes,
                  items: NOTES,
                  itemToKey: note => note.id,
                  itemToView: note => h.p([], [note.text]),
                  dynamicRowHeights: true,
                  onMessage: message => onMessage(App.Message.GotNotesMessage({ message })),
                  style,
                  mixins: VirtualList.overlayBars,
                },
                h,
              ),
            ],
          ),
      })

      In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

      Load more near the end #

        50 rows loaded

        import { Schema } from 'effect'
        import { Bundle } from 'foldkit-bundle'
        import { Bundles, Recipes, VirtualList, VirtualListSlots } from 'foldkit-mixins-ui'
        import { example } from '../../gallery/example.js'
        
        const PAGE = 50
        
        // An endless feed: with `nearEndPx`, the Bundle reports `NearEnd` when a
        // scroll brings the end of the list within 200 pixels of the view, and the
        // application adds the next page there. Here the page is made on the spot; a
        // real one would ask a server and add the rows when they come.
        const App = Bundle.compose({ loaded: Schema.Number }).pipe(
          Bundle.withChild('feed', Bundles.VirtualList, {
            args: { id: 'infinite-feed', rowHeightPx: 36, nearEndPx: 200 },
            onOut: () => model => ({ model: { ...model, loaded: model.loaded + PAGE } }),
          }),
        )
        
        // One array per count, so a render that adds no rows hands the list the same one.
        const drawn = new Map<number, ReadonlyArray<string>>()
        const rows = (count: number): ReadonlyArray<string> => {
          const known = drawn.get(count)
          if (known !== undefined) return known
          const made = Array.from({ length: count }, (_, index) => `Entry ${index + 1}`)
          drawn.set(count, made)
          return made
        }
        
        export default example({
          title: 'Load more near the end',
          slots: VirtualListSlots,
          recipe: Recipes.VirtualList,
          state: App.placements.runtime({ Model: App.Model, initial: { loaded: PAGE } }),
          view: ({ style, model, onMessage, h }) =>
            h.div(
              [h.Style({ inlineSize: 'min(24rem, 100%)' })],
              [
                VirtualList.view(
                  {
                    model: model.feed,
                    items: rows(model.loaded),
                    itemToKey: row => row,
                    itemToView: row => h.span([], [row]),
                    onMessage: message => onMessage(App.Message.GotFeedMessage({ message })),
                    style,
                  },
                  h,
                ),
                h.p([], [`${model.loaded} rows loaded`]),
              ],
            ),
        })

        In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

        Usage

        Each example above is a module of its own, shown whole and run as shown, over the theme module Create a theme writes. The page hands it the style picked above.

        The recipe ↗This code ↗