Steps for styling the header of an Antd Collapse Panel

I'm attempting to modify the text color in the header of an ant panel. Below is the entire ant collapse component code:

<Collapse accordion defaultActiveKey={defaultOpenPanel}>
  <StyledCollapsePanel key="tasksAssignedToMe" header={<TasksAssignedToMeHeader />}>
    <StyledTaskTable
      columns={COLUMNS}
      dataSource={tasksAssignedToMe}
      pagination={false}
      data-testid="tasksAssignedToMe"
      showHeader={false}
    />
  </StyledCollapsePanel>

  <StyledCollapsePanel key="tasksNotAssignedToMe" header="Tasks Not Assigned To Me">
    <StyledTaskTable
      columns={COLUMNS}
      dataSource={tasksNotAssignedToMe}
      pagination={false}
      data-testid="tasksNotAssignedToMe"
      showHeader={false}
    />
  </StyledCollapsePanel>

  <StyledCollapsePanel key="completedTasks" header="Completed Tasks">
    <StyledTaskTable
      columns={COLUMNS}
      dataSource={completedTasks}
      pagination={false}
      data-testid="completedTasks"
      showHeader={false}
    />
  </StyledCollapsePanel>
</Collapse>

I'm aiming to change the text color of the header in the last two StyledCollapsePanel's.

In my stylesheet, I included the following CSS styling:

export const StyledCollapsePanel = styled(Collapse.Panel)`
  .ant-collapse-content .ant-collapse-content-box {
    padding: 0px 0px 0px 28px;
  }
  .ant-col {
    color: 'hsl(214, 78%, 54%)';
    font-weight: 500;
    text-align: left;
    text-transform: uppercase;
  }
  .ant-collapse > .ant-collapse-item > .ant-collapse-header > .span {
    color: 'hsl(214, 78%, 54%)';
  }
`;

However, the style doesn't seem to be applying. I'm unsure of what I could be doing incorrectly here. Any suggestions?

Answer №1

Recently encountered the same issue and discovered in the documentation that the header parameter should be of type ReactNode

Here is how I resolved it:

const genExtra = () => (
  <span style={{color: 'green'}}> This text is green</span>
);

return (
           
          <Collapse defaultActiveKey={['1']} ghost>
            <Panel header={genExtra()} key="1">
              <p>{text}</p>
            </Panel>
            <Panel header="This is panel header 2" key="2">
              <p>{text}</p>
            </Panel>
            <Panel header="This is panel header 3" key="3">
              <p>{text}</p>
            </Panel>
          </Collapse>
  );

I hope this solution meets your requirements

Answer №2

Starting from version 5, there is no need to overwrite the class .ant-collapse-header. In version 5.0, Ant Design imports the style definition of components from the component's folder, which can be found at

https://github.com/ant-design/ant-design/tree/master/components/theme/interface/components.ts

import type { ComponentToken as CollapseComponentToken } from '../../collapse/style';

By visiting https://github.com/ant-design/ant-design/tree/master/components/collapse/style/index.ts

You will find the component's token defined in the Interface:

type CollapseToken = FullToken<'Collapse'> & {
  collapseContentBg: string;
  collapseHeaderBg: string;
  collapseHeaderPadding: string;  // This is what you were looking for
  collapsePanelBorderRadius: number;
  collapseContentPaddingHorizontal: number;
};

As you scroll down, you will see that collapseHeaderPadding is assigned a value from the tokens

const collapseToken = mergeToken<CollapseToken>(token, {
    collapseContentBg: token.colorBgContainer,
    collapseHeaderBg: token.colorFillAlter,
    collapseHeaderPadding: `${token.paddingSM}px ${token.padding}px`, // This is what you need
    collapsePanelBorderRadius: token.borderRadiusLG,
    collapseContentPaddingHorizontal: 16, // Fixed value
  });

These tokens are the Alias token Once you know the token name, you just need to overwrite the Alias token INSIDE the component token definition like this:

"components": {
        "Collapse": {
            "borderRadiusLG": 8,
            "colorBgContainer": "#EEF6F7 ",
            "colorBorder": "#ffffff",
            "collapseHeaderPadding": "8px 4px",
            "paddingSM": 8,
            "padding": 4
        },
}

You can save this in a JSON file and load it into ConfigureProvider, either using CssInJS or style object and pass it to the style attribute of the Collapse Panel.

Here is the final result Custom collapse panel header

Answer №3

To identify the header class, use inspect element and apply the following style:

.ant-collapse-header{
    color:rgb(70, 180, 30) !important;
}

This adjustment should resolve the issue at hand.

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

Frontend Axios request fails to retrieve necessary data from MongoDB backend database

Reaching out to the backend to retrieve posts from a mongoDB database. Successful in postman but encountering a custom error on the frontend. Puzzled as to why it's not functioning properly. Utilizing Redux to fetch user information for ...

Unable to use jQuery to choose an item from a dropdown menu and reveal a hidden text box

Seeking assistance in displaying a paragraph with text and a textbox when a specific option is selected from the dropdown menu on my form. Previously used code for radio buttons, but encountering issues with this scenario. Any guidance would be greatly app ...

Unable to modify font weight - Choose Ant Design Select Component

I'm new to programming and I'm working on creating a select component using ant-design. However, I am facing an issue where I cannot change the font-weight within ant-design. The component seems to accept all other properties that are passed exce ...

Using React and ReactQuill to trigger methods within a custom component directly from the toolbar

I found this code snippet in a demo on GitHub here: import React, { Component } from 'react'; import ReactQuill from 'react-quill'; import '../node_modules/quill/dist/quill.snow.css'; const CustomToolbar = () => ( < ...

I am unable to showcase the image at this time

Hey there, I'm having an issue with displaying an image stored inside the NextJS API folder. The alt attribute is showing up fine, but the actual image isn't displaying. When I console.log the image data, everything seems to be in place. Can anyo ...

What sets usePreloadedQuery apart from useQueryLoader?

I've been exploring graphQL and the react-relay library. These are the key points I've covered: Rendering Queries: where usePreloadedQuery is introduced. Fetching Queries for Render: where useQueryLoader is introduced. To keep it simple, I&apo ...

Troubleshooting jPlayer Preload Problem

Currently, I am utilizing HTML5 jPlayer to play audios through a recursive method. While everything is functioning smoothly and the audio is playing, there seems to be a delay when loading the new audio. To eliminate this delay between audios, I aim to pr ...

I am encountering an issue where Typescript paths specified in the tsConfig.app.json file are not resolving properly

I have defined path settings in my tsconfig.app.json file like this: "paths": { "@app/core": ["./src/app/core"] } Every time I run a test that includes import statements with relative paths, it throws the following error: ...

employing a parameterized type to accommodate a combination of two diverse items as input

I am facing a challenge with a simple use case and have been unable to find an example that covers it adequately. The situation is this: I have a function that should accept two different objects that are very similar. Therefore, I want to use a generic t ...

eBay API request error: "You do not have the necessary permissions to complete the request."

While working on integrating the eBay API, I encountered an issue with creating a payment policy. Following the instructions provided in this guide , I generated a token and sent it using Postman. However, I received an error: { "errors": [ ...

Retrieve an SVG file from the internet and incorporate it as a <svg /> element within a React component in a Next.js project

I have a situation where I am trying to style .svg files within an API being used by a NEXT.js application. Currently, the files are loaded using <img src="https://api.com/image.svg" />, but this method does not allow for styles to be appl ...

Modifying the color of specific sections of SVG elements

Interested in utilizing the d3.js timeknots component, a svg visualization that includes line and circle elements. My goal is to implement a stopwatch animation that dynamically changes the color of the svg visualization over time. I am contemplating crea ...

The importance of blending classes versus isolating classes in CSS selectors

Could somebody please direct me to the CSS hierarchy rules concerning "concatenated classes" versus "separated classes"? Furthermore, what are the correct terms for "concatenated classes" versus "separated classes" (I suspect that is why the documentation ...

Is it possible to implement Photoshop-inspired color curves filters on an HTML tag?

I am attempting to recreate the color curves filter from Photoshop using an HTML video tag. https://i.stack.imgur.com/erB1C.png The solution closest to what I need so far is how to simulate Photoshop's RGB levels with CSS and SVG Filters, but it doe ...

"Troubleshooting problems with the display:none property in a media

Encountering a small dilemma with media queries within my HTML. One of the sections in my code contains the following: <div class="header-left"> </div> <div class="header-center"> <ul> <li class="end"> ...

Transferring an image to Amazon AWS S3 before seamlessly delivering it to a React Native-built Mobile App

In my current situation, numerous individuals are utilizing my mobile application. This app is being built using React Native expo. Imagine that I upload an image to my Amazon AWS S3 bucket. My goal is for this image to be automatically sent to a specific ...

Ways to create collapsible navigation bars in your website

As someone exploring client-side development, I may be misusing the term "collapsible" in my title. What I aim to accomplish in my web application is allowing users to collapse header bars into small chevrons and expand them back when necessary. I am on t ...

Can you guide me on implementing first-person controls in react-three-fiber?

I'm attempting to set up a scenario where the camera remains stationary while allowing users to rotate the view around either the y or x axis by dragging their mouse. I'm looking for something akin to the functionality seen on threejs.org/example ...

Are you experiencing issues with the .submit() function when used in conjunction with other

Currently, I am working on a survey form that incorporates JQuery to dynamically display or hide fields based on user selections. //FORM CONDITIONALS: This script is responsible for hiding all the helpfulness radios and only displaying them when "Yes" fo ...

A guide on accessing the href attribute of an HTML tag using jQuery

My goal is to extract the ID from the href attribute of an a tag on my webpage. First, I need to prevent the page from refreshing before performing this task. The example a tags look like this: '<a href="/ArtPlaces/Delete/5">Delete</a>&ap ...