Skip to content
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

feat: export component prop types #462

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

gruhn
Copy link
Owner

@gruhn gruhn commented Dec 18, 2024

See: #460

@true-vue I tried to implement your suggestion. What do you think?

Copy link

Deploying vue-qrcode-reader with  Cloudflare Pages  Cloudflare Pages

Latest commit: caacdbe
Status: ✅  Deploy successful!
Preview URL: https://c34d7b73.vue-qrcode-reader.pages.dev
Branch Preview URL: https://export-component-prop-types.vue-qrcode-reader.pages.dev

View logs

@true-vue
Copy link
Contributor

I'm sorry for the delay. I hadn't noticed your message.

I have looked at the code and it looks fine. I'll try to import this version of component later on and will confirm that types are reachable for external usage.

Speak to you soon...

@true-vue
Copy link
Contributor

I have successfully imported both QrcodeStream and QrcodeStreamProps. Props types are OK!

Last thing which seems to be missing is EmittedError type export in src\index.ts.

Here is the code I've been testing with (I have had to use Error instead of EmittedError):

<template>
  <div class="alert alert-danger mb-4" v-if="error">{{ error }}</div>
  <div style="border: 2px solid black">
    <qrcode-stream
      :track="paintBoundingBox"
      @detect="onDetect"
      @error="onError"
      @camera-on="onCameraOn"
    ></qrcode-stream>
  </div>
  <div class="alert alert-success mb-4" v-if="result">{{ result }}</div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { QrcodeStream, type QrcodeStreamProps } from 'vue-qrcode-reader'

const result = ref('')
const error = ref('')

const paintBoundingBox: QrcodeStreamProps['track'] = (detectedCodes, ctx) => {
  for (const detectedCode of detectedCodes) {
    const {
      boundingBox: { x, y, width, height },
    } = detectedCode

    ctx!.lineWidth = 2
    ctx!.strokeStyle = '#007bff'
    ctx!.strokeRect(x, y, width, height)
  }
}

function onCameraOn(c: MediaTrackCapabilities) {
  console.log(c)
}

function onError(err: Error) {
  error.value = `[${err.name}]: `

  if (err.name === 'NotAllowedError') {
    error.value += 'you need to grant camera access permission'
  } else if (err.name === 'NotFoundError') {
    error.value += 'no camera on this device'
  } else if (err.name === 'NotSupportedError') {
    error.value += 'secure context required (HTTPS, localhost)'
  } else if (err.name === 'NotReadableError') {
    error.value += 'is the camera already in use?'
  } else if (err.name === 'OverconstrainedError') {
    error.value += 'installed cameras are not suitable'
  } else if (err.name === 'StreamApiNotSupportedError') {
    error.value += 'Stream API is not supported in this browser'
  } else if (err.name === 'InsecureContextError') {
    error.value +=
      'Camera access is only permitted in secure context. Use HTTPS or localhost rather than HTTP.'
  } else {
    error.value += err.message
  }
}

function onDetect(detectedCodes: { rawValue: string }[]) {
  result.value = `${JSON.stringify(detectedCodes.map((code) => code.rawValue))} ${new Date().getTime()}`
}
</script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants