Skip to content

[FR] Multi-root component support for custom directives #3914

@wenfangdu

Description

@wenfangdu

What problem does this feature solve?

With fragments support, components can potentially have more than one root node. When applied to a multi-root component, the directive will be ignored and the warning will be thrown.

Currently, custom directives like v-focus (which focuses an inner element) won't work on a multi-root component, can we add support for this use case?

const focus = async (el, containerVisible = true) => {
  if (containerVisible) {
    el = el.querySelector('input') ?? el.querySelector('textarea')

    await nextTick()

    el?.focus()
  }
}

createApp(App).directive('focus', {
  mounted(el, { value }) {
    focus(el, value)
  },
  updated(el, { value, oldValue }) {
    value === oldValue || focus(el, value)
  },
})
<!-- `v-cascader` is a multi-root component -->
<!--        👇 focus an inner input element currently is not possible -->
<v-cascader v-focus></v-cascader>

What does the proposed API look like?

Custom directives work on multi-root components.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions