Skip to content

Conversation

Copy link

Copilot AI commented Oct 25, 2025

Enables passing HTMLElement or Vue refs directly to useReward, eliminating the need for string IDs when using Vue 3.5's useTemplateRef.

Changes

Type signature update

  • useReward now accepts MaybeRefOrGetter<string | HTMLElement> instead of just string
  • Handles refs, getters, and plain values reactively via toValue()

Runtime validation

  • getContainerById validates HTMLElement instances with instanceof check
  • Returns HTMLElement | null for proper null handling

Type safety

  • Added explicit types: RewardType, RewardConfig
  • Proper config typing for confetti/emoji/balloons variants

Usage

<script setup lang="ts">
import { ref } from 'vue'
import { useReward } from 'vue-rewards'

// New: pass template ref directly
const containerRef = ref<HTMLElement | null>(null);
const { reward } = useReward(containerRef, 'balloons');

// Still works: traditional string ID
const { reward: confetti } = useReward('my-id', 'confetti');
</script>

<template>
  <button @click="reward">
    <span ref="containerRef"></span>
    🎈 Balloons
  </button>
</template>

Demo

Fully backward compatible. Existing string ID usage unchanged.

Original prompt

This section details on the original issue you should resolve

<issue_title>Feature: Allow string | HTMLDivElement in constructor optionally</issue_title>
<issue_description>This will make the composable dynamic instead of static id.
It's also more ergonomic to work with Vue 3.5 with useTemplateRef.
Thanks!

<script lang="ts" setup>
  const containerRef = useTemplateRef('container')
  useReward(containerRef.value)
</script>

<template>
  <div ref="container">
  </div>
</template>
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Add support for string or HTMLDivElement in constructor Support HTMLElement in useReward for direct template ref usage Oct 25, 2025
Copilot AI requested a review from HamadaFMahdi October 25, 2025 19:52
Copilot finished work on behalf of HamadaFMahdi October 25, 2025 19:52
@HamadaFMahdi
Copy link
Owner

@@andrewspy

@andrewspy
Copy link

I tried the branch by specifying the following in package.json, but somehow VSCode can't recognise vue-rewards with the message below.

package.json

"vue-rewards": "github:HamadaFMahdi/vue-rewards#copilot/add-string-html-element-support"

Typescript error:

Cannot find module 'vue-rewards' or its corresponding type declarations.

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.

Feature: Allow string | HTMLDivElement in constructor optionally

3 participants