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.

[Toast] Extend toast behavior with theme

See original GitHub issue

Description

I’d like to have a default behavior for toast (position, duration, isClosable, etc). Currently, the way to go about it is to create a custom hook.

I’d like to extend it within the theme if possible.

Problem Statement/Justification

I think the DX for this is cleaner than maintaining my own custom useToast hook.

Proposed Solution or API

export const theme = extendTheme(
  {
    components: {
      useToast: {
        defaultProps: {
          position: "bottom-right",
        },
      },
    },
  },
);

Alternatives

No response

Additional Information

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
cpv123commented, Aug 24, 2021

Thanks, @segunadebayo, sounds like a fair decision 👍

For anyone that comes across this, I ended up writing a wrapper that looks like this:

import { useToast as useChakraToast, UseToastOptions } from '@chakra-ui/react';

export default function useToast() {
  const toast = useChakraToast();
  const notify = (toastOptions: ToastOptions) => {
    toast({
      duration: 4000,
      position: 'top',
      isClosable: true,
      ...toastOptions,
    });
  };

  const notifyWithStatus = (status: ToastOptions['status']) => {
    return (toastOptions: ToastOptions) => notify({ ...toastOptions, status });
  };

  return {
	notify,
    notifyError: notifyWithStatus('error'),
    notifySuccess: notifyWithStatus('success'),
    notifyInfo: notifyWithStatus('info'),
  };
}

1reaction
segunadebayocommented, Aug 24, 2021

I totally understand that this is a feature that sounds nice. I believe this can be resolved on your end by creating a wrapper around useToast.

From an API design perspective, I don’t think it’s a good idea to add this. I know you might disagree, but that’s okay.

In the upcoming changes to the toast component, we intend to add a global toast provider to configure this from the ChakraProvider directly instead of adding that to the theme.

Thanks for understanding.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Toasts - Bootstrap
Push notifications to your visitors with a toast, a lightweight and easily customizable alert message. Toasts are lightweight notifications designed to mimic ...
Read more >
Toasts overview - Android Developers
A toast provides simple feedback about an operation in a small popup. It only fills the amount of space required for the message...
Read more >
Toast Notification Timing Adjustable · Issue #6270 - GitHub
Expected Behavior Toasts need to support a way to extend timing or set a new ... Behavior Currently the time limit is controlled...
Read more >
How to style | React-Toastify - GitHub Pages
Used to define container behavior: width, position: fixed etc... **/ .Toastify__toast-container {. } /** Used to define the position of the ToastContainer ......
Read more >
How to generate a toast when processing assignment in ...
In Theme-Cosmos, a toast is displayed only when creating a new case ... This behavior can be configured in the Cosmos UI setting...
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