# @epeejs/react-scroll-snap

> A Scroll Snap component for React

Latest version **0.1.1** (published 2021-11-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install @epeejs/react-scroll-snap
pnpm add @epeejs/react-scroll-snap
yarn add @epeejs/react-scroll-snap
bun add @epeejs/react-scroll-snap
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.1 |
| Published | 2021-11-30 |
| First published | 2021-10-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 5.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | dobble11, luoqian2019 |
| Keywords | scroll-snap, react-scroll-snap, react |

## Links

- npm: https://www.npmjs.com/package/@epeejs/react-scroll-snap
- Repository: https://github.com/epeejs/react-scroll-snap
- Homepage: https://github.com/epeejs/react-scroll-snap#readme
- Issues: https://github.com/epeejs/react-scroll-snap/issues
- npm.io page: https://npm.io/package/@epeejs/react-scroll-snap

## Dependencies (2)

- [lodash](https://npm.io/package/lodash.md) ^4.17.21
- [classnames](https://npm.io/package/classnames.md) ^2.3.1

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.1.1 (latest) — 2021-11-30
- 0.1.0 — 2021-10-25
- 0.0.2 — 2021-10-25
- 0.0.1 — 2021-10-25

## README

# react-scroll-snap

一个基于 React 的滚动快照组件

## DEMO

[![Edit react-scroll-snap](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/react-scroll-snap-2qd0x?fontsize=14&hidenavigation=1&theme=dark)

## 安装

```sh
yarn add @epeejs/react-scroll-snap
```

## 用法

```tsx
import ScrollSnap, { ScrollSnapAction } from '@epeejs/react-scroll-snap';
import React, { useEffect, useRef } from 'react';

const App = () => {
  const actionRef = useRef<ScrollSnapAction>();
  const colors = ['#E6F7FF', '#BAE7FF', '#91D5FF', '#69C0FF'];

  useEffect(() => {
    if (actionRef.current) {
      // 初始化时滑动到第三个元素
      actionRef.current.goTo(2);
    }
  }, []);

  return (
    <ScrollSnap
      actionRef={actionRef}
      onChange={(current) => {
        console.log(current);
      }}
    >
      {colors.map((m) => (
        <div
          style={{
            background: m,
            width: '80%',
            borderRadius: 16,
            height: 300,
            flexShrink: 0,
            // 设置停留时元素居中
            scrollSnapAlign: 'center',
          }}
        >
          {m}
        </div>
      ))}
    </ScrollSnap>
  );
};
```

## API

| 属性      | 说明             | 类型                     | 默认值 | 版本 |
| :-------- | :--------------- | :----------------------- | :----- | :--- |
| onChange  | 切换面板后的回调 | function(current:number) |
| actionRef | 操作函数         | `ScrollSnapAction`       |

## 方法

### ScrollSnapAction

| 名称        | 描述           |
| :---------- | :------------- |
| goTo(index) | 切换到指定面板 |

---
_Source: https://npm.io/package/@epeejs/react-scroll-snap · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
