简体   繁体   English

焦点状态不应该改变,React.JS + TailwindCSS

[英]Focus state shouldn't change, React.JS + TailwindCSS

I have a question related to tailwindcss and React.我有一个与 tailwindcss 和 React 相关的问题。 I have a property that if a <NavItem /> is focused then the background and the text will change.我有一个属性,如果<NavItem />被聚焦,那么背景和文本将会改变。 <LanguageSwitcher /> is another component, and if it is focused, I want it to not change focus state of other items in navbar. <LanguageSwitcher />是另一个组件,如果它有焦点,我希望它不会改变导航栏中其他项目的焦点状态。 <LanguageSwitcher /> doesn't affect URL. <LanguageSwitcher />不影响 URL。

Here's my code for item in navigation bar and images of how <LanguageSwitcher /> affects focus state of other items:这是我在导航栏中的项目代码以及<LanguageSwitcher />如何影响其他项目的焦点状态的图像:

LanguageSwitcher 未被点击

LanguageSwitcher 被点击

import React from "react";

import { Link } from "react-router-dom";

interface Props {
  title: string;
  url: string;
  icon: JSX.Element;
}

const NavItem: React.FC<Props> = ({ title, url, icon }) => {
  //https://tailwindcss.com/docs/hover-focus-and-other-states#pseudo-classes
  return (
    <Link
      to={url}
      className="text-center items-center flex-col justify-center font-medium  px-1 text-white  capitalize  hover:font-semibold  select-none group"
    >
      <div className="flex items-center justify-center mb-1 rounded-3xl px-2 py-1 group-hover:bg-btnHover group-focus:bg-btnActive group-focus:text-textActive transition-colors duration-75">
        {icon}
      </div>
      <span>{title}</span>
    </Link>
  );
};

export default NavItem;

Is it possible?可能吗? If so, how can I do it, maybe with useLocation from react-router-dom or directly with tailwindcss?如果是这样,我该怎么做,也许使用react-router-dom中的useLocation或直接使用 tailwindcss?

"Focus" generally means the UI element the user can currently interact with. “焦点”通常是指用户当前可以与之交互的 UI 元素。 I think you are conflating "focus state" with "active state".我认为您将“焦点状态”与“活动状态”混为一谈。 I'm guessing you want the Link to remain "active" while other UI elements have, or might not have, "focus", and are being interacted with.我猜您希望Link保持“活动”状态,而其他 UI 元素具有或可能不具有“焦点”,并且正在与之交互。 Switch to using the NavLink component and conditionally apply the "active" state CSS classname.切换到使用NavLink组件并有条件地应用“活动”状态 CSS 类名。

Here's an example * :这是一个例子*

import React from "react";
import { NavLink } from "react-router-dom";

interface Props {
  title: string;
  url: string;
  icon: JSX.Element;
}

const NavItem: React.FC<Props> = ({ title, url, icon }) => {
  //https://tailwindcss.com/docs/hover-focus-and-other-states#pseudo-classes
  return (
    <NavLink
      to={url}
      className="text-center items-center flex-col justify-center font-medium  px-1 text-white  capitalize  hover:font-semibold  select-none group"
    >
      {({ isActive }: { isActive: boolean }) => (
        <>
          <div
            className={
              [
                "flex items-center justify-center mb-1 rounded-3xl px-2 py-1 group-hover:bg-btnHover transition-colors duration-75"
                isActive ? "group-focus:bg-btnActive group-focus:text-textActive" : null
              ]
                .filter(Boolean)
                .join(" ")
            }
          >
            {icon}
          </div>
          <span>{title}</span>
        </>
      )}
    </NavLink>
  );
};

export default NavItem;

*Note: I've just guessed/plucked the CSS classnames that looked like they were related to the "focus/active" CSS styling, you will want to double-check the actual classes in your implementation. *注意:我只是猜测/摘取了看起来与“焦点/活动”CSS 样式相关的 CSS 类名,您需要仔细检查实现中的实际类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM