Set styles to child elements of an external component using styled-components

In my Component.tsx file, I am using the following template:

import {Foo} from 'external-stuff'
<Foo>
   <p>bar</p>
</Foo>

The Foo component is designed to accept a className parameter.

const Foo = ({className, title}) => {
   const Container = styled.div`
      // some styles
    `
   const Title = styled.div`
      border: 0;
    `
   const Content = styled.div`
      margin: 10px;
    `
   return ( 
      <Container className={className}>
           <Title>{title}</Title> 
           <Content>{children}</Content>
      </Container>
   )

I would like to be able to apply custom styles to the Title and Content components within Component.tsx since I cannot edit Foo.tsx directly. For instance, I want to change the border style of Title to be 1px solid instead of the default 0.

Answer №1

Have you thought about creating styled versions of your Foo components using StyledComponents?:

import {Foo} from 'external-stuff'
import { Title, Content } from '//'

const StyledTitle = styled(Title)`
  border 1px solid;
`;

 return ( 
      <Container className={className}>
           <StyledTitle>{title}</StyledTitle> 
           <Content>{children}</Content>
      </Container>
   )

This approach has worked well for me in similar situations in my own projects!

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

Unique Text: "Personalized marker/pin for interactive map feature"

Looking to create a custom image map marker/pin with a unique bottom design resembling a union shape using CSS and Vue.js. I've attempted it myself but haven't been able to achieve the exact look as shown in the reference image. Any advice or ass ...

Enhance class names by utilizing addEventListener within the useEffect hook

I'm having trouble with creating a hamburger button menu. The issue I'm facing is that when I click the button, nothing happens. Even though I can log something in the toogleHamburger function and see it in the console, the styling does not chang ...

Manage image placement using CSS object-position

I have the following code snippet: img{ width: 100%; height: 1000px; object-fit: cover; object-position: left; } <!DOCTYPE html> <html lang="en"> <head> <meta charset ...

What is the best way to display just the selection outcome?

Currently, my code displays a full list of clinics. When I select a province in the dropdown menu, it only shows the clinics located in that specific province. I would like to modify this behavior so that the full list of clinics is not visible initially ...

Validation of existing input using YUP schema

Looking to design a validation schema that checks if the user's input already exists in an array. If it does, I want to display an error message for the user. For example, if the input is "apple" and the array contains ["apple", "mango", "banana"], th ...

Is there a way to add a disappearing large space that vanishes when a line break occurs?

I am currently working on a website where I aim to have the name and airport code displayed in a centered title above an image of the airport. https://i.sstatic.net/Vwi2V.png The name and code will be separated by an em space: div.terminal-silhouette- ...

How can I achieve a stylish scrolling header similar to a Google Blog for my website?

Google's blog features a unique header design - as you scroll down, the gray bar in the header moves up until it locks at the top of the screen. While CSS's position:fixed can achieve a similar effect, Google seems to have used a combination of p ...

When using TouchableWithoutFeedback, I encountered the following error message: "Error: React.Children.only expected to receive a single React element child."

I am encountering the following issue: Error: React.Children.only expected to receive a single React element child. Whenever I incorporate the 'TouchableWithoutFeedback' component in my ReactNative project: return ( <TouchableWithoutFeed ...

JS will reach its stopping point at the specified style.zIndex

I am currently in the process of setting up button elements. I have two scripts that correspond to different types of buttons - one script runs a simple collapse menu, while the other executes a more complex collapse by shifting depths and sliding one div ...

What is the method for displaying text and icons as black in a sticky header design?

Having trouble making the menu text and icons in the sticky header black on my website. I've been searching for the correct class in the CSS styles, but haven't been able to find it. I tried using this CSS: .header-wrapper .stuck { color: #000 ...

Is it possible to have a full-width thumbnail navigator in a full-width slider container with Jssor Slider?

I apologize if this topic has already been discussed, In a responsive full-width slider container, I am utilizing thumbnail navigator 03 with the scaling option disabled. Is it feasible to have a fixed-height, but fully (100%) width thumbnail navigator t ...

What is the best way to adjust the width of a textarea based on its content

How can I dynamically set the width of a React Semantic UI textarea based on its content? Setting min-width doesn't seem to be working. Any suggestions? <Textarea key={idx} defaultValue={formattedText} className="customInpu ...

Mobile navigation bar with Bootstrap transparency

I recently encountered a challenge with my Bootstrap navbar where I needed to remove the background. To achieve this, I applied the class "navbar-fixed-top" to the main wrapper. <nav class="navbar navbar-fixed-top"> After making this change, the na ...

Guide on converting HTML datetime picker datetime-local to moment format

I am looking to convert an input type : <input type="datetime-local" name="sdTime" id="stTimeID" onChange={this.stDateTime} /> into a specific date format: const dateFormat = 'MM/DD/YYYY hh:mm:ss a'; To achieve this, I need to transfer ...

Package Compiler & React Server Rendering

I recently integrated Parcel Bundler to bundle my client files for React Server Side Rendering. After setting up the Parcel Middleware and specifying the location of my client entry point, I noticed that although Parcel was bundling my files, the ReactDOM ...

Exploring Apollo GraphQL's comprehensive approach to handling errors at both the local

In my React Web application, I am utilizing Apollo to communicate with a GraphQL server. Error handling is a crucial aspect of the application, and for this purpose, I am relying on apollo-link-error. There are two distinct categories of errors that I nee ...

What is the purpose of including a viewport meta tag on my website if I already have CSS media queries in place?

As someone who is just starting out with building websites, I have been delving into the world of meta tags. However, even after conducting thorough research, I am still unsure about the necessity of a meta viewport tag when I am already utilizing CSS me ...

Timer countdown: Looking for a countdown timer that will reset to 3:00 automatically each time it reaches 0 minutes, and can do so

Seeking a continuous countdown timer that starts from 03:00 and counts down to 0:00, then automatically resets back to 03:00, in an infinite loop. The condition is that no one should be able to manually stop this logic. After researching, I found a Ja ...

What is the best way to conceal a Material UI button with CSS?

For my TODO react app, I am using a Material UI button. My goal is to create a form with an input field and a submit button, but I want the submit button to be invisible so that users think the form submits when they press the "Return" key. import { Tex ...

uniformly distribute the list items on the horizontal navigation bar

Does anyone know how to properly space out a ul in a navigation bar, with 5px padding on the right and left and the text evenly spaced in between? body { background: #ffffff; text-align: center; font-family: helvetica, arial, san ...