How do I incorporate scrolling into Material-UI Tabs?

I am currently incorporating Material-ui Tablist into my AppBar component. However, I am facing an issue with the responsiveness of the tabs. When there are too many tabs, some of them become hidden on smaller screens.

For more information on the component, you can refer to the documentation:

https://material-ui.com/components/tabs/

You can visually see the problem in action here:

https://codesandbox.io/s/nervous-hoover-809s0?file=/src/App.js

I am wondering if it is possible to implement a scrolling feature under the AppBar component or perhaps include navigation arrows when tabs exceed the visible area?

Potentially, the scroll or arrow functionality could be integrated within this part of the code snippet:

<AppBar position="static">
            <TabList onChange={handleChange} aria-label="simple tabs example">
              <Tab label="Business Info" value="1" icon={<ContactMailIcon />} />
              <Tab label="Financial" value="2" icon={<MonetizationOnIcon />} />
              <Tab
                label="Participants"
                value="3"
                icon={<AccessibilityIcon />}
              />
              <Tab label="Statistics" value="4" icon={<EqualizerIcon />} />
              <Tab label="Alerts" value="5" icon={<ReportProblemIcon />} />
              <Tab label="Health Care" value="6" icon={<FavoriteIcon />} />
              <Tab label="Plans" value="7" icon={<ListAltIcon />} />
              <Tab
                label="Benchmark"
                value="8"
                icon={<ListAltIcon />}
              />
              <Tab
                label="Heatmap"
                value="9"
                icon={<ListAltIcon />}
              />
              <Tab
                label="Diagnostic"
                value="10"
                icon={<ListAltIcon />}
              />
            </TabList>
          </AppBar>

To get a clearer understanding, please check out the codesandbox example provided above.

Answer №1

TabList has a special prop called variant="scrollable" which enables the scrollable feature and adds arrow indicators on the sides.

If you want to see a demo of scrollable tabs, check out this link: https://material-ui.com/components/tabs/#automatic-scroll-buttons

<TabList variant="scrollable" onChange={handleChange} aria-label="simple tabs example">
  <Tab label="Business Info" value="1" icon={<ContactMailIcon />} />
  <Tab label="Financial" value="2" icon={<MonetizationOnIcon />} />
  <Tab label="Participants" value="3" icon={<AccessibilityIcon />} />
  <Tab label="Statistics" value="4" icon={<EqualizerIcon />} />
  <Tab label="Alerts" value="5" icon={<ReportProblemIcon />} />
  <Tab label="Health Care" value="6" icon={<FavoriteIcon />} />
  <Tab label="Plans" value="7" icon={<ListAltIcon />} />
  <Tab label="Benchmark" value="8" icon={<ListAltIcon />} />
  <Tab label="Heatmap" value="9" icon={<ListAltIcon />} />
  <Tab label="Diagnostic" value="10" icon={<ListAltIcon />} />
</TabList>

If you'd like to see it in action, here's the codesandbox link: https://codesandbox.io/s/affectionate-firefly-z61em?file=/src/App.js

Answer №2

@Rajiv's solution is not compatible with mobile devices. To fix this issue, add the scrollButtons={true} and allowScrollButtonsMobile properties to your code in order to display left and right scroll buttons on all screen sizes:

<Tabs
  value={value}
  onChange={handleChange}
  variant="scrollable"
  scrollButtons
  allowScrollButtonsMobile
  aria-label="scrollable force tabs example"
>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to personalize the appearance of Static File in nextjs?

Is there a way to customize the display of static files, particularly images? For instance, when accessing a static file on a website like this: Currently, it just shows a basic img element. Is it possible to dynamically add additional elements to that d ...

Searching with Sequelize for an indirect relationship: How can it be done?

Within my database, there exists a relationship between Users and Roles. A User can have multiple Roles assigned to them. These Roles are connected to Activities in such a way that they can be associated with many different activities. This association all ...

Reformat a JSON file and save as a new file

I have a lengthy list of one-level JSON data similar to the example below: json-old.json [ {"stock": "abc", "volume": "45434", "price": "31", "date": "10/12/12"}, {"stock": "abc", "volume": "45435", "price": "30", "date": "10/13/12"}, {"stock": "xyz", "vo ...

Refreshin the attached DOM to a directive without a page reload

Within a directive, I have implemented some text and a video tag in the code below: app.directive('ngAzuremediaplayer', function () { return { restrict: 'AE', priority: 10, link: function (scope, elem, attr ...

The Express API will only provide the initial key-value pair of an object in the response

I'm working on fetching nested data objects. Although I can successfully retrieve the object data in the console, I encounter an issue when attempting to access this data using return res.json(imageObject). Instead of retrieving all key-value pairs of ...

JavaScript problem indicating an error that is not a function

Recently, I've delved into the world of JavaScript and am currently exploring JavaScript patterns. I grasp the concepts well but struggle with calling functions that are already in an object. var productValues = 0; var cart = function(){ this. ...

What is the best way to determine which watchers are triggered in Vue.js?

Within my parent component, there are numerous nested child components. Whenever a click occurs on one of the child components, it triggers an update to the route. The parent component watches the route property and performs certain actions when it change ...

Tips on keeping Bootstrap Modals out of your browsing history

Imagine this scenario A visitor lands on Page A, clicks through to Page B, and then engages with a link that triggers a modal (code provided below) <a href="mycontent.html" data-target="#modal_xl" data-toggle="modal" data-backdrop="static">Click me ...

When items are removed client-side, the Listbox becomes null

Given a Web Forms project inherited by me, I am relatively new to the field of Web development. The page in question features 2 listboxes: lstCaseLoad, containing "Caseloads" (ID numbers), and lstAssignedCaseLoad, filled with Caseloads chosen by the Form U ...

Error: Unable to encode data into JSON format encountered while using Firebase serverless functions

I am currently working on deploying an API for my application. However, when using the following code snippet, I encountered an unhandled error stating "Error: Data cannot be encoded in JSON." const functions = require("firebase-functions"); const axios = ...

Utilize Angular 5 to implement URL routing by clicking a button, while also preserving the querystring parameters within the URL

I have a link that looks like this http://localhost:4200/cr/hearings?UserID=61644&AppID=15&AppGroupID=118&SelectedCaseID=13585783&SelectedRoleID=0 The router module is set up to display content based on the above URL structure { path: & ...

Arranging JSON information based on category

I am currently populating an HTML table with data retrieved from a JSON file. It currently displays all the data in the order it appears in the JSON file, but I would like to organize it into different tables based on the "group" category in the file such ...

What is the most effective method for defining 2 routes that point to the same component?

Although it may seem straightforward, I'm struggling to find the most efficient method for this particular scenario in Vue.js. I am using Vue Cli 3 and I need to have multiple routes leading to the same Home page within the application. The idea is ...

Why does Vuetify/Javascript keep throwing a ReferenceError stating that the variable is undefined?

I'm currently developing in Vuetify and I want to incorporate a javascript client for Prometheus to fetch data for my application. You can find the page Here. Despite following their example, I keep encountering a ReferenceError: Prometheus is not def ...

The Vue.js input for checkboxes and radios fails to toggle when both :checked and @input or @click are used simultaneously

Check out this example on JSFiddle! <script src="https://unpkg.com/vue"></script> <div id="app"> <label> <input type="checkbox" name="demo" :checked="isChecked" @input=" ...

I am trying to figure out how to properly utilize server-only functions within Next.js middleware

In my current project, I am utilizing Next.js 13 along with the App Router feature. While attempting to include a server-specific fetch function in middleware.js, an error message is encountered: Error: Unable to import this module from a Client Compone ...

Enhancing data management with Vuex and Firebase database integration

Within my app, I am utilizing Firebase alongside Vuex. One particular action in Vuex looks like this: async deleteTodo({ commit }, id) { await fbs.database().ref(`/todolist/${store.state.auth.userId}/${id}`) .remove() .then ...

Angular $resource encounters a 400 Bad Request error when attempting a PUT request, triggering the $resolve and $promise

My service is structured as follows (with variables removed): angular .module('app') .factory('Employee', function($resource) { return $resource("https://api.mongolab.com/api/1/databases/:dbName/collections/:collectionN ...

waiting for elements in Nightwatch.js using FluentWait

I am seeking assistance on implementing FluentWait using nightwatch.js. How can I achieve this? Within my project, I have a global.js file that includes: waitForConditionPollInterval : 300, waitForConditionTimeout : 5000, However, it seems like this ...

Testing the Mongoose save() method by mocking it in an integration test

I am currently facing an issue while trying to create a test scenario. The problem arises with the endpoint I have for a REST-API: Post represents a Mongoose model. router.post('/addPost', (req, res) => { const post = new Post(req.body); ...