Code Monkey home page Code Monkey logo

react-swipe-to-delete-component's Introduction

React-swipe-to-delete-component

Code Climate Coverage Status

A simple React component implement 'swipe to delete' UI-pattern.

Install

React-swipe-to-delete-component is available via npm.

npm install react-swipe-to-delete-component

Usage

The React-swipe-to-delete-component wrap your a content component. It's become swiped. If it's swiped more certain percent than the swipe-to-delete-component will remove a component.

Example

You may see an example here.

import React from 'react';
import { render } from 'react-dom';
// Import the react-swipe-to-delete-component
import SwipeToDelete from 'react-swipe-to-delete-component';
// Import styles of the react-swipe-to-delete-component
import 'react-swipe-to-delete-component/dist/swipe-to-delete.css';

const data = [
  {id: 1, text: 'End of summer reading list', date: '1.03.2016'},
  {id: 2, text: 'Somewhere in the middle 📸', date: '23.01.2017'},
  {id: 3, text: 'Good morning to 9M of you?!?! ❤️🙏🏻Feeling very grateful and giddy.', date: '12.01.2022'}
];

const list = data.map(item => (
  <SwipeToDelete key={item.id}>
    <a className="list-group-item">
      <h4 className="list-group-item-heading">{item.date}</h4>
      <p className="list-group-item-text">{item.text}</p>
    </a>
  </SwipeToDelete>
));

const app = (
  <div className="list-group">
    {list}
  </div>
);

render(app, document.getElementById('root'));

Props

  • tag - This is tag name of a root element. By default, it's "div". Optional.
  • classNameTag - This is classes of a root element. Optional.
  • background - This is a decoration component under a content component. By default, showed red element with trash icons. Optional.
  • deleteSwipe - This is a number. If a content component is swiped more this the number than a swipe-to-delete component will start a delete animation. By default, it's equal "0.5". Optional.
  • onDelete - This is a function. If a content component is deleted then it will be called. It receives custom props from a <SwipeToDelete /> component. Optional.
  • onCancel - This is a function. If a content component isn't deleted then it will be called. It receives custom props from a <SwipeToDelete /> component. Optional.
  • onRight/onLeft - This is a function. If a content component is swiped then these functions is called. Optional.

Styles

The class js-content is content region, js-delete is delete region. Classes js-transition-delete-right and js-transition-delete-left are added on a content component when it's swiped more than "deleteSwipe" options. Class js-transition-cancel is added when a content component swiped less than "deleteSwipe" options. Animations are made by CSS3 transition.

Changes

See the CHANGELOG.

Contributing

From opening a bug report to creating a pull request: every contribution is appreciated and welcome. If you're planing to implement a new feature or change the api please create an issue first.

License

MIT

react-swipe-to-delete-component's People

Contributors

dependabot[bot] avatar gaer87 avatar jolivettesax avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

react-swipe-to-delete-component's Issues

undefined evaluating navigator.userAgent.match

Hi,
My code produce this weird error:

import 'react-native-get-random-values';
import React, {useState} from 'react';
import {uuid} from 'uuidv4';
import {View, Text, StyleSheet} from 'react-native';
import SwipeToDelete from 'react-swipe-to-delete-component';

const App = () => {
  const [items, setItems] = useState([
    {id: uuid(), text: 'Milk'},
    {id: uuid(), text: 'Eggs'},
    {id: uuid(), text: 'Bread'},
    {id: uuid(), text: 'Juice'},
  ]);

  const List = items.map(item => (
    <SwipeToDelete key={item.id} item={item}>
      <a className="list-group-item">
        <h4 className="list-group-item-heading">{item.id}</h4>
        <p className="list-group-item-text">{item.text}</p>
      </a>
    </SwipeToDelete>
  ));

  return (
    <View>
      {List}
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    backgroundColor: 'white',
    flex: 1,
  },
});

export default App;

I don't think it is related to my code but more to the execution of react-swipe-to-delete-component.
Tested on Pixel 3 emulator with npx react-native run-android
It's too bad tis component looks so cool

Feature: Animate Deletion

Would you be willing to implement a delete transition so that the vertical movement of the list is less abrupt?

Not working with NextJS.

It is not working with Next JS whenever I am trying to implement this it is showing this error i.e. mentioned below.

<SwipeToDelete onRight={deleteHandler}>
    {/* Some Content */}
</SwipeToDelete>

image

One-sided swiping only for onDelete

I am using onDelete but I don't want it to be able to delete from swiping either way.
Is it possible to disable a swipe direction? Or better yet a prop that you can provide to say which swipe direction will trigger the delete?

Big big icons only

Hi,

This looks great. However, I can't get it to work. I've imported it at the top, and put some dummy data there. However, alll I see is large bin icons. Any ideas why? Here is my setup:

import SwipeToDelete from 'react-swipe-to-delete-component';

    <SwipeToDelete key={result._id}>
          <b>HELLO</b>
  </SwipeToDelete>

Pass key upon delete

Hi! Thanks for this great component.
Is there any way to pass the key value upon delete?
I would like to use the key in the onDelete function.

Touch screen

I can't make the react-swipe-to-delete-component work on a touch screen.
It works well on a normal screen with a mouse but not a touch screen.
I tried on two different touch screens, verified the drivers and other swipe type of components are working fine but I can't get that one to work somehow.

Action Functions

You have onLeft={onLeft} and onRight={onRight} functions, but there is no styling provision (that I could find) for making left and right call-to-actions. that are separated. (The trash icon).

I created a basic hook:

    const [isRight, setRight] = useState(false);
    const [isLeft, setLeft] = useState(false);

    const onLeft = (...args) => {
        setLeft(true);
        setRight(false);
    }
    const onRight = (...args) => {
        setRight(true);
        setLeft(false);
    }

And added classNameTag to the <SwipeToDelete object.

<SwipeToDelete
     key={item.id}
    onLeft={onLeft}
    onRight={onRight}
    classNameTag={isRight ? "is-right" : isLeft ? "is-left" : ""}
>

Then I just created some CSS to override the default CSS behavior:

.list-group{
    padding:15px;
    background-color:#FFF;
    border:2px solid #DDD;
    overflow-y:scroll;
    display:flex;
    flex-direction: column;
    border-right: 1px solid rgba(0, 0, 0, 0.12);
    height: calc(100vh - 11rem);
    border-radius: 0.75rem;
    margin-bottom: 30px;
    overflow-x: hidden;
}

.swipe-to-delete.is-right .js-delete {
    background-color: #6fc76f;
    background-image: url('data:image/svg+xml, CUSTOM SVG HERE');
    background-size: 25px 25px;
    background-position: 10px center;
    background-repeat: no-repeat;

    }
.swipe-to-delete.is-left .js-delete {
    background-color: #fd8681;
    background-image: url('data:image/svg+xml, CUSTOM SVG HERE>');
    background-size: 25px 25px;
    background-position: calc(100% - 10px) center;
    background-repeat: no-repeat;
}
.swipe-to-delete .js-delete svg:first-child {
    display:none;
}
.swipe-to-delete .js-delete svg {
    display:none;
}
.swipe-to-delete {
    overflow: visible !important;
}

This allows for two different background colors depending on the swipe direction, and also allows for custom icon based on swipe direction.

image
image

Paused on promise rejection

Hello again!
If I wrap the individual swipeable boxes in <Link> so they can be clicked to go to another page, sometimes I get an error and the app crashes.
The Chrome DevTools console says:

Paused on promise rejection
TypeError: Cannot read property 'firstChild' of null

Which points to line 397 of swipe-to-delete.min.js.
Is there any way around this? If I switch <Link> to an anchor tag <a>, I do not get this error, but using <a> in a SPA is not favourable.

Please let me know if you've got any ideas! Thanks.

Should ignore vertical swipe

Hello,

thanks a lot for your awesome component!
It's working well except when you have to scroll a list on mobile. The vertical swipe gesture is intercepted by the component. The element is slightly moving (left or right) when you scroll. It's visually disturbing. Can you try to detect that the gesture is horizontal before trigger the element animation?

Babel error

Hi, I'm trying to set up this module, but I got this error on fresh project build with create react app (typescript) :

Cannot find module 'semver' (While processing: "/node_modules/babel-preset-react-app/index.js")
console.<computed> @ index.js:1
overrideMethod @ react_devtools_backend.js:2430
handleErrors @ webpackHotDevClient.js:174
push../node_modules/react-dev-utils/webpackHotDevClient.js.connection.onmessage @ webpackHotDevClient.js:213

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.