模态调用自定义样式模态的React样式的组件特异性问题
我正在与React中的样式组件合作。我想在组件中使用以下样式的模态样式,
const StyledModal = styled(Modal)`
.modal-container-header {
display: none;
}
.modal-container-footer {
display: none ;
}
.modal-container-close {
display: none;
}
`;
也就是说,我想省略我的自定义模式的全球页脚。它在一种情况下工作正常,但不适用于另一种情况。
这是整个组件,
interface LoaderInterface {
loading: boolean;
loadingText?: string;
}
const StyledModal = styled(Modal)`
.modal-container-header {
display: none;
}
.modal-container-footer {
display: none ;
}
.modal-container-close {
display: none;
}
`;
const Loader: FC<LoaderInterface> = ({ loading, loadingText }) => {
console.log("this modal is working");
return (
<StyledModal onClose={() => loading === false} open={loading}>
<div>
<Loading size="large" />
<Heading type="h4">{loadingText}</Heading>
</div>
</StyledModal>
);
};
export default Loader;
- 当通用反应功能组件调用自定义样式模式时,它正在工作
- ,但是当模态调用此自定义模式时,它无法正常工作。当模态触发自定义模态时,调用模式的样式就会受到影响。但是在调用此自定义模式之前,我关闭了调用模式。
在开发工具中,我总是看到,例如,对于页脚,
.hPyTDi modal-container-footer {
border-top: 1px solid rgba(0,0,0,0.06);
padding: 16px 24px;
text-align: right;
}
这就是全球风格。不是我的自定义风格。当我应用显示时:note
在开发工具中的上述代码中显然正在工作。
但是,当我的自定义模式由React功能组件调用时,此问题不会持续。仅当模态(也是React组件)调用此自定义模式时,才出现此问题。
我也尝试了'&amp;'提高特异性,但行不通。
我做到了,
&.modal-container-footer {
display: none;
}
这行不通。如果我将自定义模态风格移动到全球位置,则可以运行。但是我不希望这样做,因为这适用于我希望可以看到标头/页脚的所有模式。
真的很挣扎。任何帮助都非常感谢。
I am working with styled component in React. I want to style a modal with the following style inside my component,
const StyledModal = styled(Modal)`
.modal-container-header {
display: none;
}
.modal-container-footer {
display: none ;
}
.modal-container-close {
display: none;
}
`;
That is, I want to omit the global footer, header for my custom modal. It is working fine for one case but not working for another.
Here is the entire component,
interface LoaderInterface {
loading: boolean;
loadingText?: string;
}
const StyledModal = styled(Modal)`
.modal-container-header {
display: none;
}
.modal-container-footer {
display: none ;
}
.modal-container-close {
display: none;
}
`;
const Loader: FC<LoaderInterface> = ({ loading, loadingText }) => {
console.log("this modal is working");
return (
<StyledModal onClose={() => loading === false} open={loading}>
<div>
<Loading size="large" />
<Heading type="h4">{loadingText}</Heading>
</div>
</StyledModal>
);
};
export default Loader;
- It is working when a general React functional component is calling the custom styled modal
- but it is not working when a modal calling this custom modal. When the modal is triggering the custom modal then the style of the calling modal is getting effected. But I closed the calling modal before calling this custom modal.
In DEV tool I'm always seeing, for example, for footer,
.hPyTDi modal-container-footer {
border-top: 1px solid rgba(0,0,0,0.06);
padding: 16px 24px;
text-align: right;
}
That is the global style. Not my custom style. when I apply display: none
inside the above code in the dev tool obviously it is working.
But this issue doesn't persist when my custom modal is called by a React functional component. Only this issue appears when a modal (which is also a React component) calls this custom modal.
I also tried '&' to increase specificity but it doesn't work.
I did,
&.modal-container-footer {
display: none;
}
It doesn't work. If I move the custom modal style in a global place it works. But I don't want that as then this is applied for all modal where I want the header/footer to be visible.
Really struggling with this. Any help is much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到了许多问题,即样式的组件选择器的方式,并意识到将道具传递给它们总是“更安全的”。这是因为SC动态分配类名称的方式。这可能会有所帮助吗?
对于打字稿,语法相似:
I've run into so many issues with the way Styled Components selectors, and come to realise that it's always 'safer' to pass props into them. This is because of the way SC dynamically assigns class names. Might this help?
For TypeScript, the syntax is similar: