Typescript Style Tabs: A Stylish Approach

I've been struggling to customize the tabs I created using this code, but nothing seems to be working as expected. This article was suggested to me recently, however, when I tried implementing it, I encountered errors. I also looked at this stackoverflow thread for guidance.

I attempted to use

and then created a CSS file to edit the tabs, which did work partially but didn't meet my requirements such as changing the font style, color of tab names, etc.

If anyone can provide some assistance or tips on how to properly style these tabs, I would greatly appreciate it. Thank you.

Answer №1

When formulating your question, please make sure to include a well-written code example.

However, here is the sample solution provided:

// index.js
import Tabs from "./tabs"
import "./style.css"

const App = () => {
    return (
        <Tabs>
            <span title="Apple">Apple is red</span>
            <span title="Banana">Banana is yellow</span>
        </Tabs>
    )
}

export default App
// tabs.js

import React, { useState } from 'react';
import TabTitle from './tabtitle';

const Tabs = ({ children }) => {
  const [selectedTab, setSelectedTab] = useState(0);

  return (
    <div>
      <ul className='tab-list'>
        {children.map((item, index) => (
          <TabTitle
            key={index}
            title={item.props.title}
            index={index}
            setSelectedTab={setSelectedTab}
            selectedTab={selectedTab}
          />
        ))}
      </ul>
      {children[selectedTab]}
    </div>
  );
};

export default Tabs;
// tabtitle.js

import React, { useCallback } from 'react';

const TabTitle = ({ title, setSelectedTab, index, selectedTab }) => {

    const onClick = useCallback(() => {
        setSelectedTab(index);
    }, [setSelectedTab, index]);

    return (
        <li className={`tab ${selectedTab === index ? 'active' : ''}`}>
            <button onClick={onClick}>{title}</button>
        </li>
    );
};

export default TabTitle;
// style.css

.tab {
    display: inline-block;
    padding: 8px;
}

.active > button {
    background-color: blue;
    font-weight: bold;
}

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 make the hoverzoom effect only apply to a specific object instead of all objects?

I'm currently troubleshooting my code in order to implement a hover effect on Mui <cards> when a user hovers over the object. I have managed to make it work partially, but the issue is that the effect gets applied to all objects instead of just ...

a timing mechanism that halts at a pre-determined point in time

Looking to create a countdown timer that begins at 8AM EST and ends at 6PM EST. Once the timer reaches 00:00:00, I'd like to hide the control on the website and have it reappear at the same time the next day. Any suggestions on how I can make this hap ...

Can the current website navigation be integrated into the blog script?

People might see me as a spoil sport for not using Wordpress, but I prefer a flatfile blog system for my small site. Currently, I am using the ozjournals-3.2 blog system. I need assistance in installing a header and site navigation on top of the blog page ...

A React component enclosed within a useCallback function

According to the React docs, to prevent a Child component from re-rendering in certain cases, you need to wrap it in useMemo and pass down functions in useCallback within the Parent component. However, I'm confused about the purpose of this implementa ...

Tips for seamlessly placing a div with a background image within a parent container, all while preventing any scrolling issues as the child div rotates

I created a simple demo featuring a square image with rounded corners to give it a circular appearance. The image rotates slowly, but as it moves diagonally, the horizontal width fluctuates, causing the parent container to resize. I want the parent contain ...

Deploying AWS CDK in a CodePipeline and CodeBuild workflow

I am currently attempting to deploy an AWS CDK application on AWS CodePipeline using CodeBuild actions. While the build and deploy processes run smoothly locally (as expected), encountering an issue when running on CodeBuild where the cdk command fails w ...

Issue: MUI Alert - For the data grid to function properly, each row must be assigned a unique `id` property

Trying to populate a Datagrid by fetching data from an API: const [invoiceList, setInvoiceList] = useState({id: 0}) useEffect(()=>{ axios.get("http://localhost:3001/invoices").then((response)=>{ setInvoiceList(response.data) ...

Vuetify's data table now displays the previous and next page buttons on the left side if the items-per-page option is hidden

I need help hiding the items-per-page choices in a table without affecting the next/previous functionality. To achieve this, I've set the following props: :footer-props="{ 'items-per-page-options':[10], &apo ...

The declared type 'never[]' cannot be assigned to type 'never'. This issue is identified as TS2322 when attempting to pass the value of ContextProvider using the createContext Hook

I'm encountering an issue trying to assign the state and setState to the value parameter of ContextProvider Here's the code snippet:- import React, { useState, createContext } from 'react'; import { makeStyles } from '@material-ui ...

Implementing edit fields within a table row upon clicking in a ReactJS project can be achieved without relying on the react

Is there a way in JSX to transform a specific row in a table into an editable input row when clicked, without relying on external libraries like react-table? <table className="table-data"> <tbody> <tr> <th>Name</th> ...

Laravel Breeze encountered an issue: Unable to locate a compatible version for @tanstack/[email protected]

As I venture into developing my first Laravel app with React on the front-end using Breeze, I diligently followed the setup instructions by installing PHP and Composer. Following the detailed steps outlined here and here, I executed the necessary commands: ...

What is the best way to specify types for a collection of objects that all inherit from a common class?

I am new to typescript and may be asking a beginner question. In my scenario, I have an array containing objects that all extend the same class. Here is an example: class Body{ // implementation } class Rectangle extends Body{ // implementation } class ...

Trouble with Angular: mat-sidenav not responding to autosize or mode changes when resized

Currently, I am working on integrating the mat-sidenav into a project. Most of the functionality is working well, but there is one particular issue that arises. When running the app locally in a browser and resizing the window tab multiple times, it opens ...

FormGroup not reflecting updated state

I have successfully created a functional component to render a list of select menus with menu items provided by an external source. The expected behavior is to re-render the list of selects when a select item is changed. I implemented state and initialized ...

Stop the content inside a div box from wrapping around the edges when using Bootstrap

Utilizing bootstrap to structure a webpage, I have organized three divs next to each other within a row. The widths of the divs are set as follows: 2 columns, 6 columns, and 4 columns in a 12 column layout. The middle div has a minimum width defined. As a ...

Can elements be eliminated from a div based on their order?

https://i.sstatic.net/XIUnsMRc.png Hello everyone, I am currently attempting to replicate this design using ReactJS, but I am facing challenges in getting my code to function as desired. The issue lies in positioning the profile picture next to the searc ...

Set the background color of a webpage depending on the data retrieved from the database when a specific radio button

I am facing a challenge with my radio buttons that are set to light up green when the page loads. However, I am working on an "order system" where I need a specific button or "locker" to appear red instead of green when the page loads. While I have succes ...

Tips for disabling Hot Module Reloading in NextJS

In my project built with ReactJS and NextJS, users can upload their pictures, manipulate faces, and animate them. However, we are facing an issue where the page automatically reloads due to HMR (Hot Module Replacement), causing all user data to be lost. ...

Using a try block inside another try block to handle various errors is a common practice in JavaScript

In an effort to efficiently debug my code and identify the location of errors, I have implemented a try-catch within a try block. Here is a snippet of the code: for (const searchUrl of savedSearchUrls) { console.log("here"); // function will get ...

Is there a way to have a fixed width div positioned in the center of the screen, with two additional divs on either side

I have come across this stunning image: https://i.stack.imgur.com/tUYMc.png It serves as a header for a website - click on it to see the full size. I am trying to replicate this using HTML, CSS, and images, but I'm struggling. It needs to be 100% w ...