Skip to content
LIBRARYData & collectionsUpdated

SortableList ​

Added in RC.11.1

Included in Mavis 0.1.0-rc.11.1 for Vue + Vite, Nuxt and NativeScript iOS/Android. Keep core and renderers on the same version.

Web preview
Open example ↗
vue
<MSortableList v-model="tracks" itemKey="id" :itemLabel="(track) => track.title" label="Playlist">
  <template #default="{ item }"><MText :text="item.title" /></template>
</MSortableList>
PropTypeContract
modelValue / itemKeyreadonly T[] / keyof T or functionControlled array; unique stable keys, default id.
itemLabel(item: T) => stringAccessible row name; defaults to its key.
height / disablednumber / boolean360 / false. Bounded scrolling viewport.
handleLabel / moveUpLabel / moveDownLabel / emptyLabelstringLocalizable controls.
orientation / directionvertical / horizontal, ltr / rtlDefaults to a vertical list. Horizontal rows scroll and reorder in their declared logical order.
gridColumnsnumberDefaults to 1. On Web, choose 2 or more equal columns in a vertical scrollport. The native renderer supports the linear layout.
gestureMotionGestureExperimental. Receives the handle long press, for gesture relations.

Drag the dedicated handle. Jolt owns lifting, neighboring item placement, edge scrolling and cancellation; the app accepts the new order through v-model. Move buttons provide the same operation without dragging. reorder emits { key, beforeKey, fromIndex, toIndex } after a valid move. Unknown identities and unchanged moves do not commit. The input array is never mutated.

The default slot receives { item, index, itemKey }; stateful children stay keyed while moving. An empty slot replaces the empty message. Changing data or disabling cancels the current gesture. All rows are mounted: this is intended for editable playlists, priorities and steps, not a virtualized large dataset or cross-column Kanban.

A moved row lights up where it lands and fades back, whether it moved by drag, by the move buttons or by keyboard. Rows added after the first render glow once as they arrive, in sequence. The glow is a decorative layer behind the row's content and only changes opacity, so it also plays with reduced motion. On NativeScript a drop adds a light haptic.

Catalog recordings ​

The browser drags a track in right-to-left order. The native catalog switches orientation and moves a track using the accessible move command.

Chromium

iOS Simulator

Android emulator

Dragging across grid rows on Web ​

Set :gridColumns="2" to place mounted items in two equal columns. Jolt measures the cells in DOM order and emits the same keyed move as the linear layout. The move buttons and keyboard keep that order. Changing the layout during a drag cancels the move. Native grid reordering requires a CollectionView-backed source.

Chromium shows a held track moving to the second row and returning to the start by keyboard.

vue
<script setup lang="ts">
import { computed, ref } from 'vue'
import { useCatalogLocale } from '../locale'
const props = defineProps<{ lang?: 'en' | 'pt' }>()
const { locale } = useCatalogLocale(() => props.lang)
const pt = computed(() => (props.lang ?? locale.value) === 'pt')
const t = (en: string, br: string) => (pt.value ? br : en)
const shortTitle = 'Morning light'
const entries = [
  { id: 'morning', title: shortTitle, artist: 'Luna Park', time: '3:42' },
  { id: 'coast', title: 'Coast to coast', artist: 'The Other Side', time: '4:18' },
  { id: 'blue', title: 'Blue hour', artist: 'Nocturne', time: '2:56' },
  { id: 'home', title: 'The way home', artist: 'Sunday Club', time: '5:07' },
]
const extras = [
  { id: 'tide', title: 'Low tide', artist: 'Harbor Lights', time: '3:21' },
  { id: 'porch', title: 'Porch songs', artist: 'June & the Hours', time: '4:02' },
]
const tracks = ref([...entries])
const fullTitle = 'Morning light — live at the Pavilion'
function toggleTitle() {
  tracks.value = tracks.value.map((track) =>
    track.id === 'morning'
      ? { ...track, title: track.title === fullTitle ? shortTitle : fullTitle }
      : track,
  )
}
const disabled = ref(false)
const orientation = ref<'vertical' | 'horizontal'>('vertical')
const grid = ref(false)
const direction = ref<'ltr' | 'rtl'>('ltr')
const next = () => extras.find((extra) => !tracks.value.some((track) => track.id === extra.id))
function addTrack() {
  const track = next()
  if (track) tracks.value = [...tracks.value, track]
}
</script>
<template>
  <MCard padding="lg" gap="md">
    <MRow justify="between" align="center"
      ><MColumn gap="xs"
        ><MText text="SIDE A" variant="overline" tone="primary" /><MText
          :text="t('A slower kind of Sunday.', 'Um domingo sem pressa.')"
          variant="title" /></MColumn
      ><MIcon name="headphones" :size="32" tone="primary"
    /></MRow>
    <MText
      :text="t('Set the mood. Change the order.', 'Encontre o clima. Mude a ordem.')"
      tone="muted"
    />
    <MRow gap="sm" wrap>
      <MPlatform v-if="orientation === 'vertical'" only="web">
        <MButton
          id="sort-grid"
          variant="secondary"
          :text="grid ? t('List view', 'Mostrar lista') : t('Grid view', 'Mostrar grade')"
          @click="grid = !grid"
        />
      </MPlatform>
      <MButton
        id="sort-orientation"
        variant="secondary"
        :text="
          orientation === 'vertical'
            ? t('Show horizontal tracks', 'Mostrar faixas na horizontal')
            : t('Show vertical tracks', 'Mostrar faixas na vertical')
        "
        @click="orientation = orientation === 'vertical' ? 'horizontal' : 'vertical'"
      />
      <MButton
        v-if="orientation === 'horizontal'"
        id="sort-direction"
        variant="secondary"
        :text="
          direction === 'ltr'
            ? t('Right to left', 'Da direita para a esquerda')
            : t('Left to right', 'Da esquerda para a direita')
        "
        @click="direction = direction === 'ltr' ? 'rtl' : 'ltr'"
      />
    </MRow>
    <MSortableList
      v-model="tracks"
      :label="t('Sunday playlist', 'Playlist de domingo')"
      :itemLabel="(item) => item.title"
      :height="orientation === 'horizontal' ? 152 : 420"
      :gridColumns="grid && orientation === 'vertical' ? 2 : 1"
      :orientation="orientation"
      :direction="direction"
      :disabled="disabled"
      :handleLabel="t('Reorder', 'Reordenar')"
      :moveUpLabel="
        orientation === 'horizontal'
          ? t('Move earlier', 'Mover para antes')
          : t('Move up', 'Mover para cima')
      "
      :moveDownLabel="
        orientation === 'horizontal'
          ? t('Move later', 'Mover para depois')
          : t('Move down', 'Mover para baixo')
      "
    >
      <template #default="{ item, index }"
        ><MRow gap="md" align="center"
          ><MText
            :text="String(index + 1).padStart(2, '0')"
            variant="overline"
            tone="muted"
            :shrink="0" /><MColumn :grow="1" gap="xs"
            ><MText :id="`sort-track-${item.id}`" :text="item.title" weight="600" /><MText
              :text="`${item.artist} · ${item.time}`"
              variant="caption"
              tone="muted" /></MColumn></MRow
      ></template>
    </MSortableList>
    <MRow gap="sm" wrap
      ><MButton
        :text="t('Reverse playlist', 'Inverter playlist')"
        variant="secondary"
        @click="tracks = [...tracks].reverse()" /><MButton
        :text="t('Add a track', 'Adicionar faixa')"
        icon="plus"
        variant="secondary"
        :disabled="disabled || !next()"
        @click="addTrack" /><MButton
        id="sort-title"
        :text="t('Change track title', 'Alterar título da faixa')"
        variant="secondary"
        @click="toggleTitle" /><MButton
        :text="disabled ? t('Unlock', 'Desbloquear') : t('Lock order', 'Travar ordem')"
        variant="ghost"
        @click="disabled = !disabled"
    /></MRow>
    <MText
      id="sort-order"
      :text="tracks.map((item) => item.id).join(',')"
      variant="caption"
      tone="muted"
    />
  </MCard>
</template>

On Web, iOS and Android, removing a keyed item animates its exit. If a removed row held focus, the list tries to hand focus to a surviving neighbour. Reordering an existing item keeps its identity.

Desktop ​

The handle of each item is dragged with the pointer or takes the arrow keys, and the move buttons do the same; gridColumns lays the items out in a grid. direction: 'rtl' does not mirror the list on desktop yet, and gesture has no effect. See Desktop components.

Rows slide to their new positions after a reorder and fade when removed.

Components for NativeScript and Web. Starting with Vue.