Skip to content

feat(custom-element): allow specifying additional options for shadowRoot in custom elements #12965

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

padcom
Copy link

@padcom padcom commented Feb 27, 2025

When creating shadowRoot using this.attachShadow() there are more options that can be very useful in specific scenarios.

https://developer.mozilla.org/en-US/docs/Web/API/Element/attachShadow#parameters

At the moment there is no way to pass those additional options (like delegatesFocus: true for focus management). This PR adds the ability to not only specify those but also to override the mode: 'open' should one choose to do that.

close #12964

Summary by CodeRabbit

  • New Features

    • Added support for specifying additional shadow root options (excluding mode) when defining custom elements.
  • Tests

    • Introduced a new (currently skipped) test for verifying initialization of shadowRoot with delegatesFocus.

@baiwusanyu-c baiwusanyu-c added the need test The PR has missing test cases. label Feb 27, 2025
@padcom padcom changed the title fix(runtime-dom): allow specifying additional options for for custom elements fix(runtime-dom): allow specifying additional options for shadowRoot in custom elements Feb 27, 2025
@padcom padcom changed the title fix(runtime-dom): allow specifying additional options for shadowRoot in custom elements feat(runtime-dom): allow specifying additional options for shadowRoot in custom elements Feb 27, 2025
@padcom padcom force-pushed the GH-12964 branch 2 times, most recently from 045fa73 to be80e79 Compare February 27, 2025 17:38
Copy link

github-actions bot commented Feb 28, 2025

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 101 kB (+26 B) 38.3 kB (+7 B) 34.5 kB (+30 B)
vue.global.prod.js 159 kB (+26 B) 58.5 kB (+9 B) 52 kB (+4 B)

Usages

Name Size Gzip Brotli
createApp (CAPI only) 46.6 kB 18.2 kB 16.7 kB
createApp 54.5 kB 21.2 kB 19.4 kB
createSSRApp 58.8 kB 23 kB 20.9 kB
defineCustomElement 59.5 kB (+26 B) 22.8 kB (+9 B) 20.8 kB (-3 B)
overall 68.6 kB 26.4 kB 24.1 kB

Copy link

pkg-pr-new bot commented Feb 28, 2025

Open in StackBlitz

@vue/compiler-core

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

@vue/compiler-dom

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

@vue/compiler-ssr

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

@vue/compiler-sfc

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

@vue/reactivity

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

@vue/runtime-core

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

@vue/runtime-dom

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

@vue/server-renderer

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

@vue/shared

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

@vue/compat

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

vue

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

commit: 416c35f

@edison1105 edison1105 added wait changes 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. and removed need test The PR has missing test cases. labels Feb 28, 2025
@edison1105 edison1105 added ready to merge The PR is ready to be merged. and removed wait changes labels Feb 28, 2025
@edison1105
Copy link
Member

Thanks @padcom
LGTM !

@padcom padcom requested a review from baiwusanyu-c March 3, 2025 08:56
@padcom
Copy link
Author

padcom commented Mar 4, 2025

Please excuse my impatience, but when can we expect this to be released? I have a project that makes heavy use of web components and it is something that's blocking the release of interactive components.

Copy link

coderabbitai bot commented May 19, 2025

Walkthrough

A new optional shadowRootOptions property was added to the CustomElementOptions interface and is now used when creating a custom element's shadow root, allowing options like delegatesFocus to be specified. A corresponding (skipped) test was introduced to verify that delegatesFocus is properly set, though it is disabled due to jsdom limitations.

Changes

File(s) Change Summary
packages/runtime-dom/src/apiCustomElement.ts Added shadowRootOptions to CustomElementOptions and updated VueElement to use these options when creating the shadow root.
packages/runtime-dom/tests/customElement.spec.ts Added a skipped test verifying initialization of shadowRoot with delegatesFocus.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant CustomElement
    participant ShadowRoot

    User->>CustomElement: Define custom element with shadowRootOptions (e.g., delegatesFocus)
    CustomElement->>CustomElement: Constructor invoked
    CustomElement->>ShadowRoot: attachShadow({ mode: 'open', ...shadowRootOptions })
    ShadowRoot-->>CustomElement: Shadow root created with specified options
Loading

Assessment against linked issues

Objective Addressed Explanation
Allow setting delegatesFocus and other options when creating shadowRoot in Vue custom elements (#12964)
Ensure custom element shadowRoot initialization supports focus delegation for subelements (#12964)

Poem

A shadowy root with options anew,
Now delegates focus, just for you!
Though tests must wait for jsdom’s might,
The code is ready—future’s bright.
Web components hop with glee,
Thanks to this small, sharp bunny spree! 🐇✨

✨ 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

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

🧹 Nitpick comments (1)
packages/runtime-dom/__tests__/customElement.spec.ts (1)

475-493: Good test coverage for the new feature.

The test correctly verifies that the delegatesFocus option is properly passed to the shadow root. Skipping the test due to jsdom limitations is appropriate, with clear comments explaining why it's skipped and referencing the relevant GitHub issue.

Given the jsdom limitation, consider adding a comment in the test description suggesting that this should be manually tested in a real browser environment.

// https://github.com/vuejs/core/issues/12964
// Disabled because of missing support for `delegatesFocus` in jsdom
// https://github.com/jsdom/jsdom/issues/3418
// eslint-disable-next-line vitest/no-disabled-tests
-test.skip('shadowRoot should be initialized with delegatesFocus', () => {
+test.skip('shadowRoot should be initialized with delegatesFocus (requires manual testing in a real browser)', () => {
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
Cache: Disabled due to data retention organization setting
Knowledge Base: Disabled due to data retention organization setting

📥 Commits

Reviewing files that changed from the base of the PR and between 163b365 and 416c35f.

📒 Files selected for processing (2)
  • packages/runtime-dom/__tests__/customElement.spec.ts (1 hunks)
  • packages/runtime-dom/src/apiCustomElement.ts (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
packages/runtime-dom/src/apiCustomElement.ts (1)
packages/shared/src/general.ts (1)
  • extend (24-24)
packages/runtime-dom/__tests__/customElement.spec.ts (2)
packages/runtime-dom/src/apiCustomElement.ts (1)
  • defineCustomElement (168-186)
packages/runtime-core/src/index.ts (1)
  • h (109-109)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: test / unit-test-windows
  • GitHub Check: test / e2e-test
🔇 Additional comments (2)
packages/runtime-dom/src/apiCustomElement.ts (2)

56-56: Great addition! This extends Vue's custom elements API.

Adding the shadowRootOptions property to the CustomElementOptions interface allows users to specify additional shadow DOM initialization options, such as delegatesFocus, while keeping the shadow root mode enforced as 'open'.


267-271: Well implemented shadow root options handling.

The implementation properly uses extend to merge the user-provided shadowRootOptions with { mode: 'open' }, ensuring the shadow root is always created with an 'open' mode while allowing additional configuration options to be passed.

This approach addresses the requirements from issue #12964 while maintaining compatibility with existing code.

@edison1105 edison1105 changed the title feat(runtime-dom): allow specifying additional options for shadowRoot in custom elements fix(custom-element): allow specifying additional options for shadowRoot in custom elements May 19, 2025
@edison1105
Copy link
Member

LGTM~

Maybe we can use vitest's browser mode for testing, but it's an experimental feature for now.
vitest.dev/guide/browser#configuration

@edison1105 edison1105 changed the title fix(custom-element): allow specifying additional options for shadowRoot in custom elements feat(custom-element): allow specifying additional options for shadowRoot in custom elements May 21, 2025
@edison1105 edison1105 added version: minor ✨ feature request New feature or request and removed 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. labels May 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to set focus inside of Vue.js-based web components
3 participants