npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mohalla-tech/react-swipeable-wrapper

v1.1.4

Published

Swipeable views wrapper

Downloads

19

Readme

react-swipeable-wrapper

A React component for swipeable views.

| Package | Version | | ------------------------------------- | :---------------------------------------------------------------------------------------------------------------------------------------------- | | @mohalla-tech/react-swipeable-wrapper | npm version |

Installation

npm install --save @mohalla-tech/react-swipeable-wrapper
or
yarn add @mohalla-tech/react-swipeable-wrapper

Example

Github Pages Demo

Example

Usage

import React, { useCallback, useRef, useState } from "react";

import SwipeableWrapper from "react-swipeable-wrapper";

const tabs = ["Tab 1", "Tab 2", "Tab 3"];
const initialIndex = 0;

const styles = {
  parent: {
    maxWidth: 800,
    margin: "auto",
    paddingTop: "10em",
    overflow: "hidden",
  },
  text: {
    padding: "1em 2px",
    fontStyle: "bold",
  },
  slide: {
    padding: 15,
    minHeight: 400,
    color: "#fff",
  },
  slide1: {
    background: "#40E0D0",
  },
  slide2: {
    background: "#9FE2BF",
  },
  slide3: {
    background: "#FFBF00",
  },
  tabsParent: {
    width: "50%",
  },
  tabs: {
    display: "flex",
    justifyContent: "space-around",
    height: "2rem",
  },
  bottomBar: {
    height: "4px",
    borderTopRightRadius: "50px 20px",
    borderTopLeftRadius: "50px 20px",
    background: "#000080",
    width: `${100 / tabs.length}%`,
  },
  nonSelectedTab: {
    color: "#DE3163",
  },
  selectedTab: {
    color: "#000080",
  },
};

const App = () => {
  const swipeRef = useRef(null);
  const bottomBarRef = useRef(null);

  const [currentSlideIdx, setSlideIdx] = useState(initialIndex);

  const onTabClick = useCallback(currentIndex => {
    const { getCurrentIndex, swipeToIndex } = swipeRef?.current ?? {};
    const previousIndex = getCurrentIndex();
    if (currentIndex !== previousIndex) swipeToIndex(currentIndex);
  }, []);

  const handleSlideChange = currentIndex => {
    setSlideIdx(currentIndex);
  };

  return (
    <div>
      <div style={styles.parent}>
        <div style={styles.tabsParent}>
          <div style={styles.tabs}>
            {tabs.map((tab, idx) => (
              <div
                key={tab}
                onClick={() => onTabClick(idx)}
                style={
                  currentSlideIdx === idx
                    ? styles.selectedTab
                    : styles.nonSelectedTab
                }
              >
                {tab}
              </div>
            ))}
          </div>
          <div ref={bottomBarRef} style={styles.bottomBar} />
        </div>
        <SwipeableWrapper
          ref={swipeRef}
          initialIndex={initialIndex}
          onSlideChange={handleSlideChange}
          bottomBarRef={bottomBarRef}
        >
          <div style={{ ...styles.slide, ...styles.slide1 }}>1st slide</div>
          <div style={{ ...styles.slide, ...styles.slide2 }}>2nd slide</div>
          <div style={{ ...styles.slide, ...styles.slide3 }}>3rd slide</div>
        </SwipeableWrapper>
      </div>
    </div>
  );
};

SwipeableWrapper Props

| Parameter | Type | Default | Description | | :------------------------- | :---------------------------------- | :--------- | :------------------------------------------------------------ | | bottomBarRef | React.RefObject<HTMLInputElement> | null | Ref applied on div that'll behave as bottom bar. | | onSlideChange | function | () => {} | Each time a slide is changed, this function will be executed. | | initialIndex | number | 0 | Index of the slide to be displayed on the initial mount. | | filterNodes | Array | [] | Node identifiers that will not accept swipes. | | transitionDuration | number | 300 | Duration of the transition. | | transitionTimingFunction | string | ease-out | Timing function of the transition. | | containerStyles | object | {} | Styles to override container styles. | | disableAutoScroll | boolean | false | Disable auto scroll to top. | | disableAutoAdjustHeight | boolean | false | Disable auto adjust height. |

Functions

swipeToIndex: This function lets you switch between slides.

swipeRef.current.swipeToIndex(indexOfSlide);

getCurrentIndex: This function returns the index of the slide your are on.

const currentSlide = swipeRef.current.getCurrentIndex();

Note

initialIndex prop should be memoized or constant value.

onSlideChange function should be memoized.

License

This project is licensed under the terms of the MIT license.