- objective-c - iOS 5 : Can you override UIAppearance customisations in specific classes?
- iphone - 如何将 CGFontRef 转换为 UIFont?
- ios - 以编程方式关闭标记的信息窗口 google maps iOS
- ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"
我目前正在构建一个大型 React 应用程序。 css,从来都不是我的强项。但是现在 CSS 有了 sass/cssNext/React 的内联样式。我一直在将 BEM 与 sass 结合使用,但随着我的其他应用程序变得越来越庞大,甚至开始崩溃。特别是当您能够在主要配色方案等之外“重新设计”或“主题化”页面时。
那么——有人能告诉我一个行之有效的方法来创建带有反应的 css,它可以很好地扩展,并且当人们想借用我的组件时允许定制主题。例如,
<MyComponent />
// this component has its styles but lets say Joe Schmoe wants be import
// it? But, Joe wants to overlay his custom styles?
// Is there a new paradigm that allows for an overlay or reskin within the component?
甚至是整个应用程序在某个时候都可以换肤的想法。我知道这是一个非常基本的问题,但每当我构建元素时,我的痛点似乎也是 CSS - 所以我想知道什么才是真正有效的。
最佳答案
直到最近,这在 React 世界中还没有解决。
我是 ElementalUI 的维护者之一,一个 React 组件库,在过去的 6-12 个月里,我们一直在尝试所有不同的样式设置方式。 (!) 你说出它的名字,我们已经尝试过了。 (我在 ReactNL keynote 期间谈到了我对一些最流行的库的体验以及它们崩溃的地方)
问题是当前的样式库都没有内置对主题的支持。您可以通过一种非常骇人听闻的、对用户不友好的方式使用其中的大多数,但这不是您分发组件时想要的,对吧?
这就是我们构建 styled-components
的原因. styled-components
有很多有趣的属性,其中之一就是主题直接内置到库中,使其成为构建可分发组件的完美选择!
这是简短的解释,但我鼓励您阅读我们的 documentation这解释了一切!
这是 styled-components
的基本用法:
import React from 'react';
import styled from 'styled-components';
// Create a <Wrapper> react component that renders a <section> with
// some padding and a papayawhip background
const Wrapper = styled.section`
padding: 4em;
background: papayawhip;
`;
这个变量,Wrapper
,现在是你可以渲染的 React 组件:
// Use them like any other React component – except they're styled!
<Wrapper>
<Title>Hello World, this is my first styled component!</Title>
</Wrapper>
(如果您点击图片,您将看到一个现场 Playground )
当您将内插函数传递给标记的模板文字时,我们会将传递给组件的属性传递给您。这意味着你可以适应 Prop :
import styled from 'styled-components';
const Button = styled.button`
/* Adapt the colors based on primary prop */
background: ${props => props.primary ? 'palevioletred' : 'white'};
color: ${props => props.primary ? 'white' : 'palevioletred'};
font-size: 1em;
margin: 1em;
padding: 0.25em 1em;
border: 2px solid palevioletred;
border-radius: 3px;
`;
在这里,我们创建了一个 Button
组件,您可以像任何其他 React 组件一样将其设为 primary
:
<Button>Normal</Button>
<Button primary>Primary</Button>
现在是主题方面。我们导出一个名为 ThemeProvider
的组件,您可以将 theme
传递给该组件并将您的应用程序(或应用程序的一部分)包装在:
import { ThemeProvider } from 'styled-components';
const theme = {
main: 'mediumseagreen',
};
ReactDOM.render(
<ThemeProvider theme={theme}>
<MyApp />
</ThemeProvider>,
myElem
);
现在 ThemeProvider
中的任何样式组件,无论上下文有多深,都会将此 theme
注入(inject)到 props 中。
这就是可主题化的 Button
的样子:
import styled from 'styled-components';
const Button = styled.button`
/* Color the background and border with theme.main */
background: ${props => props.theme.main};
border: 2px solid ${props => props.theme.main};
/* …more styles here… */
`;
现在您的 Button
将采用它传递的 theme
并根据它更改它的样式! (您也可以通过 defaultProps
提供默认值)
这就是 styled-components
的要点,以及它如何帮助构建可分发组件!
我们目前有一个 WIP doc about writing third-party component libraries我鼓励您检查一下,当然,普通文档也很不错。我们已尝试涵盖所有基础内容,因此如果您发现任何不喜欢的内容或您认为遗漏的内容,请立即告诉我们,我们将进行讨论!
如果您对 styled-components
有任何其他问题,请随时在此处回复或在 Twitter 上联系。 ( @mxstbr )
关于带有 React 的 CSS 架构也可以被主题化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40859193/
这个问题已经有答案了: Cannot create AlertDialog: AppCompat error (2 个回答) 已关闭 6 年前。 当我在列表项中调用警报对话框时,我的应用程序崩溃了。我
我在 Angular 应用程序中安装了 Material UI,现在我收到错误,没有导出的成员 Observable 错误, 我删除了节点模块并重新安装,问题仍然存在 ERROR in node_mo
我有一个架构,其中有两个独立的应用程序。原始来源是一个sql数据库。 App1 监听 CDC 表以跟踪对该数据库中表的更改、规范化和序列化这些更改。它获取这些序列化消息并将它们发送到 Kafka 主题
这个问题在这里已经有了答案: Material Design, AppCompat, and Backwards Compatibility (1 个回答) 关闭 6 年前。 我收到如下错误信息:
我喜欢新 Office 套件和 Visual Studio 上的窗口镶边: 当然,我仍在为 Windows 7 开发应用程序,但我想知道是否有一种快速且简单的方法(阅读:WPF 样式或 Windows
我正在使用 HoloEverywhere-1.6.8。 我有一个基于 Holo.Theme 的自定义主题。 ... 我遇到了下面的崩溃,但它只出现在以下设备上: Galaxy Tab 10.1 P
我正在尝试为 Angular 的 DevExtreme 小部件加载主题。我采用了不同的方法: 在 angular.json 中设置样式但不会产生任何影响: "projects": { "my-proj
我想定义一个 android 样式,它扩展了一个在不同的应用程序/包中定义的样式,而不是作为库导入。 从对android资源的xml引用的定义here : @[:]/ 似乎可以在定义资源的地方指定一个
我正在尝试测试一种制作主题的方法,但我使用的方法并没有给我预期的结果。这是我的设置: drawable/dummy.xml 值/mythemes.xml @style
通过 telnet 使用 IMAP,我希望能够从特定的给定电子邮件中提取主题。现在我知道 fetch 命令负责从电子邮件中获取数据。 我的问题是,如何在不使用对 BODY[HEADER.FIELDS
我刚刚开始使用 RStudio 中的一些新的 knitr 功能。 我已经尝试在 R Markdown 设置对话框中选择几个不同的主题,但这些似乎对我生成的文档的样式没有任何明显的影响。应该,还是我错过
在我的应用程序中,我有多种主题样式(您可以将它们视为不同的、单独的 CSS 样式文件)。我想开始使用 CSS 模块,但我什至不知道如何 import我的第一个文件。 让我们假设以下(简单)目录结构:
有没有一种方法可以在一个 Azure 主题订阅上拥有多个监听客户端,并且它们都接收所有消息?我的理解是订阅的唯一实现是发布的消息仅传递到该订阅上的一个客户端,因为它就像一个队列。 可以使用同一订阅将这
我有一台 super 光滑的显示器,所以白天我可以比深色主题上的代码更好地看到自己的倒影。因此,我认为如果我可以在 vimrc 中有一个简单的 if 开关来根据一天中的时间设置深色主题或浅色主题,那就
我希望在我的 Symfony2 项目中提供基本的主题支持,因此我希望为每个主题提供单独的静态文件(css、js、img)。 我尝试添加 assetic: read_from: %kernel
有没有一种方法可以在一个 Azure 主题订阅上拥有多个监听客户端,并且它们都接收所有消息?我的理解是订阅的唯一实现是发布的消息仅传递到该订阅上的一个客户端,因为它就像一个队列。 可以使用同一订阅将这
在 NES 上有多个处于 WAITING 状态的“Discovery Worker”和“Keep Alive”线程是预期的行为吗? "DiscoveryWorker-10" Id=62 WAITING
我正在尝试找到最适合加载图像的颜色并将其应用到背景中。适应图像并使 UI 感觉更自然。 到目前为止我已经找到了 2 个方案: 1> 平均像素(下面的代码): final Color acclimati
我知道每个请求都由一个 servlet 线程提供服务,但是对于一个用户 session ,两个请求是否可以由两个不同的线程提供服务? 如果上述情况真的发生,那么第一个请求服务线程存储的线程局部变量被第
我无法理解操作栏外观与主题化之间的交互模式。我的应用设置为使用默认主题,我认为它是深色的: 通过应用范围内的样式从应用中删除操作栏会导致主要 Activity 的黑色背景: 没有 and
我是一名优秀的程序员,十分优秀!