CommandRender.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { Check, Copy } from 'lucide-react'
  2. import { forwardRef, useState } from 'react'
  3. import { cn, copyToClipboard } from 'ui'
  4. const CommandRender = forwardRef<HTMLDivElement, { commands: any[]; className?: string }>(
  5. ({ commands, className }, ref) => {
  6. return (
  7. <div ref={ref} className={cn('space-y-4', className)}>
  8. {commands.map((item: any, idx: number) => (
  9. <Command key={`command-${idx}`} item={item} />
  10. ))}
  11. </div>
  12. )
  13. }
  14. )
  15. CommandRender.displayName = 'CommandRender'
  16. export default CommandRender
  17. const Command = ({ item }: any) => {
  18. const [isCopied, setIsCopied] = useState(false)
  19. return (
  20. <div className="space-y-1">
  21. <span className="font-mono text-sm text-foreground-lighter">{`> ${item.comment}`}</span>
  22. <div className="flex items-center gap-2">
  23. <div className="flex gap-2 font-mono text-sm font-normal text-foreground">
  24. <span className="text-foreground-lighter">$</span>
  25. <span>
  26. <span>{item.jsx ? item.jsx() : null} </span>
  27. <button
  28. type="button"
  29. className="text-foreground-lighter hover:text-foreground"
  30. onClick={() => {
  31. function onCopy(value: any) {
  32. setIsCopied(true)
  33. copyToClipboard(value)
  34. setTimeout(() => setIsCopied(false), 3000)
  35. }
  36. onCopy(item.command)
  37. }}
  38. >
  39. {isCopied ? (
  40. <Check size={14} strokeWidth={3} className="text-brand" />
  41. ) : (
  42. <Copy size={14} />
  43. )}
  44. </button>
  45. </span>
  46. </div>
  47. </div>
  48. </div>
  49. )
  50. }