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

How to style using Styled Components (Doc: https://fkhadra.github.io/react-toastify/how-to-style/#how-to-style-with-styled-components) #342

Closed
ezeikel opened this issue Apr 19, 2019 · 12 comments
Labels

Comments

@ezeikel
Copy link

ezeikel commented Apr 19, 2019

Are there any examples where <ToastContainer /> can be styled using styled-components?

Currently doing this:

const ReactToastAdapter = ({ className, bodyClassName, progressClassName, ...props }) => {
  return  (
    <ToastContainer
      className={className}
      bodyClassName={bodyClassName}
      progressClassName={progressClassName}
      {...props}
    />
  )
};

const StyledToastContainer = styled(ReactToastAdapter).attrs({
  bodyClassName: 'body',
  progressClassName: 'progress',
})`
  /* .Toastify__toast-container */
  bottom: 0;
  left: 0;
  padding: 0;
  margin: 0;
  width: 100%;
  .Toastify__close-button {
    display: none;
  }
  .Toastify__toast {
    background-color: var(--color-black);
    margin: 0;
    cursor: auto;
  }
  .toast {
    background-color: var(--color-black);
  }
  .body {
    background-color: var(--color-black);
    color: var(--color-white);
    font-family: var(--default-font-family);
  }
`;
@ezeikel
Copy link
Author

ezeikel commented Apr 19, 2019

This seemed to do the trick:

const StyledToastContainer = styled(ToastContainer).attrs({
  className: 'toast-container',
  toastClassName: 'toast',
  bodyClassName: 'body',
  progressClassName: 'progress',
})`
  /* .toast-container */
  bottom: 0;
  left: 0;
  padding: 0;
  margin: 0;
  width: 100%;
  .toast {
    background-color: var(--color-black);
    margin: 0;
    cursor: auto;
  }
  button[aria-label="close"] {
    display: none;
  }
  .toast {
    background-color: var(--color-black);
  }
  .body {
    background-color: var(--color-black);
    color: var(--color-white);
    font-family: var(--default-font-family);
    margin: 0;
    display: grid;
    align-items: center;
  }
`;

@ezeikel
Copy link
Author

ezeikel commented Apr 19, 2019

Still getting this warning in the console though 🤔:

Screenshot 2019-04-19 at 19 55 28

@JrFarias
Copy link

The styled-component verify your children component that be wrapped to pass your classes.

https://github.com/styled-components/styled-components/blob/master/packages/styled-components/src/utils/classNameUsageCheckInjector.js#L41

However as the child is a div that does not have the classes passed by the styled-component this warning occurs.
https://github.com/fkhadra/react-toastify/blob/master/src/components/ToastContainer.js#L422

@fkhadra
Copy link
Owner

fkhadra commented May 17, 2019

Hey,

Sorry for the late reply. Wanna make a PR to make the ToastContainer work with styled-components ?

@JrFarias
Copy link

Yeah, I'll fork this project and send a PR with the modification asap.

@sgoudie
Copy link

sgoudie commented Jun 11, 2019

@JrFarias Hitting the same problem. Did you work on a fix for this?

@alextiley
Copy link

For those interested, you can work-around the warning mentioned in @ezeikel last comment by creating a wrapper component and applying the className to that instead:

import React from 'react';
import styled from 'styled-components';
import { ToastContainer, ToastContainerProps } from 'react-toastify';

export const WrappedToastContainer = ({ className, ...rest }: ToastContainerProps & { className?: string }) => (
  <div className={className}>
    <ToastContainer {...rest} />
  </div>
);

export default styled(WrappedToastContainer).attrs({
  // custom props
})`
  .Toastify__toast-container {}
  .Toastify__toast {}
  .Toastify__toast--error {}
  .Toastify__toast--warning {}
  .Toastify__toast--success {}
  .Toastify__toast-body {}
  .Toastify__progress-bar {}
`;

@fkhadra
Copy link
Owner

fkhadra commented May 10, 2020

https://fkhadra.github.io/react-toastify/how-to-style/#how-to-style-with-styled-components

@fkhadra fkhadra closed this as completed May 10, 2020
@fkhadra fkhadra changed the title How to style using Styled Components How to style using Styled Components (Doc: https://fkhadra.github.io/react-toastify/how-to-style/#how-to-style-with-styled-components) May 10, 2020
@AriHrannar
Copy link

https://fkhadra.github.io/react-toastify/how-to-style/#how-to-style-with-styled-components

It doesnt seem like this link works anymore? It just brings me to the demo page - I would expect that it should be a special page with code examples of how to style with styled-components?

@fkhadra
Copy link
Owner

fkhadra commented Mar 8, 2021

@AriHrannar can you try from a private tab ? I suspect a cache issue

@AriHrannar
Copy link

@AriHrannar can you try from a private tab ? I suspect a cache issue

Awesome, thanks that did it :)

@phoniks
Copy link

phoniks commented Feb 8, 2022

If anyone else is having issues with this and is working with both typescript and styled-components, I found that I wasn't getting my styles passed through because I hadn't told typescript to expect a className on my components. Once I fixed that my custom styles started showing up. This is thestack over flow answer that made it clear to me what I was doing wrong - but @alextiley's example above also shows the proper way to handle this. It might help to have a note to this effect in the documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants