Is there a way to customize the color of a React component from a different source?

I am currently utilizing a React component library called

vertical-timeline-component-react
.


    <Fragment>
      <Timeline>
        <Content>
          <ContentYear
            startMonth="12"
            monthType="text"
            startDay="24"
            startYear="2016"
          />
          <ContentBody style={{color: 'red'}} title="Amazing Title">
          </ContentBody>
        </Content>
        <Content>
          <ContentYear
            startMonth="12"
            monthType="text"
            startDay="24"
            startYear="2016"
          />
          <ContentBody title="Amazing Title">
          </ContentBody>
        </Content>
      </Timeline>
    </Fragment>

The code snippet above demonstrates the use of two instances of the Content component within the library's example. My goal is to customize the text color inside each ContentBody differently.

Initial attempts with inline styling like

<ContentBody style={{color: 'red'}} title="Amazing Title">
proved ineffective in achieving the desired result.

Subsequent investigation revealed the className of the text body as .jertxS. To address this, I created a separate styles.css file to modify the font color for this class. However, this change applied universally across all ContentBodys instead of individually.

Given the absence of specific customization templates within the library, I am seeking guidance on alternate techniques to achieve unique text colors for each ContentBody.

Answer №1

Don't hesitate to check the source code when you're unsure. Fortunately, in this modern age, most npm packages come with publicly available source code. The source code for this library can be found on GitHub. You can specifically view what the ContentBody component does here.

const ContentBody = (props) => {
    const { title, children } = props;

    return (
        <BodyComponent className='body-component'>
            <BodyComponentTitle className='title-body-component'>
                {title}
            </BodyComponentTitle>
            {children}
        </BodyComponent>
    );
};

It seems like they've used a fixed className of 'body-component' that you could potentially target. It doesn't seem like they are passing styles or classNames through props, so you'll have to work with what's available. Alternatively, you could fork and extend the library to suit your requirements, and even contribute back your changes for others to benefit from.

Depending on the extent of your modifications, you could also simply copy the source files into your project and start making direct alterations and use them. Since the project is licensed under MIT, you're free to integrate the code into your application.

Answer №2

Be cautious when utilizing this particular class, as it could potentially be altered during your subsequent build.

Prior to proceeding, verify if there is a color property available in their API. If not, explore the option of including a "className" parameter. If that doesn't work, consider employing a more intricate CSS selector such as 'div > div:nth-of-type > ...'

Answer №3

As mentioned by @Alan P, consider including a className attribute and defining a corresponding class in your CSS file.

component.js

<ContentBody className="custom-color" title="Awesome Title">
</ContentBody>

index.css

.custom-color{ color: blue !important }

This adjustment should take precedence over the existing color styles.

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

What is the method to establish the table format in HTML using several for each loops?

I need to arrange the table structure for product plans as follows: +------------------+---------------+---------------+ | product1 | product2 | product3 | +------------------+---------------+---------------+ | product1plan1 | product ...

How can the edit feature be implemented in AngularJS?

I am struggling with implementing an edit field in AngularJS. Can someone please help me with this? Here is the code for my CRUD operations: var app = angular.module('myApp', []) app.controller('myCtrl', ['$scope', functio ...

Having trouble keeping the passport authenticated during chai tests

I have been developing tests for routes that require authentication with passport and express-session. In normal circumstances, the routes authenticate successfully and everything functions as expected. However, during testing with chai, the authenticatio ...

Tips for using the identical function on matched elements

I am working on a code where I want each textbox input to change its corresponding image. The function is the same for all partners in the list (txt & img). I have looked around and found some similar posts, but I am struggling to make the function wor ...

Utilize a pre-defined layout in a Vue component that is sent as a property

As a complete beginner, I kindly ask for your patience as I navigate through the coding basics. I am looking to utilize a template specified in the prop. The template is located within the DOM. My intention for approaching it this way is to reuse the comp ...

Event listener is failing to execute the functions

Even though the inline onmouseover="verdadero()" function is properly set up Upon further inspection, it appears that while the event listener is added to the box element, the function is not being triggered when hovering over it, and console.lo ...

How to retrieve the column names of a table using Web SQL?

Working on extracting column lists from Web SQL (Chrome's local database). One approach is to gather information from sqlite_master. SELECT name, sql FROM sqlite_master WHERE type="table" AND name = "'+name+'"; As an example, here is a sam ...

Deactivate radio buttons when the table value is greater than or equal to one

On my webpage, there are radio buttons for 'Yes' and 'No'. When 'Yes' is selected, a hidden <div> appears where users can fill in fields. Upon clicking the 'Add' button, the information is displayed in a table. ...

Canceling pending asynchronous actions in React/Redux: A step-by-step guide

Imagine the scenario where a user navigates to a page and two asynchronous Redux actions are dispatched simultaneously to fetch related sets of data. If one of these fetches fails, the component will detect it and render an error component on the next cycl ...

Change object values to capital letters

Upon retrieving myObject with JSON.stringify, I am now looking to convert all the values (excluding keys) to uppercase. In TypeScript, what would be the best way to accomplish this? myObj = { "name":"John", "age":30, "cars": [ { "name":"Ford", ...

Implement OAuth for user authentication and registration using a customized username feature on an express.js platform

Within my node.js+express.js stack, I have enabled users to register/login using both Twitter and Facebook, as well as through a native registration form. To manage this functionality, I am utilizing everyauth for handling Twitter and Facebook authenticati ...

connecting parameters to functions in javascript

I'm attempting to execute a second query once the first query has been resolved by using the following code: const task1 = nextQuery => $.get('1.json', data => nextQuery()); const task2 = nextQuery => $.get('2.json', ...

What is the best way to keep tab content fixed when switching?

I've successfully implemented tab switching, but I need to make the content fixed. How can I achieve this so that no matter the length of the content, it remains fixed in its position when switching tabs? The current issue is that when the content is ...

Using Typescript to replicate Object.defineProperties

Is there a way to emulate Object.defineProperties from JavaScript in Typescript? I am interested in achieving something similar using the syntax of Typescript: Object.defineProperties(someObject.prototype, { property: {get: function() { return v ...

The message sent by the postman returns an empty JSON response

When attempting to send a request to test the MongoDB connection, I am getting an empty object as a response. I have checked and suspect that my test.http file is being treated as text instead of JSON... despite using app.use(express.json()) for parsing. T ...

Issue with JQuery dialog not triggering autocomplete

I have integrated the JQuery 1.7.2 and JQuery-UI 1.8.18 libraries with both http://docs.jquery.com/UI/Dialog and http://docs.jquery.com/UI/Autocomplete. On a standard page load, the autocomplete function works perfectly with a text box in a form. It' ...

What are some best practices for integrating CSS grid layouts with React for optimal results?

My current project involves developing a Single Page Application using ReactJs and CSS grid layouts for styling components. However, I've encountered an issue where the two technologies do not seamlessly integrate: CSS grid layouts are typically appli ...

Achieve the positioning of a Bootstrap navbar within a div without it getting wrapped by following

I have been attempting to overlay my navbar on top of a background image by nesting it within a div and using absolute positioning. However, I've run into an issue where the navbar-header/navbar-brand section causes the rest of the navbar to wrap onto ...

Guide on sending an AJAX request to a server

I'm currently working on implementing AJAX and have encountered a roadblock. Specifically, I need assistance with sending a request to the server when a user clicks on a specific image, expecting the server to return that image. While I know how the s ...

How can a controller in AngularJS detect when the back button of the browser is pressed

I've created a browser trivia game where authenticated players can select a game type, triggering a socket.io event in my Node.js server. The view then transitions to a "Searching for game" screen with a loading icon as the server waits for enough pla ...