118 lines
3.0 KiB
JavaScript
118 lines
3.0 KiB
JavaScript
"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");
|
|
const Extra = createAppHeaderSlot("extra");
|
|
|
|
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,
|
|
showExtra: 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;
|
|
result.logoOptimize = child.props.optimize === true;
|
|
} 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;
|
|
} else if (slot === "extra") {
|
|
result.showExtra = true;
|
|
result.extraLogoSrc = child.props.logoSrc || child.props.src;
|
|
result.extraLogoAlt = child.props.alt;
|
|
result.extraText = child.props.text ?? toPlainText(child.props.children);
|
|
}
|
|
|
|
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,
|
|
Extra,
|
|
Bi,
|
|
});
|