UNPKG

react-js-plugins

Version:

A powerful and efficient React utility library designed to enhance application performance by streamlining and simplifying the management of complex asynchronous operations.

52 lines (51 loc) 1.82 kB
var currentState = null; var currentPath = window.location.pathname; // Listen for browser navigation (back/forward buttons) window.addEventListener('popstate', function (event) { var state = event.state || {}; currentPath = window.location.pathname; currentState = state.state || null; // Trigger custom event for other parts of your app to listen to window.dispatchEvent(new CustomEvent('locationchange', { detail: { path: currentPath, state: currentState } })); }); export var _navigate = function (path, options) { if (options === void 0) { options = {}; } var _a = options.replace, replace = _a === void 0 ? false : _a, _b = options.state, state = _b === void 0 ? null : _b; // Create history state object var historyState = { state: state, path: path }; if (replace) { window.history.replaceState(historyState, '', path); } else { window.history.pushState(historyState, '', path); } // Update global state currentPath = path; currentState = state; // Notify other parts of the application window.dispatchEvent(new CustomEvent('locationchange', { detail: { path: path, state: state } })); }; export var _go = function (delta) { window.history.go(delta); }; export var _goBack = function () { window.history.back(); }; export var _goForward = function () { window.history.forward(); }; export var _getCurrentPath = function () { return currentPath; }; export var _getCurrentState = function () { return currentState; }; // Helper function to get both path and state export var _getLocation = function () { return ({ pathname: currentPath, state: currentState }); }; // Optional: Type-safe state access with generic export var _getTypedState = function () { return currentState; };