3

I want to export styled-components/macro from my own .ts file but it is not working.

custom-styled.ts

import styled  from 'styled-components/macro'
import * as styledComponents from 'styled-components' 


import { ThemeInterface } from './theme'

const {
  css,
  createGlobalStyle,
  keyframes,
  withTheme,
  ThemeProvider,
} = styledComponents as styledComponents.ThemedStyledComponentsModule<ThemeInterface>


export { css, createGlobalStyle, keyframes, ThemeProvider, withTheme }
export default styled

App.tsx: when I import it directly from import styled from 'styled-components/macro' it gives intended debugging features. But when I try from my own file, there is a problem. Thank you beforehand.

I tried with export { css, createGlobalStyle, keyframes, ThemeProvider, withTheme, styled }. But without success.

import React from 'react';
import styled from './custom-styled'

const StyledContainer = styled.div`
background: black;
`
const StyledSpan = styled.span`
  color: white;
  font-size: 20px;
`

function App() {
    return (
        <StyledContainer>
            <StyledSpan>
                Test
            </StyledSpan>
        </StyledContainer>
    );
}

export default App;

Expected result should be: enter image description here

Jasurbek Nabijonov
  • 1,607
  • 3
  • 24
  • 37
  • Perhaps this post will help you [How to easily inspect styled-components using dev tools?](https://stackoverflow.com/a/45505519/15545116) – Anton Oct 17 '21 at 13:35
  • @ЖнецЪ Solution using react-app-rewrite I think is not optimal for v4.x+ as it said in documentation. I think I need to write a GitHub issue in the styled-components repo about this problem. Thank you for helping. – Jasurbek Nabijonov Oct 18 '21 at 09:07

1 Answers1

2

Edit dazziling-code

If you are using typescript you must import @types/styled-components:

npm i @types/styled-components

Also needs babel-plugin-styled-components:

npm install --save-dev babel-plugin-styled-components

And create a .babelrc config file in the root derictory.

.babelrc

{
  "plugins": [
    [
      "babel-plugin-styled-components",
      {
        "ssr": true,
        "minify": true,
        "transpileTemplateLiterals": false,
        "pure": false,
        "displayName": false, // generate another classname
        "fileName": false, // generate another classname
        "preprocess": false
      }
    ]
  ]
}

App.tsx

import styled, {
  Thing,
  GlobalStyle,
  StyledContainer,
  StyledSpan
} from "./custom-styled";
import Reusable from "./Reusable";

const StyledContainer2 = styled(StyledContainer)``;
const StyledSpan2 = styled(StyledSpan)``;
const StyledSpan3 = styled(StyledSpan)`
  color: orange;
  font-weight: bold;
  font-size: 1.5em;
`;

function App() {
  return (
    <StyledContainer2>
      <GlobalStyle />
      <Thing>Thing from suctom-styled</Thing>
      <StyledSpan2>StyledSpan</StyledSpan2>
      <StyledSpan3>Extend StyledSpan</StyledSpan3>
      <Reusable />
    </StyledContainer2>
  );
}

export default App;

custom-styled.ts

import styled, {
  css,
  createGlobalStyle,
  keyframes,
  ThemeProvider
} from "styled-components/macro";

export const GlobalStyle = createGlobalStyle`
  body {
    overflow: hidden;
    background-color: lightcoral;
  }
`;
export const Thing = styled.div`
  color: red;
`;

const move = keyframes`
  50%{ transform: translateX(100px)}
`;

export const StyledContainer = styled.div`
  display: flex;
  flex-flow: column;
  background: black;
`;
export const StyledSpan = styled.span`
  color: white;
  font-size: 20px;
  animation: ${move} 2s linear infinite;
`;

export { css, createGlobalStyle, keyframes, ThemeProvider };
export default styled;

Reusable.tsx

import { Thing, StyledContainer, StyledSpan } from "./custom-styled";

function Reusable() {
  return (
    <StyledContainer>
      <Thing>Reusable Thing</Thing>
      <StyledSpan>Reusable StyledSpan</StyledSpan>
    </StyledContainer>
  );
}

export default Reusable;

DOM inspector local

enter image description here

React DevTool local

enter image description here

Anton
  • 8,058
  • 1
  • 9
  • 27
  • Hello. Thank you for your answer. In the first example, we cannot see more detailed names for example `styled-StyledSpan`. Because I wanted better debugging names and import styled-components from one place. – Jasurbek Nabijonov Oct 16 '21 at 19:21
  • 1
    You cann't create component with dash. Or do you mean remove unnecessary generated classes and add custom classes names? Is `custom-styled.ts` the place where you will store `styled-components`? – Anton Oct 16 '21 at 20:39
  • Problem that I couldn’t solve is re export styled-components/macro from custom-styled.ts. Reason for that is for better debugging names for components. Without styled-components/macro it gives standard name. For example styled-span. If I import styled-components/macro directly into my components instead of from custom-styled.ts, it works fine. Thank you. – Jasurbek Nabijonov Oct 16 '21 at 23:38
  • 1
    I have updated the sandbox and if i'm right understood you wanted class names like an in image above. – Anton Oct 17 '21 at 07:16
  • I tried in sandbox `"displayName": true` which should change in React dev tools from `styled.span` to `styled.StyledSpan`, but I didn't work. I need this because in my project I have maybe 5-10 divs and all of the same `styled.div` in React dev tools, and better debugging is necessary to distinguish these divs. – Jasurbek Nabijonov Oct 17 '21 at 10:01
  • Hello, I have added a screenshot of the expected result. If I import `styled-components/macro` directly it works as expected but from custom-styled.ts it doesn't work. – Jasurbek Nabijonov Oct 17 '21 at 12:22
  • 1
    I moved **StyledSpan** to *custom-styled* and removed all styles from *App.tsx* and *Reusable.tsx* in the sandbox . Also copied code from sandbox to local mashine for testing with **react devtool** and got the same result, what you will do expect. You can see in the screenshots above. – Anton Oct 17 '21 at 13:26
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/238246/discussion-between-jasurbek-nabijonov-and-). – Jasurbek Nabijonov Oct 17 '21 at 14:30
  • 1
    Updated the answer and sandbox. That last solution to came in my mind, is very close what you need. This is all my knowledge about `styled-components`. – Anton Oct 18 '21 at 09:35
  • 1
    Thank you very much. I think my team cannot adapt this solution because in that case, we need to create many boilerplates in custom-styled.ts. We have icons, images, different types of heading, and other tags. I will try to address this issue in styled-components github repo. – Jasurbek Nabijonov Oct 18 '21 at 11:45