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.

overlayClassName and styled-components not playing nicely

See original GitHub issue

I’m running into a problem trying to encapsulate my Modal styles with styled-components. The following approach fails to work and the defaultProps() route is a no go as well. After looking at how react-modal generates the modal elements outside of the <App/> root in the DOM, it’s not surprising this is causing problems. It looks like buildClassName() is correctly adding the ‘modal-overlay’ className, yet even the second approach doesn’t work. I suspect styled-components doesn’t inject the CSS as it doesn’t see any of it’s children, or any of the <App/> children using it. Any ideas on how to get this to work to avoid using a global css class for the overlay?

const overlayClassName = 'modal-overlay'
const StyledModal = styled(Modal).attrs({
  overlayClassName
})`
  styles: here;

  &.${overlayClassName} {
    doesnt: work;
  }
  .${overlayClassName} {
    also-doesnt: work;
  }
`

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

81reactions
smagchcommented, Apr 5, 2018

I solved the problem without using .attrs().

function ReactModalAdapter ({ className, ...props }) {
  const contentClassName = `${className}__content`;
  const overlayClassName = `${className}__overlay`;
  return (
    <Modal
      portalClassName={className}
      className={contentClassName}
      overlayClassName={overlayClassName}
      {...props}
    />
  )
}

const StyledModal = styled(ReactModalAdapter)`

  &__overlay {
    position: fixed;
    top: 0px;
    left: 0px;
    right: 0px;
    bottom: 0px;
    background-color: rgba(255, 255, 255, 0.75);
  }

  &__content {
    position: absolute;
    top: 40px;
    left: 40px;
    right: 40px;
    bottom: 40px;
    border: 1px solid #ccc;
    background: #fff;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    borderRadius: 4px;
    outline: none;
    padding: 20px;
  }
`;
32reactions
NathanielHillcommented, Feb 6, 2018

For anyone searching, I solved this using a wrapper function, assuggested by @exogen

function ReactModalAdapter ({ className, modalClassName, ...props }) {
  return (
    <ReactModal
      className={modalClassName}
      portalClassName={className}
      {...props}
    />
  )
}

const StyledModal = styled(ReactModalAdapter).attrs({
  overlayClassName: 'Overlay',
  modalClassName: 'Modal'
})`
  .Modal {
    styles: here;
  }
  .Overlay {
    styles: here;
  }
`
Read more comments on GitHub >

github_iconTop Results From Across the Web

Advanced Usage - styled-components
This component provides a theme to all React components underneath itself via the context API. In the render tree all styled-components will have...
Read more >
Classes - react-modal documentation
You can use the className and overlayClassName props to control the CSS ... If you specify className , the default content styles will...
Read more >
AntD Tooltip with styled-components - CodeSandbox
Activating extension 'vscode.typescript-language-features' failed: Could not find bundled tsserver.js.
Read more >
Popover - Headless UI
These components are completely unstyled, so how you style your Popover is up to you. ... Button and not disturb the normal document...
Read more >
styled-components.Overlay JavaScript and Node.js ... - Tabnine
Best JavaScript code snippets using styled-components. ... badgeLabels) || {}; return ( <Overlay className={className} isFaded={status.type === BADGE_TYPES.
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