-
Notifications
You must be signed in to change notification settings - Fork 4
feat(resources): add ability to choose package manager #173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
6b19c56
5cf5d08
6512ff7
8f2f1cc
715258c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export type PackageManager = "npm" | "yarn" | "pnpm" | "bun"; | ||
|
||
export function transformNpmCommand( | ||
prefix: string, | ||
cmd: string, | ||
packageManagerTarget: "yarn" | "bun" | "pnpm" | "npm", | ||
) { | ||
if (prefix === "npm") { | ||
if (cmd.split(" ")[0] === "install" && packageManagerTarget === "yarn") { | ||
return `${packageManagerTarget} ${cmd.replace("install", "add")}`; | ||
} | ||
return `${packageManagerTarget} ${cmd}`; | ||
} | ||
switch (packageManagerTarget) { | ||
case "bun": | ||
return `bunx ${cmd}`; | ||
case "pnpm": | ||
return `pnpm dlx ${cmd}`; | ||
} | ||
return `${prefix} ${cmd}`; | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
import { useEffect, useState } from "react"; | ||
import { type Resource } from "~/lib/resources.server"; | ||
import { Link, useSearchParams } from "@remix-run/react"; | ||
import { transformNpmCommand } from "~/lib/transformNpmCommand"; | ||
import type { PackageManager } from "~/lib/transformNpmCommand"; | ||
import { DetailsMenu, DetailsPopup } from "./details-menu"; | ||
|
||
import { Form, Link, useSearchParams, useSubmit } from "@remix-run/react"; | ||
import cx from "clsx"; | ||
import iconsHref from "~/icons.svg"; | ||
|
||
|
@@ -67,6 +71,13 @@ export function InitCodeblock({ | |
let [npxOrNpmMaybe, ...otherCode] = initCommand.trim().split(" "); | ||
let [copied, setCopied] = useState(false); | ||
|
||
function handleCopied(copied: boolean, packageManager: PackageManager) { | ||
setCopied(copied); | ||
navigator.clipboard.writeText( | ||
transformNpmCommand(npxOrNpmMaybe, otherCode.join(" "), packageManager), | ||
); | ||
} | ||
|
||
// Reset copied state after 4 seconds | ||
useEffect(() => { | ||
if (copied) { | ||
|
@@ -106,30 +117,104 @@ export function InitCodeblock({ | |
</code> | ||
</pre> | ||
|
||
<button | ||
type="button" | ||
onClick={() => { | ||
setCopied(true); | ||
navigator.clipboard.writeText(initCommand); | ||
}} | ||
data-code-block-copy | ||
<CopyCodeBlock setCopied={handleCopied} copied={copied} /> | ||
</div> | ||
); | ||
} | ||
|
||
type CopyCodeBlockProps = { | ||
copied: boolean; | ||
setCopied: (copied: boolean, packageManager: PackageManager) => void; | ||
}; | ||
|
||
function CopyCodeBlock({ copied, setCopied }: CopyCodeBlockProps) { | ||
const [isMenuOpen, setIsMenuOpen] = useState(false); | ||
const [searchParams] = useSearchParams(); | ||
|
||
return ( | ||
<DetailsMenu | ||
className="absolute right-4 top-0 !opacity-100" | ||
data-copied={copied} | ||
onToggle={() => setIsMenuOpen((oldValue) => !oldValue)} | ||
> | ||
<summary | ||
className="_no-triangle absolute top-0 grid" | ||
data-copied={copied} | ||
className="outline-none" | ||
> | ||
{/* had to put these here instead of as a mask so we could add an opacity */} | ||
<svg | ||
aria-hidden | ||
className="h-5 w-5 text-gray-500 hover:text-black dark:text-gray-400 dark:hover:text-gray-100" | ||
viewBox="0 0 24 24" | ||
<span | ||
data-code-block-copy | ||
data-copied={copied} | ||
className={`absolute right-0 top-0 opacity-0 hover:opacity-100 ${copied || isMenuOpen ? "!opacity-100" : ""}`} | ||
> | ||
{copied ? ( | ||
<use href={`${iconsHref}#check-mark`} /> | ||
) : ( | ||
<use href={`${iconsHref}#copy`} /> | ||
)} | ||
</svg> | ||
<span className="sr-only">Copy code to clipboard</span> | ||
</button> | ||
</div> | ||
<svg | ||
aria-hidden | ||
className="h-5 w-5 text-gray-500 hover:text-black dark:text-gray-400 dark:hover:text-gray-100" | ||
viewBox="0 0 24 24" | ||
> | ||
{copied ? ( | ||
<use href={`${iconsHref}#check-mark`} /> | ||
) : ( | ||
<use href={`${iconsHref}#copy`} /> | ||
)} | ||
</svg> | ||
<span className="sr-only">Copy code to clipboard</span> | ||
</span> | ||
</summary> | ||
<DetailsPopup | ||
className="!left-auto !right-0 top-10" | ||
childrenClassName="!w-[110px]" | ||
|
||
> | ||
<Form preventScrollReset replace className="flex flex-col"> | ||
<input | ||
type="hidden" | ||
name="category" | ||
value={searchParams.get("category") || ""} | ||
/> | ||
|
||
<PackageManagerButton | ||
packageManager="npm" | ||
setCopied={(copied) => setCopied(copied, "npm")} | ||
/> | ||
<PackageManagerButton | ||
packageManager="yarn" | ||
setCopied={(copied) => setCopied(copied, "yarn")} | ||
/> | ||
<PackageManagerButton | ||
packageManager="pnpm" | ||
setCopied={(copied) => setCopied(copied, "pnpm")} | ||
/> | ||
<PackageManagerButton | ||
packageManager="bun" | ||
setCopied={(copied) => setCopied(copied, "bun")} | ||
/> | ||
</Form> | ||
</DetailsPopup> | ||
</DetailsMenu> | ||
); | ||
} | ||
|
||
type PackageManagerButtonProps = { | ||
packageManager: PackageManager; | ||
setCopied: (copied: boolean) => void; | ||
}; | ||
|
||
function PackageManagerButton({ | ||
packageManager, | ||
setCopied, | ||
}: PackageManagerButtonProps) { | ||
const submit = useSubmit(); | ||
return ( | ||
<button | ||
className="rounded-md p-1.5 text-left text-sm hover:cursor-pointer hover:bg-gray-50" | ||
type="submit" | ||
onClick={() => { | ||
submit({ | ||
preventScrollReset: false, | ||
}); | ||
setCopied(true); | ||
}} | ||
> | ||
{packageManager} | ||
</button> | ||
); | ||
} | ||
brookslybrand marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
Uh oh!
There was an error while loading. Please reload this page.