@tker-react/layout 0.2.12 → 0.2.13

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,28 @@ function consumeLayoutConfig() {
112
112
  navigateAdapter: _navigateAdapter
113
113
  };
114
114
  }
115
+ let _activePath = "";
116
+ const _pathParamsMap = /* @__PURE__ */ new Map();
117
+ let _activePathListeners = [];
118
+ function setActivePath(path, fullPath) {
119
+ _activePath = path;
120
+ if (fullPath && fullPath !== path) {
121
+ _pathParamsMap.set(path, fullPath);
122
+ }
123
+ _activePathListeners.forEach((l) => l());
124
+ }
125
+ function subscribeActivePath(listener) {
126
+ _activePathListeners.push(listener);
127
+ return () => {
128
+ _activePathListeners = _activePathListeners.filter((l) => l !== listener);
129
+ };
130
+ }
131
+ function getActivePathSnapshot() {
132
+ return _activePath;
133
+ }
134
+ function getFullPathByActivePath(path) {
135
+ return _pathParamsMap.get(path) || path;
136
+ }
115
137
 
116
138
  const LayoutContext = createContext(null);
117
139
  const LayoutInteractionContext = createContext(null);
@@ -135,11 +157,7 @@ const ActivePathContext = createContext("");
135
157
  function useActivePathContext() {
136
158
  return useContext(ActivePathContext);
137
159
  }
138
- function LayoutProvider({
139
- children,
140
- activePath = "",
141
- activeFullPath
142
- }) {
160
+ function LayoutProvider({ children }) {
143
161
  const config = consumeLayoutConfig();
144
162
  const navigateRef = useRef(null);
145
163
  navigateRef.current = config.navigateAdapter;
@@ -178,12 +196,7 @@ function LayoutProvider({
178
196
  menuDataRef.current = config.menus;
179
197
  const layoutModeRef = useRef(config.layoutMode);
180
198
  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]);
199
+ const activePath = useSyncExternalStore(subscribeActivePath, getActivePathSnapshot);
187
200
  const toggleMenuOpen = useCallback((path, forceOpen) => {
188
201
  setOpenKeys((prev) => {
189
202
  const next = new Set(prev);
@@ -214,7 +227,7 @@ function LayoutProvider({
214
227
  toggleMenuOpen(path);
215
228
  }
216
229
  } else {
217
- const fullPath = pathParamsMap.current.get(path) || path;
230
+ const fullPath = getFullPathByActivePath(path);
218
231
  if (window.location.pathname + window.location.search !== fullPath) {
219
232
  navigateRef.current?.(fullPath);
220
233
  }
@@ -231,7 +244,7 @@ function LayoutProvider({
231
244
  setCollapsedState(nextCollapsed);
232
245
  }, []);
233
246
  const getFullPath = useCallback(
234
- (path) => pathParamsMap.current.get(path) || path,
247
+ (path) => getFullPathByActivePath(path),
235
248
  []
236
249
  );
237
250
  const isConcretePage = useCallback(
@@ -609,8 +622,8 @@ const LayoutInner = memo(function LayoutInner2({ children }) {
609
622
  ] })
610
623
  ] });
611
624
  });
612
- function Layout({ children, ...props }) {
613
- return /* @__PURE__ */ jsx(LayoutProvider, { ...props, children: /* @__PURE__ */ jsx(LayoutInner, { children }) });
614
- }
625
+ const Layout = memo(function Layout2({ children }) {
626
+ return /* @__PURE__ */ jsx(LayoutProvider, { children: /* @__PURE__ */ jsx(LayoutInner, { children }) });
627
+ });
615
628
 
616
- export { Layout, LayoutProvider, setBreadcrumbAdapter, setCollapsedWidth, setExpandedWidth, setHomePath, setLayoutMode, setLogoAdapter, setMaxTabs, setMenuAdapter, setMenus, setNavigateAdapter, setTabAdapter, setToolbarAdapter, setUserAvatarAdapter };
629
+ 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.13",
4
4
  "private": false,
5
5
  "license": "MIT",
6
6
  "type": "module",