Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 152 additions & 0 deletions src/components/CopyPageButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import {
useFloating,
useInteractions,
useClick,
useDismiss,
shift,
offset,
autoUpdate,
FloatingPortal,
} from "@floating-ui/react";
import { useState } from "react";
import {
PiMarkdownLogo,
PiClipboardTextLight,
PiArrowSquareOutLight,
PiCheckCircleLight,
PiXCircleLight,
} from "react-icons/pi";

type CopyState = "idle" | "success" | "error";

export default function CopyPageButton() {
const [isOpen, setIsOpen] = useState(false);
const [copyState, setCopyState] = useState<CopyState>("idle");

const { refs, floatingStyles, context } = useFloating({
open: isOpen,
onOpenChange: setIsOpen,
middleware: [shift(), offset(5)],
whileElementsMounted: autoUpdate,
});

const click = useClick(context);
const dismiss = useDismiss(context);

const { getReferenceProps, getFloatingProps } = useInteractions([
click,
dismiss,
]);

const handleViewMarkdown = () => {
const markdownUrl = new URL("index.md", window.location.href).toString();
window.open(markdownUrl, "_blank");
};

const handleCopyMarkdown = async () => {
const markdownUrl = new URL("index.md", window.location.href).toString();
try {
const response = await fetch(markdownUrl);

if (!response.ok) {
throw new Error(`Received ${response.status} on ${response.url}`);
}

const markdown = await response.text();
await navigator.clipboard.writeText(markdown);

setCopyState("success");
setTimeout(() => {
setCopyState("idle");
}, 1500);
} catch (error) {
console.error("Failed to copy Markdown:", error);

setCopyState("error");
setTimeout(() => {
setCopyState("idle");
}, 1500);
}
};

const options = [
{
label: "Copy Page as Markdown",
description: "Copy the raw Markdown content to clipboard",
icon: PiClipboardTextLight,
onClick: handleCopyMarkdown,
},
{
label: "View Page as Markdown",
description: "Open the Markdown file in a new tab",
icon: PiArrowSquareOutLight,
onClick: handleViewMarkdown,
},
];

const getButtonContent = () => {
if (copyState === "success") {
return (
<>
<PiCheckCircleLight className="h-4 w-4 text-green-600" />
<span>Copied!</span>
</>
);
}

if (copyState === "error") {
return (
<>
<PiXCircleLight className="h-4 w-4 text-red-600" />
<span>Failed to copy</span>
</>
);
}

return (
<>
<PiMarkdownLogo />
<span>Copy Page</span>
</>
);
};

return (
<>
<button
ref={refs.setReference}
{...getReferenceProps()}
className="inline-flex h-8 cursor-pointer items-center justify-center gap-2 rounded border border-[--sl-color-hairline] bg-transparent px-3 text-sm text-black hover:bg-[--sl-color-bg-nav]"
>
{getButtonContent()}
</button>
{isOpen && (
<FloatingPortal>
<ul
ref={refs.setFloating}
style={floatingStyles}
{...getFloatingProps()}
className="min-w-[240px] list-none rounded border border-[--sl-color-hairline] bg-[--sl-color-bg] pl-0 shadow-md"
>
{options.map(({ label, description, icon: Icon, onClick }) => (
<li key={label}>
<button
onClick={onClick}
className="relative block w-full cursor-pointer bg-transparent px-3 py-2 text-left text-black no-underline hover:bg-[--sl-color-bg-nav]"
>
<div className="flex items-center gap-2 text-sm">
<Icon className="h-4 w-4" />
{label}
</div>
<div className="ml-6 mt-0.5 text-xs text-[--sl-color-gray-3]">
{description}
</div>
</button>
</li>
))}
</ul>
</FloatingPortal>
)}
</>
);
}
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export { default as AvailableNotifications } from "./AvailableNotifications.astr
export { default as CompatibilityFlag } from "./CompatibilityFlag.astro";
export { default as CompatibilityFlags } from "./CompatibilityFlags.astro";
export { default as ComponentsUsage } from "./ComponentsUsage.astro";
export { default as CopyPageButton } from "./CopyPageButton.tsx";
export { default as CURL } from "./CURL.astro";
export { default as Description } from "./Description.astro";
export { default as Details } from "./Details.astro";
Expand Down
5 changes: 5 additions & 0 deletions src/components/overrides/PageTitle.astro
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Aside } from "@astrojs/starlight/components";
import Description from "~/components/Description.astro";
import SpotlightAuthorDetails from "~/components/SpotlightAuthorDetails.astro";
import LastReviewed from "~/components/LastReviewed.astro";
import CopyPageButton from "~/components/CopyPageButton.tsx";

import { getEntry } from "astro:content";

Expand Down Expand Up @@ -113,6 +114,10 @@ const hideBreadcrumbs = Astro.locals.starlightRoute.hideBreadcrumbs;

{component && <Aside>To see a list of pages this component is used on, please visit the <a href={`/style-guide/components/usage/#${component.toLowerCase()}`}>usage page</a>.</Aside>}

<div class="flex justify-end items-center">
<CopyPageButton client:idle />
</div>

<style>
:root {
--color-link-breadcrumbs: var(--sl-color-text-accent);
Expand Down
Loading