When multiple toasts are added, only the last one expires

Open
#2,673 3 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
javascript, react
Domain
frontend

Research direction

Start by reproducing the minimal create-react-app example with several Toast components inside ToastContainer and a 1000ms duration. Inspect the Toast and ToastContainer behavior around duration and onRequestClose; done means every added toast expires, rather than only the last one, while the setDuration workaround is no longer needed.

Written by the indexing model from the issue text.

Description

stale toast

Hi

First of all, great library. It makes designing according to accessibility principles so, so easy (and fun, too!).

During designing my app, I've noticed that when you add multiple Toast with duration property set to ToastContainer, only the last item gets deleted. All of them should get deleted after the set duration.

Here's a minimal working example illustrating what I'm talking about. I've created my app using create-react-app:

import React from 'react';
import {
  Button,
  Toast,
  ToastContainer
} from '@salesforce/design-system-react';

class App extends React.Component {
  state = {
    toasts: []
  }

  addToast = () => {
    const toastsCopy = [...this.state.toasts]
    toastsCopy.push(
      <Toast
        labels={{
          heading: 'Lorem ipsum'
        }}
        duration={1000}
        onRequestClose={() => this.deleteToast(this.state.toasts.length - 1)}
      />
    )
    this.setState({
      toasts: toastsCopy
    })
  }

  deleteToast = id => {
    const toastsCopy = [...this.state.toasts]
    toastsCopy[id] = null
    this.setState({
      toasts: toastsCopy
    })
  }
  render() {
    return (
      <div className="App">
        <Button
          label="Show toast"
          onClick={() => this.addToast()}
        />
        <ToastContainer>
          {this.state.toasts}
        </ToastContainer>
      </div>
    )
  }
}

export default App;

The possible workaround is to override the default behavior - do not use the duration property, but implement deleting an item using setDuration yourself.

Dominant language
JavaScript
Stars
994
Forks
444
Avg merge
3d 23h
Merged PRs (30d)
44

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from salesforce/design-system-react

All issues in salesforce/design-system-react

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.