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

Created on 19 Apr 2019  路  8Comments  路  Source: fkhadra/react-toastify

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);
  }
`;
How to

Most helpful comment

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 {}
`;

All 8 comments

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;
  }
`;

Still getting this warning in the console though 馃:

Screenshot 2019-04-19 at 19 55 28

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

Hey,

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

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

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

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 {}
`;
Was this page helpful?
0 / 5 - 0 ratings

Related issues

albert-olive picture albert-olive  路  5Comments

fkhadra picture fkhadra  路  4Comments

LouisCuvelier picture LouisCuvelier  路  3Comments

tobiasfriden picture tobiasfriden  路  3Comments

giocodes picture giocodes  路  3Comments