PXD
v0.2.1-dev

Stick To Bottom

Keep a scroll container pinned to the bottom while content updates, until the user scrolls away. Scrolling back to the bottom restores auto-follow.

Default

Item 1
Item 2
Item 3
<script setup lang="ts">
import { ref } from 'vue'

const items = ref(['Item 1', 'Item 2', 'Item 3'])

function append() {
  items.value.push(`Item ${items.value.length + 1}`)
}
</script>

<template>
  <div class="flex flex-col gap-2">
    <PButton @click="append">Add item</PButton>

    <PStickToBottom class="h-40 rounded-lg border border-dashed" wrapper-class="p-2">
      <div v-for="item in items" :key="item" class="p-2 rounded bg-background-100">
        {{ item }}
      </div>
    </PStickToBottom>
  </div>
</template>

Disabled auto-follow

Log 1
<script setup lang="ts">
import { ref } from 'vue'

const items = ref(['Log 1'])

function append() {
  items.value.push(`Log ${items.value.length + 1}`)
}
</script>

<template>
  <div class="flex flex-col gap-2">
    <PButton @click="append">Append log</PButton>

    <PStickToBottom disabled class="h-36 rounded-lg border border-dashed" wrapper-class="p-2">
      <div v-for="item in items" :key="item" class="py-1">
        {{ item }}
      </div>
    </PStickToBottom>
  </div>
</template>

Fader

Set fader to show a gradient at the scrollable edges.

Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
Item 7
Item 8
Item 9
Item 10
Item 11
Item 12
Item 13
Item 14
Item 15
Item 16
Item 17
Item 18
Item 19
Item 20
<script setup lang="ts">
import { ref } from 'vue'

const items = ref(Array.from({ length: 20 }, (_, index) => `Item ${index + 1}`))
</script>

<template>
  <PStickToBottom fader class="h-40 rounded-lg border border-dashed" wrapper-class="p-2">
    <div v-for="item in items" :key="item" class="p-2 rounded bg-background-100">
      {{ item }}
    </div>
  </PStickToBottom>
</template>

Interaction keeps the position

A resize the user causes — expanding a collapse, selecting text — never pulls the view to the bottom: while the layout that interaction started is still moving, the viewport belongs to it. Pointerdowns inside the action slot are excluded, so controls there keep following.

Panel 1

Lorem ipsum dolor sit amet consectetur adipisicing elit. Amet, cupiditate magnam? Quae alias debitis beatae nesciunt veniam excepturi soluta magnam vitae.

Panel 2

Lorem ipsum dolor sit amet consectetur adipisicing elit. Amet, cupiditate magnam? Quae alias debitis beatae nesciunt veniam excepturi soluta magnam vitae.

Panel 3

Lorem ipsum dolor sit amet consectetur adipisicing elit. Amet, cupiditate magnam? Quae alias debitis beatae nesciunt veniam excepturi soluta magnam vitae.

Panel 4

Lorem ipsum dolor sit amet consectetur adipisicing elit. Amet, cupiditate magnam? Quae alias debitis beatae nesciunt veniam excepturi soluta magnam vitae.

Panel 5

Lorem ipsum dolor sit amet consectetur adipisicing elit. Amet, cupiditate magnam? Quae alias debitis beatae nesciunt veniam excepturi soluta magnam vitae.

Panel 6

Lorem ipsum dolor sit amet consectetur adipisicing elit. Amet, cupiditate magnam? Quae alias debitis beatae nesciunt veniam excepturi soluta magnam vitae.

Panel 7

Lorem ipsum dolor sit amet consectetur adipisicing elit. Amet, cupiditate magnam? Quae alias debitis beatae nesciunt veniam excepturi soluta magnam vitae.

Panel 8

Lorem ipsum dolor sit amet consectetur adipisicing elit. Amet, cupiditate magnam? Quae alias debitis beatae nesciunt veniam excepturi soluta magnam vitae.
<script setup lang="ts">
import ArrowUpIcon from '@gdsicon/vue/arrow-up'
</script>

<template>
  <PStickToBottom class="relative h-56 rounded-lg border border-dashed" wrapper-class="p-2">
    <PCollapseGroup multiple>
      <PCollapse v-for="i in 8" :key="i" :title="`Panel ${i}`">
        <div class="mb-4">
          Lorem ipsum dolor sit amet consectetur adipisicing elit. Amet, cupiditate magnam? Quae
          alias debitis beatae nesciunt veniam excepturi soluta magnam vitae.
        </div>
      </PCollapse>
    </PCollapseGroup>

    <template #action="{ isAtBottom, forceStickToBottom }">
      <PButton
        v-if="!isAtBottom"
        class="mx-auto flex"
        size="sm" shape="rounded" icon
        @click="forceStickToBottom"
      >
        <ArrowUpIcon class="rotate-180" />
      </PButton>
    </template>
  </PStickToBottom>
</template>

Scroll to bottom action

Use the action slot with PBacktop to jump back to the bottom after the user scrolls away. The slot exposes isAtBottom, scrollToBottom, and forceStickToBottom.

Message 1
Message 2
Message 3
Message 4
Message 5
Message 6
Message 7
Message 8
Message 9
Message 10
Message 11
Message 12
<script setup lang="ts">
import { ref } from 'vue'
import ArrowUpIcon from '@gdsicon/vue/arrow-up'

const items = ref(Array.from({ length: 12 }, (_, i) => `Message ${i + 1}`))

function append() {
  items.value.push(`Message ${items.value.length + 1}`)
}
</script>

<template>
  <div class="flex flex-col gap-2">
    <PButton @click="append">Send message</PButton>

    <PStickToBottom class="relative h-40 rounded-lg border border-dashed" wrapper-class="p-2">
      <div v-for="item in items" :key="item" class="p-2 rounded bg-background-100">
        {{ item }}
      </div>

      <template #action="{ isAtBottom, forceStickToBottom }">
        <PButton
          v-if="!isAtBottom"
          class="mx-auto flex"
          size="sm" shape="rounded" icon
          @click="forceStickToBottom"
        >
          <ArrowUpIcon class="rotate-180" />
        </PButton>
      </template>
    </PStickToBottom>
  </div>
</template>

Props

NameTypeDefaultDescription
thresholdnumber16Distance from bottom considered "at bottom"
faderbooleanfalseShow gradient faders at the scrollable edges, hidden within threshold of an edge
fader-sizenumber-Fader gradient length in px, defaulting to 24
fader-colorstring-Fader gradient color, any CSS color value
fader-direction'horizontal' | 'vertical' | 'both'-Edges to fade: horizontal, vertical or both
disabledbooleanfalseStop auto-scrolling while the container is at the bottom
content-classComponentClass-Extra class on the inner content wrapper
wrapper-classComponentClass-Extra class on the scroll container; the wrapper receives class and attributes

Events

NameTypeDescription
change(isAtBottom: boolean, details: ChangeDetails) => voidEmitted when the at-bottom state changes.

Slots

NameDescription
defaultScrollable content.
actionOverlay action area rendered outside the scroll container. Slot props: { container, isAtBottom, scrollToBottom, forceStickToBottom }; container is the scroll element.

Methods

NameTypeDescription
containerRefHTMLElementScroll container element exposed for external scroll APIs.
contentRefHTMLElementInner content wrapper element exposed for external measurement.
isAtBottombooleanWhether the container is currently within the bottom threshold.
scrollToBottom() => voidScroll instantly to the bottom, leaving horizontal scroll untouched.
forceStickToBottom() => voidScroll to the bottom and re-enable auto-stick.
stickIfNeeded() => voidScroll to the bottom only while following and disabled is not true.
update() => voidRe-measure whether the container is at the bottom.

Source

Source