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

Toast transparency causing issue with text in background #102

Open
dgrcode opened this issue Nov 17, 2021 · 2 comments
Open

Toast transparency causing issue with text in background #102

dgrcode opened this issue Nov 17, 2021 · 2 comments
Labels
bug Something isn't working

Comments

@dgrcode
Copy link
Collaborator

dgrcode commented Nov 17, 2021

Screenshot 2021-11-17 at 11 46 14

I haven't checked if this is happening in light mode as well, but I've seen a few things using opacity in dark mode (borders, dividers, etc), so I wouldn't be surprised if this was a chakra ui issue on the dark mode default theme.

@dgrcode
Copy link
Collaborator Author

dgrcode commented Nov 18, 2021

It seems to be expected: chakra-ui/chakra-ui#3861

The solution seems to be adding variant: "solid" in the toast call argument. I don't like how it looks in light mode with solid, so I'll go with the following for new toasts.

import { useColorModeValue } from "@chakra-ui/react";
const toastVariant = useColorModeValue("subtle", "solid");

toast({
  description: "Can't get the message to sign. Please try again",
  status: "error",
  variant: toastVariant,
});

@codenamejason codenamejason added this to the Scaffold Directory App milestone Nov 19, 2021
@codenamejason codenamejason added the bug Something isn't working label Nov 19, 2021
@apancutt
Copy link

apancutt commented Feb 20, 2023

An alternative workaround that allows you to use the subtle, top-accent and left-accent variants in dark mode while keeping a slightly tinted background based on the status:

Patch the useToast() function provided by Chakra…

// src/helper/toast.ts
import { useToast as chakraUseToast, useColorModeValue } from '@chakra-ui/react';

export const useToast = () => chakraUseToast({
  containerStyle: useColorModeValue(undefined, {
    bg: 'chakra-body-bg',
    rounded: 'md',
  }),
});

Use the patched function instead of the one provided by Chakra…

// src/component/MyComponent.tsx
import { Button, VStack } from '@chakra-ui/react';
import type { FunctionComponent } from 'react';
import { useCallback } from 'react';
import { useToast } from '../helper/toast';

const variants = [ 'subtle', 'top-accent', 'left-accent' ] as const;

export const MyComponent: FunctionComponent = () => {

  const toast = useToast();

  const showToast = useCallback<(variant: typeof variants[number]) => void>((variant) => {
    toast({
      variant,
      title: `Example ${variant} toast`,
    });
  }, [ toast ]);

  return (
    <VStack>
      {variants.map((variant) => (
        <Button key={variant} onClick={() => showToast(variant)}>Show {variant} Toast</Button>
      ))}
    </VStack>
  );

};

EDIT: Use a patched useToast() instead of having to declare containerStyles each time a toast is used

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants