简体   繁体   English

将样式化组件添加到通常的类包装器之外并通过 ajax 调用的引导模式

[英]Adding styled component to bootstrap modal which is outside of usual class wrapper and called via ajax

I am working with Bootstrap modals and building a react component as well as passing styled components.我正在使用 Bootstrap 模态并构建反应组件以及传递样式组件。 The issue I am facing is that the modal is appended to the dom via ajax and resides outside of the exported styled component wrapper I am building.我面临的问题是模式通过 ajax 附加到 dom 并驻留在我正在构建的导出样式组件包装器之外。

Here is part of my react:这是我的反应的一部分:

  import { ModalWrapper } from './ModalStyles';
  ...
  return (
      <>
        <ButtonSecondary
          onClick={handleShow}
          title={buttonText}
          route="/"
          brand={props.brand}
        >
        </ButtonSecondary>

        <ModalWrapper> // this does not exist as the container has not yet been called by the ajax method
          <Modal
          show={show}
          onHide={handleClose}
          >
            <Modal.Header>
              <Modal.Title>{title}</Modal.Title>
            </Modal.Header>
            <Modal.Body>
              <p>{message}</p>
            </Modal.Body>

            <Modal.Footer>
              <ButtonPrimary
                onClick={handleClose}
              >
              </ButtonPrimary>
              <ButtonSecondary
                onClick={handleClose}
              >
              </ButtonSecondary>
            </Modal.Footer>
        </Modal>
      </ModalWrapper>
    </>
  );

My styled component:我的样式组件:

export const ModalWrapper = styled.div`
  ${modalStyles};
  
  .modal-header {
    ${mixins.borderRadiusNone};
    background-color: ${colors.baseBlue};
    color: ${colors.white};
  }
`;

As as interim measure I changed <ModalWrapper> to <InnerModalWrapper> and put it inside the modal.作为临时措施,我将<ModalWrapper>更改为<InnerModalWrapper>并将其放入模态。 This works for any child property below the modal-content.这适用于模态内容下的任何子属性。 However I still want to access class properties on the parents of this element.但是我仍然想访问这个元素的父类的类属性。

Is it possible to just use classnames in styled-components?是否可以只在样式组件中使用类名? Does everything need to be exported as a wrapper.是否需要将所有内容导出为包装器。 I am migrating from sass.我正在从 sass 迁移。 I am also guessing people don't mix styled components and sass.我也猜测人们不会混合样式组件和 sass。 I am thinking about how we want to use global styles, I don't want or need to write a styled component for everything.我正在考虑如何使用全局样式,我不想或不需要为所有内容编写样式组件。 Their should be some base inheritance.他们应该是一些基础继承。

Well it was a bit of an ache but I have something working via global styles.嗯,这有点痛苦,但我有一些通过全局样式工作的东西。

However before that I tried to pass the class name of the wrapper into the Modal div like so:但是在此之前,我尝试将包装器的类名传递到 Modal div 中,如下所示:

<Modal className={ModalWrapper.toString().replace('.','')}

This passes the first class name to the wrapper (and removes the preceding dot) however this class name appears to be fixed, whereas the second class name passed to the wrapper seems to be the dynamic one (which is changed in subsequent builds and which is the one I want to access).这会将第一个类名传递给包装器(并删除前面的点),但是这个类名似乎是固定的,而传递给包装器的第二个类名似乎是动态的(在后续构建中更改,并且我想访问的那个)。

在此处输入图片说明

So this method failed.所以这个方法失败了。 iyPhHa in this instance.在这种情况下, iyPhHa

So I just created a globalStyles file and dumped all my modal styles in there.所以我刚刚创建了一个 globalStyles 文件并将我所有的模态样式转储到那里。 This also feels a bit clunky, but it works and it keeps the styles outside of the main dom.这也感觉有点笨拙,但它有效并且将样式保留在主 dom 之外。 It was something like this (I would be amazed if anybody reads this).它是这样的(如果有人读到这个,我会感到惊讶)。

const GlobalStyles = styled.div`
   .custom-modal {
      border-radius: 0 // bootstrap overrides etc.
   }
`;

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

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