CSS悬停高亮不覆盖父组件。

huangapple go评论70阅读模式
英文:

CSS hover highlight doesn't cover parent component

问题

I'm trying to create a languages option component. It has language codes placed vertically in a parent component as such:

CSS悬停高亮不覆盖父组件。

When I hover over the language options, it highlights the letter codes (a separate styled component). I can't figure out how to get it to cover the width of the parent component.

Here's the code:

Styling:

import styled, { css } from 'styled-components';

export const LanguagesWrapper = styled.div`
  background-color: #b0b2b7;
  border-radius: 5px;
  display: flex;
  flex-direction: column;
  flex-grow: 1; /* Add flex-grow property to expand vertically */
  padding: 0.3rem;
  gap: 0.8rem;
`;

export const StyledLanguagesTitle = styled.p`
  font-size: ${({theme}) => theme.typography.fontSize.medium};
  cursor: pointer;
  transition: background-color 0.3s;
  &:hover {
    background-color: #526d82;
  }
`;

export const ButtonForReferenceLink = styled.button<{
  icon: string;
}>`
  position: relative;
  width: 1.3rem;
  height: 1.3rem;
  background-image: url(${({icon}) => icon});
  background-repeat: no-repeat;
  background-position: center;
  background-size: contain;
  background-color: transparent;
  border: none;
  cursor: pointer;
  :first-of-type {
    height: 1.1rem;
  }
`;
import {useState} from 'react';
import {
  ButtonForReferenceLink,
  LanguagesWrapper,
  StyledLanguagesTitle,
} from './styles';
import {LanguageOptionsProps, ReferenceLinkProps} from './types';
import openNewTab from '../ModalsContent/References/utils/openNewTab';

const ReferenceLink = ({
  handleMouseOver,
  handleMouseOut,
}: ReferenceLinkProps) => {
  return (
    <ButtonForReferenceLink
      onMouseOver={handleMouseOver}
      onMouseOut={handleMouseOut}
      icon='/images/arrow-up-right-from-square-solid.svg'
    />
  );
};

const LanguageOptions = ({languages, link}: LanguageOptionsProps) => {
  const [isHovering, setIsHovering] = useState(false);

  const handleMouseOver = () => {
    setIsHovering(true);
  };

  const handleMouseLeave = () => {
    setIsHovering(false);
  };

  return (
    <>
      {!isHovering && (
        <ReferenceLink
          handleMouseOver={handleMouseOver}
          handleMouseOut={handleMouseLeave}
        />
      )}

      {isHovering && (
        <LanguagesWrapper
          onMouseOver={handleMouseOver}
          onMouseLeave={handleMouseLeave}>
          {languages.map((language, index) => (
            <StyledLanguagesTitle onClick={() => openNewTab(link)} key={index}>
              {language}
            </StyledLanguagesTitle>
          ))}
        </LanguagesWrapper>
      )}
    </>
  );
};

export default LanguageOptions;

I tried playing around with the CSS but no avail

英文:

I'm trying to create a languages option component. It has language codes placed vertically in a parent component as such:

CSS悬停高亮不覆盖父组件。

When I hover over the language options, it highlights the letter codes (a separate styled component). I can't figure out how to get it to cover the width of the parent component.

Here's the code:

Styling:

import styled, { css } from &#39;styled-components&#39;;


export const LanguagesWrapper = styled.div`
  background-color: #b0b2b7;
  border-radius: 5px;
  display: flex;
  flex-direction: column;
  flex-grow: 1; /* Add flex-grow property to expand vertically */
  padding: 0.3rem;
  gap: 0.8rem;
  
`;

export const StyledLanguagesTitle = styled.p`
  font-size: ${({theme}) =&gt; theme.typography.fontSize.medium};
  cursor: pointer;
  transition: background-color 0.3s;
  &amp;:hover {
    background-color: #526d82;
  }
`;

export const ButtonForReferenceLink = styled.button&lt;{
  icon: string;
}&gt;`
  position: relative;
  width: 1.3rem;
  height: 1.3rem;
  background-image: url(${({icon}) =&gt; icon});
  background-repeat: no-repeat;
  background-position: center;
  background-size: contain;
  background-color: transparent;
  border: none;
  cursor: pointer;
  :first-of-type {
    height: 1.1rem;
  }
`;
import {useState} from &#39;react&#39;;
import {
  ButtonForReferenceLink,
  LanguagesWrapper,
  StyledLanguagesTitle,
} from &#39;./styles&#39;;
import {LanguageOptionsProps, ReferenceLinkProps} from &#39;./types&#39;;
import openNewTab from &#39;../ModalsContent/References/utils/openNewTab&#39;;

const ReferenceLink = ({
  handleMouseOver,
  handleMouseOut,
}: ReferenceLinkProps) =&gt; {
  return (
    &lt;ButtonForReferenceLink
      onMouseOver={handleMouseOver}
      onMouseOut={handleMouseOut}
      icon={&#39;/images/arrow-up-right-from-square-solid.svg&#39;}
      /* onClick={() =&gt; openNewTab(link)} */
    /&gt;
  );
};

const LanguageOptions = ({languages, link}: LanguageOptionsProps) =&gt; {
  const [isHovering, setIsHovering] = useState(false);

  const handleMouseOver = () =&gt; {
    setIsHovering(true);
  };

  const handleMouseLeave = () =&gt; {
    setIsHovering(false);
  };

  return (
    &lt;&gt;
      {!isHovering &amp;&amp; (
        &lt;ReferenceLink
          handleMouseOver={handleMouseOver}
          handleMouseOut={handleMouseLeave}
        /&gt;
      )}

      {isHovering &amp;&amp; (
        &lt;LanguagesWrapper
          onMouseOver={handleMouseOver}
          onMouseLeave={handleMouseLeave}&gt;
          {languages.map((language, index) =&gt; (
            &lt;StyledLanguagesTitle onClick={()=&gt;openNewTab(link)
            } key={index} &gt;
              {language}
              
            &lt;/StyledLanguagesTitle&gt;
          ))}
        &lt;/LanguagesWrapper&gt;
      )}
    &lt;/&gt;
  );
};

export default LanguageOptions;

I tried playing around with the CSS but no avail

答案1

得分: 0

在提供的代码片段中,我已经进行了必要的调整。LanguagesWrapper 现在只处理背景颜色、边框半径、弹性属性和子元素之间的间距。StyledLanguagesTitle 组件具有 padding: 0.3rem 声明和悬停效果样式。

请注意,根据您的具体需求,您可能需要根据所有语言标题(如“EN”、“FR”、“DE”、“IT”)的特定要求来调整填充值,否则可能会导致过多的空间。

希望这为您解决了问题。

英文:

The problem here is that you have a padding: 0.3rem applied in your LanguagesWrapper. This padding will push any children of the LanguagesWrapper 0.3rem units inside of it, meaning the children will fill the width of its parent minus the 0.3rem on each side.

To make the hover effect fill the full width of the parent, you need to declare the padding: 0.3rem on your StyledLanguagesTitle component instead.

import styled from &#39;styled-components&#39;;

export const LanguagesWrapper = styled.div`
  // removed padding: 0.3rem;
  background-color: #b0b2b7;
  border-radius: 5px;
  display: flex;
  flex-direction: column;
  flex-grow: 1; /* Add flex-grow property to expand vertically */
  gap: 0.8rem;
`;

export const StyledLanguagesTitle = styled.p`
  padding: 0.3rem; // added padding here;
  font-size: ${({ theme }) =&gt; theme.typography.fontSize.medium};
  cursor: pointer;
  transition: background-color 0.3s;
  &amp;:hover {
    background-color: #526d82;
  }
`;

In the provided code snippet, I have made the necessary adjustment. The LanguagesWrapper now only handles the background color, border radius, flex properties, and gap between the children. The StyledLanguagesTitle component has the padding: 0.3rem declaration and the hover effect styles.

Please note that you may need to adjust the padding value based on your specific requirements, as it might lead to excessive space if applied to all language titles like "EN", "FR", "DE", "IT".

I hope this clarifies the issue for you.

huangapple
  • 本文由 发表于 2023年5月25日 15:18:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76329759.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定