【问题标题】:Can't Style Grommet icon inside Anchor无法在 Anchor 内设置 Grommet 图标
【发布时间】:2021-02-03 19:34:59
【问题描述】:

我是使用样式组件的 Grommet 新手。 我已经检查了所有文档,但找不到解决方案。

问题

我有一个带有图标和标签的锚。 问题是当我悬停或处于活动状态时,我无法定位样式图标。 文本/标签虽然改变了样式。我该如何实现/解决这个问题?

我也尝试过使用样式化组件并将图标和文本放入 Grommet Box 中,但没有奏效。

请帮忙!

import React from "react";
import { Anchor, Box, Text } from "grommet";
import styled from "styled-components";
import { Currency as PayoutIcon, Menu as MenuIcon } from "grommet-icons";

const StyledAnchor = styled(Anchor)`
  display: flex;
  height: 56px;
  color: #808191;
  padding: px 20px;
  border-radius: 12px;
  background: transparent;
  width: max-content;

  text-decoration: none;
  font-family: Inter;
  color: #808191;
  padding: 0px 20px;
  background: transparent;
  transition: all 0.25s ease 0s;
  text-decoration: none;
  border: none;

  &:visited {
    text-decoration: none;
    border: none;
  }

  &:hover {
    color: #6c5dd3;
    text-decoration: none;
  }
  &:active {
    color: #fff;
    background: #6c5dd3;
    text-decoration: none;
    border: none;
  }

  &:focus {
    color: #fff;
    background: #6c5dd3;
    textdecoration: none;
    border: none;
  }
`;
const SidebarItem = () => {
  return (
    // <Box color="#808191" hoverIndicator="true">
    <StyledAnchor
      color="#808191"
      label="Payouts"
      onClick={() => {}}
      href="#"
      icon={<PayoutIcon />}
    />
    // </Box>
  );
};

export default SidebarItem;

【问题讨论】:

    标签: javascript css reactjs styled-components grommet


    【解决方案1】:

    对于您正在寻找的样式的粒度,我认为您可以直接使用 Button 组件而不是 Anchor,但是,Button 的使用更符合您所描述的侧边栏交互元素的可访问性标准 (WCAG)以上。

    Grommet 与 styled-components 配合使用效果最好,但grommet 主题化也非常强大,知道如何利用它的功能将帮助您减少 styled-components 的使用。 最近,grommet 扩展了 Button 主题(种类/默认按钮),这应该可以为您解决问题,而且不需要样式组件,这里是一个示例:

    import React, { useState } from "react";
    import { render } from "react-dom";
    import { Box, Grommet, Button } from "grommet";
    import { Currency as PayoutIcon } from "grommet-icons";
    
    const theme = {
      global: {
        colors: {
          myColor: "#808191",
          "background-contrast": {
            dark: "#FFFFFF14",
            light: "#0000000A"
          },
          "active-background": "background-contrast",
          "active-text": "red",
          icon: "text",
          // focus color is an important indication for keyboard navigation accessibility, 
          // it will be an ill advice to set it to undefined and remove focus 
          focus: "teal",
          text: {
            dark: "#C0CADC",
            light: "#444444"
          }
        }
      },
      button: {
        default: {
          color: "#808191",
          border: undefined,
          font: {
            weight: 700
          },
          padding: {
            horizontal: "12px",
            vertical: "6px"
          }
        },
        hover: {
          default: {
            background: {
              color: "background-contrast"
            },
            color: "brand"
          },
          secondary: {
            border: {
              width: "3px"
            },
            padding: {
              horizontal: "9px",
              vertical: "3px"
            }
          }
        },
        active: {
          background: {
            color: "aliceblue"
          },
          color: "teal",
          secondary: {
            border: {
              color: "transparent"
            }
          }
        }
      }
    };
    
    const SidebarItem = () => {
      const [active, setActive] = useState();
      return (
        <Button
          active={active}
          label="Payouts"
          icon={<PayoutIcon />}
          onClick={() => {
            setActive(!active);
          }}
          href="#"
        />
      );
    };
    
    export const App = () => {
      return (
        <Grommet theme={theme}>
          <Box pad="small" align="start">
            <SidebarItem />
          </Box>
        </Grommet>
      );
    };
    
    render(<App />, document.getElementById("root"));
    
    

    这是一个codesandbox 用于实时运行它。

    按钮具有活动/悬停/禁用等粒度,您基本上可以使用主题anchor.extend 在 Anchor 中获得相同的功能,但这种方式更简洁。

    【讨论】:

    • 你好史密。非常感谢您的回复和帮助。 1)主题对象中的“选项”键是什么意思?它是另一种类型的按钮作为“默认”还是“主要”?无法在按钮文档中找到信息 2)当我单击按钮(“活动”或“焦点”)时,我仍然无法删除边框,也无法在按钮“活动”或“焦点”时更改背景颜色“。你能帮我理解一下吗?找不到按钮的焦点或焦点。单击或之后如何制作特定颜色的背景?当按钮为焦点时。无法理解如何在主题中编辑它
    • 当按钮被点击(“活动”)或“焦点”(点击后如何保持)时,您在沙盒中发送给我的示例没有任何作用。还想不通
    • 1) 我已经清理了示例中的option。 2)您提出的border 是标记正在聚焦的交互式元素的通用行为,因此应该可以帮助具有可访问性需求的用户,例如键盘导航。它是 WCAG 的网络标准,建议保留。话虽如此,您可以在主题的global.colors.focus 上将焦点颜色控制为undefined(或任何其他颜色),如代码示例中所示。您可以使用 active 制作具有特定颜色的按钮,您只需要一个 active 状态,我修复了示例以更好地演示它。
    • 没问题。我认为您的意图更倾向于active 状态而不是focus。所以在这种情况下,这很容易,button.active 主题上的extend 属性应该可以帮助您设置所需的任何 CSS 样式。例如,button.active 没有填充条目,因此可以通过将extend: 'padding: 40px;' for example 添加到button.active 来使用extend 选项轻松设置此属性的样式。我将它添加到沙箱中,以便您查看,您在extend 方向上的正确轨道,我希望这个答案有所帮助。
    • 目前没有记录在案的行为,但它可能会起作用。我个人的偏好是保持应用程序简单,并问自己是否需要 4 种常用按钮(因为您已经有了默认的、主要的、次要的以及您有兴趣添加的新按钮)。在向派对添加更多按钮之前,我会尝试使用这三种现有类型的按钮作为我的常用按钮。在某些情况下,您需要一个“一次性”按钮,而在这种特定情况下,您不需要主题,您可以利用子项的按钮功能。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多