xladmin-react-router 0.1.9

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/README.md ADDED
@@ -0,0 +1,46 @@
1
+ <div align="center">
2
+ <a href="./README.md">
3
+ <img src="https://img.shields.io/badge/English-blue?style=for-the-badge" alt="English">
4
+ </a>
5
+ <a href="./docs/README.ru.md">
6
+ <img src="https://img.shields.io/badge/%D0%A0%D1%83%D1%81%D1%81%D0%BA%D0%B8%D0%B9-red?style=for-the-badge" alt="Russian">
7
+ </a>
8
+ </div>
9
+
10
+ # xladmin-react-router
11
+
12
+ `xladmin-react-router` provides the React Router adapter for `xladmin`.
13
+
14
+ ## Install
15
+
16
+ ```bash
17
+ npm install xladmin xladmin-react-router react-router-dom
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ ```tsx
23
+ import {Shell, createFetchXLAdminClient} from 'xladmin';
24
+ import {useReactRouterXLAdminRouter} from 'xladmin-react-router';
25
+
26
+ export function AdminShell({models, blocks}) {
27
+ const client = createFetchXLAdminClient({baseUrl: '/api/admin'});
28
+ const router = useReactRouterXLAdminRouter();
29
+
30
+ return (
31
+ <Shell client={client} models={models} blocks={blocks} basePath="/admin" locale="en" router={router}>
32
+ {/* your route page */}
33
+ </Shell>
34
+ );
35
+ }
36
+ ```
37
+
38
+ ## Exports
39
+
40
+ - `useReactRouterXLAdminRouter()`
41
+ - `createReactRouterXLAdminRouter(...)`
42
+
43
+ ## Docs
44
+
45
+ - [Russian README](./docs/README.ru.md)
46
+ - [xladmin package](../xladmin-core/README.md)
@@ -0,0 +1,13 @@
1
+ import { XLAdminRouter } from 'xladmin';
2
+
3
+ type ReactRouterXLAdminAdapter = {
4
+ pathname: string;
5
+ search: string;
6
+ push: (href: string) => void;
7
+ replace: (href: string) => void;
8
+ back: () => void;
9
+ };
10
+ declare function createReactRouterXLAdminRouter(adapter: ReactRouterXLAdminAdapter): XLAdminRouter;
11
+ declare function useReactRouterXLAdminRouter(): XLAdminRouter;
12
+
13
+ export { type ReactRouterXLAdminAdapter, createReactRouterXLAdminRouter, useReactRouterXLAdminRouter };
package/dist/index.js ADDED
@@ -0,0 +1,81 @@
1
+ "use client";
2
+
3
+ // src/index.tsx
4
+ import { useEffect, useRef } from "react";
5
+ import { useLocation, useNavigate } from "react-router-dom";
6
+ function createReactRouterXLAdminRouter(adapter) {
7
+ return {
8
+ getLocation: () => ({
9
+ pathname: adapter.pathname,
10
+ search: adapter.search
11
+ }),
12
+ subscribe: () => () => {
13
+ },
14
+ push: (href) => {
15
+ adapter.push(href);
16
+ },
17
+ replace: (href) => {
18
+ adapter.replace(href);
19
+ },
20
+ back: () => {
21
+ adapter.back();
22
+ }
23
+ };
24
+ }
25
+ function useReactRouterXLAdminRouter() {
26
+ const location = useLocation();
27
+ const navigate = useNavigate();
28
+ const listenersRef = useRef(/* @__PURE__ */ new Set());
29
+ const locationRef = useRef({
30
+ pathname: location.pathname,
31
+ search: location.search
32
+ });
33
+ const previousLocationRef = useRef(locationRef.current);
34
+ const navigateRef = useRef(navigate);
35
+ const routerRef = useRef(null);
36
+ locationRef.current = {
37
+ pathname: location.pathname,
38
+ search: location.search
39
+ };
40
+ navigateRef.current = navigate;
41
+ if (routerRef.current === null) {
42
+ routerRef.current = createReactRouterXLAdminRouter({
43
+ pathname: locationRef.current.pathname,
44
+ search: locationRef.current.search,
45
+ push: (href) => {
46
+ navigateRef.current(href);
47
+ },
48
+ replace: (href) => {
49
+ navigateRef.current(href, { replace: true });
50
+ },
51
+ back: () => {
52
+ navigateRef.current(-1);
53
+ }
54
+ });
55
+ routerRef.current.getLocation = () => ({
56
+ pathname: locationRef.current.pathname,
57
+ search: locationRef.current.search
58
+ });
59
+ routerRef.current.subscribe = (listener) => {
60
+ listenersRef.current.add(listener);
61
+ return () => {
62
+ listenersRef.current.delete(listener);
63
+ };
64
+ };
65
+ }
66
+ useEffect(() => {
67
+ const previousLocation = previousLocationRef.current;
68
+ if (previousLocation.pathname === locationRef.current.pathname && previousLocation.search === locationRef.current.search) {
69
+ return;
70
+ }
71
+ previousLocationRef.current = locationRef.current;
72
+ for (const listener of listenersRef.current) {
73
+ listener();
74
+ }
75
+ }, [location.pathname, location.search]);
76
+ return routerRef.current;
77
+ }
78
+ export {
79
+ createReactRouterXLAdminRouter,
80
+ useReactRouterXLAdminRouter
81
+ };
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "xladmin-react-router",
3
+ "version": "0.1.9",
4
+ "description": "React Router adapter for xladmin.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "main": "./dist/index.js",
8
+ "module": "./dist/index.js",
9
+ "types": "./dist/index.d.ts",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/Artasov/xladmin.git",
13
+ "directory": "xladmin-frontend/packages/xladmin-react-router"
14
+ },
15
+ "homepage": "https://github.com/Artasov/xladmin/tree/main/xladmin-frontend/packages/xladmin-react-router",
16
+ "bugs": {
17
+ "url": "https://github.com/Artasov/xladmin/issues"
18
+ },
19
+ "exports": {
20
+ ".": {
21
+ "types": "./dist/index.d.ts",
22
+ "import": "./dist/index.js",
23
+ "default": "./dist/index.js"
24
+ }
25
+ },
26
+ "files": [
27
+ "dist",
28
+ "README.md"
29
+ ],
30
+ "publishConfig": {
31
+ "access": "public"
32
+ },
33
+ "scripts": {
34
+ "build": "node ../../scripts/clean.mjs && tsup src/index.tsx --format esm --dts",
35
+ "check": "tsc --noEmit -p tsconfig.json",
36
+ "test": "vitest run --environment node --pool threads"
37
+ },
38
+ "peerDependencies": {
39
+ "react": ">=19.0.0 <20.0.0",
40
+ "react-dom": ">=19.0.0 <20.0.0",
41
+ "react-router-dom": ">=6.4.0 <8.0.0",
42
+ "xladmin": "^0.1.9"
43
+ }
44
+ }