question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Debug class names do not work when styled function is reexported with macros

See original GitHub issue

When importing styled and using it in the same file, debug class names work as expected.

// main.js
import styled from "styled-components/macro";
const Text = styled.div({ color: "red" });

When reexporting the styled function in another file, debug class names disappear

// css.js
import styled from "styled-components/macro";
export { styled }
// main.js
import { styled } from "./css.js";
const Text = styled.div({ color: "red" });

Perhaps this is limitation of the babel macros? If so maybe we should add limitations chapter in the docs (https://www.styled-components.com/docs/tooling#babel-macro).


Versions:

"styled-components": "4.1.3"
"react-scripts": "2.1.3"

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

16reactions
atdragocommented, Feb 11, 2019

I ran into this issue because I was trying to import styled, provide it a theme interface, then reexport it as described here, but using the babel macro instead of the root package.

If this is your use case, I think I found a solution. Instead of having the extra styled-components.ts file mentioned in the docs, you can redeclare the "styled-components/macro" module using your Theme interface, then import styled from 'styled-components/macro' directly. This solves the file-only problem.

Here’s what I have in my global.d.ts (ITheme is my theme interface located in a different file):

declare module 'styled-components/macro' {
    import { ThemedStyledComponentsModule } from 'styled-components';
    import { ITheme } from 'typings';

    const ModuleInterface: ThemedStyledComponentsModule<ITheme>;

    export const createGlobalStyle: typeof ModuleInterface.createGlobalStyle;
    export const css: typeof ModuleInterface.css;
    export const keyframes: typeof ModuleInterface.keyframes;
    export const isStyledComponent: typeof ModuleInterface.isStyledComponent;
    export const ServerStyleSheet: typeof ModuleInterface.ServerStyleSheet;
    export const StyleSheetManager: typeof ModuleInterface.StyleSheetManager;
    export const ThemeConsumer: typeof ModuleInterface.ThemeConsumer;
    export const ThemeContext: typeof ModuleInterface.ThemeContext;
    export const ThemeProvider: typeof ModuleInterface.ThemeProvider;
    export const withTheme: typeof ModuleInterface.withTheme;
    export default ModuleInterface.default;
}

Then, make this change everywhere you imported styled previously:

- import styled from './styled-components.ts';
+ import styled from 'styled-components/macro';

I don’t know if the above could be achieved more elegantly, but would love feedback!

2reactions
chrisericksoncommented, Mar 12, 2019

I spoke a little too soon – with the above solution, my props.theme value in any styled component is an any rather than ITheme.

Read more comments on GitHub >

github_iconTop Results From Across the Web

re export styled from styled components macro not working
App.tsx: when I import it directly from import styled from 'styled-components/macro' it gives intended debugging features.
Read more >
How To Use Styled-Components In React - Smashing Magazine
Unique class names. Styled components are independent of each other, and you do not have to worry about their names because the library ......
Read more >
Build settings reference | Apple Developer Documentation
A detailed list of individual Xcode build settings that control or change the way a target is built.
Read more >
Tooling - styled-components
This plugin adds support for server-side rendering, minification of styles, and a nicer debugging experience. Usage. Install the babel-plugin first: npm install ...
Read more >
Template Designer Documentation - Jinja
A Jinja template doesn't need to have a specific extension: .html ... If a macro name starts with an underscore, it's not exported...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found