Skip to content

New common prop: as #4416

Closed
Closed

Description

@myasonik

The idea

<EuiAnyComponent as={tag} />

as should allow for passing any HTML tag name and would swap out the outermost element for whatever the user passes in.

Why?

Two reasons:

  1. A lot of minor a11y issues come from needing a different tag around a component. Sometimes the component can simply be wrapped by the consuming app (e.g., when the component is a div or something equally semantically empty) but other times that would change the meaning (e.g., when the component provides a semantically meaningful tag).
  2. Could be a nice dev convenience — reducing the amount of tag/component nesting just to get the right structure (e.g., instead of <EuiScreenreaderOnly><p>{text}</p></EuiScreenreadOnly> we can reduce a level of nesting by using <EuiScreenreaderOnly as="p">{text}</EuiScreenreadOnly>.

Inspired to open this issue by ongoing discussion in #4413 but I know the thought has crossed my mind before too so it seemed like time to open an issue for it.

Activity

  1. snide commented on Jan 6, 2021

    @snide
    Contributor

    Good idea! The naming pattern we've followed for this is component, and then we've named them Element inside. It's in use in several of the components similar to https://github.com/elastic/eui/blob/master/src/components/form/form.tsx. I bring it up not to advocate keeping that pattern, but to acknowledge we'd need to migrate from it if we shifted.

    Likely the devil is in the details here. It'd be nice to not have to write all the boilerplate we usually do...

      const Element = this.props.component;
    
      return (
        <Element className={classes} {...(rest as HTMLAttributes<HTMLElement>)}>
          {children}
        </Element>
      );
    
  2. myasonik commented on Jan 7, 2021

    @myasonik
    ContributorAuthor

    I'm not married to as being the prop name. If component is already in use in some places than keeping that might be worth it (though the naming does seem a little unclear imo). tag, wrappingTag, outerTag also come to mind as maybe more descriptive name options.

    The main thing here is to expose this prop on every EUI component.

    The boilerplate it requires is one thing but I am also kind of nervous of what happens to types with all this. EUI's types can be quite complicated sometimes and this will only make them more-so, I imagine.

    And another thing that'll need to be figured out is whether or not actually to expose this on every component or only components that have some sort of wrapping element. Like, does it make sense to have this prop on EuiButton or any of the form elements? Probably not... But should we for consistency's sake? 🤷‍♂️

    If people largely think this is a good idea, then we can start to explore those caveats and hammer out the details.

  3. chandlerprall commented on Jan 7, 2021

    @chandlerprall
    Contributor

    I'm on board overall, some quick thoughts:

    • as is the common pattern across libraries, regardless of other action we should deprecate the existing component props and replace with as

    • I'd hesitate to make this a sweeping, blanket change across every component. We have decided on strict tag/element usage in some cases for very specific reasons including semantics, screen reading, and keyboard accessibility. Allowing these components to be changed seems too reactive; EuiLink & EuiButton come to mind.

    • Agreeing with a previous comment, we'd want to find a way to enable this on components with as little boilerplate as we can

    • TypeScript support may suffer. It's very possible the "least boilerplate" approach means passing as loses a lot of type checking.

    • Coming back to what components we would or wouldn't do this with, we should get a feel for which we'd expect to include in the change.

  4. cchaos commented on Jan 11, 2021

    @cchaos
    Contributor

    +++++ To all the points made and only adding that, I like the approach and direction it's heading. I've seen this pattern work well with things like Chakra UI, but do worry about the Typescript part of it. So a "pattern" or "service" that will help us to use the same setup in components where we want it will be ideal.

  5. cchaos commented on Feb 16, 2021

    @cchaos
    Contributor

    I'm actually running into this right now where I really would like EuiPanel to render as a different element like <main> for the page layout stuff. I tried re-creating how EuiPageBody is handling it, but ran into issues when the panel could be a button because of an onClick.

  6. cchaos commented on Feb 23, 2021

    @cchaos
    Contributor

    I think mostly what I'm looking for at the moment, is a repeatable TS pattern we can start using in components where we want this behavior.

  7. snide commented on Feb 23, 2021

    @snide
    Contributor

    Another example of where this came up (with avatars).

    #4549 (comment)

  8. cchaos commented on Mar 19, 2021

    @cchaos
    Contributor

    Just as a note, this is going to be pretty tricky to get right with Typescript.

    For example, just about this whole folder by Tailwind is dedicated to supporting this in TS: https://github.com/tailwindlabs/headlessui/tree/main/packages/%40headlessui-react/src/utils

  9. github-actions commented on Sep 16, 2021

    @github-actions

    👋 Hey there. This issue hasn't had any activity for 180 days. We'll automatically close it if that trend continues for another week. If you feel this issue is still valid and needs attention please let us know with a comment.

  10. added
    stale-issue
    on Sep 16, 2021
  11. cchaos commented on Sep 16, 2021

    @cchaos
    Contributor

    We really need some general guidance on this. Have we found a pattern that we like yet? If so, can we document it?

  12. removed
    stale-issue
    on Sep 16, 2021
  13. github-actions commented on Mar 15, 2022

    @github-actions

    👋 Hey there. This issue hasn't had any activity for 180 days. We'll automatically close it if that trend continues for another week. If you feel this issue is still valid and needs attention please let us know with a comment.

  14. added
    stale-issue
    on Mar 15, 2022
  15. removed
    stale-issue
    on Mar 15, 2022
  16. github-actions commented on Sep 12, 2022

    @github-actions

    👋 Hey there. This issue hasn't had any activity for 180 days. We'll automatically close it if that trend continues for another week. If you feel this issue is still valid and needs attention please let us know with a comment.

  17. added
    stale-issue
    on Sep 12, 2022
  18. github-actions commented on Sep 19, 2022

    @github-actions

    ❌ We're automatically closing this issue due to lack of activity. Please comment if you feel this was done in error.

  19. added
    stale-issue-closed
    on Sep 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stale-issuestale-issue-closed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions