optimize header-logo
This commit is contained in:
parent
97407cffc0
commit
4e322e3bd3
@ -882,7 +882,7 @@ function ExampleAppHeader() {
|
||||
return (
|
||||
<AppHeader bg="#314E89">
|
||||
<AppHeader.BrandSection>
|
||||
<AppHeader.Logo src="/logo.png" />
|
||||
<AppHeader.Logo src="/logo.png" optimize />
|
||||
|
||||
<AppHeader.Brand>
|
||||
<AppHeader.Title>عنوان سامانه</AppHeader.Title>
|
||||
@ -902,6 +902,10 @@ function ExampleAppHeader() {
|
||||
|
||||
`AppHeader` در flow عادی صفحه باقی میماند و عرض کامل میگیرد؛ بنابراین اگر داخل `VStack`/`Stack` قرار بگیرد، محتوای بعدی زیر Header قرار نمیگیرد.
|
||||
|
||||
برای مشاهده قابلیت روی دمو، لوگوی PNG نمونه با ابعاد 1254×1254 از مسیر `/logo.png` سرو میشود. دمو به طور پیشفرض `optimize` را فعال میکند؛ با دکمهٔ زیر هدر میتوان PNG اصلی و نسخه بهینهشده را مقایسه کرد و وضعیت WebP/Fallback را دید. در اجرای لوکال ممکن است سرویس تصاویر به فایل روی `localhost` دسترسی نداشته باشد.
|
||||
|
||||
`AppHeader.Logo` بهصورت پیشفرض URL را تغییر نمیدهد. اگر `optimize` را پاس بدهید، فقط در مرورگر URL نسبی (مثل `/logo.png`) با origin همان سامانه مقصد کامل میشود، سپس `processImage` با ابعاد نمایش لوگو، `q: 70` و `webp: true` صدا زده میشود. تغییر عرض نمایش (مثلاً breakpoint موبایل/دسکتاپ) باعث بروزرسانی تصویر میشود. اگر سرویس بهینهسازی خطا بدهد، لوگوی اصلی جایگزین میشود. این قابلیت به `domain` یا هیچ dependency اضافی در پروژه مقصد نیاز ندارد. برای استفاده از `optimize` باید نسخه npm/Git UI Kit حاوی این prop را در سامانه مقصد نصب کرد؛ اما سامانههای قبلی که `optimize` نمیدهند نیازی به آپدیت ندارند.
|
||||
|
||||
`bg` اختیاری است و رنگ پسزمینه پیشفرض Header را override میکند. `AppHeader.Extra` نیز اختیاری است و لوگو + متن دلخواه میگیرد. `AppHeader.Extra` و `AppHeader.Bi` مستقل از هم هستند و میتوان هرکدام را حذف کرد. اگر هر دو وجود داشته باشند، در انتهای Header ترتیب نمایشی `Extra` سپس `Bi` است.
|
||||
|
||||
فایل remote از این آدرس بارگذاری میشود:
|
||||
|
||||
BIN
deploy-demo/public/logo.png
Normal file
BIN
deploy-demo/public/logo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 49 KiB |
BIN
dist/logo.png
vendored
Normal file
BIN
dist/logo.png
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 49 KiB |
4
dist/remote-ui-app-header.d.ts
vendored
4
dist/remote-ui-app-header.d.ts
vendored
@ -3,6 +3,8 @@ import type { ReactNode } from "react";
|
||||
export type RemoteAppHeaderProps = {
|
||||
logoSrc?: string;
|
||||
logoAlt?: string;
|
||||
/** Request a resized WebP logo from the image service. Defaults to false. */
|
||||
logoOptimize?: boolean;
|
||||
title?: ReactNode;
|
||||
description?: ReactNode;
|
||||
biLogoSrc?: string;
|
||||
@ -27,7 +29,7 @@ type AppHeaderSlotProps = {
|
||||
|
||||
export const AppHeader: ((props: RemoteAppHeaderProps) => any) & {
|
||||
BrandSection: (props: AppHeaderSlotProps) => any;
|
||||
Logo: (props: AppHeaderSlotProps & { src?: string; alt?: string }) => any;
|
||||
Logo: (props: AppHeaderSlotProps & { src?: string; alt?: string; optimize?: boolean }) => any;
|
||||
Brand: (props: AppHeaderSlotProps) => any;
|
||||
Title: (props: AppHeaderSlotProps) => any;
|
||||
Description: (props: AppHeaderSlotProps) => any;
|
||||
|
||||
38
dist/remote-ui-app-header.js
vendored
38
dist/remote-ui-app-header.js
vendored
@ -1,5 +1,5 @@
|
||||
import { jsx as E } from "react/jsx-runtime";
|
||||
import d, { useRef as m, useEffect as A } from "react";
|
||||
import { jsx as A } from "react/jsx-runtime";
|
||||
import d, { useRef as m, useEffect as x } from "react";
|
||||
import { D as S } from "./chunks/constants-CRZ9QfxV.js";
|
||||
const l = /* @__PURE__ */ new Map();
|
||||
function R(t, e) {
|
||||
@ -21,12 +21,12 @@ function b({
|
||||
defaultRemoteUrl: o,
|
||||
normalizeProps: i
|
||||
}) {
|
||||
return function(s) {
|
||||
return function(p) {
|
||||
const c = m(null), n = m(null), f = m(null);
|
||||
return f.current = i ? i(s) : s, A(() => {
|
||||
let x = !0;
|
||||
return f.current = i ? i(p) : p, x(() => {
|
||||
let g = !0;
|
||||
return R(t, o).then(() => {
|
||||
if (!x || !c.current) return;
|
||||
if (!g || !c.current) return;
|
||||
const a = document.createElement(e);
|
||||
a.componentProps = f.current, c.current.appendChild(a), n.current = a;
|
||||
}).catch((a) => {
|
||||
@ -35,11 +35,11 @@ function b({
|
||||
a
|
||||
);
|
||||
}), () => {
|
||||
x = !1, n.current && (n.current.remove(), n.current = null);
|
||||
g = !1, n.current && (n.current.remove(), n.current = null);
|
||||
};
|
||||
}, [t, o, e]), A(() => {
|
||||
}, [t, o, e]), x(() => {
|
||||
n.current && (n.current.componentProps = f.current);
|
||||
}), /* @__PURE__ */ E(
|
||||
}), /* @__PURE__ */ A(
|
||||
"span",
|
||||
{
|
||||
ref: c,
|
||||
@ -50,14 +50,14 @@ function b({
|
||||
);
|
||||
};
|
||||
}
|
||||
const g = Symbol.for("uikit.remote.app-header.slot");
|
||||
function p(t) {
|
||||
const E = Symbol.for("uikit.remote.app-header.slot");
|
||||
function s(t) {
|
||||
function e() {
|
||||
return null;
|
||||
}
|
||||
return e[g] = t, e;
|
||||
return e[E] = t, e;
|
||||
}
|
||||
const y = p("brandSection"), H = p("logo"), L = p("brand"), C = p("title"), P = p("description"), T = p("bi"), w = p("extra");
|
||||
const y = s("brandSection"), H = s("logo"), L = s("brand"), C = s("title"), P = s("description"), T = s("bi"), w = s("extra");
|
||||
function u(t) {
|
||||
var e;
|
||||
if (!(t == null || typeof t == "boolean")) {
|
||||
@ -78,8 +78,8 @@ function B(t) {
|
||||
d.Children.forEach(i, (r) => {
|
||||
var c, n;
|
||||
if (!d.isValidElement(r)) return;
|
||||
const s = (c = r.type) == null ? void 0 : c[g];
|
||||
s === "logo" ? (e.logoSrc = r.props.src, e.logoAlt = r.props.alt) : s === "title" ? e.title = u(r.props.children) : s === "description" ? e.description = u(r.props.children) : s === "bi" ? (e.showBi = !0, e.biLogoSrc = r.props.logoSrc || r.props.src, e.biLogoAlt = r.props.alt, e.biText = r.props.text) : s === "extra" && (e.showExtra = !0, e.extraLogoSrc = r.props.logoSrc || r.props.src, e.extraLogoAlt = r.props.alt, e.extraText = r.props.text ?? u(r.props.children)), (n = r.props) != null && n.children && o(r.props.children);
|
||||
const p = (c = r.type) == null ? void 0 : c[E];
|
||||
p === "logo" ? (e.logoSrc = r.props.src, e.logoAlt = r.props.alt, e.logoOptimize = r.props.optimize === !0) : p === "title" ? e.title = u(r.props.children) : p === "description" ? e.description = u(r.props.children) : p === "bi" ? (e.showBi = !0, e.biLogoSrc = r.props.logoSrc || r.props.src, e.biLogoAlt = r.props.alt, e.biText = r.props.text) : p === "extra" && (e.showExtra = !0, e.extraLogoSrc = r.props.logoSrc || r.props.src, e.extraLogoAlt = r.props.alt, e.extraText = r.props.text ?? u(r.props.children)), (n = r.props) != null && n.children && o(r.props.children);
|
||||
});
|
||||
}
|
||||
return o(t), e;
|
||||
@ -93,10 +93,10 @@ const D = b({
|
||||
...B(t)
|
||||
}
|
||||
});
|
||||
function _(t) {
|
||||
return /* @__PURE__ */ E(D, { ...t });
|
||||
function O(t) {
|
||||
return /* @__PURE__ */ A(D, { ...t });
|
||||
}
|
||||
const O = Object.assign(_, {
|
||||
const $ = Object.assign(O, {
|
||||
BrandSection: y,
|
||||
Logo: H,
|
||||
Brand: L,
|
||||
@ -106,5 +106,5 @@ const O = Object.assign(_, {
|
||||
Bi: T
|
||||
});
|
||||
export {
|
||||
O as AppHeader
|
||||
$ as AppHeader
|
||||
};
|
||||
|
||||
154
dist/remote/AppHeader.js
vendored
154
dist/remote/AppHeader.js
vendored
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
2
dist/remote/index.html
vendored
2
dist/remote/index.html
vendored
@ -5,7 +5,7 @@
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="color-scheme" content="light" />
|
||||
<title>UI Kit Remote Demo</title>
|
||||
<script type="module" crossorigin src="./demo-assets/index-CGeO2iR0.js"></script>
|
||||
<script type="module" crossorigin src="./demo-assets/index-CS0Nxz6w.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
|
||||
BIN
dist/remote/logo.png
vendored
Normal file
BIN
dist/remote/logo.png
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 49 KiB |
BIN
public/logo.png
Normal file
BIN
public/logo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 49 KiB |
@ -13,7 +13,6 @@ import {
|
||||
Text,
|
||||
} from "@chakra-ui/react";
|
||||
import { AppHeader, AppSidebar, Pagination } from "../remote/ui/index.jsx";
|
||||
import demoLogo from "./demo-logo.svg";
|
||||
import demoExtraLogo from "./demo-extra-logo.svg";
|
||||
import biLogo from "../remote/ui/AppHeader/bi-logo.webp";
|
||||
|
||||
@ -23,7 +22,7 @@ export default function PageHeader() {
|
||||
return (
|
||||
<AppHeader bg="#314E89">
|
||||
<AppHeader.BrandSection>
|
||||
<AppHeader.Logo src="/logo.png" alt="نشان سامانه" />
|
||||
<AppHeader.Logo src="/logo.png" alt="نشان سامانه" optimize />
|
||||
|
||||
<AppHeader.Brand>
|
||||
<AppHeader.Title>عنوان سامانه</AppHeader.Title>
|
||||
@ -209,6 +208,40 @@ const IconLock = () => (
|
||||
);
|
||||
|
||||
function HeaderDocs() {
|
||||
const [optimizeLogo, setOptimizeLogo] = useState(true);
|
||||
const [logoStatus, setLogoStatus] = useState("در حال بررسی تصویر...");
|
||||
const headerPreviewRef = useRef(null);
|
||||
|
||||
// Observe the real image in the remote Shadow DOM, including fallback.
|
||||
// This is only a demo diagnostic; AppHeader itself does not depend on it.
|
||||
useEffect(() => {
|
||||
const update = () => {
|
||||
const image = headerPreviewRef.current
|
||||
?.querySelector("uikit-app-header")
|
||||
?.shadowRoot?.querySelector("img.logo");
|
||||
if (!image?.src || !image.complete) {
|
||||
setLogoStatus("در حال بارگذاری تصویر...");
|
||||
return;
|
||||
}
|
||||
if (!image.naturalWidth) {
|
||||
setLogoStatus("خطا در بارگذاری لوگو؛ آدرس فایل PNG اصلی را بررسی کنید.");
|
||||
return;
|
||||
}
|
||||
const dimensions = `${Math.round(image.getBoundingClientRect().width)}×${Math.round(image.getBoundingClientRect().height)}px`;
|
||||
const optimized = image.src.startsWith("https://img.d.aiengines.ir/") && image.src.endsWith("@webp");
|
||||
setLogoStatus(
|
||||
optimized
|
||||
? `WebP بهینهشده از سرویس تصاویر | ابعاد نمایش: ${dimensions}`
|
||||
: optimizeLogo
|
||||
? `تصویر اصلی PNG (Fallback) | ابعاد نمایش: ${dimensions}`
|
||||
: `تصویر اصلی PNG | ابعاد نمایش: ${dimensions}`,
|
||||
);
|
||||
};
|
||||
update();
|
||||
const interval = window.setInterval(update, 350);
|
||||
return () => window.clearInterval(interval);
|
||||
}, [optimizeLogo]);
|
||||
|
||||
return (
|
||||
<Stack spacing={5}>
|
||||
<Text color="gray.600" fontSize="sm">
|
||||
@ -220,11 +253,11 @@ function HeaderDocs() {
|
||||
<CodeBlock>{HEADER_USAGE}</CodeBlock>
|
||||
|
||||
<Box>
|
||||
<Text mb={2} fontWeight="700" fontSize="sm">نمونه نمایش</Text>
|
||||
<Box overflow="auto" borderWidth="1px" borderColor="gray.200" borderRadius="lg">
|
||||
<Text mb={2} fontWeight="700" fontSize="sm">نمونه نمایش لوگوی ۱۲۵۴×۱۲۵۴ PNG</Text>
|
||||
<Box ref={headerPreviewRef} overflow="auto" borderWidth="1px" borderColor="gray.200" borderRadius="lg">
|
||||
<AppHeader bg="#314E89">
|
||||
<AppHeader.BrandSection>
|
||||
<AppHeader.Logo src={demoLogo} alt="نشان نمونه سامانه" />
|
||||
<AppHeader.Logo src="/logo.png" alt="نشان نمونه سامانه" optimize={optimizeLogo} />
|
||||
<AppHeader.Brand>
|
||||
<AppHeader.Title>عنوان سامانه</AppHeader.Title>
|
||||
<AppHeader.Description>توضیحات سامانه</AppHeader.Description>
|
||||
@ -238,6 +271,16 @@ function HeaderDocs() {
|
||||
<AppHeader.Bi src={biLogo} />
|
||||
</AppHeader>
|
||||
</Box>
|
||||
<Flex mt={3} gap={3} align="center" justify="space-between" wrap="wrap">
|
||||
<Text color="gray.600" fontSize="xs">{logoStatus}</Text>
|
||||
<Button size="xs" variant="outline" onClick={() => setOptimizeLogo((current) => !current)}>
|
||||
{optimizeLogo ? "نمایش PNG اصلی" : "نمایش WebP بهینهشده"}
|
||||
</Button>
|
||||
</Flex>
|
||||
<Text mt={2} color="gray.500" fontSize="xs">
|
||||
در اجرای لوکال، لوگو از پوشه public بارگذاری میشود. سرویس تصاویر معمولاً به localhost
|
||||
دسترسی ندارد؛ در این حالت PNG اصلی بهصورت Fallback نمایش داده میشود.
|
||||
</Text>
|
||||
</Box>
|
||||
|
||||
<Box fontSize="sm" color="gray.600">
|
||||
|
||||
@ -1,3 +1,5 @@
|
||||
import { processImage } from "../../../utils/images.js";
|
||||
|
||||
// Self-contained remote AppHeader.
|
||||
// No host/runtime dependencies and no window.__UIKIT_REMOTE_DEPS__ access.
|
||||
// Font family is inherited from the host application; all other visual styles stay isolated.
|
||||
@ -175,6 +177,7 @@ class AppHeaderElement extends HTMLElement {
|
||||
super();
|
||||
|
||||
this._props = {};
|
||||
this._disposeOptimizedLogo = null;
|
||||
this._shadow = this.attachShadow({ mode: "open" });
|
||||
|
||||
const style = document.createElement("style");
|
||||
@ -198,11 +201,114 @@ class AppHeaderElement extends HTMLElement {
|
||||
this._render();
|
||||
}
|
||||
|
||||
disconnectedCallback() {
|
||||
this._disposeOptimizedLogo?.();
|
||||
this._disposeOptimizedLogo = null;
|
||||
}
|
||||
|
||||
_setOptimizedLogo(logo, source) {
|
||||
// This module runs in the destination document, not on the UI Kit domain.
|
||||
// Never resolve the host origin during SSR or inside the React wrapper.
|
||||
if (typeof window === "undefined") {
|
||||
logo.src = source;
|
||||
return;
|
||||
}
|
||||
|
||||
let absoluteSource;
|
||||
try {
|
||||
const url = new URL(source, window.location.href);
|
||||
if (url.protocol !== "http:" && url.protocol !== "https:") {
|
||||
logo.src = source;
|
||||
return;
|
||||
}
|
||||
absoluteSource = url.href;
|
||||
} catch {
|
||||
logo.src = source;
|
||||
return;
|
||||
}
|
||||
|
||||
let active = true;
|
||||
let failed = false;
|
||||
let lastRequestedUrl = "";
|
||||
let frameId;
|
||||
let resizeObserver;
|
||||
|
||||
const onError = () => {
|
||||
if (!active || failed) return;
|
||||
failed = true;
|
||||
// If the optimization service cannot serve this image, retain the logo.
|
||||
logo.src = source;
|
||||
};
|
||||
|
||||
const update = () => {
|
||||
if (!active || failed || !logo.isConnected) return;
|
||||
const rect = logo.getBoundingClientRect();
|
||||
const width = Math.max(0, Math.round(rect.width));
|
||||
if (!width) return;
|
||||
|
||||
// Before an image has loaded, the browser cannot know its aspect ratio.
|
||||
// Start from the available bounds (square logo is the common case); after
|
||||
// load and on resizing, measure the *actual rendered element* again.
|
||||
let height;
|
||||
if (logo.naturalWidth && logo.naturalHeight) {
|
||||
height = Math.max(0, Math.round(rect.height));
|
||||
} else {
|
||||
const maxHeight = Number.parseFloat(getComputedStyle(logo).maxHeight);
|
||||
height = Number.isFinite(maxHeight)
|
||||
? Math.min(width, Math.round(maxHeight))
|
||||
: width;
|
||||
}
|
||||
if (!height) return;
|
||||
|
||||
const url = processImage(absoluteSource, {
|
||||
w: width,
|
||||
h: height,
|
||||
q: 70,
|
||||
webp: true,
|
||||
});
|
||||
if (url === lastRequestedUrl) return;
|
||||
lastRequestedUrl = url;
|
||||
logo.src = url;
|
||||
};
|
||||
|
||||
const scheduleUpdate = () => {
|
||||
if (!active || failed || frameId !== undefined) return;
|
||||
frameId = window.requestAnimationFrame(() => {
|
||||
frameId = undefined;
|
||||
update();
|
||||
});
|
||||
};
|
||||
|
||||
logo.addEventListener("error", onError);
|
||||
logo.addEventListener("load", scheduleUpdate);
|
||||
|
||||
if (typeof ResizeObserver !== "undefined") {
|
||||
resizeObserver = new ResizeObserver(scheduleUpdate);
|
||||
resizeObserver.observe(logo);
|
||||
} else {
|
||||
window.addEventListener("resize", scheduleUpdate);
|
||||
}
|
||||
scheduleUpdate();
|
||||
|
||||
this._disposeOptimizedLogo = () => {
|
||||
active = false;
|
||||
if (frameId !== undefined) window.cancelAnimationFrame(frameId);
|
||||
resizeObserver?.disconnect();
|
||||
window.removeEventListener("resize", scheduleUpdate);
|
||||
logo.removeEventListener("error", onError);
|
||||
logo.removeEventListener("load", scheduleUpdate);
|
||||
};
|
||||
}
|
||||
|
||||
_render() {
|
||||
if (!this._mount) return;
|
||||
|
||||
this._disposeOptimizedLogo?.();
|
||||
this._disposeOptimizedLogo = null;
|
||||
|
||||
const {
|
||||
logoSrc,
|
||||
logoOptimize = false,
|
||||
logoAlt = "logo",
|
||||
title,
|
||||
description,
|
||||
@ -230,10 +336,10 @@ class AppHeaderElement extends HTMLElement {
|
||||
|
||||
if (logoSrc) {
|
||||
const logo = createElement("img", "logo");
|
||||
logo.src = logoSrc;
|
||||
logo.alt = logoAlt || "logo";
|
||||
logo.loading = "lazy";
|
||||
logo.loading = logoOptimize ? "eager" : "lazy";
|
||||
brandSection.appendChild(logo);
|
||||
if (!logoOptimize) logo.src = logoSrc;
|
||||
}
|
||||
|
||||
const brand = createElement("div", "brand");
|
||||
@ -299,6 +405,11 @@ class AppHeaderElement extends HTMLElement {
|
||||
|
||||
header.appendChild(row);
|
||||
this._mount.appendChild(header);
|
||||
|
||||
if (logoSrc && logoOptimize === true) {
|
||||
const logo = brandSection.querySelector(".logo");
|
||||
if (logo) this._setOptimizedLogo(logo, logoSrc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
4
src/remote/ui/AppHeader/index.d.ts
vendored
4
src/remote/ui/AppHeader/index.d.ts
vendored
@ -3,6 +3,8 @@ import type { ReactNode } from "react";
|
||||
export type RemoteAppHeaderProps = {
|
||||
logoSrc?: string;
|
||||
logoAlt?: string;
|
||||
/** Request a resized WebP logo from the image service. Defaults to false. */
|
||||
logoOptimize?: boolean;
|
||||
title?: ReactNode;
|
||||
description?: ReactNode;
|
||||
biLogoSrc?: string;
|
||||
@ -27,7 +29,7 @@ type AppHeaderSlotProps = {
|
||||
|
||||
export const AppHeader: ((props: RemoteAppHeaderProps) => any) & {
|
||||
BrandSection: (props: AppHeaderSlotProps) => any;
|
||||
Logo: (props: AppHeaderSlotProps & { src?: string; alt?: string }) => any;
|
||||
Logo: (props: AppHeaderSlotProps & { src?: string; alt?: string; optimize?: boolean }) => any;
|
||||
Brand: (props: AppHeaderSlotProps) => any;
|
||||
Title: (props: AppHeaderSlotProps) => any;
|
||||
Description: (props: AppHeaderSlotProps) => any;
|
||||
|
||||
@ -59,6 +59,7 @@ function extractCompoundAppHeader(children) {
|
||||
if (slot === "logo") {
|
||||
result.logoSrc = child.props.src;
|
||||
result.logoAlt = child.props.alt;
|
||||
result.logoOptimize = child.props.optimize === true;
|
||||
} else if (slot === "title") {
|
||||
result.title = toPlainText(child.props.children);
|
||||
} else if (slot === "description") {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user