All files / web/src/components/common QRCodeButton.tsx

12.18% Statements 24/197
100% Branches 0/0
0% Functions 0/1
12.18% Lines 24/197

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 1981x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                                                                                                                                                                                                                                                                                                            
import * as Popover from '@radix-ui/react-popover'
import type { CSSProperties } from 'react'
import { useState } from 'react'
import { useClipboard } from '@/hooks/useClipboard'
import { Z_INDEX } from '@/constants/zIndex'
import { AbacusQRCode } from './AbacusQRCode'
 
export interface QRCodeButtonProps {
  /**
   * The URL to encode in the QR code and display
   */
  url: string
 
  /**
   * Optional custom styles for the trigger button
   */
  style?: CSSProperties
}
 
/**
 * Button that opens a popover with a QR code for the share link
 * Includes the URL text with a copy button
 */
export function QRCodeButton({ url, style }: QRCodeButtonProps) {
  const [open, setOpen] = useState(false)
  const { copied, copy } = useClipboard()

  const buttonStyles: CSSProperties = {
    cursor: 'pointer',
    transition: 'all 0.2s ease',
    display: 'flex',
    alignItems: 'center',
    justifyContent: 'center',
    border: '2px solid rgba(251, 146, 60, 0.4)',
    background: 'linear-gradient(135deg, rgba(251, 146, 60, 0.2), rgba(251, 146, 60, 0.3))',
    borderRadius: '8px',
    padding: '4px',
    fontSize: '16px',
    color: 'rgba(253, 186, 116, 1)',
    height: '100%',
    aspectRatio: '1',
    flexShrink: 0,
    ...style,
  }

  const hoverStyles: CSSProperties = {
    background: 'linear-gradient(135deg, rgba(251, 146, 60, 0.3), rgba(251, 146, 60, 0.4))',
    borderColor: 'rgba(251, 146, 60, 0.6)',
  }

  return (
    <Popover.Root open={open} onOpenChange={setOpen}>
      <Popover.Trigger asChild>
        <button
          type="button"
          style={buttonStyles}
          onMouseEnter={(e) => {
            Object.assign(e.currentTarget.style, hoverStyles)
          }}
          onMouseLeave={(e) => {
            Object.assign(e.currentTarget.style, buttonStyles)
          }}
        >
          <AbacusQRCode value={url} size={84} />
        </button>
      </Popover.Trigger>

      <Popover.Portal>
        <Popover.Content
          align="center"
          side="bottom"
          sideOffset={8}
          style={{
            background: 'linear-gradient(135deg, #1a1a2e 0%, #16213e 100%)',
            border: '2px solid rgba(251, 146, 60, 0.4)',
            borderRadius: '12px',
            padding: '20px',
            boxShadow: '0 8px 32px rgba(0, 0, 0, 0.4)',
            zIndex: Z_INDEX.GAME_NAV.HAMBURGER_NESTED_DROPDOWN,
            maxWidth: '320px',
          }}
        >
          {/* Title */}
          <div
            style={{
              fontSize: '16px',
              fontWeight: 'bold',
              color: 'rgba(253, 186, 116, 1)',
              marginBottom: '12px',
              textAlign: 'center',
            }}
          >
            Scan to Join
          </div>

          {/* QR Code */}
          <div
            style={{
              background: 'white',
              padding: '16px',
              borderRadius: '8px',
              display: 'flex',
              justifyContent: 'center',
              alignItems: 'center',
              marginBottom: '16px',
            }}
          >
            <AbacusQRCode value={url} size={200} />
          </div>

          {/* URL with copy button */}
          <div
            style={{
              marginBottom: '8px',
            }}
          >
            <div
              style={{
                fontSize: '11px',
                color: 'rgba(209, 213, 219, 0.7)',
                marginBottom: '6px',
                textAlign: 'center',
              }}
            >
              Or copy link:
            </div>
            <button
              type="button"
              onClick={() => copy(url)}
              style={{
                width: '100%',
                cursor: 'pointer',
                transition: 'all 0.2s ease',
                display: 'flex',
                alignItems: 'center',
                justifyContent: 'center',
                gap: '6px',
                background: copied
                  ? 'linear-gradient(135deg, rgba(34, 197, 94, 0.2), rgba(34, 197, 94, 0.3))'
                  : 'linear-gradient(135deg, rgba(59, 130, 246, 0.2), rgba(59, 130, 246, 0.3))',
                border: copied
                  ? '2px solid rgba(34, 197, 94, 0.5)'
                  : '2px solid rgba(59, 130, 246, 0.4)',
                borderRadius: '8px',
                padding: '8px 12px',
                fontSize: '11px',
                fontWeight: 600,
                color: copied ? 'rgba(134, 239, 172, 1)' : 'rgba(147, 197, 253, 1)',
              }}
              onMouseEnter={(e) => {
                if (!copied) {
                  e.currentTarget.style.background =
                    'linear-gradient(135deg, rgba(59, 130, 246, 0.3), rgba(59, 130, 246, 0.4))'
                  e.currentTarget.style.borderColor = 'rgba(59, 130, 246, 0.6)'
                }
              }}
              onMouseLeave={(e) => {
                if (!copied) {
                  e.currentTarget.style.background =
                    'linear-gradient(135deg, rgba(59, 130, 246, 0.2), rgba(59, 130, 246, 0.3))'
                  e.currentTarget.style.borderColor = 'rgba(59, 130, 246, 0.4)'
                }
              }}
            >
              {copied ? (
                <>
                  <span style={{ fontSize: '12px' }}>✓</span>
                  <span>Copied!</span>
                </>
              ) : (
                <>
                  <span style={{ fontSize: '12px' }}>🔗</span>
                  <span
                    style={{
                      overflow: 'hidden',
                      textOverflow: 'ellipsis',
                      whiteSpace: 'nowrap',
                      maxWidth: '200px',
                    }}
                  >
                    {url}
                  </span>
                </>
              )}
            </button>
          </div>

          <Popover.Arrow
            style={{
              fill: 'rgba(251, 146, 60, 0.4)',
            }}
          />
        </Popover.Content>
      </Popover.Portal>
    </Popover.Root>
  )
}