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

@ssibrahimbas/capacitor-backbutton

v4.0.0

Published

It simplifies the use of the backbutton for the capacitor.

Downloads

8

Readme

Maintainers

| Maintainer | GitHub | Web | |-----------------------|-------------------------------------------------|---------------------------------------------------------| | Sami Salih İbrahimbaş | ssibrahimbas | @ssibrahimbas |

Motivation

Although Capacitor itself provides backButton support, this is not effective. Namely, you can have more than one popup in a page. This popup should close when the user triggers the back button on android devices. Capacitor provides this. But when this popup closes, its page should not come back either. All that needs to be done is to close the popup. Here, the capacitor does not provide it, but we provide it with this package.

@ssibrahimbas/capacitor-backbutton package creates a priority-based queue in the background. Each listener function subscribes to this queue. The function with the highest priority runs first, and can have the next listener run if it wishes.

This pack is clearly inspired by Ionic's backbutton engine. But it is expensive to work with Ionic infrastructure just for the backbutton. That's why I wrote this package and you can provide backbutton management in a very light and performance way.

| Dependencies | GitHub | Npm | |--------------------|------------------------------------------------------------|---------------------------------------------------------------------------------| | @ssibrahimbas/core | @ssibrahimbas/core | @ssibrahimbas/core | | @capacitor/app | @capacitor/app | @capacitor/app |

@ssibrahimbas/core package provides Queue, PriorityQueue and Stack infrastructure. You know that JavaScript and TypeScript themselves do not have this structure.

Installation


$ npm install @ssibrahimbas/capacitor-backbutton

Or with yarn:


$ yarn add @ssibrahimbas/capacitor-backbutton

API

BackButtonHardware

Types

Examples

BackButtonHardware

listen()

It starts listening for the backbutton event. It does not affect the subscription. You can subscribe or unsubscribe before or after listening.

Returns: void

subscribe(...)

Subscribe to the backbutton event

| Param | Type | |---------------|---------------------------| | eventListener | BackButtonListener |

Returns: void

unsubscribe(...)

Unsubscribe to the backbutton event

| Param | Type | |---------------|---------------------------| | eventListener | BackButtonListener |

Returns: void

Types

IBackButtonHardware

| Field | Type | |---------------|---------------------------------| | name | string | | $app | AppPlugin | | listen | () => void | | subscribe | (eventListener: BackButtonListener) => void | | unsubscribe | (eventListener: BackButtonListener) => void |

BackButtonListener

each listener must subscribe to the event according to this type.

The unique value is required to unsubscribe. Priority value is required to detect priority. The callback value is required to run the function.

| Field | Type | |---------------|------------------------| | unique | string | | priority | number | | callback | CallbackFunction |

CallbackFunction

listener function

| Param | Type | |---------------|---------------------------| | event | BackButtonListenerEvent | | next | NextFunction |

Returns: void

NextFunction

type of function that runs the next listener

Returns: void

Usage

For TypeScript


import {App as app, BackButtonListenerEvent} from "@capacitor/app";
import {BackButtonHardware, IBackButtonHardware, NextFunction} from "@ssibrahimbas/capacitor-backbutton";

const backButtonHardware: IBackButtonHardware = new BackButtonHardware(app);

function exitApp(): void {
    app.exitApp();
}

function prevPage() : void {
    // router.back();
}

function closePopup() : void {
    // close popup
}

backButtonHardware.subscribe({
    unique: "closePopup",
    priority: 100,
    callback: (event: BackButtonListenerEvent, next: NextFunction) => {
        closePopup();
        // call next if you want next listener to run after popup closes
    }
})

backButtonHardware.subscribe({
    unique: "exitApp",
    priority: -1,
    callback: (event: BackButtonListenerEvent, next: NextFunction) => {
        if(event.canGoBack) return next();
        extiApp();
    }
})

backButtonHardware.subscribe({
    unique: "prevPage",
    priority: 1,
    callback: (event: BackButtonListenerEvent, next: NextFunction) => {
        prevPage();
        next();
    }
})

For Vue


// main.js
import {App as app} from "@capacitor/app";
import {BackButtonHardware} from "@ssibrahimbas/capacitor-backbutton";
import {createApp} from "vue";

const BackButton = {
	install: (app) => {
		app.config.globalProperties.$backButton = new BackButtonHardware(app);
    }
}

const app = createApp();
app.use(BackButton);
app.mount("#app");

// app.vue

<template>
  // bla bla
</template>

<script>
export default {
  data() {
    return {
      popup: {
        show: true
      }
    }
  },
  methods: {
    closePopup(event, next) {
      // if the popup is not shown, the next listener is called. For this example, the prevPage listener works.
      if(this.popup.show) {
        this.popup.show = false;
        return;
      }
      next();
    },
    prevPage(event, next) {
      this.$router.back();
    }
  },
  mounted() {
    this.$backButton.listen();
    this.$backButton.subscribe({
      unique: "appClosePopup",
      priority: 100,
      callback: this.closePopup
    });
    this.$backButton.subscribe({
      unique: "appPrevPage",
      priority: 1,
      callback: this.prevPage
    });
  }
}
</script>

// Use Composition API

<script>
import {ref, onMounted, getCurrentInstance} from "vue"

export default {
  setup(){
    const popupShow = ref(false);
    
    const closePopup = (event, next) => {
      if(this.popupShow.value) {
        this.popupShow.value = false;
        return;
      }
      next();
    }
    
    const prevPage = (event, next) => {
      // router back
    }
    
    onMounted(() => {
      const backButton = getCurrentInstance().appContext.config.globalProperties.$backButton;
      backButton.listen();
      backButton.subscribe({
        unique: "appClosePopup",
        priority: 100,
        callback: closePopup
      });
      backButton.subscribe({
        unique: "appPrevPage",
        priority: 1,
        callback: prevPage
      });
    })
    
    return {
      closePopup,
      prevPage,
      popupShow
    }
  }
}
</script>


// Use Vue 3 Hooks and TypeScript

<script setup lang="ts">
import { useBackButton, type CallbackFunction, type BackButtonListener } from "@ssibrahimbas/capacitor-backbutton";
import { ref, onMounted, onUnMounted } from "vue"
import { useRouter } from "vue-router"

const popupShow = ref<boolean>(false);
const { subscribe, unsubscribe, listen } = useBackButton();
const router = useRouter();

const closePopup : CallbackFunction = (event, next) : void => {
  if(this.popupShow.value) {
    this.popupShow.value = false;
    return;
  }
  next();
}

const prevPage : CallbackFunction = (event, next) : void => {
  router.back();
}

const getSubscribers = () : BackButtonListener[] => {
  return [
    {
    unique: "appClosePopup",
    priority: 100,
    callback: closePopup
  }, 
  {
    unique: "appPrevPage",
    priority: 1,
    callback: prevPage
  }
  ]
}

onMounted(() : void => {
  listen();
  subscribe(...getSubscribers());
})

onUnMounted(() : void => {
  unsubscribe(...getSubscribers())
})
</script>