Overwriting nested styles in React Bootstrap using Styled-Components

sutee

I'm using styled-components with React Bootstrap. We want a tab (Tab 3 in this example) to be colored based on a status. It works well when the tab is not active, but when it's active, the Bootstrap style overwrites the style.

I've identified the style that's overwriting my color in the console. When I use console to turn off the style, I see my intended tab color.

I'm just not sure how to target this style using style-components. I've tried experimenting with different ideas from the official doc but haven't been able to get it working.

My styled component:

 export const TabNavItem = styled(NavItem)`
      background-color: ${props => (props.colorize ? 'orange' : 'white')};
      }
    `;

Usage:

  render() {
    const { children, active, colorize } = this.props;
    return (
      <TabNavItem onClick={this.handleChangeLocation} active={active} colorize={colorize}>
        {children}
      </TabNavItem>
    );
  }

Color works fine when tab is not active:

Color works fine when tab is not active

When tab is active, color is covered by Bootstrap styles:

When tab is active, color is covered by Bootstrap styles

This is the style I need to overwrite (background-color):

This is the style I need to overwrite (background-color)

strider

I typically wouldn't apply styled-components directly to each react-bootstrap components, but instead create a single one to wrap the entire component scope and rely more on CSS classes to get things done.

For example in your case, I would do something like:

const StyledWrap = styled.div`
  & .nav-tabs > .active > li {
    background: white;
  }

  & .nav-tabs > .active.colorize > li {
    background: orange;
  }
`
const MyCustomNavbar = (props) => {
  return (
    <StyledWrap>
      /* ... */
        <NavItem active={true} />
        <NavItem className="colorize"/>
        <NavItem active={true} className="colorize" />
      /* ... */
    </StyledWrap>
  )
}

In my opinion, its a cleaner pattern that keeps your component specific CSS properly scoped, concentrated and comprehensive.

Generally, I find that applying styled-components only once per component scope keeps things simple and manageable. When you apply it in multiple places you end up doing a lot of prefixing and end up with code that looks like this:

<StyledNavbar>
  <StyledNav>
    <StyledNavItem active={true} />
    <StyledNavDropdown>
      <StyledMenuItem eventKey={1.1}>Action 1</StyledMenuItem>
      <StyledMenuItem eventKey={1.2}>Action 2</StyledMenuItem>
    </StyledNavDropdown>
  </StyledNav>
</StyledNavbar>

This is an extreme example, but you get the idea.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

React styled-components not applying styles to components

How to do overlapping styles with Styled Components React?

Overriding react components styles with styled component

Can not apply styles to custom components in React Native using styled-components?

How to create shared styles using styled components?

Styling nested components using styled components

Styled components not applying styles

How to apply styles to multiple components using styled components

Using props in React with styled components across components

React app with styled components and twin.macro set global styles

Share the same styles between two types of component with React Styled Components

React: Why not just use styles object instead of styled-components?

React-Router not correctly rendering Styled-Components Styles

How to override third party default styles using Styled Components

How to extend Link and NavLink with same styles using Styled Components

Using props inside react styled components

Using CSS attr() with styled components in react Hooks

How to style react styled components using classNames

Dynamically Styled Button in React Native using Styled Components

Where to put styled components styles

Multiple nested components in styled components

Using styled-components to style my own React components

On styling of class components using React styled-components

How to style a nested functional component using styled components

React - How to add new styles to a component using styled-component

Setup for React styled components

Using animation in styled components

Change inline styles to styled-components

idiomatic way to share styles in styled-components?