Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@mini-apps/navigation

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@mini-apps/navigation

Navigation library for VK Mini Apps

  • 2.0.6
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
5
Maintainers
1
Weekly downloads
 
Created
Source

Mini Apps Navigation NPM

A package to organize navigation in Mini Apps applications

Installation

yarn add @mini-apps/navigation

or

npm i @mini-apps/navigation

Description

This package was created with aim on mobile applications. So, the main terms which are used in this library are view, modal, popup etc. You should not wait for any Web based terms used in context of Navigator. Here, I tried to recreate a logic used in native mobile applications and waiting for other developers contribution.

Glossary

State

State is like the one of the most often met terms in this project. It is described with view, modal, popup, params and modifiers. Properties modal and popup are not required, but view is. Normally, there cannot be any situation when view is unknown, we should always show something, so that is the reason why view is required. params is passed custom dictionary.

Modifier

Modifier is a modification applied to state. It can be any string and can be used for any purposes.

Classes

BrowserNavigator

BrowserNavigator is adopted for browser and works with window.history. It extends some Navigator methods like on, off and go with some extra lines of code, which are written mostly to interact with browser's history.

It rewires such functions as window.history.pushState and window.history.replaceState, so then, each history item has stable state, which contains current location index and location history.

Usage
Creating instance
import {BrowserNavigator} from '@mini-apps/navigation';

const navigator = new BrowserNavigator();
Initialization

When window.history becomes available, it is required to call navigator init, to make him override window.history.pushState and window.history.replaceState and add event listener to window's popstate event.

navigator.init();

To cancel overrides and remove event listener:

navigator.unmount();
Initial navigator state

To make navigator initialization easier, there is a function extractBrowserNavigatorSettings which returns initial settings for navigator if it is possible.

import {extractBrowserNavigatorSettings} from '@mini-apps/navigation';

// Extract settings from browsers history
const navigatorSettings = extractBrowserNavigatorSettings();

// Initialize with received settings
navigator.init(navigatorSettings ? navigatorSettings : undefined);

If first parameter in init was not passed, navigator watches if current history length is equal to 1, it understands that previously, it was not mounted here before. It replaces current location with modifier root which indicates about root location.

Popstate listener

When mount() is called, navigator adds its own listener, which watches for location changes and automatically updates internal location. The logic is rather simple - it tries to extract location info from history item state and correctly updates navigator. But in case of fail, it will recognize pushed location as new one. So, it will try to parse it and push to navigator. Otherwise, an error will be thrown.

Keywords

FAQs

Package last updated on 03 Aug 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc