Skip to content

refactor: simplify slot handling in base-popper component#128

Merged
hexqi merged 2 commits intoopentiny:release/v0.2.xfrom
gene9831:composables/useSlotRefs
Jul 4, 2025
Merged

refactor: simplify slot handling in base-popper component#128
hexqi merged 2 commits intoopentiny:release/v0.2.xfrom
gene9831:composables/useSlotRefs

Conversation

@gene9831
Copy link
Copy Markdown
Collaborator

@gene9831 gene9831 commented Jun 27, 2025

  • Removed unnecessary complexity in trigger VNode management by introducing a new useSlotRefs composable.
  • Updated the base-popper component to utilize the new composable for better readability and maintainability.
  • Enhanced the handling of trigger VNodes and their references, improving overall performance and clarity.

Summary by CodeRabbit

  • New Features

    • Introduced a new utility to manage references for slot content, improving how slot elements are handled within components.
  • Refactor

    • Simplified internal logic for managing trigger elements and their references, resulting in cleaner and more maintainable components.
  • Chores

    • Updated shared composables to export new utilities for broader use across the application.

- Removed unnecessary complexity in trigger VNode management by introducing a new `useSlotRefs` composable.
- Updated the base-popper component to utilize the new composable for better readability and maintainability.
- Enhanced the handling of trigger VNodes and their references, improving overall performance and clarity.
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jun 27, 2025

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

A new composable, useSlotRefs, was introduced to manage Vue slot VNode references and normalization. The base-popper component was refactored to utilize this composable, replacing manual VNode and ref handling. The composables index was updated to re-export useSlotRefs, extending the shared composables API.

Changes

File(s) Change Summary
packages/components/src/base-popper/index.vue Refactored to use useSlotRefs for slot VNode and ref management, simplified event handler and watcher logic.
packages/components/src/shared/composables/useSlotRefs.ts Added new useSlotRefs composable for normalized slot VNode and ref management.
packages/components/src/shared/composables/index.ts Re-exported all exports from useSlotRefs, extending the composables API.

Poem

In the garden of slots, a new helper appears,
useSlotRefs brings order, dispelling old fears.
No more manual counting, or fragments to chase,
Just composable magic, with a rabbit’s swift grace.
Hopping through triggers, with refs in a row—
The code is now simpler, and ready to grow! 🐇✨

✨ Finishing Touches
🧪 Generate Unit Tests
  • Create PR with Unit Tests
  • Post Copyable Unit Tests in a Comment

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 auto-generate unit tests to generate unit tests for 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.

@gene9831
Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jun 27, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Copy Markdown

@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: 2

🧹 Nitpick comments (2)
packages/components/src/shared/composables/useSlotRefs.ts (2)

44-50: Consider using immediate: true for the length watcher.

The watcher adjusts the refs array length when the computed length changes. Using flush: 'post' is good, but you might want immediate: true to ensure the initial length is set correctly.

watch(
  length,
  (len) => {
    refs.value.length = len
  },
-  { flush: 'post' },
+  { flush: 'post', immediate: true },
)

5-5: Add JSDoc documentation for the composable.

The composable would benefit from comprehensive documentation explaining its purpose, parameters, and return values.

Add documentation:

+/**
+ * Composable for managing references to Vue slot VNodes
+ * @param slot - Optional slot function that returns VNodes
+ * @param renderAll - Whether to render all VNodes (true) or just the first one (false)
+ * @returns Object containing vnodes, refs, and helper functions
+ */
export function useSlotRefs(slot?: (...args: any[]) => VNode[], renderAll?: boolean) {
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between c19738b and da25af4.

📒 Files selected for processing (3)
  • packages/components/src/base-popper/index.vue (5 hunks)
  • packages/components/src/shared/composables/index.ts (1 hunks)
  • packages/components/src/shared/composables/useSlotRefs.ts (1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: gene9831
PR: opentiny/tiny-robot#59
File: packages/components/src/suggestion-popover/index.vue:131-133
Timestamp: 2025-05-27T03:35:11.008Z
Learning: In the SuggestionPopover component (packages/components/src/suggestion-popover/index.vue), the click handler can be bound unconditionally because the `show` computed property has a custom setter that prevents state mutations when `props.trigger === 'manual'`. This design centralizes trigger mode logic in the computed property rather than requiring conditional checks in event handlers.
packages/components/src/base-popper/index.vue (3)
Learnt from: gene9831
PR: opentiny/tiny-robot#59
File: packages/components/src/suggestion-popover/index.vue:131-133
Timestamp: 2025-05-27T03:35:11.008Z
Learning: In the SuggestionPopover component (packages/components/src/suggestion-popover/index.vue), the click handler can be bound unconditionally because the `show` computed property has a custom setter that prevents state mutations when `props.trigger === 'manual'`. This design centralizes trigger mode logic in the computed property rather than requiring conditional checks in event handlers.
Learnt from: gene9831
PR: opentiny/tiny-robot#59
File: packages/components/src/suggestion-popover/index.vue:0-0
Timestamp: 2025-05-27T03:45:56.392Z
Learning: In Vue components using VueUse's onClickOutside, calling stopPropagation() in the onClickOutside handler can prevent subsequent click event handlers on the same element from being triggered. This means that clicking a trigger element while a popover is open can successfully close the popover without immediately reopening it, even when the onClickOutside only listens to the popover element and not the trigger element.
Learnt from: gene9831
PR: opentiny/tiny-robot#123
File: packages/components/src/bubble/message/Message.vue:22-40
Timestamp: 2025-06-25T07:04:18.791Z
Learning: In the Message component (packages/components/src/bubble/message/Message.vue), the renderer resolution is intentionally not reactive to type changes - the component is designed to work with a fixed type that doesn't change after initialization.
🔇 Additional comments (6)
packages/components/src/shared/composables/index.ts (1)

1-1: LGTM! Clean export addition.

The export follows the existing pattern and properly exposes the new composable.

packages/components/src/base-popper/index.vue (5)

34-39: LGTM! Clean integration of the useSlotRefs composable.

The destructuring assignment properly extracts all needed values from the composable, and the parameter passing for renderAllTriggers is correct.


53-55: Good refactoring of event handler mapping.

The event handler mapping now uses the composable's triggerVNodes output, which is cleaner than the previous manual VNode management.


130-135: Improved template with safer key bindings.

The template now uses isVNode(vnode) ? vnode.key : index for safer key binding, which handles cases where the VNode might not have a key property.


113-117: Consider the impact of removing flush timing on the watcher.

The watcher for props.show had its flush timing changed. This could affect when the update() function is called relative to DOM updates.

Verify that the update() timing still works correctly with the new flush behavior:

#!/bin/bash
# Search for other similar watchers in the codebase to understand the pattern
rg -A 5 -B 2 "flush.*post.*show" packages/components/src/

4-5: I’ll search across the Vue package to locate isVNode in its type definitions:

#!/bin/bash
# Search for any export or definition of isVNode in Vue's shipped types
rg "export.*isVNode" -n node_modules/vue
rg "isVNode" -n node_modules/vue

Comment thread packages/components/src/shared/composables/useSlotRefs.ts
Comment thread packages/components/src/shared/composables/useSlotRefs.ts Outdated
- Simplified the logic for handling Fragment nodes in the `useSlotRefs` composable to return children of the first Fragment found in the VNode array.
- Enhanced the type checking in the `resolveRef` function to ensure proper handling of elements and Vue component instances, improving error messaging for invalid inputs.
@hexqi hexqi merged commit c810d5e into opentiny:release/v0.2.x Jul 4, 2025
@gene9831 gene9831 deleted the composables/useSlotRefs branch July 4, 2025 03:44
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.

3 participants