首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >减少React with Styled Components中的重复代码行

问减少React with Styled Components中的重复代码行
EN

Stack Overflow用户
提问于 2021-05-13 19:22:23
回答 2查看 63关注 0票数 0

构建一个accordion样式组件,以便在折叠状态下只显示前40个字符,并且在展开时用户可以看到整个内容。我刚接触React/Styled组件,但我觉得我是在重复我的样式,而有一种更有效的方法可以将样式链接到现有的组件上,有人能给我提个建议吗?

代码语言:javascript
复制
const Accordion = styled.div`
  background-color: #e5e9eb;
  height: 87px;
  width: 612px;
  border-radius: 2px;
  border: 1px solid #27282a;
  margin-bottom: 48px;
  display: flex;
  align-items: center;

  span {
    font-size: 14px;
    line-height: 20px;
    padding-left: 24px;
  }
`;

const AccordionExpanded = styled.div`
  background-color: #e5e9eb;
  height: 174px;
  width: 612px;
  border-radius: 2px;
  border: 1px solid #27282a;
  margin-bottom: 48px;
  display: flex;
  align-items: center;

  span {
    font-size: 14px;
    line-height: 20px;
    padding-left: 24px;
  }
`;

const Title = styled.h3`
font-size: 12px;
letter-spacing: 1px;
text-transform: uppercase;
padding-left: 24px;
`;

const ExpandIcon = styled.img`
height: 40px;
width: 40px;
float: right;
margin-right: 12px;
`;

const CollapseIcon = styled.img`
height: 40px;
width: 40px;
float: right;
margin-right: 12px;
-webkit-transform: rotate(180deg);
`;

const ExpandableString = ({ attribute, className }: Props) => {
  const [isExpanded, setIsExpanded] = React.useState(false);
  const fullDescription = attribute.readonlyvalue;
  const shortHeading = fullDescription.substring(0, 40) + '...';

  function toggleContent() {
    setIsExpanded(prev => !prev);
  }

  return (
    isExpanded ? <AccordionExpanded className={className}>
      <Title>Goods being sent</Title>
      <span>{fullDescription}</span>
      <CollapseIcon onClick={toggleContent} src={chevron} alt="Collapse content" />
    </AccordionExpanded> : <Accordion className={className}>
      <Title>Goods being sent</Title>
      <span>{shortHeading}</span>
      <ExpandIcon onClick={toggleContent} src={chevron} alt="Expand content" />
    </Accordion>
  );
};
EN

回答 2

Stack Overflow用户

发布于 2021-05-13 19:25:11

您可以使用此npm库将普通css编写为类,然后将其生成变量以用于React样式的组件。

https://www.npmjs.com/package/@inspiraller/create-css-vars

票数 1
EN

Stack Overflow用户

发布于 2021-05-14 03:54:41

使用样式组件中的css包装重复的代码。

syntax=>

代码语言:javascript
复制
import styled,{css} from 'styled-components'

const repeatedstyle=css`
    //duplicate styles
`
const maincomponent=styled.div`
    ${repeatedstyle}
`;
代码语言:javascript
复制
import styled,{css} from 'styled-components'

const accordianstyles=css`
    background-color: #e5e9eb;
    border-radius: 2px;
    border: 1px solid #27282a;
    margin-bottom: 48px;
    display: flex;
    align-items: center;
`;
const accordianspanstyles=css`
    font-size: 14px;
    line-height: 20px;
    padding-left: 24px;
`;
const iconstyles=css`
    height: 40px;
    width: 40px;
    float: right;
    margin-right: 12px;
`;

const Accordion = styled.div`
  height: 87px;
  width: 612px;
  ${accordianstyles}
  span {
      ${accordianspanstyles}
  }
`;

const AccordionExpanded = styled.div`
  height: 174px;
  width: 612px;
  ${accordianstyles}

  span {
    ${accordianspanstyles}
  }
`;

const Title = styled.h3`
font-size: 12px;
letter-spacing: 1px;
text-transform: uppercase;
padding-left: 24px;
`;

const ExpandIcon = styled.img`
    ${iconstyles}
`;

const CollapseIcon = styled.img`
    ${iconstyles}
-webkit-transform: rotate(180deg);
`;

const ExpandableString = ({ attribute, className }: Props) => {
  const [isExpanded, setIsExpanded] = React.useState(false);
  const fullDescription = attribute.readonlyvalue;
  const shortHeading = fullDescription.substring(0, 40) + '...';

  function toggleContent() {
    setIsExpanded(prev => !prev);
  }

  return (
    isExpanded ? <AccordionExpanded className={className}>
      <Title>Goods being sent</Title>
      <span>{fullDescription}</span>
      <CollapseIcon onClick={toggleContent} src={chevron} alt="Collapse content" />
    </AccordionExpanded> : <Accordion className={className}>
      <Title>Goods being sent</Title>
      <span>{shortHeading}</span>
      <ExpandIcon onClick={toggleContent} src={chevron} alt="Expand content" />
    </Accordion>
  );
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67518378

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档