简体   繁体   English

React, styled-components - 显示 cursor: pointer; 如果菜单打开在页面上?

[英]React, styled-components - Display cursor: pointer; on page if menu open?

I have a styled.div for displaying page content underneath a navbar.我有一个styled.div用于在导航栏下方显示页面内容。 The mobile size page has a button that unveils a nav menu onClick , and I have a click handler for the page component so if the user clicks on the page content ( PageContainer ) while the menu is open, it closes the menu.移动尺寸页面有一个按钮,可以显示导航菜单onClick ,我有一个页面组件的点击处理程序,所以如果用户在菜单打开时点击页面内容 ( PageContainer ),它会关闭菜单。

Here's the nav component - it's the entry point for the site, and all the pages get rendered below in the PageContainer (I'm using hookrouter ):这是 nav 组件——它是站点的入口点,所有页面都在PageContainer下方呈现(我正在使用hookrouter ):

const Nav = () => {
    const [isOpen, setIsOpen] = useState(true);
    const routeResult = useRoutes(directions);
  
  // Click handler for the page content area
    const backgroundClickHandler = () => {
        if (isOpen) {
            setIsOpen(!isOpen);
        }
  };
  
  // Click handler for menu toggle button
    const toggleClickHandler = () => setIsOpen(!isOpen);

    return (
        <Container>
            {/* Mobile menu toggle button ( */}
            <ToggleContainer>
                <button
                    className='fas fa-bars fa-2x'
                    onClick={toggleClickHandler}
                ></button>
            </ToggleContainer>
      {/* Mobile menu  */}
        <NavWrapper>
                    <ul>
                        {routes.map(({ key, href, label }) => (
              <li key={key}>
                                <A href={href} className='nav-route'>
                                    {label}
                                </A>
                            </li>
                        ))}
                    </ul>
                </NavWrapper>
      {/* This is the page content area */}
            <PageContainer onClick={backgroundClickHandler}>
                {routeResult || <NotFoundPage />}
            </PageContainer>
        </Container>
    );
};

export default Nav;

Here's the PageContainer styled.div :这是PageContainer styled.div

const PageContainer = styled.div`
    position: absolute;
    top: 16rem;

    @media (min-width: 40rem) {
        position: relative;
        top: auto;
    }
`;

How would you recommend I go about adding cursor: pointer;你会如何建议我 go 关于添加cursor: pointer; to the PageContainer if useState isOpen = true ?如果useState isOpen = truePageContainer

I thought about making another PageContainer , eg PageContainerIsOpen , that could be used if the menu is open and render each one conditionally via a ternary expression, but I'm afraid it would be very expensive.我考虑制作另一个PageContainer ,例如PageContainerIsOpen ,如果菜单打开并通过三元表达式有条件地呈现每个菜单,则可以使用它,但我担心它会非常昂贵。 Is there a way I can build the condition into the PageContainer itself?有没有办法将条件构建到PageContainer本身? Other ideas?其他想法?

You can pass down props to a styled component.您可以将道具传递给样式化的组件。 For instance you can do something like:例如,您可以执行以下操作:

const PageContainer = styled.div`
   cursor: ${props => props.isOpen? 'pointer' : 'unset'};
`

And call it like:并称它为:

<PageContainer isOpen={isOpen} />

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

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