Skip to content
LIBRARYData & collections

SwipeDeck ​

Included in RC.12

Available in Mavis 0.1.0-rc.12 for Vue + Vite, Nuxt and NativeScript. Keep the Mavis packages at the same version.

Web preview
Open example ↗
vue
<MSwipeDeck
  v-model="currentId"
  :items="cards"
  label="Discover"
  :canUndo="history.length > 0"
  @decide="history.push($event)"
  @undo="undo"
/>
PropTypeDefaultContract
itemsreadonly SwipeCard[]requiredUnique id, title, optional description/image.
modelValuestring | number | nullnullCurrent card identity. null or a missing id displays the empty state.
threshold / heightnumber90 / 340Swipe decision distance and card height in px/DIP.
animated / disabledbooleantrue / falseExit/return motion and input availability.
canUndobooleanfalseEnables the undo action; the application owns history.
acceptLabel / rejectLabel / undoLabel / emptyLabelstringEnglishLocalized actions and empty state.

Jolt owns dragging, spring return and the rotated exit. Buttons provide the same decisions without a gesture. decide({item, decision}) emits accept/reject; update:modelValue proposes the next item. Undo only emits undo; restore the previous id in your application. Rejected updates settle the same card. The item slot receives {item, index}; keep actions outside the drag surface.

With animated, the card leans toward the side it is dragged to and a stamp with the accept or reject label fades in as it nears the threshold, while the cards behind rise toward the front. After a decision the next card rises into place. When the application restores the previous card after undo, it comes back through the side it left. On NativeScript, decisions add a haptic. animated: false keeps the card flat and removes the stamps.

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)
import type { ChoiceValue, SwipeCard, SwipeDecision } from '@getcortexlabs/mavis-core'
const current = ref<ChoiceValue | null>('forest'),
  animated = ref(true)
const history = ref<{ item: SwipeCard; decision: SwipeDecision }[]>([])
const cards = computed(() => [
  {
    id: 'forest',
    title: t('A morning among trees.', 'Uma manhã entre árvores.'),
    description: t(
      'Leave the noise behind. Find a little more green.',
      'Deixe o ruído para trás. Encontre um pouco mais de verde.',
    ),
  },
  {
    id: 'coast',
    title: t('Follow the coastline.', 'Siga a costa.'),
    description: t(
      'Salt in the air. No plans until sunset.',
      'Sal no ar. Sem planos até o pôr do sol.',
    ),
  },
  {
    id: 'city',
    title: t('Get beautifully lost.', 'Perca-se por aí.'),
    description: t(
      'One unfamiliar street. A thousand possibilities.',
      'Uma rua desconhecida. Mil possibilidades.',
    ),
  },
])
function undo() {
  const last = history.value.pop()
  if (last) current.value = last.item.id
}
function restart() {
  current.value = 'forest'
  history.value = []
}
</script>
<template>
  <MColumn gap="lg"
    ><MText text="ELSEWHERE / DISCOVERY CLUB" variant="overline" tone="primary" /><MSwipeDeck
      id="discovery-deck"
      v-model="current"
      :items="cards"
      :label="t('Where to next?', 'Para onde agora?')"
      :animated="animated"
      :canUndo="history.length > 0"
      :acceptLabel="t('Keep', 'Guardar')"
      :rejectLabel="t('Pass', 'Passar')"
      :undoLabel="t('Undo', 'Desfazer')"
      :emptyLabel="t('Your next adventure is waiting.', 'Sua próxima aventura está esperando.')"
      @decide="history.push($event)"
      @undo="undo"
    >
      <template #item="{ item, index }"
        ><MColumn gap="lg"
          ><MText :text="`DESTINATION / 0${index + 1}`" variant="overline" tone="primary" /><MIcon
            :name="index === 0 ? 'trees' : index === 1 ? 'waves-horizontal' : 'compass'"
            :size="40" /><MText :text="item.title" variant="title" /><MText
            :text="item.description"
            tone="muted" /></MColumn
      ></template> </MSwipeDeck
    ><MText
      id="deck-decisions"
      :text="history.map((entry) => `${entry.item.id}:${entry.decision}`).join(', ') || '—'"
      variant="caption" /><MRow gap="md" wrap
      ><MSwitch
        v-model="animated"
        :label="t('Expressive motion', 'Movimento expressivo')" /><MButton
        :text="t('Start again', 'Recomeçar')"
        variant="ghost"
        @click="restart" /></MRow
  ></MColumn>
</template>

Desktop ​

On the desktop the card is dragged sideways with the pointer and decided past threshold; a secondary click on it offers the choices of the buttons as a menu. The card slides and fades out without rotating, and there is no haptic. See Desktop components.

Components for NativeScript and Web. Starting with Vue.