Skip to content

Improved API for UI components, reducing boilerplate for forwarding attributes and events #5983

Closed
@chrisvfritz

Description

@chrisvfritz

What problem does this feature solve?

There are many cases where attributes passed to a Vue component should not be added to the root element, but rather a sub-element. For example, in this UI component, an incredible amount of props must be used to ensure that attributes are added to the input element, instead of the wrapper div.

Additionally, it's often desirable to expose all event listeners on a form element to the parent, which also requires a lot of boilerplate currently if the element is not the root (in which case, the .native modifier can solve the problem).

What does the proposed API look like?

EDIT: Start here to catch up on the discussion.

Currently by default, the "exposed" element (the one that arbitrary attributes can be added to) is always the root element. A new directive could be used to define a different exposed element. Some ideas for the name of the directive:

  • v-expose (probably my personal favorite)
  • v-expose-attrs (probably clearer, but lengthier)
  • v-main
  • v-primary

If v-expose is added to an element, it will accept attributes passed to its component - and these attributes will no longer be passed to the root element.

Other features that may be nice:

  • If the directive is defined on multiple elements, attributes will be duplicated across each of them
  • In the case where a subset of attributes should be accepted by an element, v-expose could accept a string or array of strings (e.g. v-expose="class" or v-expose="['class', 'type', 'placeholder']"). In this case, these attributes would be added to the element (again, instead of to the root element), but all other attributes would be added to the root element or to the element(s) with a valueless v-expose.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions