add AppHeader
This commit is contained in:
parent
1161dad904
commit
4935e3deca
53
README.md
53
README.md
@ -846,3 +846,56 @@ function UsersTable({ data, columns }) {
|
||||
`Sidebar` compares `currentPath` with each item's `path` and `aliases`, ignoring query strings, hashes, and trailing slashes. Use the actual application route as `path`; use `aliases` only for alternate routes that should keep the same item active.
|
||||
|
||||
Active sidebar items are rendered as non-links to avoid redundant same-route navigations. This is especially useful on pages that synchronize URL query params.
|
||||
|
||||
---
|
||||
|
||||
# Remote AppHeader (self-contained)
|
||||
|
||||
`AppHeader` یک remote مستقل است و برای فایل remote خودش از `setupUikitRemoteDeps` یا
|
||||
`window.__UIKIT_REMOTE_DEPS__` استفاده نمیکند.
|
||||
|
||||
```jsx
|
||||
import { AppHeader } from "uikit/remote/ui";
|
||||
|
||||
function ExampleAppHeader() {
|
||||
return (
|
||||
<AppHeader>
|
||||
<AppHeader.BrandSection>
|
||||
<AppHeader.Logo src="/logo.png" />
|
||||
|
||||
<AppHeader.Brand>
|
||||
<AppHeader.Title>عنوان سامانه</AppHeader.Title>
|
||||
<AppHeader.Description>توضیحات سامانه</AppHeader.Description>
|
||||
</AppHeader.Brand>
|
||||
</AppHeader.BrandSection>
|
||||
|
||||
<AppHeader.Bi />
|
||||
</AppHeader>
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
`AppHeader` در flow عادی صفحه باقی میماند و عرض کامل میگیرد؛ بنابراین اگر داخل `VStack`/`Stack` قرار بگیرد، محتوای بعدی زیر Header قرار نمیگیرد.
|
||||
|
||||
فایل remote از این آدرس بارگذاری میشود:
|
||||
|
||||
```text
|
||||
https://uikit.d.aiengines.ir/AppHeader.js
|
||||
```
|
||||
|
||||
برای `AppHeader.js` هیچ dependency از host تزریق نمیشود. خود remote به صورت Web Component
|
||||
ایزوله با Shadow DOM اجرا میشود. تنها `font-family` از سامانه مقصد inherit میشود تا AppHeader
|
||||
از همان فونت برنامه استفاده کند؛ سایر استایلها همچنان ایزوله هستند. مسیر React wrapper فقط
|
||||
`react` خود برنامه را برای mount کردن custom element استفاده میکند و هیچ وابستگی به Chakra ندارد.
|
||||
|
||||
|
||||
## AppHeader local preview
|
||||
|
||||
برای دیدن `remote/ui/AppHeader` قبل از انتشار:
|
||||
|
||||
```bash
|
||||
yarn install
|
||||
yarn dev
|
||||
```
|
||||
|
||||
سپس آدرس Vite که در ترمینال نمایش داده میشود (معمولاً `http://localhost:5173`) را باز کنید. در حالت dev، همان سورس `src/remote/ui/AppHeader/AppHeader.remote.jsx` بهصورت remote لوکال لود میشود.
|
||||
|
||||
113
dist/chunks/index-C31G86RF.js
vendored
Normal file
113
dist/chunks/index-C31G86RF.js
vendored
Normal file
@ -0,0 +1,113 @@
|
||||
import { jsx as A } from "react/jsx-runtime";
|
||||
import m, { useRef as d, useEffect as g } from "react";
|
||||
const S = {
|
||||
GBadge: "https://uikit.d.aiengines.ir/GBadge.js",
|
||||
Pagination: "https://uikit.d.aiengines.ir/Pagination.js",
|
||||
AppHeader: "https://uikit.d.aiengines.ir/AppHeader.js"
|
||||
}, L = "__UIKIT_REMOTE_DEPS__", u = /* @__PURE__ */ new Map();
|
||||
function _(e, t) {
|
||||
const r = `${e}:${t}`;
|
||||
return u.has(r) || u.set(
|
||||
r,
|
||||
import(
|
||||
/* webpackIgnore: true */
|
||||
/* @vite-ignore */
|
||||
t
|
||||
).catch((s) => {
|
||||
throw u.delete(r), s;
|
||||
})
|
||||
), u.get(r);
|
||||
}
|
||||
function b({
|
||||
componentName: e,
|
||||
tagName: t,
|
||||
defaultRemoteUrl: r,
|
||||
normalizeProps: s
|
||||
}) {
|
||||
return function(i) {
|
||||
const p = d(null), o = d(null), f = d(null);
|
||||
return f.current = s ? s(i) : i, g(() => {
|
||||
let E = !0;
|
||||
return _(e, r).then(() => {
|
||||
if (!E || !p.current) return;
|
||||
const a = document.createElement(t);
|
||||
a.componentProps = f.current, p.current.appendChild(a), o.current = a;
|
||||
}).catch((a) => {
|
||||
console.error(
|
||||
`[uikit/remote/ui] Failed to load ${e}:`,
|
||||
a
|
||||
);
|
||||
}), () => {
|
||||
E = !1, o.current && (o.current.remove(), o.current = null);
|
||||
};
|
||||
}, [e, r, t]), g(() => {
|
||||
o.current && (o.current.componentProps = f.current);
|
||||
}), /* @__PURE__ */ A(
|
||||
"span",
|
||||
{
|
||||
ref: p,
|
||||
style: {
|
||||
display: "contents"
|
||||
}
|
||||
}
|
||||
);
|
||||
};
|
||||
}
|
||||
const R = Symbol.for("uikit.remote.app-header.slot");
|
||||
function c(e) {
|
||||
function t() {
|
||||
return null;
|
||||
}
|
||||
return t[R] = e, t;
|
||||
}
|
||||
const H = c("brandSection"), P = c("logo"), y = c("brand"), T = c("title"), h = c("description"), B = c("bi");
|
||||
function l(e) {
|
||||
var t;
|
||||
if (!(e == null || typeof e == "boolean")) {
|
||||
if (typeof e == "string" || typeof e == "number")
|
||||
return String(e);
|
||||
if (Array.isArray(e))
|
||||
return e.map(l).filter(Boolean).join("");
|
||||
if (m.isValidElement(e))
|
||||
return l((t = e.props) == null ? void 0 : t.children);
|
||||
}
|
||||
}
|
||||
function C(e) {
|
||||
const t = {
|
||||
showBi: !1
|
||||
};
|
||||
function r(s) {
|
||||
m.Children.forEach(s, (n) => {
|
||||
var p, o;
|
||||
if (!m.isValidElement(n)) return;
|
||||
const i = (p = n.type) == null ? void 0 : p[R];
|
||||
i === "logo" ? (t.logoSrc = n.props.src, t.logoAlt = n.props.alt) : i === "title" ? t.title = l(n.props.children) : i === "description" ? t.description = l(n.props.children) : i === "bi" && (t.showBi = !0, t.biLogoSrc = n.props.logoSrc || n.props.src, t.biLogoAlt = n.props.alt, t.biText = n.props.text), (o = n.props) != null && o.children && r(n.props.children);
|
||||
});
|
||||
}
|
||||
return r(e), t;
|
||||
}
|
||||
const j = b({
|
||||
componentName: "AppHeader",
|
||||
tagName: "uikit-app-header",
|
||||
defaultRemoteUrl: S.AppHeader,
|
||||
normalizeProps: ({ children: e, ...t }) => e == null ? t : {
|
||||
...t,
|
||||
...C(e)
|
||||
}
|
||||
});
|
||||
function k(e) {
|
||||
return /* @__PURE__ */ A(j, { ...e });
|
||||
}
|
||||
const O = Object.assign(k, {
|
||||
BrandSection: H,
|
||||
Logo: P,
|
||||
Brand: y,
|
||||
Title: T,
|
||||
Description: h,
|
||||
Bi: B
|
||||
});
|
||||
export {
|
||||
O as A,
|
||||
S as D,
|
||||
L as R
|
||||
};
|
||||
502
dist/layout.js
vendored
502
dist/layout.js
vendored
File diff suppressed because one or more lines are too long
36
dist/remote-ui-app-header.d.ts
vendored
Normal file
36
dist/remote-ui-app-header.d.ts
vendored
Normal file
@ -0,0 +1,36 @@
|
||||
import type { ReactNode } from "react";
|
||||
|
||||
export type RemoteAppHeaderProps = {
|
||||
logoSrc?: string;
|
||||
logoAlt?: string;
|
||||
title?: ReactNode;
|
||||
description?: ReactNode;
|
||||
biLogoSrc?: string;
|
||||
biLogoAlt?: string;
|
||||
biText?: string;
|
||||
showBi?: boolean;
|
||||
backgroundColor?: string;
|
||||
dir?: "rtl" | "ltr" | "auto";
|
||||
children?: ReactNode;
|
||||
};
|
||||
|
||||
type AppHeaderSlotProps = {
|
||||
children?: ReactNode;
|
||||
[key: string]: any;
|
||||
};
|
||||
|
||||
export const AppHeader: ((props: RemoteAppHeaderProps) => any) & {
|
||||
BrandSection: (props: AppHeaderSlotProps) => any;
|
||||
Logo: (props: AppHeaderSlotProps & { src?: string; alt?: string }) => any;
|
||||
Brand: (props: AppHeaderSlotProps) => any;
|
||||
Title: (props: AppHeaderSlotProps) => any;
|
||||
Description: (props: AppHeaderSlotProps) => any;
|
||||
Bi: (
|
||||
props: AppHeaderSlotProps & {
|
||||
logoSrc?: string;
|
||||
src?: string;
|
||||
alt?: string;
|
||||
text?: string;
|
||||
},
|
||||
) => any;
|
||||
};
|
||||
6
dist/remote-ui-app-header.js
vendored
Normal file
6
dist/remote-ui-app-header.js
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
import "react/jsx-runtime";
|
||||
import "react";
|
||||
import { A as m } from "./chunks/index-C31G86RF.js";
|
||||
export {
|
||||
m as AppHeader
|
||||
};
|
||||
8
dist/remote-ui.d.ts
vendored
8
dist/remote-ui.d.ts
vendored
@ -22,3 +22,11 @@ export type {
|
||||
PaginationLevel,
|
||||
PaginationProps,
|
||||
} from "./Pagination";
|
||||
|
||||
export {
|
||||
AppHeader,
|
||||
} from "./AppHeader";
|
||||
|
||||
export type {
|
||||
RemoteAppHeaderProps,
|
||||
} from "./AppHeader";
|
||||
|
||||
93
dist/remote-ui.js
vendored
93
dist/remote-ui.js
vendored
@ -1,23 +1,21 @@
|
||||
import B, { useRef as d, useEffect as g } from "react";
|
||||
import g, { useRef as d, useEffect as h } from "react";
|
||||
import { createRoot as k } from "react-dom/client";
|
||||
import { Text as v, Input as w, IconButton as E, HStack as y, Button as I, Badge as C, ChakraProvider as T } from "@chakra-ui/react";
|
||||
import { jsx as _ } from "react/jsx-runtime";
|
||||
const h = {
|
||||
GBadge: "https://uikit.d.aiengines.ir/GBadge.js",
|
||||
Pagination: "https://uikit.d.aiengines.ir/Pagination.js"
|
||||
}, s = "__UIKIT_REMOTE_DEPS__";
|
||||
function D(e = {}) {
|
||||
import { Text as v, Input as w, IconButton as y, HStack as E, Button as C, Badge as I, ChakraProvider as x } from "@chakra-ui/react";
|
||||
import { R as f, D as B } from "./chunks/index-C31G86RF.js";
|
||||
import { A as z } from "./chunks/index-C31G86RF.js";
|
||||
import { jsx as D } from "react/jsx-runtime";
|
||||
function T(e = {}) {
|
||||
if (typeof window > "u") return;
|
||||
const t = window[s] || {};
|
||||
window[s] = {
|
||||
const t = window[f] || {};
|
||||
window[f] = {
|
||||
...t,
|
||||
React: e.React || t.React || B,
|
||||
React: e.React || t.React || g,
|
||||
createRoot: e.createRoot || t.createRoot || k,
|
||||
ChakraProvider: e.ChakraProvider || t.ChakraProvider || T,
|
||||
Badge: e.Badge || t.Badge || C,
|
||||
Button: e.Button || t.Button || I,
|
||||
HStack: e.HStack || t.HStack || y,
|
||||
IconButton: e.IconButton || t.IconButton || E,
|
||||
ChakraProvider: e.ChakraProvider || t.ChakraProvider || x,
|
||||
Badge: e.Badge || t.Badge || I,
|
||||
Button: e.Button || t.Button || C,
|
||||
HStack: e.HStack || t.HStack || E,
|
||||
IconButton: e.IconButton || t.IconButton || y,
|
||||
Input: e.Input || t.Input || w,
|
||||
Text: e.Text || t.Text || v,
|
||||
...e.deps || {},
|
||||
@ -29,15 +27,15 @@ function D(e = {}) {
|
||||
};
|
||||
}
|
||||
const a = /* @__PURE__ */ new Map();
|
||||
function x(e = []) {
|
||||
function S(e = []) {
|
||||
if (typeof window > "u") return !1;
|
||||
const t = window[s];
|
||||
const t = window[f];
|
||||
return e.every((r) => t == null ? void 0 : t[r]);
|
||||
}
|
||||
function S(e = []) {
|
||||
typeof window > "u" || x(e) || D();
|
||||
function H(e = []) {
|
||||
typeof window > "u" || S(e) || T();
|
||||
}
|
||||
function G(e, t) {
|
||||
function b(e, t) {
|
||||
const r = `${e}:${t}`;
|
||||
return a.has(r) || a.set(
|
||||
r,
|
||||
@ -55,24 +53,24 @@ function P({
|
||||
tagName: t,
|
||||
defaultRemoteUrl: r,
|
||||
requiredDeps: u,
|
||||
normalizeProps: f
|
||||
normalizeProps: m
|
||||
}) {
|
||||
return function({
|
||||
theme: m,
|
||||
chakraProviderProps: l,
|
||||
...p
|
||||
theme: s,
|
||||
chakraProviderProps: p,
|
||||
...l
|
||||
}) {
|
||||
const i = d(null), n = d(null), c = d(null);
|
||||
return c.current = {
|
||||
...f ? f(p) : p,
|
||||
...m !== void 0 ? { theme: m } : {},
|
||||
...l ? { chakraProviderProps: l } : {}
|
||||
}, g(() => {
|
||||
const c = d(null), n = d(null), i = d(null);
|
||||
return i.current = {
|
||||
...m ? m(l) : l,
|
||||
...s !== void 0 ? { theme: s } : {},
|
||||
...p ? { chakraProviderProps: p } : {}
|
||||
}, h(() => {
|
||||
let R = !0;
|
||||
return S(u), G(e, r).then(() => {
|
||||
if (!R || !i.current) return;
|
||||
return H(u), b(e, r).then(() => {
|
||||
if (!R || !c.current) return;
|
||||
const o = document.createElement(t);
|
||||
o.componentProps = c.current, i.current.appendChild(o), n.current = o;
|
||||
o.componentProps = i.current, c.current.appendChild(o), n.current = o;
|
||||
}).catch((o) => {
|
||||
console.error(
|
||||
`[uikit/remote/ui] Failed to load ${e}:`,
|
||||
@ -81,12 +79,12 @@ function P({
|
||||
}), () => {
|
||||
R = !1, n.current && (n.current.remove(), n.current = null);
|
||||
};
|
||||
}, [e, r, t]), g(() => {
|
||||
n.current && (n.current.componentProps = c.current);
|
||||
}), /* @__PURE__ */ _(
|
||||
}, [e, r, t]), h(() => {
|
||||
n.current && (n.current.componentProps = i.current);
|
||||
}), /* @__PURE__ */ D(
|
||||
"span",
|
||||
{
|
||||
ref: i,
|
||||
ref: c,
|
||||
style: {
|
||||
display: "contents"
|
||||
}
|
||||
@ -94,26 +92,26 @@ function P({
|
||||
);
|
||||
};
|
||||
}
|
||||
function b(e) {
|
||||
function G(e) {
|
||||
if (typeof e == "string" || typeof e == "number")
|
||||
return e;
|
||||
e != null && console.warn(
|
||||
"[uikit/remote/ui] GBadge only accepts text/number children across the remote boundary."
|
||||
);
|
||||
}
|
||||
const q = P({
|
||||
const $ = P({
|
||||
componentName: "GBadge",
|
||||
tagName: "uikit-g-badge",
|
||||
defaultRemoteUrl: h.GBadge,
|
||||
defaultRemoteUrl: B.GBadge,
|
||||
requiredDeps: ["React", "createRoot", "ChakraProvider", "Badge"],
|
||||
normalizeProps: ({ children: e, ...t }) => ({
|
||||
...t,
|
||||
children: b(e)
|
||||
children: G(e)
|
||||
})
|
||||
}), K = P({
|
||||
}), j = P({
|
||||
componentName: "Pagination",
|
||||
tagName: "uikit-pagination",
|
||||
defaultRemoteUrl: h.Pagination,
|
||||
defaultRemoteUrl: B.Pagination,
|
||||
requiredDeps: [
|
||||
"React",
|
||||
"createRoot",
|
||||
@ -126,7 +124,8 @@ const q = P({
|
||||
]
|
||||
});
|
||||
export {
|
||||
q as GBadge,
|
||||
K as Pagination,
|
||||
D as setupUikitRemoteDeps
|
||||
z as AppHeader,
|
||||
$ as GBadge,
|
||||
j as Pagination,
|
||||
T as setupUikitRemoteDeps
|
||||
};
|
||||
|
||||
190
dist/remote/AppHeader.js
vendored
Normal file
190
dist/remote/AppHeader.js
vendored
Normal file
File diff suppressed because one or more lines are too long
@ -3,7 +3,7 @@
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>UI Kit Header Preview</title>
|
||||
<title>UI Kit AppHeader Preview</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
|
||||
@ -29,6 +29,10 @@
|
||||
"./remote/ui": {
|
||||
"types": "./dist/remote-ui.d.ts",
|
||||
"import": "./dist/remote-ui.js"
|
||||
},
|
||||
"./remote/ui/AppHeader": {
|
||||
"types": "./dist/remote-ui-app-header.d.ts",
|
||||
"import": "./dist/remote-ui-app-header.js"
|
||||
}
|
||||
},
|
||||
"sideEffects": false,
|
||||
@ -41,12 +45,13 @@
|
||||
"build": "yarn build:lib && yarn build:remote && yarn copy-types",
|
||||
"build:lib": "vite build",
|
||||
"build:remote": "vite build --mode remote",
|
||||
"copy-types": "yarn copy-index-types && yarn copy-utils-types && yarn copy-core-types && yarn copy-layout-types && yarn copy-remote-ui-types",
|
||||
"copy-types": "yarn copy-index-types && yarn copy-utils-types && yarn copy-core-types && yarn copy-layout-types && yarn copy-remote-ui-types && yarn copy-remote-ui-app-header-types",
|
||||
"copy-index-types": "cp src/index.d.ts dist/index.d.ts",
|
||||
"copy-utils-types": "cp src/utils/index.d.ts dist/utils.d.ts",
|
||||
"copy-core-types": "cp src/core/index.d.ts dist/core.d.ts",
|
||||
"copy-remote-ui-types": "cp src/remote/ui/index.d.ts dist/remote-ui.d.ts",
|
||||
"copy-layout-types": "cp src/layout/index.d.ts dist/layout.d.ts"
|
||||
"copy-layout-types": "cp src/layout/index.d.ts dist/layout.d.ts",
|
||||
"copy-remote-ui-app-header-types": "cp src/remote/ui/AppHeader/index.d.ts dist/remote-ui-app-header.d.ts"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@chakra-ui/react": "^2.0.0",
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 4.2 KiB After Width: | Height: | Size: 63 KiB |
75
src/dev/AppHeaderPreview.jsx
Normal file
75
src/dev/AppHeaderPreview.jsx
Normal file
@ -0,0 +1,75 @@
|
||||
import {
|
||||
Box,
|
||||
ChakraProvider,
|
||||
Code,
|
||||
Container,
|
||||
Divider,
|
||||
Heading,
|
||||
Stack,
|
||||
Text,
|
||||
VStack,
|
||||
} from "@chakra-ui/react";
|
||||
import { AppHeader } from "../remote/ui/index.jsx";
|
||||
import demoLogo from "./demo-logo.svg";
|
||||
import biLogo from "../remote/ui/AppHeader/bi-logo.webp";
|
||||
|
||||
function ActualAppHeader() {
|
||||
return (
|
||||
<AppHeader>
|
||||
<AppHeader.BrandSection>
|
||||
<AppHeader.Logo src={demoLogo} />
|
||||
|
||||
<AppHeader.Brand>
|
||||
<AppHeader.Title>عنوان سامانه</AppHeader.Title>
|
||||
<AppHeader.Description>توضیحات سامانه</AppHeader.Description>
|
||||
</AppHeader.Brand>
|
||||
</AppHeader.BrandSection>
|
||||
|
||||
<AppHeader.Bi src={biLogo} />
|
||||
</AppHeader>
|
||||
);
|
||||
}
|
||||
|
||||
export default function AppHeaderPreview() {
|
||||
return (
|
||||
<ChakraProvider>
|
||||
<Box minH="100vh" bg="gray.50" dir="rtl">
|
||||
<VStack spacing={0} align="stretch">
|
||||
<ActualAppHeader />
|
||||
|
||||
<Box borderTop="3px solid" borderColor="red.400" />
|
||||
|
||||
<Container maxW="7xl" py={{ base: 5, lg: 8 }}>
|
||||
<Stack spacing={5}>
|
||||
<Box>
|
||||
<Heading size="md">AppHeader Preview</Heading>
|
||||
<Text mt={2} color="gray.600">
|
||||
این صفحه همان <Code>remote/ui/AppHeader</Code> را با سورس remote
|
||||
لوکال render میکند تا قبل از انتشار قابل بررسی باشد.
|
||||
</Text>
|
||||
</Box>
|
||||
|
||||
<Divider />
|
||||
|
||||
<Box p={5} bg="white" borderWidth="1px" borderRadius="lg">
|
||||
<Text fontWeight="bold">شروع محتوای صفحه</Text>
|
||||
<Text mt={2} color="gray.600">
|
||||
AppHeader و محتوا داخل یک VStack هستند. خط قرمز باید دقیقاً بعد از
|
||||
Header دیده شود و محتوا نباید زیر Header قرار بگیرد.
|
||||
</Text>
|
||||
</Box>
|
||||
|
||||
<Box p={5} bg="white" borderWidth="1px" borderRadius="lg">
|
||||
<Text fontWeight="bold">تست Responsive</Text>
|
||||
<Text mt={2} color="gray.600">
|
||||
عرض مرورگر را کم و زیاد کن تا حالت موبایل و دسکتاپ AppHeader را
|
||||
بررسی کنی.
|
||||
</Text>
|
||||
</Box>
|
||||
</Stack>
|
||||
</Container>
|
||||
</VStack>
|
||||
</Box>
|
||||
</ChakraProvider>
|
||||
);
|
||||
}
|
||||
@ -1,9 +1,14 @@
|
||||
import React from "react";
|
||||
import ReactDOM from "react-dom/client";
|
||||
import HeaderPreview from "./HeaderPreview.jsx";
|
||||
import { DEFAULT_REMOTE_URLS } from "../remote/ui/_core/constants.js";
|
||||
|
||||
// In dev, load the exact remote source from Vite instead of the deployed CDN file.
|
||||
DEFAULT_REMOTE_URLS.AppHeader = "/src/remote/ui/AppHeader/AppHeader.remote.jsx";
|
||||
|
||||
const { default: AppHeaderPreview } = await import("./AppHeaderPreview.jsx");
|
||||
|
||||
ReactDOM.createRoot(document.getElementById("root")).render(
|
||||
<React.StrictMode>
|
||||
<HeaderPreview />
|
||||
<AppHeaderPreview />
|
||||
</React.StrictMode>,
|
||||
);
|
||||
|
||||
254
src/remote/ui/AppHeader/AppHeader.remote.jsx
Normal file
254
src/remote/ui/AppHeader/AppHeader.remote.jsx
Normal file
File diff suppressed because one or more lines are too long
BIN
src/remote/ui/AppHeader/bi-logo.webp
Normal file
BIN
src/remote/ui/AppHeader/bi-logo.webp
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 63 KiB |
36
src/remote/ui/AppHeader/index.d.ts
vendored
Normal file
36
src/remote/ui/AppHeader/index.d.ts
vendored
Normal file
@ -0,0 +1,36 @@
|
||||
import type { ReactNode } from "react";
|
||||
|
||||
export type RemoteAppHeaderProps = {
|
||||
logoSrc?: string;
|
||||
logoAlt?: string;
|
||||
title?: ReactNode;
|
||||
description?: ReactNode;
|
||||
biLogoSrc?: string;
|
||||
biLogoAlt?: string;
|
||||
biText?: string;
|
||||
showBi?: boolean;
|
||||
backgroundColor?: string;
|
||||
dir?: "rtl" | "ltr" | "auto";
|
||||
children?: ReactNode;
|
||||
};
|
||||
|
||||
type AppHeaderSlotProps = {
|
||||
children?: ReactNode;
|
||||
[key: string]: any;
|
||||
};
|
||||
|
||||
export const AppHeader: ((props: RemoteAppHeaderProps) => any) & {
|
||||
BrandSection: (props: AppHeaderSlotProps) => any;
|
||||
Logo: (props: AppHeaderSlotProps & { src?: string; alt?: string }) => any;
|
||||
Brand: (props: AppHeaderSlotProps) => any;
|
||||
Title: (props: AppHeaderSlotProps) => any;
|
||||
Description: (props: AppHeaderSlotProps) => any;
|
||||
Bi: (
|
||||
props: AppHeaderSlotProps & {
|
||||
logoSrc?: string;
|
||||
src?: string;
|
||||
alt?: string;
|
||||
text?: string;
|
||||
},
|
||||
) => any;
|
||||
};
|
||||
108
src/remote/ui/AppHeader/index.jsx
Normal file
108
src/remote/ui/AppHeader/index.jsx
Normal file
@ -0,0 +1,108 @@
|
||||
"use client";
|
||||
|
||||
import React from "react";
|
||||
|
||||
import { DEFAULT_REMOTE_URLS } from "../_core/constants";
|
||||
import { createSelfContainedRemoteElementComponent } from "../_core/createSelfContainedRemoteElementComponent";
|
||||
|
||||
const APP_HEADER_SLOT = Symbol.for("uikit.remote.app-header.slot");
|
||||
|
||||
function createAppHeaderSlot(name) {
|
||||
function AppHeaderSlot() {
|
||||
return null;
|
||||
}
|
||||
|
||||
AppHeaderSlot[APP_HEADER_SLOT] = name;
|
||||
return AppHeaderSlot;
|
||||
}
|
||||
|
||||
const BrandSection = createAppHeaderSlot("brandSection");
|
||||
const Logo = createAppHeaderSlot("logo");
|
||||
const Brand = createAppHeaderSlot("brand");
|
||||
const Title = createAppHeaderSlot("title");
|
||||
const Description = createAppHeaderSlot("description");
|
||||
const Bi = createAppHeaderSlot("bi");
|
||||
|
||||
function toPlainText(value) {
|
||||
if (value === null || value === undefined || typeof value === "boolean") {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (typeof value === "string" || typeof value === "number") {
|
||||
return String(value);
|
||||
}
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
return value.map(toPlainText).filter(Boolean).join("");
|
||||
}
|
||||
|
||||
if (React.isValidElement(value)) {
|
||||
return toPlainText(value.props?.children);
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function extractCompoundAppHeader(children) {
|
||||
const result = {
|
||||
showBi: false,
|
||||
};
|
||||
|
||||
function visit(node) {
|
||||
React.Children.forEach(node, (child) => {
|
||||
if (!React.isValidElement(child)) return;
|
||||
|
||||
const slot = child.type?.[APP_HEADER_SLOT];
|
||||
|
||||
if (slot === "logo") {
|
||||
result.logoSrc = child.props.src;
|
||||
result.logoAlt = child.props.alt;
|
||||
} else if (slot === "title") {
|
||||
result.title = toPlainText(child.props.children);
|
||||
} else if (slot === "description") {
|
||||
result.description = toPlainText(child.props.children);
|
||||
} else if (slot === "bi") {
|
||||
result.showBi = true;
|
||||
result.biLogoSrc = child.props.logoSrc || child.props.src;
|
||||
result.biLogoAlt = child.props.alt;
|
||||
result.biText = child.props.text;
|
||||
}
|
||||
|
||||
if (child.props?.children) {
|
||||
visit(child.props.children);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
visit(children);
|
||||
return result;
|
||||
}
|
||||
|
||||
const RemoteAppHeaderElement = createSelfContainedRemoteElementComponent({
|
||||
componentName: "AppHeader",
|
||||
tagName: "uikit-app-header",
|
||||
defaultRemoteUrl: DEFAULT_REMOTE_URLS.AppHeader,
|
||||
normalizeProps: ({ children, ...props }) => {
|
||||
if (children === undefined || children === null) {
|
||||
return props;
|
||||
}
|
||||
|
||||
return {
|
||||
...props,
|
||||
...extractCompoundAppHeader(children),
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
function AppHeaderRoot(props) {
|
||||
return <RemoteAppHeaderElement {...props} />;
|
||||
}
|
||||
|
||||
export const AppHeader = Object.assign(AppHeaderRoot, {
|
||||
BrandSection,
|
||||
Logo,
|
||||
Brand,
|
||||
Title,
|
||||
Description,
|
||||
Bi,
|
||||
});
|
||||
@ -1,6 +1,7 @@
|
||||
export const DEFAULT_REMOTE_URLS = {
|
||||
GBadge: "https://uikit.d.aiengines.ir/GBadge.js",
|
||||
Pagination: "https://uikit.d.aiengines.ir/Pagination.js",
|
||||
AppHeader: "https://uikit.d.aiengines.ir/AppHeader.js",
|
||||
};
|
||||
|
||||
export const REMOTE_DEPS_KEY = "__UIKIT_REMOTE_DEPS__";
|
||||
|
||||
@ -0,0 +1,83 @@
|
||||
"use client";
|
||||
|
||||
import React, { useEffect, useRef } from "react";
|
||||
|
||||
const remotePromises = new Map();
|
||||
|
||||
function loadRemote(componentName, remoteFileUrl) {
|
||||
const cacheKey = `${componentName}:${remoteFileUrl}`;
|
||||
|
||||
if (!remotePromises.has(cacheKey)) {
|
||||
remotePromises.set(
|
||||
cacheKey,
|
||||
import(
|
||||
/* webpackIgnore: true */
|
||||
/* @vite-ignore */
|
||||
remoteFileUrl
|
||||
).catch((error) => {
|
||||
remotePromises.delete(cacheKey);
|
||||
throw error;
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
return remotePromises.get(cacheKey);
|
||||
}
|
||||
|
||||
export function createSelfContainedRemoteElementComponent({
|
||||
componentName,
|
||||
tagName,
|
||||
defaultRemoteUrl,
|
||||
normalizeProps,
|
||||
}) {
|
||||
return function SelfContainedRemoteElementComponent(props) {
|
||||
const containerRef = useRef(null);
|
||||
const elementRef = useRef(null);
|
||||
const latestPropsRef = useRef(null);
|
||||
|
||||
latestPropsRef.current = normalizeProps ? normalizeProps(props) : props;
|
||||
|
||||
useEffect(() => {
|
||||
let active = true;
|
||||
|
||||
loadRemote(componentName, defaultRemoteUrl)
|
||||
.then(() => {
|
||||
if (!active || !containerRef.current) return;
|
||||
|
||||
const element = document.createElement(tagName);
|
||||
element.componentProps = latestPropsRef.current;
|
||||
containerRef.current.appendChild(element);
|
||||
elementRef.current = element;
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(
|
||||
`[uikit/remote/ui] Failed to load ${componentName}:`,
|
||||
error,
|
||||
);
|
||||
});
|
||||
|
||||
return () => {
|
||||
active = false;
|
||||
|
||||
if (elementRef.current) {
|
||||
elementRef.current.remove();
|
||||
elementRef.current = null;
|
||||
}
|
||||
};
|
||||
}, [componentName, defaultRemoteUrl, tagName]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!elementRef.current) return;
|
||||
elementRef.current.componentProps = latestPropsRef.current;
|
||||
});
|
||||
|
||||
return (
|
||||
<span
|
||||
ref={containerRef}
|
||||
style={{
|
||||
display: "contents",
|
||||
}}
|
||||
/>
|
||||
);
|
||||
};
|
||||
}
|
||||
8
src/remote/ui/index.d.ts
vendored
8
src/remote/ui/index.d.ts
vendored
@ -22,3 +22,11 @@ export type {
|
||||
PaginationLevel,
|
||||
PaginationProps,
|
||||
} from "./Pagination";
|
||||
|
||||
export {
|
||||
AppHeader,
|
||||
} from "./AppHeader";
|
||||
|
||||
export type {
|
||||
RemoteAppHeaderProps,
|
||||
} from "./AppHeader";
|
||||
|
||||
@ -4,3 +4,4 @@ export { setupUikitRemoteDeps } from "./_core/setupRemoteDeps";
|
||||
|
||||
export { GBadge } from "./GBadge";
|
||||
export { Pagination } from "./Pagination";
|
||||
export { AppHeader } from "./AppHeader";
|
||||
|
||||
@ -44,6 +44,8 @@ export default defineConfig(({ mode }) => {
|
||||
|
||||
target: "es2020",
|
||||
|
||||
assetsInlineLimit: 8192,
|
||||
|
||||
minify: "esbuild",
|
||||
|
||||
sourcemap: false,
|
||||
@ -58,6 +60,10 @@ export default defineConfig(({ mode }) => {
|
||||
process.cwd(),
|
||||
"src/remote/ui/Pagination/Pagination.remote.jsx",
|
||||
),
|
||||
AppHeader: path.resolve(
|
||||
process.cwd(),
|
||||
"src/remote/ui/AppHeader/AppHeader.remote.jsx",
|
||||
),
|
||||
},
|
||||
|
||||
formats: ["es"],
|
||||
@ -103,6 +109,7 @@ export default defineConfig(({ mode }) => {
|
||||
icons: path.resolve(process.cwd(), "src/icons/index.js"),
|
||||
|
||||
"remote-ui": path.resolve(process.cwd(), "src/remote/ui/index.jsx"),
|
||||
"remote-ui-app-header": path.resolve(process.cwd(), "src/remote/ui/AppHeader/index.jsx"),
|
||||
},
|
||||
|
||||
formats: ["es"],
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user