GithubHelp home page GithubHelp logo

Comments (5)

oddvernes avatar oddvernes commented on September 17, 2024

Hello!
I am unable to reproduce this issue. with the following code, it closes after 1 second
(tested in a create vite app with 0.40.1)

function SnackbarSample() {
    const [message, setMessage] = useState({ open: false, text: '' })

    return (
      <>
        <Button
          onClick={() => setMessage({ open: true, text: 'test' })}
        >
          Show snackbar for 1 second
        </Button>
        <Snackbar
          open={message.open}
          onClose={() => setMessage({ open: false, text: '' })}
          autoHideDuration={1000}
        >
          {message.text}
        </Snackbar>
      </>
    )
}

So there must be some missing piece to this puzzle, with how it is implemented perhaps?

from design-system.

ThomasRyschawyEquinor avatar ThomasRyschawyEquinor commented on September 17, 2024

I found the missing piece to this puzzle, it's an interval.
In the below example the Snackbar does not autohide at all when the button is clicked.

import { useEffect, useState } from 'react'
import {
  Button,
  Snackbar,
} from '@equinor/eds-core-react';
import './App.css'

function App() {
  const [message, setMessage] = useState({ open: false, text: '' });
  const [data, setData] = useState(0);

  const handleClick = () => {
    setMessage({ open: true, text: 'Hello, World!' });
  };

  useEffect(() => {
    const interval = setInterval(() => {
      setData(data => data + 1);
    }, 1000);
    return () => clearInterval(interval);
  }, []);

  return (
    <>
      <div>
        <Button onClick={() => handleClick()}>
          Show Snackbar + {data}
        </Button>
        <Snackbar
          open={message.open}
          onClose={() => setMessage({ open: false, text: '' })}
          autoHideDuration={2000}
        >
          {message.text}
        </Snackbar>
      </div>
    </>
  )
}

export default App

from design-system.

ThomasRyschawyEquinor avatar ThomasRyschawyEquinor commented on September 17, 2024

I found another way to trigger the issue using a second button.
Steps:
-press first button (Snackbar will show)
-press second button every second

Result:
As long as you (continuously) press the second button the Snackbar will stay open

It looks like each render resets the Snackbar timeout

Code:

import { useState } from 'react'
import {
  Button,
  Snackbar,
} from '@equinor/eds-core-react';
import './App.css'

function App() {
  const [message, setMessage] = useState({ open: false, text: '' });
  const [data, setData] = useState(0);

  const handleClick = () => {
    setMessage({ open: true, text: 'Hello, World!' });
  };

  const handleDataClick = () => {
    setData(data + 1);
  };

  return (
    <>
      <div>
        <Button onClick={() => handleClick()}>
          Show Snackbar
        </Button>
        <Button style={{marginLeft: '10px'}} onClick={() => handleDataClick()}>
          Data + {data}
        </Button>
      </div>
      <div>
        <Snackbar
          open={message.open}
          onClose={() => setMessage({ open: false, text: '' })}
          autoHideDuration={2000}
        >
          {message.text}
        </Snackbar>
      </div>
    </>
  )
}

export default App

from design-system.

oddvernes avatar oddvernes commented on September 17, 2024

Aha... sounds like something needs to be memiozed properly

from design-system.

oddvernes avatar oddvernes commented on September 17, 2024

Looks like removing the onClose function from the useEffect dependency array fixed it. I'll make a patch release with this fix 👍

from design-system.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.