Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

styled-components not working in viewports #399

Closed
Em-Ant opened this issue Mar 15, 2023 · 2 comments
Closed

styled-components not working in viewports #399

Em-Ant opened this issue Mar 15, 2023 · 2 comments
Labels
documentation Improvements or additions to documentation
Milestone

Comments

@Em-Ant
Copy link

Em-Ant commented Mar 15, 2023

Describe the bug

If using styled-components, the styles are correctly applied when the story width is unset, but not when you are in viewport mode.

width: unset
image

width: small
image

Reproduction

https://stackblitz.com/edit/ladle-3czkbf?file=src/basic.stories.tsx

Environment

  • OS: MacOS Ventura 13.2.1
  • Browser: Chrome 111.0.5563.64 (Official Build) (x86_64)
    (but I don't think this problem is platform dependent)
@Em-Ant Em-Ant added the needs triage needs to be reviewed label Mar 15, 2023
@Em-Ant
Copy link
Author

Em-Ant commented Mar 17, 2023

I think it's related #207, right ?

fixed adding global provider in .ladle/components.tsx with the following code:

import React, { type FC, type ReactNode, useState, useEffect } from 'react';
import { StyleSheetManager } from 'styled-components';

const getDocument = (story: string) => {
  const iframe = document.querySelector(
    `[title='Story ${story}']`
  ) as HTMLIFrameElement;
  return iframe && iframe.contentDocument ? iframe.contentDocument : document;
};

export const Provider: FC<{
  globalState: {
    theme: string;
    rtl: string;
    story: string;
    width: number;
  };
  children: ReactNode;
}> = ({ children, globalState }) => {
  const [target, setTarget] = useState(getDocument(globalState.story).head);
  useEffect(() => {
    setTarget(getDocument(globalState.story).head);
  }, [globalState.width]);

  return <StyleSheetManager target={target}>{children}</StyleSheetManager>;
};

@tajo tajo added documentation Improvements or additions to documentation and removed needs triage needs to be reviewed labels Mar 18, 2023
@tajo tajo added this to the v3 milestone Sep 4, 2023
@tajo
Copy link
Owner

tajo commented Sep 17, 2023

Will be completely fixed with v3 to make it working out of the box. There is also an SWC styled-components plugin that can be optionally added to make the debugging easier. This will be covered in new docs.

@tajo tajo closed this as completed Sep 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants