Questions tagged [styled-components]

styled-components is a JavaScript library for styling React applications. It removes the mapping between styles and components, and lets you write actual CSS augmented with JavaScript.

styled-components is an open source project developed by Glen Maddern and Max Stoiber that aims to make styling component-based systems, right now solely focussed on React, easier.


This is what using styled-components looks like:

import React from 'react';
import styled from 'styled-components';

// Create a <Title> react component that renders an <h1> which is
// centered, palevioletred and sized at 1.5em
const Title = styled.h1`
  font-size: 1.5em;
  text-align: center;
  color: palevioletred;
`;

// Create a <Wrapper> react component that renders a <section> with
// some padding and a papayawhip background
const Wrapper = styled.section`
  padding: 4em;
  background: papayawhip;
`;

These two variables, Title and Wrapper, are now a React components you can render like any other React component:

<Wrapper>
  <Title>Hello World, this is my first styled component!</Title>
</Wrapper>

The above code rendered in the browser(Live demo)

For more information and examples see the official documentation!

4586 questions
1
vote
1 answer

How to ignore style changes in styled-components during snapshot tests in jest

Assume i have a simple component I wish to snapshot test: const Component = styled.div`{ color: red; }` My snapshot test wishes to ensure that the div renders, but the styling is not important. So if I did a snapshot test on this Component, and i…
plusheen
  • 1,128
  • 1
  • 8
  • 23
1
vote
2 answers

Override specific component`s child div with styled-components on layout page

I have layout page component where all other components divs are having rule overflow-y: hidden applied. Rules are applied with use of styled-components. I want one specific (named "Filters") component`s child div to be visible. I tried something…
P H
  • 127
  • 3
  • 15
1
vote
0 answers

How to share same attributes in styled components?

I am using "useWindowDimensions" from react native: import { useWindowDimensions } from 'react-native'; I am getting the width and height like this: const dimensions = useWindowDimensions(); ...and passing it as a prop: return (
1
vote
1 answer

Cannot override styles with styled component in react-phone-input-2

Styles can be overridden with additional CSS file imported but not with the styled component. Why? import React from "react"; import styled from "styled-components/macro"; import PhoneInput from "react-phone-input-2"; import…
Julia
  • 674
  • 1
  • 6
  • 18
1
vote
1 answer

Styled-components add styles to custom component

I want to add styles to the custom component with a help of the styled-components library. I found some similar questions on StackOverflow with answers but it seems not to work for me. I do something like this: Here is my custom component const…
zakharov.arthur
  • 187
  • 2
  • 13
1
vote
1 answer

Mui 5 and styled components - Problem with overriding theme base values

I'm using Mui5 with SC and I have issues with overriding theme base values. I have base theme like this for base mui components, for example: const theme = createTheme({ components: { MuiButton: { styleOverrides: { root: { …
1
vote
2 answers

css transform: translate transition behaving strangely

On this sandbox, I've recreated the classic sliding-puzzle game. On my GameBlock component, I'm using a combination of css transform: translate(x,y) and transition: transform in order to animate the sliding game-pieces: const StyledGameBlock =…
itaydafna
  • 1,968
  • 1
  • 13
  • 26
1
vote
1 answer

How to use 'not' pseudo-class in styled components

I want to apply styling to all divs that are not children of 'thirdParty' container. To do this, I expected this to work: const GlobalStyle = createGlobalStyle` div{ &:not(.thirdParty *) { ...styles... } } ` But this causes the…
Rollie
  • 4,391
  • 3
  • 33
  • 55
1
vote
4 answers

make container invisible but not content

I have a map and on the map i have zoom in and zoom out functionality, my question is how to make the red part invisible but not buttons, so when user searchs location on the map, this div should not block the view, only thos button should be…
walee
  • 575
  • 4
  • 16
1
vote
0 answers

Error while using next/image with MDX content - `validateDOMNesting`

I'm creating a Next.js blog using mdx-bundler and styled-components. I'm using remark-mdx-images to import images into JSX. I created a custom component (styled.div) to embed the next/image in it. But when I run the blog in browser, i'm receiving an…
Gangula
  • 5,193
  • 4
  • 30
  • 59
1
vote
2 answers

Getting "Types of property 'accessibilityRole' are incompatible" error

Am getting typescript error when i extend th TextProps from react-native and pass it to the Text component created by styled-component Overload 1 of 2, '(props: Omit & { styles?:…
gokulsgr
  • 166
  • 5
1
vote
2 answers

white-space and text-overflow styles doesn't works in react native

I'm trying use styled_components to replace some texts when line is break with the follows css styles: white-space: nowrap; overflow: hidden; text-overflow: ellipsis; But when i use text-overflow and/or white-space styles, i have a error, because…
Fellipe Neves
  • 41
  • 1
  • 4
1
vote
1 answer

Using styled-components to show componentName_fileName in inspector

I've created a new create-react-app as a test to see if I can get the styled-components to show the component name and file name when debugging a component as I have tried adding this to multiple projects without any success. I will outline my steps…
Andy_a
  • 21
  • 3
1
vote
1 answer

Continuous cache-misses from Cloudfront when using HTML img tag, but getting cache-hits with Postman/browser requests

I have a Cloudfront distribution with a S3 origin bucket (using Cloudformation/SAM to delegate resources) that's acting strangely. The S3 bucket only stores images. Whenever I request an image through the src attribute, the network tab in dev-tools…
1
vote
1 answer

OnClick toggles all items

I want to toggle each of the item that I clicked on but Its keeps toggling all the Items. Using the useContext api import React, { useState, useEffect } from "react"; const MyContext = React.createContext({ …
1 2 3
99
100