Files
supabase/apps/design-system/components/copy-button.tsx
b8243e06d8 Feat/design system site (#26586)
* init site

* add nav menu

* add props table component

* init all the examples

• also includes moving a bunch of ./ui files to ./ui-patterns

* update package

• might need to remove some of these

* Update rehype-component.ts

* move nav

* more changes

* add new source information to contentlayer

* fix copy buttons

* add text-confirm. start new concept of "fragments"

* move base path. add homepage. add theme selection

* added colors

* add form-item-layout

* temporary fix code blocks in light mode. they currently don't switch theme to theme

* start adding code themes

* Update dark.json

* update code block themes

* fix animations

* add cdmk search

* export registry of icons in ./icons package

* add comments

* add icon copy stuff

* Update icons.tsx

* more docs

* more docs. cleaned up colors and icons docs

* Update theming.mdx

* add more docs

* update

* add package

* Update tailwind.config.js

* update content

* Update drawer-demo.tsx

* Update aspect-ratio-demo.tsx

* add new blocks

* Update source-panel.tsx

* Update source-panel.tsx

* Update source-panel.tsx

* add new source block

* Update source-panel.tsx

* Update _app.tsx

* Update page.tsx

* Delete layout-old.tsx

* remove old themes

* remove old ui registry

* comment out nav items

* Update package-lock.json

* Update AssistantChatForm.tsx

* move back

* move again

* update

* Update index.tsx

* Update package-lock.json

* Update package-lock.json

* Update package-lock.json

* update package

* udpate prettier

* fix tag issues

* Update package-lock.json

* update to contentlayer2

* update package-lock.json

* fix type errors

* ignore spelling

* Update avoid-typos.yml

* Update avoid-typos.yml

* Update package-lock.json

* Use ui type definitions.

* move tsconfig base stuff. fix content layer issue

* Update package-lock.json

* Update package-lock.json

---------

Co-authored-by: Alaister Young <a@alaisteryoung.com>
Co-authored-by: Ivan Vasilov <vasilov.ivan@gmail.com>
2024-05-23 17:39:56 +08:00

180 lines
4.9 KiB
TypeScript

'use client'
import * as React from 'react'
import { DropdownMenuTriggerProps } from '@radix-ui/react-dropdown-menu'
// import { NpmCommands } from 'types/unist'
// import { Event, trackEvent } from '@/lib/events'
import { cn } from 'ui'
import { Button } from 'ui'
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from 'ui'
import { Check, Copy } from 'lucide-react'
interface CopyButtonProps extends React.HTMLAttributes<HTMLButtonElement> {
value: string
src?: string
// event?: Event['name']
}
export async function copyToClipboardWithMeta(
value: string
// event?: Event
) {
navigator.clipboard.writeText(value)
if (event) {
// trackEvent(event)
}
}
export function CopyButton({
value,
className,
src,
// event,
...props
}: CopyButtonProps) {
const [hasCopied, setHasCopied] = React.useState(false)
React.useEffect(() => {
setTimeout(() => {
setHasCopied(false)
}, 2000)
}, [hasCopied])
return (
<Button
size="small"
type="outline"
className={cn(
'relative z-10 h-6 w-6 text-foreground-muted hover:bg-surface-100 hover:text-foreground p-0',
className
)}
onClick={() => {
copyToClipboardWithMeta(
value
// event
// ? {
// name: event,
// properties: {
// code: value,
// },
// }
// : undefined
)
setHasCopied(true)
}}
{...props}
>
<span className="sr-only">Copy</span>
{hasCopied ? <Check className="h-3 w-3 text-brand-600" /> : <Copy className="h-3 w-3" />}
</Button>
)
}
interface CopyWithClassNamesProps extends DropdownMenuTriggerProps {
value: string
classNames: string
className?: string
}
export function CopyWithClassNames({
value,
classNames,
className,
...props
}: CopyWithClassNamesProps) {
const [hasCopied, setHasCopied] = React.useState(false)
React.useEffect(() => {
setTimeout(() => {
setHasCopied(false)
}, 2000)
}, [hasCopied])
const copyToClipboard = React.useCallback((value: string) => {
copyToClipboardWithMeta(value)
setHasCopied(true)
}, [])
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
size="small"
type="outline"
className={cn(
'relative z-10 h-6 w-6 text-zinc-50 hover:bg-zinc-700 hover:text-zinc-50',
className
)}
>
{hasCopied ? <Check className="h-3 w-3" /> : <Copy className="h-3 w-3" />}
<span className="sr-only">Copy</span>
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuItem onClick={() => copyToClipboard(value)}>Component</DropdownMenuItem>
<DropdownMenuItem onClick={() => copyToClipboard(classNames)}>Classname</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
)
}
interface CopyNpmCommandButtonProps extends DropdownMenuTriggerProps {
// commands: Required<NpmCommands>
}
// export function CopyNpmCommandButton({ commands, className, ...props }: CopyNpmCommandButtonProps) {
// const [hasCopied, setHasCopied] = React.useState(false)
// React.useEffect(() => {
// setTimeout(() => {
// setHasCopied(false)
// }, 2000)
// }, [hasCopied])
// // const copyCommand = React.useCallback((value: string, pm: 'npm' | 'pnpm' | 'yarn' | 'bun') => {
// // copyToClipboardWithMeta(value,
// // // {
// // // name: 'copy_npm_command',
// // // properties: {
// // // command: value,
// // // pm,
// // // },
// // })
// // setHasCopied(true)
// // }, [])
// return (
// <DropdownMenu>
// <DropdownMenuTrigger asChild>
// <Button
// // size="icon"
// type="outline"
// className={cn(
// 'relative z-10 h-6 w-6 text-zinc-50 hover:bg-zinc-700 hover:text-zinc-50',
// className
// )}
// >
// {hasCopied ? <CheckIcon className="h-3 w-3" /> : <CopyIcon className="h-3 w-3" />}
// <span className="sr-only">Copy</span>
// </Button>
// </DropdownMenuTrigger>
// <DropdownMenuContent align="end">
// <DropdownMenuItem onClick={() => copyCommand(commands.__npmCommand__, 'npm')}>
// npm
// </DropdownMenuItem>
// <DropdownMenuItem onClick={() => copyCommand(commands.__yarnCommand__, 'yarn')}>
// yarn
// </DropdownMenuItem>
// <DropdownMenuItem onClick={() => copyCommand(commands.__pnpmCommand__, 'pnpm')}>
// pnpm
// </DropdownMenuItem>
// <DropdownMenuItem onClick={() => copyCommand(commands.__bunCommand__, 'bun')}>
// bun
// </DropdownMenuItem>
// </DropdownMenuContent>
// </DropdownMenu>
// )
// }