1.2.7 • Published 2 years ago
react-custom-swipe v1.2.7
react-custom-swipe
Custom-Swipe: A Lightweight and Headless Frontend Library
Custom-Swipe is a versatile solution designed for seamless user interactions in web applications. Whether you're building a responsive web app or enhancing the user experience on various devices, Custom-Swipe offers a range of swipe features. It serves as both a basic component for easy integration and a powerful headless hook for developers seeking granular control.
Key Features
- Lightweight: Minimize your bundle size with our lightweight library.
- Responsive: Ensure smooth interactions across various devices and screen sizes.
- Customizable: Tailor swipe behavior to suit your application's specific needs.
- Headless Hook: For developers who prefer a programmatic approach, our headless hook provides extensive control over swipe events.
Explore the possibilities with Custom-Swipe and elevate your frontend development experience. demo
Install
1.yarn
yarn add react-custom-swipe2.npm
npm install react-custom-swipeUse Example
- Use Component
import React from 'react';
import ReactSwipe from 'react-custom-swipe';
const App = () => {
return (
<div>
<h1>Component Demo</h1>
<ReactSwipe
item={[1, 2, 3, 4, 5, <div>item</div>, <img src={'src'} alt={'src'} />]}
containerProps={{ style: { border: '1px solid' } }}
itemProps={{ style: { border: '1px solid' } }}
config={{
isHistory: false,
paramName: 'index',
historyCallback: (state) => console.log('swipeState', state),
isCarousel: true,
}}
/>
</div>
);
};- Use Hook
import React, { createRef, useState } from 'react';
import { useSwipe } from 'react-custom-swipe';
const App = () => {
const [item, setItem] = useState([<div>test</div>, <div>test2</div>, 1, '2']);
const ref = useRef<HTMLUListElement>(null);
const DotsRef = useRef<HTMLUListElement>(null);
const { swipeEvents, handleSlide, changeIndex } = useSwipe(ref, item.length, {
...config,
historyCallback: (state) => {
config?.historyCallback && config?.historyCallback(state);
handleDot(state.currentStep);
},
});
const handleDot = (index: number) => {
if (DotsRef.current !== null) {
DotsRef.current.childNodes.forEach((node: ChildNode, idx: number) => {
const Node = node as HTMLLIElement;
Node.className = index === idx ? 'active' : '';
});
}
};
useEffect(() => {
const index = new URLSearchParams(location.search).get('index');
if (index) handleDot(parseInt(index));
}, []);
return (
<div
{...containerProps}
className={`swipe-container ${containerProps?.className}`}>
{config?.isCarousel && !config.isHistory ? (
<div>
<button
className='swipe-button swipe-left-button'
onClick={() => handleSlide('L')}>
〈
</button>
<button
className='swipe-button swipe-right-button'
onClick={() => handleSlide('R')}>
〉
</button>
<Carousel itemLength={item.length} ref={DotsRef} />
</div>
) : null}
<ul className='swipe-wrap' ref={ref} {...swipeEvents}>
{item.map((item, key) => {
return (
<li
key={key}
{...itemProps}
className={`swipe-item ${itemProps?.className}`}>
{item}
</li>
);
})}
</ul>
</div>)
};PROPS
- Swipe(component)
items:Array<React.ReactNode>swipe items.containerProps?:React.HTMLAttributes<HTMLDivElement>container(Root Node) props as react components.itemProps?:React.HTMLAttributes<HTMLLIElement>swipe item box(li node) props as react components.config?:ConfigPropsswipe option config.isHistory?:booleanhistory change or push(default: false)(true ? push : replace).paramName?:stringquerystring key name(default: index).historyCallback?:(state: SwipeStateProps) => voidswipeEnd event custom callback props swipe state.isCarousel?:booleanuse carousel mode need config isHistory flag false.direction?:row | columnuse vertical swipe option.(default: row)
- useSwipe(hook)
dom:React.RefObject<HTMLElement>react ref props events target.length:numberswipe item length(maxlength).config?:ConfigPropsswipe option config.isHistory?:booleanhistory change or push(default: false)(true ? push : replace).paramName?:stringquerystring key name(default: index).historyCallback?:(state: SwipeStateProps) => voidswipeEnd event custom callback props swipe state.direction?:row | columnuse vertical swipe option.(default: row)
useSwipe(hook) returnswipeEvents:UseSwipeEvents<T>; React Swipe Event Handlers.handleSlide:(flag: 'L' | 'R') => void; use Slide handler.changeIndex:(index: number) => void; use goto index handler.
Features
- Swipe
- Infinite swipe(scroll)
- Carousel
LICENSE
MIT
1.2.7
2 years ago
1.2.6
2 years ago
1.2.5
2 years ago
1.2.4
2 years ago
1.2.3
2 years ago
1.2.2
2 years ago
1.2.1
2 years ago
1.2.0
2 years ago
1.1.1
2 years ago
1.1.0
2 years ago
1.0.9
2 years ago
1.0.8
2 years ago
1.0.7
2 years ago
1.0.11
2 years ago
1.0.10
2 years ago
1.0.13
2 years ago
1.0.12
2 years ago
1.0.6
3 years ago
1.0.5
3 years ago
1.0.4
3 years ago
1.0.3
3 years ago
1.0.2
3 years ago
1.0.1
3 years ago
1.0.0
3 years ago