# useSnapPaging

## Usage

Gives a [Carousel](/en/components/carousel) iOS paging on native snap's stops: pass the value as `:has-snap="paging"`. Under a finger and on a trackpad swipe, the row follows the gesture, then a flick moves one view, or one item with `{ pages: 'item' }`, and the row rests on an item's start edge. However fast the flick, the row moves at most one page from where the gesture began. A touch catches a page turn in flight, and a still touch then rests the row on the nearest page.

A view page lands on the item start edge nearest one viewport on, and always at least one item on. An item whose stop sits within half a step of the row's end belongs to the end page, so every input, native snap included, rests where the row ends.

A mouse wheel and a mouse drag keep native scrolling and snapping. The edge buttons, the arrow keys, Shift plus the wheel, and the template ref's `scrollNext`, `scrollPrev` and `scrollTo` move the row through the paging, so a touch can catch those moves too. While the paging holds the row, the edge buttons and fades follow the row as it is shown, in the same frame. With reduced motion, a page turn jumps straight to its page.

```ts
import { useSnapPaging } from '@astryx-vue/core'
```

```vue
<script setup lang="ts">
import { useSnapPaging } from '@astryx-vue/core'
import { Carousel } from '@astryx-vue/core/Carousel'

const paging = useSnapPaging({ pages: 'item' })
</script>

<template>
  <Carousel :has-snap="paging" label="Photos">
    <!-- cards -->
  </Carousel>
</template>
```

Only a carousel given the value loads the paging code. `options` may be a ref or a getter; a new `pages` applies from the next flick. Carousel keeps the value it first attached.

## Best practices

| Guidance | Practices |
| --- | --- |
| Do | Use it for a row read one view at a time, such as onboarding screens or a photo strip, where a fast flick must not skip past the next view. |
| Do | Pass `{ pages: 'item' }` when several items show at once and each flick should move only one of them. |
| Don't | Use it for a row people scan quickly; plain `hasSnap` keeps native momentum, which can pass several items in one swipe. |

## Examples

### useSnapPaging — Onboarding

Onboarding screens with snap paging. A flick moves one view; the switch makes it move one card.

```vue
<script setup lang="ts">
import { useSnapPaging } from '@astryx-vue/core'
import { Card } from '@astryx-vue/core/Card'
import { Carousel } from '@astryx-vue/core/Carousel'
import { Stack } from '@astryx-vue/core/Stack'
import { Switch } from '@astryx-vue/core/Switch'
import { Text } from '@astryx-vue/core/Text'
import { ref } from 'vue'

const screens = [
  { title: 'Welcome', body: 'Swipe to see what is new.' },
  { title: 'Shared albums', body: 'Invite friends to add their photos.' },
  { title: 'Memories', body: 'See a day from years ago.' },
  { title: 'Search', body: 'Find a photo by what is in it.' },
  { title: 'All set', body: 'Your library is ready.' },
]

const isOneCard = ref(false)
// A flick moves one view, or one card with pages: 'item'.
const paging = useSnapPaging(() => ({ pages: isOneCard.value ? 'item' : 'view' }))
</script>

<template>
  <Stack direction="vertical" :gap="3" :max-width="520" style="padding: 8px">
    <Switch v-model="isOneCard" label="One card per flick" />
    <Carousel :gap="2" :has-snap="paging" label="Onboarding">
      <Card v-for="screen in screens" :key="screen.title" :width="220" :min-height="160">
        <Stack direction="vertical" :gap="1">
          <Text type="body" weight="bold">
            {{ screen.title }}
          </Text>
          <Text type="body" color="secondary">
            {{ screen.body }}
          </Text>
        </Stack>
      </Card>
    </Carousel>
  </Stack>
</template>
```

## Parameters

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `options` | `MaybeRefOrGetter` | `{}` | Paging options, or a ref or getter that returns them. Read at each flick, so a new value applies from the next one. |
| `options.pages` | `'view' \| 'item'` | `'view'` | What one flick moves. `'view'` moves one view and lands on the item start edge nearest one viewport on; `'item'` moves one item. |

## Returns

| Name | Type | Description |
| --- | --- | --- |
| `paging` | `SnapPaging` | Snap paging for Carousel's `hasSnap`. Carousel attaches it to its scroll area on mount and keeps the value it first attached; switching back to `true` or `false` detaches it. |
