use styled-components to share styles between components/elements

Joji

I am using styled-components along with Gatsby. I used styled-components to style the Link component provided by Gatsby for my home page.

const HomePageLink = styled(Link)`
  display: inline-block;
  font-size: ${fontSizes.xxlarge};
  text-decoration: none;
  box-shadow: none;
  :link,
  :visited {
    color: ${colors.slate};
  }
  :hover,
  :active {
    color: ${colors.red};
  }
`

However I realize that I also need to style a plain html <a /> tag using the exactly same style. I wonder is there a way to make the style component above adapt to <a /> tags without duplicating the code like this

const HomePageAnchorTag = styled.a`
  display: inline-block;
  font-size: ${fontSizes.xxlarge};
  text-decoration: none;
  box-shadow: none;
  :link,
  :visited {
    color: ${colors.slate};
  }
  :hover,
  :active {
    color: ${colors.red};
  }
`
Dennis Vash

Yes, use css API which allows you to construct reusable CSS blocks.

A helper function to generate CSS from a template literal with interpolations.

import styled, { css } from 'styled-components';
const reusableCSS = css`
  display: inline-block;
  font-size: ${fontSizes.xxlarge};
  text-decoration: none;
  box-shadow: none;
  :link,
  :visited {
    color: ${colors.slate};
  }
  :hover,
  :active {
    color: ${colors.red};
  }
`;

const HomePageAnchorTag = styled.a`
  ${reusableCSS}
`;

const HomePageLink = styled(Link)`
  ${reusableCSS}
`;

Usually, you will notice a mixins.js file that contains exports of reusable css block, for example:

// mixins.js
const flexCenter = css`...`
export default { flexCenter };

// usage inside styled components.
${mixins.flexCenter}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

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

idiomatic way to share styles in styled-components?

Styled components not applying styles

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

Where to put styled components styles

React styled-components not applying styles to components

How to do overlapping styles with Styled Components React?

Change inline styles to styled-components

Overriding react components styles with styled component

How to create shared styles using styled components?

Extending styles with styled-components not working

Styled-components use function in styled object

How to use global or shared styles between components in ionic 4?

Share object between components

How to apply styles to multiple components using styled components

Is it wrong to use Subject observables to share data between parent and child components?

Styled Components - Inline styling overlaps the media query styles

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

How to import Ant Design css styles in GlobalStyles of styled-components.?

Can I set styles to document body from styled-components?

styled-components: extend styles and change element type

How to add multiple styles based on property with styled-components?

styled-components - how to set styles on html or body tag?

Overwriting nested styles in React Bootstrap using Styled-Components

How to override third party default styles using Styled Components

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

Styled-Components: specify styles of children on parents hover

Best way to override child styles in styled-components

React-Router not correctly rendering Styled-Components Styles