It simplifies the use of the backbutton for the capacitor.
npm install @ssibrahimbas/capacitor-backbutton@ssibrahimbas/capacitor-backbutton
It simplifies the use of the backbutton for the capacitor.
| Maintainer | GitHub | Web |
|-----------------------|-------------------------------------------------|---------------------------------------------------------|
| Sami Salih İbrahimbaş | ssibrahimbas | @ssibrahimbas |
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.
``bash
$ npm install @ssibrahimbas/capacitor-backbutton
`
`bash
$ yarn add @ssibrahimbas/capacitor-backbutton
`
* listen()
* subscribe(...)
* unsubscribe(...)
* IBackButtonHardware
* BackButtonListener
* CallbackFunction
* NextFunction
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 to the backbutton event
| Param | Type |
|---------------|---------------------------|
| eventListener | BackButtonListener |
Returns: void
Unsubscribe to the backbutton event
| Param | Type |
|---------------|---------------------------|
| eventListener | BackButtonListener |
Returns: void
| Field | Type |
|---------------|---------------------------------|
| name | string |
| $app | AppPlugin |
| listen | () => void |
| subscribe | (eventListener: BackButtonListener) => void |
| unsubscribe | (eventListener: BackButtonListener) => void |
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 |
listener function
| Param | Type |
|---------------|---------------------------|
| event | BackButtonListenerEvent |
| next | NextFunction |
Returns: void
type of function that runs the next listener
Returns: void
`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();
}
})
`
`javascript
// 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");
`
`vue
// app.vue
// bla bla
// Use Composition API
// Use Vue 3 Hooks and TypeScript
``