Skip to content

Commit

Permalink
Adds min prop to set opacity
Browse files Browse the repository at this point in the history
  • Loading branch information
goncy committed Jul 6, 2018
1 parent 059cfd9 commit 9c64044
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ export default App;

Offset in px swiped to consider as swipe

#### min
`Number`

Offset when opacity fade should start

#### onBeforeSwipe
`Function`

Expand Down
5 changes: 3 additions & 2 deletions src/Swipeable.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const SWIPE_CONFIG = {

const DEFAULT_PROPS = {
limit: 120,
min: 40,
};

const INITIAL_STATE = {
Expand Down Expand Up @@ -130,15 +131,15 @@ export default class Swipeable extends PureComponent {

render() {
const {offset, swiped, pristine, forced} = this.state;
const {children, limit, buttons} = this.props;
const {children, limit, buttons, min} = this.props;

return (
<Fragment>
<Spring
from={{offset: 0, opacity: 1}}
to={{
offset,
opacity: getOpacity(offset, limit),
opacity: getOpacity(offset, limit, min),
}}
onRest={() => swiped && this.onAfterSwipe()}
immediate={pristine || (!forced && Math.abs(offset) >= limit)}
Expand Down
6 changes: 5 additions & 1 deletion src/helpers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
export const getDirection = offset => (offset > 0 ? "right" : "left");
export const getOpacity = (offset, limit) => 1 - Math.abs(offset) / limit;
export const getOffset = (start, end) => -((start - end) * 0.75);
export const getEvent = e => (e.touches ? e.touches[0] : e);
export const withX = fn => e => fn(getEvent(e).pageX);
export const getLimitOffset = (limit, direction) =>
direction === "right" ? limit : -limit;
export const getOpacity = (offset, limit, min) =>
1 -
(Math.abs(offset) < min
? 0
: (Math.abs(offset) - min) / Math.abs(limit - min));

0 comments on commit 9c64044

Please sign in to comment.