@forge/react

11.18.0-next.011.18.0-next.1
out/router/hooks/useNavigate.js
+out/router/hooks/useNavigate.jsNew file
+50
Index: package/out/router/hooks/useNavigate.js
===================================================================
--- package/out/router/hooks/useNavigate.js
+++ package/out/router/hooks/useNavigate.js
@@ -0,0 +1,50 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+exports.useNavigate = void 0;
+const react_1 = require("react");
+const RouterContext_1 = require("../components/RouterContext");
+const resolvePath = (currentPathname, to) => {
+    if (to.startsWith('/')) {
+        return to;
+    }
+    const currentPath = currentPathname.endsWith('/') ? currentPathname.slice(0, -1) : currentPathname;
+    const combined = currentPath + '/' + to;
+    const segments = combined.split('/').filter(Boolean);
+    const resolved = [];
+    for (const segment of segments) {
+        if (segment === '..') {
+            resolved.pop();
+        }
+        else if (segment !== '.') {
+            resolved.push(segment);
+        }
+    }
+    return '/' + resolved.join('/');
+};
+/**
+ * This hook returns a function that lets you programmatically navigate to different routes in your app. It must be used within a Router component.
+ *
+ * @see [useNavigate](https://developer.atlassian.com/platform/forge/ui-kit/hooks/use-navigate/) in UI Kit documentation for more information
+ */
+const useNavigate = () => {
+    const context = (0, react_1.useContext)(RouterContext_1.RouterContext);
+    if (!context) {
+        throw new Error('useNavigate must be used within a Router component');
+    }
+    const { history } = context;
+    const navigate = (0, react_1.useCallback)((to, options) => {
+        if (typeof to === 'number') {
+            history.go(to);
+            return;
+        }
+        const path = resolvePath(history.location.pathname, to);
+        if (options?.replace) {
+            history.replace(path);
+        }
+        else {
+            history.push(path);
+        }
+    }, [history]);
+    return navigate;
+};
+exports.useNavigate = useNavigate;