@djangocfg/ui-core 2.1.648 → 2.1.651

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@djangocfg/ui-core",
3
- "version": "2.1.648",
3
+ "version": "2.1.651",
4
4
  "description": "Pure React UI component library without Next.js dependencies - for Electron, Vite, CRA apps",
5
5
  "keywords": [
6
6
  "ui-components",
@@ -144,8 +144,8 @@
144
144
  "check:docs": "node scripts/check-docs.mjs"
145
145
  },
146
146
  "peerDependencies": {
147
- "@djangocfg/api": "^2.1.648",
148
- "@djangocfg/i18n": "^2.1.648",
147
+ "@djangocfg/api": "^2.1.651",
148
+ "@djangocfg/i18n": "^2.1.651",
149
149
  "consola": "^3.4.2",
150
150
  "lucide-react": "^1.68.0",
151
151
  "moment": "^2.30.1",
@@ -220,10 +220,10 @@
220
220
  "vaul": "1.1.2"
221
221
  },
222
222
  "devDependencies": {
223
- "@djangocfg/api": "^2.1.648",
224
- "@djangocfg/eslint-config": "^2.1.648",
225
- "@djangocfg/i18n": "^2.1.648",
226
- "@djangocfg/typescript-config": "^2.1.648",
223
+ "@djangocfg/api": "^2.1.651",
224
+ "@djangocfg/eslint-config": "^2.1.651",
225
+ "@djangocfg/i18n": "^2.1.651",
226
+ "@djangocfg/typescript-config": "^2.1.651",
227
227
  "@storybook/react-vite": "^10.6.5",
228
228
  "@types/node": "^24.13.3",
229
229
  "@types/react": "19.3.0",
@@ -67,6 +67,9 @@ export {
67
67
  parseAsJson,
68
68
  // useRouter (composite facade)
69
69
  useRouter,
70
+ // usePathnameChange (a callback when the path changes, not the query)
71
+ usePathnameChange,
72
+ watchPathname,
70
73
  // useScrollToTop (covers the routes Next's own scroll handler skips)
71
74
  useScrollToTop,
72
75
  // Per-page switches for the scroll jump and the loading bar
@@ -101,4 +104,6 @@ export type {
101
104
  QueryParserBuilder,
102
105
  UseRouterReturn,
103
106
  UseScrollToTopOptions,
107
+ PathnameChangeHandler,
108
+ UsePathnameChangeOptions,
104
109
  } from './router';
@@ -15,6 +15,7 @@ Framework-agnostic navigation primitives for `@djangocfg/ui-core`. Built on plai
15
15
  | `useIsActive` | `boolean` for "current pathname matches this href" — for nav-item highlighting. |
16
16
  | `useQueryState` | Typed `useState`-style hook bound to ONE URL key (with parsers + `clearOnDefault`). |
17
17
  | `useHashState` | `useState`-style hook bound to the URL fragment (`#…`). Writes bypass the adapter — see [`useHashState`](#usehashstate-url-fragment) below. |
18
+ | `usePathnameChange` | Callback when the PATH changes (a push, replace, back or forward to another pathname); a query or hash change does not count. For a surface that covers the page and must close on navigation: `usePathnameChange(close, { enabled: open && isMobile })`. `watchPathname` is the non-React core. |
18
19
  | `useLocationProperty` | Subscribe to ONE derived field of `window.location` (avoids re-renders on unrelated fields). |
19
20
  | `useRouter` | Convenience facade composing the above. |
20
21
  | `RouterAdapterProvider` | Swap the navigation backend (e.g. Next.js's router). |
@@ -107,8 +107,18 @@ export function NextRouterAdapter({ children, defaultScroll }: NextRouterAdapter
107
107
  window.addEventListener(NAVIGATE_EVENT, stop);
108
108
  return () => window.removeEventListener(NAVIGATE_EVENT, stop);
109
109
  }, []);
110
+ // Next's App Router commits its own navigations through the `pushState` it captured at boot, before our patch, so
111
+ // `NAVIGATE_EVENT` never fires for them. Announce every new pathname ourselves: `usePathnameChange`, `useLocation` and
112
+ // any other subscriber then see a Next navigation like any other (seen on gptkino 2026-11-11: the phone assistant
113
+ // stayed open after a link). The first pathname is not a navigation.
114
+ const firstPathname = useRef(true);
110
115
  useEffect(() => {
111
116
  progressRef.current?.engine.done();
117
+ if (firstPathname.current) {
118
+ firstPathname.current = false;
119
+ return;
120
+ }
121
+ window.dispatchEvent(new Event(NAVIGATE_EVENT));
112
122
  }, [pathname]);
113
123
 
114
124
  const adapter = useMemo<RouterAdapter>(
@@ -38,6 +38,10 @@ export type { NavigationBehavior } from '../../route-progress';
38
38
  export { useScrollToTop } from './useScrollToTop';
39
39
  export type { UseScrollToTopOptions } from './useScrollToTop';
40
40
 
41
+ // usePathnameChange (a callback when the PATH changes; query and hash do not count)
42
+ export { usePathnameChange, watchPathname } from './usePathnameChange';
43
+ export type { PathnameChangeHandler, UsePathnameChangeOptions } from './usePathnameChange';
44
+
41
45
  // useNavigate
42
46
  export { useNavigate } from './useNavigate';
43
47
  export type { NavigateOptions, UseNavigateReturn } from './useNavigate';
@@ -0,0 +1,63 @@
1
+ 'use client';
2
+
3
+ /**
4
+ * usePathnameChange — run a callback when the page moves to another PATH.
5
+ *
6
+ * WHY: a surface that covers the page (a sheet, a drawer, a menu) has to go away when the reader navigates
7
+ * (a link inside it, a button that calls `router.push`). It must NOT go away when only the query string or the
8
+ * hash changes: a filter, a tab or a page number written to the URL is a state of the same page.
9
+ *
10
+ * Framework-agnostic like the rest of the router hooks: it listens to `popstate` and to the `pushState` /
11
+ * `replaceState` event of `useLocation`, so it sees every router (Next, React Router, the History API) without
12
+ * being told which one is mounted. The first render is never a change; the baseline is the path at the moment the
13
+ * hook becomes `enabled`.
14
+ *
15
+ * @example
16
+ * const isMobile = useIsMobile();
17
+ * usePathnameChange(() => setOpen(false), { enabled: open && isMobile });
18
+ */
19
+
20
+ import { useEffect, useRef } from 'react';
21
+
22
+ import { NAVIGATE_EVENT, patchHistoryOnce } from './useLocation';
23
+
24
+ /** Called with the new path and the one before it. */
25
+ export type PathnameChangeHandler = (pathname: string, previous: string) => void;
26
+
27
+ /**
28
+ * Non-React core: calls `onChange` each time `window.location.pathname` differs from the last one seen, and returns
29
+ * the unsubscribe. Same path (a query or hash change, a repeated `replaceState`) never calls it. No-op on the server.
30
+ */
31
+ export function watchPathname(onChange: PathnameChangeHandler): () => void {
32
+ if (typeof window === 'undefined') return () => {};
33
+ patchHistoryOnce();
34
+ let last = window.location.pathname;
35
+ const check = () => {
36
+ const next = window.location.pathname;
37
+ if (next === last) return;
38
+ const previous = last;
39
+ last = next;
40
+ onChange(next, previous);
41
+ };
42
+ window.addEventListener('popstate', check);
43
+ window.addEventListener(NAVIGATE_EVENT, check);
44
+ return () => {
45
+ window.removeEventListener('popstate', check);
46
+ window.removeEventListener(NAVIGATE_EVENT, check);
47
+ };
48
+ }
49
+
50
+ export interface UsePathnameChangeOptions {
51
+ /** Subscribe only while true (for example while a sheet is open). Default: true. */
52
+ enabled?: boolean;
53
+ }
54
+
55
+ /** Runs `handler` after each change of the pathname while `enabled`; query and hash changes are ignored. */
56
+ export function usePathnameChange(handler: PathnameChangeHandler, { enabled = true }: UsePathnameChangeOptions = {}): void {
57
+ const handlerRef = useRef(handler);
58
+ handlerRef.current = handler;
59
+ useEffect(() => {
60
+ if (!enabled) return;
61
+ return watchPathname((pathname, previous) => handlerRef.current(pathname, previous));
62
+ }, [enabled]);
63
+ }