mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
* fix(docs): check catalog link values by type and isolate the preview A catalog page restores its variable values from the query string, so a shared link decides what the preview composition is given. Values from a link are now kept only when they fit the variable they name: a finite number, a boolean, one of an enum's options, a colour, an image address, or text with no angle brackets. Anything else opens at its default. The preview player now runs the composition with an opaque origin (sandbox-origin="opaque"), so a composition cannot reach the docs page. The bootstrap already drives the player only through its API and postMessage, and every docs asset is served with CORS, so playback, seek-on-remount and edits behave as before. chatgpt-exchange sets its editable text with textContent instead of building innerHTML from it. A new check, check:registry-innerhtml, fails any registry file that reads variables and adds markup writes; the files that do so today are listed with their current counts and may only go down. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(docs): keep a link's text out of copied commands until the reader sets it (#7785) A catalog page restores tuned values from its link, and the install command, the render command and the agent request carried those values as if the reader had typed them. The commands quote only for bash and zsh, and the agent request is pasted as the reader's own words. The page now tracks which variables the reader set on it. A value that only came with the link goes into a command when it is a number, a boolean, or short text of letters, digits, spaces and plain punctuation, and into the agent request only when it is a number, a boolean or one of the item's own options. A page opened with values from its link says so and offers "Reset to defaults", and the install command wraps instead of scrolling so the whole line is visible. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
179 lines
7.1 KiB
React
179 lines
7.1 KiB
React
/**
|
|
* The install command, with a copy button that is always visible.
|
|
*
|
|
* A plain code fence renders a copy control only on hover, so it is invisible
|
|
* to anyone who has not already guessed it is there, and absent from the
|
|
* accessibility tree entirely. This is the one line on a catalog page that
|
|
* every reader is here to take, so the affordance is spelled out.
|
|
*
|
|
* The button is icon-only, and both icons are Mintlify's own: the same
|
|
* clipboard and check paths, at the same 16px, that the sitewide code-block
|
|
* copy button draws. Two copy affordances sit on a catalog page, and they
|
|
* should read as one control used twice. Sizing, hover plate, focus ring and
|
|
* the brand-coloured check live in `custom.css` next to the rules that style
|
|
* that sitewide button, so the pair cannot drift apart.
|
|
*
|
|
* The "Copied" announcement is a sibling `role="status"`, not a swapped
|
|
* `aria-label`: it is the pattern Mintlify already uses on the code-block
|
|
* button, so a screen reader hears the same word from both controls, and it
|
|
* does not rename a control while it holds focus.
|
|
*
|
|
* navigator.clipboard is unavailable on insecure origins, which is exactly the
|
|
* local `mint dev` preview these pages are written against. The textarea path
|
|
* below is the fallback, not decoration.
|
|
*/
|
|
export const InstallCommand = ({ command, item }) => {
|
|
const [copied, setCopied] = React.useState(false);
|
|
|
|
/**
|
|
* The values a reader tuned, read from the same query string the variables
|
|
* panel writes.
|
|
*
|
|
* The URL is the shared state rather than a prop, because this line and the
|
|
* panel are separate components mounted by MDX with no parent between them.
|
|
* It also means a shared link and this command agree without either knowing
|
|
* the other exists.
|
|
*
|
|
* Read after mount: the first render happens on the server, where there is
|
|
* no query string to read.
|
|
*/
|
|
const [tuned, setTuned] = React.useState("");
|
|
|
|
React.useEffect(() => {
|
|
if (!item) return;
|
|
|
|
// >>> vars argument
|
|
/**
|
|
* The ` --vars '<json>'` suffix for a query value, or "" when nothing usable is in it.
|
|
*
|
|
* Keeps variable-id keys with string, finite number or boolean values, and
|
|
* escapes `'` so the JSON stays one shell argument in bash and zsh.
|
|
*
|
|
* `edited` lists the ids the reader set on this page. Any other string came
|
|
* with the link, from whoever shared it, and the command may be pasted into
|
|
* fish, PowerShell or cmd.exe, which quote differently. So such a string is
|
|
* kept only when it is short and holds nothing those shells read as syntax.
|
|
*/
|
|
function varsArgument(raw, edited = []) {
|
|
if (!raw) return "";
|
|
const parsed = JSON.parse(raw);
|
|
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return "";
|
|
const kept = Object.entries(parsed).filter(([id, value]) => {
|
|
if (!/^[A-Za-z_][\w-]*$/.test(id)) return false;
|
|
if (typeof value === "number") return Number.isFinite(value);
|
|
if (typeof value === "boolean") return true;
|
|
if (typeof value !== "string") return false;
|
|
return edited.includes(id) || (value.length <= 200 && /^[A-Za-z0-9 .,:_#+\-/@()?]*$/.test(value));
|
|
});
|
|
if (kept.length === 0) return "";
|
|
return ` --vars '${JSON.stringify(Object.fromEntries(kept)).replace(/'/g, "'\\''")}'`;
|
|
}
|
|
// <<< vars argument
|
|
|
|
const read = () => {
|
|
try {
|
|
const edited = window.__hfEditedVars?.[item];
|
|
setTuned(
|
|
varsArgument(
|
|
new URLSearchParams(window.location.search).get(`vars-${item}`),
|
|
Array.isArray(edited) ? edited : [],
|
|
),
|
|
);
|
|
} catch {
|
|
// A hand-edited link should leave the plain command, not break it.
|
|
setTuned("");
|
|
}
|
|
};
|
|
|
|
read();
|
|
// `replaceState` fires nothing, so the panel says so itself; `popstate`
|
|
// covers the back button.
|
|
window.addEventListener("hf-vars-changed", read);
|
|
window.addEventListener("popstate", read);
|
|
return () => {
|
|
window.removeEventListener("hf-vars-changed", read);
|
|
window.removeEventListener("popstate", read);
|
|
};
|
|
}, [item]);
|
|
|
|
const fullCommand = `${command}${tuned}`;
|
|
|
|
const copy = async () => {
|
|
try {
|
|
if (navigator.clipboard && window.isSecureContext) {
|
|
await navigator.clipboard.writeText(fullCommand);
|
|
} else {
|
|
// The scratch textarea has to take focus to be selected, and removing
|
|
// it drops focus on <body> — so a reader who copied with the keyboard
|
|
// would Tab from the top of the page again. Hand focus back.
|
|
const previous = document.activeElement;
|
|
const scratch = document.createElement("textarea");
|
|
scratch.value = fullCommand;
|
|
scratch.setAttribute("readonly", "");
|
|
scratch.style.position = "fixed";
|
|
scratch.style.opacity = "0";
|
|
document.body.appendChild(scratch);
|
|
scratch.select();
|
|
document.execCommand("copy");
|
|
document.body.removeChild(scratch);
|
|
previous?.focus?.();
|
|
}
|
|
setCopied(true);
|
|
setTimeout(() => setCopied(false), 2000);
|
|
} catch {
|
|
// Leave the command on screen and selectable. Reporting a failure the
|
|
// reader cannot act on is worse than letting them select it by hand.
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="hf-install-command not-prose my-4 flex items-stretch overflow-hidden rounded-xl border border-zinc-200 bg-zinc-50 dark:border-zinc-800 dark:bg-zinc-900">
|
|
<code className="flex-1 border-r border-zinc-200 px-4 py-3 font-mono text-sm text-zinc-800 dark:border-zinc-800 dark:text-zinc-100">
|
|
{fullCommand}
|
|
</code>
|
|
<button
|
|
type="button"
|
|
onClick={copy}
|
|
data-copied={copied ? "true" : "false"}
|
|
aria-label={`Copy ${command} to the clipboard`}
|
|
className="hf-install-copy"
|
|
>
|
|
<svg
|
|
className="hf-install-copy-clipboard"
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
width="16"
|
|
height="16"
|
|
viewBox="0 0 18 18"
|
|
fill="none"
|
|
stroke="currentColor"
|
|
strokeWidth="1.5"
|
|
strokeLinecap="round"
|
|
strokeLinejoin="round"
|
|
aria-hidden="true"
|
|
>
|
|
<path d="M14.25 5.25H7.25C6.14543 5.25 5.25 6.14543 5.25 7.25V14.25C5.25 15.3546 6.14543 16.25 7.25 16.25H14.25C15.3546 16.25 16.25 15.3546 16.25 14.25V7.25C16.25 6.14543 15.3546 5.25 14.25 5.25Z" />
|
|
<path d="M2.80103 11.998L1.77203 5.07397C1.61003 3.98097 2.36403 2.96397 3.45603 2.80197L10.38 1.77297C11.313 1.63397 12.19 2.16297 12.528 3.00097" />
|
|
</svg>
|
|
<svg
|
|
className="hf-install-copy-check"
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
width="16"
|
|
height="16"
|
|
viewBox="0 0 18 18"
|
|
fill="none"
|
|
stroke="currentColor"
|
|
strokeWidth="2"
|
|
strokeLinecap="round"
|
|
strokeLinejoin="round"
|
|
aria-hidden="true"
|
|
>
|
|
<path d="M2.75 9.5L6.5 13.25L15.25 4.5" />
|
|
</svg>
|
|
</button>
|
|
<span className="hf-install-copy-status" role="status" aria-live="polite">
|
|
{copied ? "Copied" : ""}
|
|
</span>
|
|
</div>
|
|
);
|
|
};
|