Skip to content

fix(custom-element): allow injecting values ​​from app context in nested elements (fix #13212) #13219

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

adrianbrs
Copy link

@adrianbrs adrianbrs commented Apr 17, 2025

Before this change, when a custom element was nested within another, the values provided via configureApp were ignored. Now, the provides object in the app context of a custom element inherits the provides object from its parent, allowing values to be injected properly using the app context in nested custom elements.

Note

In this implementation, values ​​provided in the configureApp method will take precedence over values ​​injected from parent elements. I believe this should be the expected behavior, since each custom element should have its own scope, but we can discuss it further if needed.

Close #13212

Playground:
https://deploy-preview-13219--vue-sfc-playground.netlify.app/#eNrdVk2P2jAQ/SuuL2QlIKraEw1ILeKwPXSrbm91D1EygGniWLZDkRD/vRNPPiDNIrqHql0OwZ55nnkzz3Zy5O+1nu5L4DMe2cRI7ZgFV2qWxWozF9zZg+ALoWSuC+PYkaWwlgqWpXVFvsogB+XGTKodJPivTbGXKbATW5siZyMMPHonlFBJoaxjD6UDw+ZDMYLgjs0X7CgUwx/B7TY2kCKewgcjMozuMGSHKuqgDcjPexip1AXGzz2GUAZLNooRh4BsjEWp3GPpNMHp9vXCVxCFODqz68WjJzZjR2J4ikJ9CfDr0O/J/e6+r/ig2/Pqu21WOBa2lijsaFGVp3H17Fq3lpvSAAobxFrfNQ7GcDatFWp7OWZtVwdh1E1E1W0lEGWsntRD6rKv4hZ52+ikA0b3g4nfeVWOf3wX+EJfxC7o5N2DMWiaXOjcQ1/KdcNmWG5lVkn3/x542tO+jr8p+IC8TXvlmgWvkvNW2ukGsEayTYCMjZC18r0FJMgTa8Z0UVfpMOut+eoW/1G+ZkP5Rjwn3ySplHlO1nplndsLXBOIQnoRYvtx4iDXWezAixEN9auWqe/zWTpBn+awiMIrzjr4IIQ49V0NqSg8487H+CqnW2G6s4XC973vmOBJkWuZgXnQTuLZERw3JCUVPM6y4udHb3OmBH/Q/ZotJD8G7Lvqa2GGg88GLJg9CN76XGxQRnKvHj/BAcetMy/SMkP0FecXsEVWVhwJ9qFUKdI+w3m29/5DRarNV7s6OFC2Kaoi2t2TguO3yfJK6R3dN9O3bQppLy6xyv2tkVhw3/lGB8HJ8b0+vSd++gXFBQq6

Summary by CodeRabbit

  • Bug Fixes

    • Improved warning messages when overwriting provided values, distinguishing between own and inherited properties.
    • Enhanced dependency injection logic for custom elements to correctly handle inherited values in nested scenarios.
  • Tests

    • Added a test to verify provide/inject inheritance and overriding across nested custom elements.
  • Refactor

    • Updated the inheritance mechanism for dependency injection in custom elements to use prototype chaining for more accurate context inheritance.

…sted elements

Before this change, when a custom element was nested within another, the values provided via `configureApp` were ignored. Now, the provides object in the app context of a custom element inherits the provides object from its parent, allowing values to be injected properly using the app context in nested custom elements.

Close vuejs#13212
@adrianbrs adrianbrs marked this pull request as ready for review April 17, 2025 22:34
Copy link

github-actions bot commented Apr 18, 2025

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 101 kB (+144 B) 38.3 kB (+47 B) 34.5 kB (+59 B)
vue.global.prod.js 159 kB (+144 B) 58.4 kB (+51 B) 52 kB (+96 B)

Usages

Name Size Gzip Brotli
createApp (CAPI only) 46.6 kB 18.2 kB 16.7 kB
createApp 54.5 kB (+6 B) 21.2 kB (+6 B) 19.4 kB (-3 B)
createSSRApp 58.7 kB (+6 B) 23 kB (+6 B) 20.9 kB (+5 B)
defineCustomElement 59.5 kB (+144 B) 22.8 kB (+40 B) 20.8 kB (+31 B)
overall 68.6 kB (+6 B) 26.4 kB (+5 B) 24 kB (+9 B)

Copy link

pkg-pr-new bot commented Apr 18, 2025

Open in StackBlitz

@vue/compiler-core

npm i https://pkg.pr.new/@vue/compiler-core@13219

@vue/compiler-dom

npm i https://pkg.pr.new/@vue/compiler-dom@13219

@vue/compiler-sfc

npm i https://pkg.pr.new/@vue/compiler-sfc@13219

@vue/compiler-ssr

npm i https://pkg.pr.new/@vue/compiler-ssr@13219

@vue/reactivity

npm i https://pkg.pr.new/@vue/reactivity@13219

@vue/runtime-core

npm i https://pkg.pr.new/@vue/runtime-core@13219

@vue/runtime-dom

npm i https://pkg.pr.new/@vue/runtime-dom@13219

@vue/server-renderer

npm i https://pkg.pr.new/@vue/server-renderer@13219

@vue/shared

npm i https://pkg.pr.new/@vue/shared@13219

vue

npm i https://pkg.pr.new/vue@13219

@vue/compat

npm i https://pkg.pr.new/@vue/compat@13219

commit: e73daea

@edison1105 edison1105 added 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. scope: custom elements labels Apr 18, 2025
…rnal instance

The internal instance of a custom element already inherits the app context’s provides object via the prototype chain, so there’s no need to override it or add extra logic to the `provide` function.
@edison1105 edison1105 added the ready to merge The PR is ready to be merged. label May 13, 2025
Copy link

coderabbitai bot commented May 13, 2025

Walkthrough

The changes update the dependency injection context handling for Vue custom elements. They refactor how provides objects are inherited using prototype chaining instead of direct assignment, improve warning messages for provide/inject key overwrites, and add a test to verify correct injection inheritance and overriding in nested custom elements.

Changes

File(s) Change Summary
packages/runtime-core/src/apiCreateApp.ts Refined the warning logic in the provide method to distinguish between overwriting own properties and inherited properties in the provides object, using the hasOwn utility.
packages/runtime-core/src/apiInject.ts Updated injection logic to check for custom elements and use the correct provides source, ensuring custom elements retrieve injected values from the app context when appropriate.
packages/runtime-dom/src/apiCustomElement.ts Changed the inheritance mechanism for the dependency injection context in VueElement to use prototype chaining via a new private _inheritParentContext method, replacing direct assignment of the provides object. Applied this method during parent setting and before app configuration.
packages/runtime-dom/__tests__/customElement.spec.ts Added a test verifying that nested custom elements correctly inherit and override injection contexts, and that appropriate warnings are emitted for missing injections and overwrites.

Sequence Diagram(s)

sequenceDiagram
    participant ParentCustomElement
    participant ChildCustomElement
    participant AppContext

    ParentCustomElement->AppContext: provide('key', value)
    ChildCustomElement->AppContext: inherit provides via prototype chain
    ChildCustomElement->AppContext: provide('key', newValue)
    ChildCustomElement->AppContext: inject('key')
    Note right of ChildCustomElement: Receives newValue (own), falls back to parent if not present
Loading

Assessment against linked issues

Objective Addressed Explanation
Ensure nested Vue custom elements have isolated injection scopes and can override parent provides (#13212)
Ensure inner custom elements can access their own provided values, with precedence over parent-provided ones (#13212)
Ensure proper warnings are emitted for missing injections and overwriting provides (#13212)

Suggested reviewers

  • LittleSound

Poem

In the garden where custom elements grow,
Rabbits hop and dependencies flow.
With prototype chains and warnings anew,
Each nested bunny gets its own view.
Inheritance works, the tests all pass,
Hooray for injection—now top of the class!
🐇✨


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 87cda11 and e73daea.

📒 Files selected for processing (1)
  • packages/runtime-core/src/apiCreateApp.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/runtime-core/src/apiCreateApp.ts
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: test / e2e-test
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 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.

@edison1105 edison1105 changed the title fix(custom-elements): allow injecting values ​​from app context in nested elements (fix #13212) fix(custom-element): allow injecting values ​​from app context in nested elements (fix #13212) May 14, 2025
@edison1105
Copy link
Member

/ecosystem-ci run

@vuejs vuejs deleted a comment from edison1105 May 14, 2025
@vue-bot
Copy link
Contributor

vue-bot commented May 14, 2025

📝 Ran ecosystem CI: Open

suite result latest scheduled
nuxt success success
router success success
pinia success success
test-utils success success
vue-i18n success ⏹️ cancelled
quasar success success
radix-vue success success
vite-plugin-vue success success
primevue success success
language-tools success success
vant success success
vitepress success success
vue-simple-compiler success success
vue-macros success success
vueuse success success
vuetify success success

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. ready to merge The PR is ready to be merged. scope: custom elements
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Injection breaks when nesting Vue custom elements
3 participants