@tker-react/layout 0.2.12 → 0.2.14

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/dist/index.d.mts CHANGED
@@ -1,12 +1,11 @@
1
- import * as react_jsx_runtime from 'react/jsx-runtime';
1
+ import * as react from 'react';
2
2
  import { ReactNode, ComponentType } from 'react';
3
+ import * as react_jsx_runtime from 'react/jsx-runtime';
3
4
 
4
5
  interface LayoutProps {
5
6
  children?: ReactNode;
6
- activePath?: string;
7
- activeFullPath?: string;
8
7
  }
9
- declare function Layout({ children, ...props }: LayoutProps): react_jsx_runtime.JSX.Element;
8
+ declare const Layout: react.MemoExoticComponent<({ children }: LayoutProps) => react_jsx_runtime.JSX.Element>;
10
9
 
11
10
  interface BreadcrumbItem {
12
11
  path: string;
@@ -74,10 +73,8 @@ interface LayoutAdapters {
74
73
 
75
74
  interface LayoutProviderProps {
76
75
  children?: ReactNode;
77
- activePath?: string;
78
- activeFullPath?: string;
79
76
  }
80
- declare function LayoutProvider({ children, activePath, activeFullPath, }: LayoutProviderProps): react_jsx_runtime.JSX.Element;
77
+ declare function LayoutProvider({ children }: LayoutProviderProps): react_jsx_runtime.JSX.Element;
81
78
 
82
79
  declare function setMenus(data: MenuItem[]): void;
83
80
  declare function setMenuAdapter(c: ComponentType<MenuAdapterProps>): void;
@@ -92,6 +89,7 @@ declare function setLayoutMode(mode: "side-menu" | "top-menu"): void;
92
89
  declare function setExpandedWidth(width: string): void;
93
90
  declare function setCollapsedWidth(width: string): void;
94
91
  declare function setNavigateAdapter(fn: (path: string) => void): void;
92
+ declare function setActivePath(path: string, fullPath?: string): void;
95
93
 
96
- export { Layout, LayoutProvider, setBreadcrumbAdapter, setCollapsedWidth, setExpandedWidth, setHomePath, setLayoutMode, setLogoAdapter, setMaxTabs, setMenuAdapter, setMenus, setNavigateAdapter, setTabAdapter, setToolbarAdapter, setUserAvatarAdapter };
94
+ export { Layout, LayoutProvider, setActivePath, setBreadcrumbAdapter, setCollapsedWidth, setExpandedWidth, setHomePath, setLayoutMode, setLogoAdapter, setMaxTabs, setMenuAdapter, setMenus, setNavigateAdapter, setTabAdapter, setToolbarAdapter, setUserAvatarAdapter };
97
95
  export type { BreadcrumbAdapterProps, BreadcrumbItem, LayoutAdapters, LogoAdapterProps, MenuAdapterProps, MenuItem, TabAdapterProps, TabItem, UserAvatarAdapterProps };
package/dist/index.d.ts CHANGED
@@ -1,12 +1,11 @@
1
- import * as react_jsx_runtime from 'react/jsx-runtime';
1
+ import * as react from 'react';
2
2
  import { ReactNode, ComponentType } from 'react';
3
+ import * as react_jsx_runtime from 'react/jsx-runtime';
3
4
 
4
5
  interface LayoutProps {
5
6
  children?: ReactNode;
6
- activePath?: string;
7
- activeFullPath?: string;
8
7
  }
9
- declare function Layout({ children, ...props }: LayoutProps): react_jsx_runtime.JSX.Element;
8
+ declare const Layout: react.MemoExoticComponent<({ children }: LayoutProps) => react_jsx_runtime.JSX.Element>;
10
9
 
11
10
  interface BreadcrumbItem {
12
11
  path: string;
@@ -74,10 +73,8 @@ interface LayoutAdapters {
74
73
 
75
74
  interface LayoutProviderProps {
76
75
  children?: ReactNode;
77
- activePath?: string;
78
- activeFullPath?: string;
79
76
  }
80
- declare function LayoutProvider({ children, activePath, activeFullPath, }: LayoutProviderProps): react_jsx_runtime.JSX.Element;
77
+ declare function LayoutProvider({ children }: LayoutProviderProps): react_jsx_runtime.JSX.Element;
81
78
 
82
79
  declare function setMenus(data: MenuItem[]): void;
83
80
  declare function setMenuAdapter(c: ComponentType<MenuAdapterProps>): void;
@@ -92,6 +89,7 @@ declare function setLayoutMode(mode: "side-menu" | "top-menu"): void;
92
89
  declare function setExpandedWidth(width: string): void;
93
90
  declare function setCollapsedWidth(width: string): void;
94
91
  declare function setNavigateAdapter(fn: (path: string) => void): void;
92
+ declare function setActivePath(path: string, fullPath?: string): void;
95
93
 
96
- export { Layout, LayoutProvider, setBreadcrumbAdapter, setCollapsedWidth, setExpandedWidth, setHomePath, setLayoutMode, setLogoAdapter, setMaxTabs, setMenuAdapter, setMenus, setNavigateAdapter, setTabAdapter, setToolbarAdapter, setUserAvatarAdapter };
94
+ export { Layout, LayoutProvider, setActivePath, setBreadcrumbAdapter, setCollapsedWidth, setExpandedWidth, setHomePath, setLayoutMode, setLogoAdapter, setMaxTabs, setMenuAdapter, setMenus, setNavigateAdapter, setTabAdapter, setToolbarAdapter, setUserAvatarAdapter };
97
95
  export type { BreadcrumbAdapterProps, BreadcrumbItem, LayoutAdapters, LogoAdapterProps, MenuAdapterProps, MenuItem, TabAdapterProps, TabItem, UserAvatarAdapterProps };
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import "./layout.css";
2
2
  import { jsx, jsxs } from 'react/jsx-runtime';
3
- import { useRef, useCallback, useMemo, useState, useEffect, createContext, useContext, memo } from 'react';
3
+ import { useRef, useCallback, useMemo, useState, useSyncExternalStore, createContext, useContext, memo, useEffect } from 'react';
4
4
 
5
5
  function findMenuItemByPath(menuData, path) {
6
6
  for (const item of menuData) {
@@ -112,6 +112,29 @@ function consumeLayoutConfig() {
112
112
  navigateAdapter: _navigateAdapter
113
113
  };
114
114
  }
115
+ let _activePath = typeof window !== "undefined" ? window.location.pathname : "";
116
+ const _pathParamsMap = /* @__PURE__ */ new Map();
117
+ let _activePathListeners = [];
118
+ function setActivePath(path, fullPath) {
119
+ if (fullPath && fullPath !== path) {
120
+ _pathParamsMap.set(path, fullPath);
121
+ }
122
+ if (path === _activePath) return;
123
+ _activePath = path;
124
+ _activePathListeners.forEach((l) => l());
125
+ }
126
+ function subscribeActivePath(listener) {
127
+ _activePathListeners.push(listener);
128
+ return () => {
129
+ _activePathListeners = _activePathListeners.filter((l) => l !== listener);
130
+ };
131
+ }
132
+ function getActivePathSnapshot() {
133
+ return _activePath;
134
+ }
135
+ function getFullPathByActivePath(path) {
136
+ return _pathParamsMap.get(path) || path;
137
+ }
115
138
 
116
139
  const LayoutContext = createContext(null);
117
140
  const LayoutInteractionContext = createContext(null);
@@ -135,11 +158,7 @@ const ActivePathContext = createContext("");
135
158
  function useActivePathContext() {
136
159
  return useContext(ActivePathContext);
137
160
  }
138
- function LayoutProvider({
139
- children,
140
- activePath = "",
141
- activeFullPath
142
- }) {
161
+ function LayoutProvider({ children }) {
143
162
  const config = consumeLayoutConfig();
144
163
  const navigateRef = useRef(null);
145
164
  navigateRef.current = config.navigateAdapter;
@@ -178,12 +197,7 @@ function LayoutProvider({
178
197
  menuDataRef.current = config.menus;
179
198
  const layoutModeRef = useRef(config.layoutMode);
180
199
  layoutModeRef.current = config.layoutMode;
181
- const pathParamsMap = useRef(/* @__PURE__ */ new Map());
182
- useEffect(() => {
183
- if (activeFullPath && activeFullPath !== activePath) {
184
- pathParamsMap.current.set(activePath, activeFullPath);
185
- }
186
- }, [activePath, activeFullPath]);
200
+ const activePath = useSyncExternalStore(subscribeActivePath, getActivePathSnapshot);
187
201
  const toggleMenuOpen = useCallback((path, forceOpen) => {
188
202
  setOpenKeys((prev) => {
189
203
  const next = new Set(prev);
@@ -214,7 +228,7 @@ function LayoutProvider({
214
228
  toggleMenuOpen(path);
215
229
  }
216
230
  } else {
217
- const fullPath = pathParamsMap.current.get(path) || path;
231
+ const fullPath = getFullPathByActivePath(path);
218
232
  if (window.location.pathname + window.location.search !== fullPath) {
219
233
  navigateRef.current?.(fullPath);
220
234
  }
@@ -231,7 +245,7 @@ function LayoutProvider({
231
245
  setCollapsedState(nextCollapsed);
232
246
  }, []);
233
247
  const getFullPath = useCallback(
234
- (path) => pathParamsMap.current.get(path) || path,
248
+ (path) => getFullPathByActivePath(path),
235
249
  []
236
250
  );
237
251
  const isConcretePage = useCallback(
@@ -609,8 +623,8 @@ const LayoutInner = memo(function LayoutInner2({ children }) {
609
623
  ] })
610
624
  ] });
611
625
  });
612
- function Layout({ children, ...props }) {
613
- return /* @__PURE__ */ jsx(LayoutProvider, { ...props, children: /* @__PURE__ */ jsx(LayoutInner, { children }) });
614
- }
626
+ const Layout = memo(function Layout2({ children }) {
627
+ return /* @__PURE__ */ jsx(LayoutProvider, { children: /* @__PURE__ */ jsx(LayoutInner, { children }) });
628
+ });
615
629
 
616
- export { Layout, LayoutProvider, setBreadcrumbAdapter, setCollapsedWidth, setExpandedWidth, setHomePath, setLayoutMode, setLogoAdapter, setMaxTabs, setMenuAdapter, setMenus, setNavigateAdapter, setTabAdapter, setToolbarAdapter, setUserAvatarAdapter };
630
+ export { Layout, LayoutProvider, setActivePath, setBreadcrumbAdapter, setCollapsedWidth, setExpandedWidth, setHomePath, setLayoutMode, setLogoAdapter, setMaxTabs, setMenuAdapter, setMenus, setNavigateAdapter, setTabAdapter, setToolbarAdapter, setUserAvatarAdapter };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tker-react/layout",
3
- "version": "0.2.12",
3
+ "version": "0.2.14",
4
4
  "private": false,
5
5
  "license": "MIT",
6
6
  "type": "module",