Coder Social home page Coder Social logo

proof-of-concept-vue-composition-api-tabs's Introduction

Vue Composition API Tabs

View live example on Netlify.

In this repo, I built a Vue-powered tab-switcher.

In Vue 2, the classic way to solve this problem is with a compound component. More specifically, you could create a TabContainer component to hold shared state (i.e. an ID for the currently active tab), a Tab component that listens for click events and sets the active tab accordingly, and a TabPanel component to conditionally render the correct content.

<TabContainer>
  <Tab>...</Tab>
  <Tab>...</Tab>
  
  <TabPanel>...</TabPanel>
  <TabPanel>...</TabPanel>
</TabContainer>

With Vue 3, however, it's possible to store that shared state in a composition function, and fully eliminate the need for a TabContainer component.

Furthermore, it's possible to implement all event listening, attribute binding, and conditional rendering inside the composition function as well. The function can return refs, and the developer can simply attach those refs to their HTML to add all functionality offered by the composition function.

<!--
  A root element is included here for accessibility purposes,
  but it's not a component and doesn't manage any shared state.
-->
<div :ref="tablist.list.ref" class="tablist">
  <div
    v-for="({ tab, isSelected }, index) in tablist.tabs.values"
    :key="index"
    :ref="tablist.tabs.ref"
    class="tab"
    :class="isSelected ? 'selected' : ''"
  >
    {{ tab }}
  </div>
  <div
    v-for="({ panel }, index) in tablist.panels.values"
    :key="index"
    :ref="tablist.panels.ref"
    class="panel"
  >
    {{ panel }}
  </div>
</div>

Visit src/App.vue for a complete example of what this API looks like for the developer. Visit src/useTabList.ts to get a sense of the authoring experience.

Finally, be aware that this repo exists to demonstrate the basic concepts of authoring and consuming a particular Vue pattern—it's not a fully accessible demo. To demonstrate attribute binding, I added aria roles, but in an enterprise-grade solution, I would go further, authoring the useTabList composition function to attach additional event listeners to make sure the tablist is keyboard accessible.

Motivation

Compound components are a useful pattern, but I think the pattern I present in this repo offers some improvements:

  1. It allows the author to collocate logic, whereas compound components tend to force the author to spread related logic across various components.
  2. All state is managed and accessed in the same function scope, whereas compound components often force the auther to share state through provide/inject.
  3. This pattern returns full control over markup to the developer. It gives the developer a stronger feeling of writing plain HTML and CSS, sprinkled with :ref bindings and simple setup functions whenever advanced functionality is needed.

Meta

This example is a Vite app. To run locally:

  1. Clone the repo
  2. npm install
  3. npm run dev
  4. Visit http://localhost:3000

proof-of-concept-vue-composition-api-tabs's People

Contributors

alexvipond avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.