Skip to content

Conversation

@Bao0630
Copy link
Contributor

@Bao0630 Bao0630 commented Jun 9, 2025

#187 替换 onWatchCleanup useTemplateRef 实现vue向下兼容

Summary by CodeRabbit

  • New Features

    • Introduced a utility for template refs to enhance compatibility with Vue template ref usage.
  • Refactor

    • Updated watcher and effect cleanup logic throughout the app to use the standard Vue 3 cleanup API, improving code consistency and reliability.
    • Replaced deprecated or removed APIs with current best practices for cleanup in watchers and effects.
    • Adjusted imports to use the new template ref utility where applicable.
  • Chores

    • Removed unused or deprecated imports for cleaner and more maintainable code.

@netlify
Copy link

netlify bot commented Jun 9, 2025

Deploy Preview for antd-design-x-vue ready!

Name Link
🔨 Latest commit f111cd4
🔍 Latest deploy log https://app.netlify.com/projects/antd-design-x-vue/deploys/6846fca8639a8000081ca4e6
😎 Deploy Preview https://deploy-preview-348--antd-design-x-vue.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 9, 2025

Walkthrough

This update systematically replaces the deprecated or removed onWatcherCleanup utility with Vue's standard onCleanup callback for watcher and effect cleanup logic across multiple example and source files. Additionally, a local polyfill for useTemplateRef is introduced and adopted in relevant components, updating imports and usage accordingly.

Changes

Files/Groups Change Summary
docs/examples-setup/bubble/markdown.vue,
docs/examples/bubble/markdown.vue,
src/bubble/BubbleList.vue,
src/sender/useSpeech.ts
Replaced onWatcherCleanup with onCleanup in watchEffect callbacks for cleanup logic.
docs/examples-setup/sender/actions.vue,
docs/examples-setup/sender/basic.vue,
docs/examples-setup/sender/sendStyle.vue,
docs/examples/sender/actions.vue,
docs/examples/sender/basic.vue,
docs/examples/sender/sendStyle.vue,
src/bubble/hooks/useTypedEffect.ts,
src/attachments/DropArea.vue,
src/attachments/FileList/FileListCard.vue
Updated watcher callbacks to use onCleanup parameter instead of onWatcherCleanup for cleanup registration.
src/_util/hooks/useTemplateRefPolyfill.ts Added new polyfill hook: useTemplateRef, type alias TemplateRef, and exported knownTemplateRefs set.
src/attachments/Attachments.vue,
src/attachments/FileList/FileList.vue,
src/attachments/PlaceholderUploader.vue,
src/attachments/SilentUploader.vue
Updated import of useTemplateRef to use the new local polyfill module.
src/bubble/Bubble.vue Removed unused useTemplateRef import from Vue.

Sequence Diagram(s)

sequenceDiagram
    participant Component
    participant VueWatcher
    participant CleanupCallback

    Component->>VueWatcher: watch(..., (newVal, oldVal, onCleanup) => { ... })
    VueWatcher-->>Component: Triggers callback on change
    Component->>CleanupCallback: Register cleanup via onCleanup(() => { ... })
    VueWatcher-->>CleanupCallback: Calls cleanup function on invalidation/unmount
Loading
sequenceDiagram
    participant Component
    participant useTemplateRefPolyfill
    participant VueInstance

    Component->>useTemplateRefPolyfill: useTemplateRef('refKey')
    useTemplateRefPolyfill->>VueInstance: Attach shallowRef to refs['refKey']
    useTemplateRefPolyfill-->>Component: Return (readonly) shallowRef
Loading

Poem

In the garden of code where watchers dwell,
Old onWatcherCleanup we bid farewell.
Now onCleanup keeps our logic neat,
With polyfilled refs, our code’s complete.
The rabbits rejoice, their whiskers a-twitch—
For cleanup and refs now seamlessly switch!
🐇✨

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

docs/examples-setup/sender/actions.vue

Oops! Something went wrong! :(

ESLint: 8.57.1

Error: Failed to load parser 'vue-eslint-parser' declared in '.eslintrc.js': Cannot find module 'vue-eslint-parser'
Require stack:

  • /.eslintrc.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1405:15)
    at require.resolve (node:internal/modules/helpers:145:19)
    at Object.resolve (/node_modules/.pnpm/@eslint[email protected]/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:2346:46)
    at ConfigArrayFactory._loadParser (/node_modules/.pnpm/@eslint[email protected]/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3325:39)
    at ConfigArrayFactory._normalizeObjectConfigDataBody (/node_modules/.pnpm/@eslint[email protected]/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3099:43)
    at _normalizeObjectConfigDataBody.next ()
    at ConfigArrayFactory._normalizeObjectConfigData (/node_modules/.pnpm/@eslint[email protected]/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3040:20)
    at _normalizeObjectConfigData.next ()
    at ConfigArrayFactory.loadInDirectory (/node_modules/.pnpm/@eslint[email protected]/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:2886:28)
    at CascadingConfigArrayFactory._loadConfigInAncestors (/node_modules/.pnpm/@eslint[email protected]/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3871:46)
docs/examples-setup/sender/basic.vue

Oops! Something went wrong! :(

ESLint: 8.57.1

Error: Failed to load parser 'vue-eslint-parser' declared in '.eslintrc.js': Cannot find module 'vue-eslint-parser'
Require stack:

  • /.eslintrc.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1405:15)
    at require.resolve (node:internal/modules/helpers:145:19)
    at Object.resolve (/node_modules/.pnpm/@eslint[email protected]/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:2346:46)
    at ConfigArrayFactory._loadParser (/node_modules/.pnpm/@eslint[email protected]/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3325:39)
    at ConfigArrayFactory._normalizeObjectConfigDataBody (/node_modules/.pnpm/@eslint[email protected]/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3099:43)
    at _normalizeObjectConfigDataBody.next ()
    at ConfigArrayFactory._normalizeObjectConfigData (/node_modules/.pnpm/@eslint[email protected]/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3040:20)
    at _normalizeObjectConfigData.next ()
    at ConfigArrayFactory.loadInDirectory (/node_modules/.pnpm/@eslint[email protected]/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:2886:28)
    at CascadingConfigArrayFactory._loadConfigInAncestors (/node_modules/.pnpm/@eslint[email protected]/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3871:46)
docs/examples/sender/basic.vue

Oops! Something went wrong! :(

ESLint: 8.57.1

Error: Failed to load parser 'vue-eslint-parser' declared in '.eslintrc.js': Cannot find module 'vue-eslint-parser'
Require stack:

  • /.eslintrc.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1405:15)
    at require.resolve (node:internal/modules/helpers:145:19)
    at Object.resolve (/node_modules/.pnpm/@eslint[email protected]/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:2346:46)
    at ConfigArrayFactory._loadParser (/node_modules/.pnpm/@eslint[email protected]/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3325:39)
    at ConfigArrayFactory._normalizeObjectConfigDataBody (/node_modules/.pnpm/@eslint[email protected]/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3099:43)
    at _normalizeObjectConfigDataBody.next ()
    at ConfigArrayFactory._normalizeObjectConfigData (/node_modules/.pnpm/@eslint[email protected]/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3040:20)
    at _normalizeObjectConfigData.next ()
    at ConfigArrayFactory.loadInDirectory (/node_modules/.pnpm/@eslint[email protected]/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:2886:28)
    at CascadingConfigArrayFactory._loadConfigInAncestors (/node_modules/.pnpm/@eslint[email protected]/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3871:46)
  • 16 others
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/bubble/BubbleList.vue (1)

49-49: Consider removing or implementing the commented useTemplateRef line.

Since the PR objectives mention replacing useTemplateRef API, this commented line might need to be addressed. If useTemplateRef is no longer needed, consider removing the comment.

-// const listRef = useTemplateRef<HTMLDivElement>(null);
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 767371d and f111cd4.

📒 Files selected for processing (19)
  • docs/examples-setup/bubble/markdown.vue (2 hunks)
  • docs/examples-setup/sender/actions.vue (2 hunks)
  • docs/examples-setup/sender/basic.vue (2 hunks)
  • docs/examples-setup/sender/sendStyle.vue (2 hunks)
  • docs/examples/bubble/markdown.vue (2 hunks)
  • docs/examples/sender/actions.vue (2 hunks)
  • docs/examples/sender/basic.vue (2 hunks)
  • docs/examples/sender/sendStyle.vue (2 hunks)
  • src/_util/hooks/useTemplateRefPolyfill.ts (1 hunks)
  • src/attachments/Attachments.vue (2 hunks)
  • src/attachments/DropArea.vue (3 hunks)
  • src/attachments/FileList/FileList.vue (2 hunks)
  • src/attachments/FileList/FileListCard.vue (4 hunks)
  • src/attachments/PlaceholderUploader.vue (1 hunks)
  • src/attachments/SilentUploader.vue (1 hunks)
  • src/bubble/Bubble.vue (1 hunks)
  • src/bubble/BubbleList.vue (2 hunks)
  • src/bubble/hooks/useTypedEffect.ts (2 hunks)
  • src/sender/useSpeech.ts (3 hunks)
🔇 Additional comments (26)
src/bubble/Bubble.vue (1)

58-60:

✅ Verification successful

Inconsistent migration approach compared to other files.

While the current ref usage works correctly, other files in this PR are using the useTemplateRef polyfill. The TODO comment mentions a readonly warning issue, but this should be verified to ensure the polyfill doesn't have the same problem.


🏁 Script executed:

#!/bin/bash
# Check if other files successfully use the useTemplateRef polyfill without the readonly warning
ast-grep --pattern 'useTemplateRef<$_>($_)'

Length of output: 45


🏁 Script executed:

#!/bin/bash
# Search for any usage of useTemplateRef across the repository
rg -n "useTemplateRef" .

Length of output: 2705


🏁 Script executed:

#!/bin/bash
# Inspect the implementation of the useTemplateRef polyfill
sed -n '1,200p' src/_util/hooks/useTemplateRefPolyfill.ts

Length of output: 1350


🏁 Script executed:

#!/bin/bash
# Display the first 200 lines of src/bubble/Bubble.vue for context
sed -n '1,200p' src/bubble/Bubble.vue

Length of output: 5609


🏁 Script executed:

#!/bin/bash
# Search for divRef usage in the file to see its application in the template
rg -n "divRef" src/bubble/Bubble.vue

Length of output: 193


Ref polyfill not applicable in JSX—current ref approach is correct

The useTemplateRef polyfill is designed to work with string-based <template ref="key"> in SFCs, where it defines getters/setters on the component’s refs object. In JSX/TSX (<div ref={divRef} />), Vue treats the ref prop as an object ref and assigns the element directly to divRef.value. Since the polyfill wraps the underlying shallowRef in a readonly proxy in development, attempting to write to ret.value will trigger the warning you observed.

• In TSX files like Bubble.vue, continue using const divRef = ref<HTMLDivElement>(null)
• You can safely remove the commented-out useTemplateRef line and its TODO, or update the comment to note “polyfill only applies to template string refs, not JSX ref objects.”

No further changes required here.

src/attachments/Attachments.vue (1)

3-3: LGTM! Clean migration to polyfill.

The import changes correctly implement the migration from Vue's native useTemplateRef to the local polyfill, maintaining the same API while potentially improving Vue version compatibility.

Also applies to: 14-14

src/attachments/PlaceholderUploader.vue (1)

6-7: LGTM! Proper migration to polyfill.

The import restructuring correctly moves useTemplateRef to the local polyfill while maintaining other Vue imports. The implementation should remain functionally identical.

src/attachments/SilentUploader.vue (1)

2-4:

✅ Verification successful

LGTM! Consistent migration pattern.

The import changes correctly implement the migration strategy. Since this polyfill is now used across multiple components, ensure the polyfill implementation in src/_util/hooks/useTemplateRefPolyfill.ts handles all edge cases properly.


🏁 Script executed:

#!/bin/bash
# Verify the polyfill implementation exists and check its robustness
cat src/_util/hooks/useTemplateRefPolyfill.ts

Length of output: 1340


Approve: Consistent migration and robust polyfill implementation

I’ve reviewed useTemplateRefPolyfill.ts and the implementation handles edge cases—duplicate key registration, missing component instance, and readonly wrapping in dev mode—gracefully. No further changes are needed here.

docs/examples-setup/bubble/markdown.vue (1)

7-7: LGTM! Proper Vue 3 API modernization.

The changes correctly replace the deprecated onWatcherCleanup with the standard onCleanup parameter provided by watchEffect. This improves Vue compatibility while maintaining the same cleanup functionality.

Also applies to: 26-33

docs/examples/sender/sendStyle.vue (1)

5-5: LGTM! Correct Vue 3 watcher cleanup implementation.

The changes properly modernize the watch callback to use the onCleanup parameter instead of the deprecated onWatcherCleanup. The timer cleanup logic is preserved while improving Vue API compliance.

Also applies to: 17-27

docs/examples/bubble/markdown.vue (1)

7-7: LGTM! Consistent Vue 3 API modernization.

The changes correctly implement the same onWatcherCleanup to onCleanup migration pattern as other files, maintaining consistent cleanup behavior while using the proper Vue 3 API.

Also applies to: 27-37

src/bubble/hooks/useTypedEffect.ts (1)

2-2: LGTM! Critical core library API modernization.

This change in the core useTypedEffect hook properly migrates from onWatcherCleanup to the standard Vue 3 onCleanup parameter. The timeout cleanup logic for the typing effect is preserved while improving Vue compatibility across the library.

Also applies to: 36-46

src/attachments/DropArea.vue (2)

3-3: LGTM: Correct removal of deprecated import.

The onWatcherCleanup import has been properly removed since it's now replaced by the onCleanup parameter in the watch callback.


31-31: LGTM: Proper migration to Vue 3 standard cleanup API.

The watch callback has been correctly updated to use the onCleanup parameter instead of the deprecated onWatcherCleanup. The cleanup logic remains functionally equivalent, ensuring proper removal of drag event listeners.

Also applies to: 57-62

docs/examples-setup/sender/actions.vue (2)

4-4: LGTM: Import cleanup is correct.

The onWatcherCleanup import has been properly removed and replaced with the standard Vue 3 approach.


14-14: LGTM: Correct implementation of watcher cleanup.

The migration to using the onCleanup parameter from the watch callback is properly implemented. The timeout cleanup functionality remains identical while using the modern Vue 3 API.

Also applies to: 22-24

docs/examples-setup/sender/basic.vue (2)

4-4: LGTM: Consistent import migration.

The deprecated onWatcherCleanup import has been correctly removed.


14-14: LGTM: Proper watcher cleanup implementation.

The watch callback correctly uses the onCleanup parameter, and the timeout clearing logic remains functionally equivalent to the previous implementation.

Also applies to: 20-22

docs/examples-setup/sender/sendStyle.vue (2)

5-5: LGTM: Import statement properly updated.

The onWatcherCleanup import has been correctly removed as part of the Vue API migration.


15-15: LGTM: Consistent watcher cleanup migration.

The implementation correctly uses the onCleanup parameter from the watch callback, maintaining the same timeout cleanup functionality while following Vue 3 best practices.

Also applies to: 21-23

src/bubble/BubbleList.vue (1)

65-70: LGTM: Correct migration to Vue's standard cleanup pattern.

The watchEffect cleanup logic has been properly migrated from onWatcherCleanup to the standard onCleanup callback parameter. The initialization state management remains functionally equivalent.

src/sender/useSpeech.ts (1)

56-80: LGTM: Proper cleanup migration with memory leak prevention.

The watchEffect has been correctly updated to use the onCleanup callback parameter instead of onWatcherCleanup. The cleanup logic properly prevents memory leaks by nullifying the permission status onchange handler when the effect is invalidated.

docs/examples/sender/actions.vue (1)

15-26: LGTM: Correct watch cleanup pattern implementation.

The watch function has been properly updated to use the onCleanup callback parameter instead of onWatcherCleanup. The timer cleanup logic is correctly preserved, ensuring timeouts are cleared when the watcher is invalidated.

docs/examples/sender/basic.vue (1)

15-25: LGTM: Consistent cleanup pattern implementation.

The watch function cleanup has been correctly migrated to use the onCleanup callback parameter. The timer cleanup logic is properly preserved, maintaining the same behavior while using Vue's standard cleanup mechanism.

src/attachments/FileList/FileList.vue (2)

3-3: LGTM! Import updates align with Vue API modernization.

The removal of deprecated onWatcherCleanup and adoption of the local useTemplateRef polyfill properly supports the downward compatibility objectives.

Also applies to: 8-8


40-46: Correct migration to onCleanup callback pattern.

The watchEffect properly uses the onCleanup callback parameter instead of the deprecated onWatcherCleanup API. The logic remains functionally equivalent.

src/attachments/FileList/FileListCard.vue (2)

4-4: LGTM! Consistent import modernization.

The import changes mirror the pattern from other files in this refactor, correctly adopting the polyfilled useTemplateRef and removing deprecated APIs.

Also applies to: 13-13


165-179: Proper cleanup pattern implementation.

The watch correctly uses the onCleanup callback to manage the synced flag, preventing memory leaks and stale state updates during asynchronous operations.

src/_util/hooks/useTemplateRefPolyfill.ts (2)

1-11: Well-structured polyfill setup with proper Vue internals usage.

The imports and development mode detection follow Vue's internal patterns. The EMPTY_OBJ constant correctly provides an immutable reference for comparison.


14-40: Robust useTemplateRef implementation with comprehensive error handling.

The polyfill correctly:

  • Validates component instance context
  • Handles existing property descriptors
  • Provides helpful development warnings
  • Uses proper property descriptor configuration

Comment on lines +41 to +46
const ret = __DEV__ ? readonly(r) : r
if (__DEV__) {
knownTemplateRefs.add(ret)
}
return ret
} No newline at end of file
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Appropriate development mode safety measures.

The readonly wrapping in development mode and WeakSet tracking provide good developer experience while maintaining performance in production.

Verify that this polyfill is used consistently across the codebase:


🏁 Script executed:

#!/bin/bash
# Description: Check that useTemplateRef imports are consistently using the polyfill

# Search for any remaining direct Vue useTemplateRef imports
echo "=== Checking for direct Vue useTemplateRef imports ==="
rg "import.*useTemplateRef.*from ['\"]vue['\"]" --type ts --type vue

# Search for polyfill usage
echo "=== Checking polyfill usage ==="
rg "import.*useTemplateRef.*from.*useTemplateRefPolyfill" --type ts --type vue

Length of output: 403


🏁 Script executed:

#!/bin/bash
# Re-run import checks across TS and Vue files without relying on rg file types

echo "=== Checking for direct Vue useTemplateRef imports ==="
rg -g '*.ts' -g '*.tsx' -g '*.vue' "import.*useTemplateRef.*from ['\"][^'\"]*vue['\"]"

echo "=== Checking polyfill usage ==="
rg -g '*.ts' -g '*.tsx' -g '*.vue' "import.*useTemplateRef.*from.*useTemplateRefPolyfill"

Length of output: 1015


Inconsistent polyfill import detected

One example file still pulls useTemplateRef directly from Vue instead of the polyfill:

• docs/examples/attachments/basic.vue

- import { ref, computed, useTemplateRef } from 'vue'
+ import { ref, computed } from 'vue'
+ import { useTemplateRef } from '../../../src/_util/hooks/useTemplateRefPolyfill'

Please update this and any other occurrences to use the polyfill.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const ret = __DEV__ ? readonly(r) : r
if (__DEV__) {
knownTemplateRefs.add(ret)
}
return ret
}
- import { ref, computed, useTemplateRef } from 'vue'
+ import { ref, computed } from 'vue'
+ import { useTemplateRef } from '../../../src/_util/hooks/useTemplateRefPolyfill'
🤖 Prompt for AI Agents
In src/_util/hooks/useTemplateRefPolyfill.ts around lines 41 to 46, the code
correctly implements the polyfill with development mode safety measures.
However, the issue is that elsewhere in the codebase, such as in
docs/examples/attachments/basic.vue, useTemplateRef is imported directly from
Vue instead of this polyfill. To fix this, search the entire codebase for any
imports of useTemplateRef directly from 'vue' and replace them with imports from
the polyfill module to ensure consistent usage.

@@ -0,0 +1,46 @@
import { type ShallowRef, readonly, shallowRef, getCurrentInstance } from 'vue'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi,这段useTemplateRefPolyfill代码是原创或是引用自哪里吗?
我未在vue相关文档中了解到此代码,是否可以使用vue文档中提到的写法来代替useTemplateRef?
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useTemplateRef代码是基于vue仓库的,hooks的具体实现不是很复杂就实现了一个。如果需要改为同名ref的形式,我再提交一下修改。

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

等我看一下这个实现先

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

看起来vue源码中也仅是语法糖的封装,这个复刻实现是可行的👍

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Bao0630 运行 pnpm docs:dev 打开bubble页面有如下报错:
image

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Bao0630 如果报错不易解决,也可以替换为ref写法

@wzc520pyfm
Copy link
Owner

另一个让我纠结的点是,我们是否有必要将文档demo的写法也都改成vue3.3?我觉得文档中可以保留vue3.5的写法,毕竟这也是vue推荐的写法。
是否可以像vue那样加一段说明?比如:vue3.3需要替换useTemplateRef写法为xxxx

@Bao0630
Copy link
Contributor Author

Bao0630 commented Jun 26, 2025

另一个让我纠结的点是,我们是否有必要将文档demo的写法也都改成vue3.3?我觉得文档中可以保留vue3.5的写法,毕竟这也是vue推荐的写法。 是否可以像vue那样加一段说明?比如:vue3.3需要替换useTemplateRef写法为xxxx

在改代码的时候我也注意到了这个问题,我觉得两种都是可行的,因为最小改动的原因就没有修改demo中的写法。

我个人倾向是可以修改成3.3的写法,毕竟新的api目前来说使用并不广范,按照大部分使用vue的开发者的视角,阅读文档的疑问会少些,而且修改的demo的工作量应该不会特别大

@wzc520pyfm
Copy link
Owner

我个人倾向是可以修改成3.3的写法,毕竟新的api目前来说使用并不广范,按照大部分使用vue的开发者的视角,阅读文档的疑问会少些,而且修改的demo的工作量应该不会特别大

Agree!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants