css 如何风格脉轮用户界面吐司-与颜色-与脉轮用户界面/React2.4.4

vq8itlhq  于 2022-12-27  发布在  React
关注(0)|答案(3)|浏览(314)
    • bounty将在6天后过期**。回答此问题可获得+100声望奖励。Mel希望引起更多人对此问题的关注:我试图找到一种方法来风格脉轮用户界面烤面包与品牌颜色。文档似乎是不正确的。

我正在尝试弄清楚如何添加颜色和backgroundColor样式到我的脉轮UI吐司。
我有一个祝酒词部分:

import type { UseToastOptions } from "@chakra-ui/react"
import { useToast as useChakraToast } from "@chakra-ui/react"

export function useToast() {
  const toast = useChakraToast()
  const handleToast = (props: UseToastOptions) => {
    toast({
      position: "bottom-right",
      isClosable: true,
      status: "success",

      ...props,
    })
  }
  return handleToast
}

我也尝试过使用它,效果很好,但我无法添加颜色样式:

import { useApolloClient } from "@apollo/client"
import { useRouter } from "next/router"

import { useToast } from "./useToast"

export const useLogout = () => {
  const client = useApolloClient()
  const router = useRouter()
  const toast = useToast()
  const handleLogout = async () => {
    await router.replace("/logout")
    await fetch("/api/logout", { method: "post" })
    await client.resetStore()
    toast({ description: "Successfully logged out!" } )
  }
  return handleLogout
}

I'd like to find a way to make success equal color: "brand.white" and bg: "brand.green" in the useToast component - but it wont accept those values. I also cant add them in the logout toast.
我能把它们放在哪里?
我尝试向theme. tsx添加属性,如下所示:

components: {
    Button,
    Input,
    Select,
    Textarea,
    // I tried Alert and ToastOptions instead of Toast, but it doesn't make any difference 
    Toast: {
      baseStyle: {},
      defaultProps: {},
      variants: {
        // I tried sprinkling this everywhere - it doesn't make any difference
        background: "unset",
        solid: {
        background: "unset",
        success: {
          background: "unset",
          bg: "brand.green",
          color: "brand.white",
        },
        error: {
          bg: "brand.red",
          color: "brand.white",
        },
        info: {
          bg: "brand.blue",
          color: "brand.white",
        }

      },
      },
    },

但toast组件没有观察到它们。我还尝试将组件头从Toast更改为Alert和ToastOptions,但这两种尝试都不起作用。
注意到[chakra用户界面文档的这一页][1],我尝试为警报样式定制一个组件,如下所示:

import { alertAnatomy } from '@chakra-ui/anatomy'
import { createMultiStyleConfigHelpers } from '@chakra-ui/react'

const { definePartsStyle, defineMultiStyleConfig } =
  createMultiStyleConfigHelpers(alertAnatomy.keys)

const baseStyle = definePartsStyle({
  // define the part you're going to style
        variants: {
            solid: {
            background: "unset",
            success: {
                background: "unset",
                bg: "brand.green",
                color: "brand.white",
            },
            error: {
                background: "unset",
                bg: "brand.tomato",
                color: "brand.white",
            },
            info: {
                background: "unset",
                bg: "brand.blue",
                color: "brand.sludge",
            }
        }  
      

  },
})

export const alertTheme = defineMultiStyleConfig({ baseStyle })

VSCode告诉我:
模块"@chakra-ui/react "没有导出的成员" createMultiStyleConfigHelpers "
我用的是"@chakra-ui/react ":"2.4.4",并且无法让脉轮用户界面文档中显示的方法工作。[1]:https://chakra-ui.com/docs/components/alert/theming

whlutmcx

whlutmcx1#

我在设计Alert组件的样式时也遇到了同样的问题,经过一番研究,我设法在theme.js中更改组件样式,如下所示:

//_app.js
import { theme } from 'theme';

<ChakraProvider theme={theme}>
//... 
</ChakraProvider >

//theme.js

import { extendTheme } from '@chakra-ui/react';

const textStyles = {...}
const colors = {...}

const components = {
  Alert: {
    variants: {
      solid: {
        container: {
          bg: '#fff',
          ...
        },
        title: {
          color: 'brand',
        },
        description: {
          ...
        },
        icon: {
          ...
        },
      },
    },
  },
};

export const theme = extendTheme({ colors, components, textStyles });
mwecs4sa

mwecs4sa2#

在chakra-ui中使用自定义烤面包更有用。

const toast = useToast()
toast({
      duration: 4000,
      isClosable: true,
      render: () => (
        <CustomToast
          description="Your Description"
          status="success" // "info" | "error"
        />
      ),
    })

和自定义toast组件可能会使用chakra的Alert组件。

<Alert status={status}> {description} </ Alert>
bxgwgixi

bxgwgixi3#

根据脉轮UI document,一个基本的解决方案是为Alert创建自定义变量,它由Toast内部使用,因此Toast可以通过使用变量来设置样式。
当在新安装的脉轮用户界面环境中使用基本设置进行测试时,导入createMultiStyleConfigHelpers似乎没有发生错误。
下面是一个简单的现场演示,用于测试:stackblitz.
首先在theme中定义自定义变量的样式:

// '@chakra-ui/anatomy' may need to be installed

import { alertAnatomy } from '@chakra-ui/anatomy';
import {
  ChakraProvider,
  extendTheme,
  createMultiStyleConfigHelpers,
} from '@chakra-ui/react';

const { definePartsStyle, defineMultiStyleConfig } =
  createMultiStyleConfigHelpers(alertAnatomy.keys);

// 👇 Defining styles for the first custom variant
const customSuccess = definePartsStyle({
  container: {
    border: '1px solid',
    borderColor: 'teal.200',
    background: 'teal.500',
    _dark: {
      borderColor: 'teal.600',
      background: 'teal.800',
    },
  },
  title: {
    color: 'pink.200',
    _dark: {
      color: 'pink.200',
    },
  },
});

// 👇 Defining styles for the second custom variant
const customError = definePartsStyle({
  container: {
    border: '1px solid',
    borderColor: 'pink.200',
    background: 'pink.400',
    _dark: {
      borderColor: 'pink.600',
      background: 'pink.800',
    },
  },
  title: {
    color: 'teal.200',
    _dark: {
      color: 'teal.300',
    },
  },
});

const alertTheme = defineMultiStyleConfig({
  variants: { customSuccess, customError },
});

export const theme = extendTheme({
  components: {
    Alert: alertTheme,
  },
});

然后应用于useToast,其variant属性使用为Alert设置的相同变体:
x一个一个一个一个x一个一个二个x

相关问题