我在我的React应用程序中使用样式组件并希望使用动态主题。有些区域会使用我的黑暗主题,有些会使用光线。因为样式化组件必须在它们所使用的组件之外声明,我们如何动态地传递主题?
答案 0 :(得分:31)
这正是ThemeProvider
组件的用途!
您的样式化组件在插入函数时可以访问特殊的theme
道具:
const Button = styled.button`
background: ${props => props.theme.primary};
`
此<Button />
组件现在将动态响应ThemeProvider
定义的主题。你如何定义一个主题?将任何对象传递给theme
的{{1}}道具:
ThemeProvider
我们通过const theme = {
primary: 'palevioletred',
};
<ThemeProvider theme={theme}>
<Button>I'm now palevioletred!</Button>
</ThemeProvider>
为您的样式组件提供主题,这意味着无论组件和ThemeProvider之间有多少组件或DOM节点,它仍将完全相同:
context
这意味着您可以将整个应用程序包装在一个const theme = {
primary: 'palevioletred',
};
<ThemeProvider theme={theme}>
<div>
<SidebarContainer>
<Sidebar>
<Button>I'm still palevioletred!</Button>
</Sidebar>
</SidebarContainer>
</div>
</ThemeProvider>
中,并且所有样式化的组件都将获得该主题。您可以动态交换该属性以在明暗主题之间切换!
您可以根据需要在应用中添加尽可能少的ThemeProvider
个ThemeProvider
个。大多数应用程序只需要一个包装整个应用程序,但要让你的应用程序的一部分是浅色主题和其他一些黑暗主题,你只需将它们包装在两个具有不同主题的ThemeProvider
中:
const darkTheme = {
primary: 'black',
};
const lightTheme = {
primary: 'white',
};
<div>
<ThemeProvider theme={lightTheme}>
<Main />
</ThemeProvider>
<ThemeProvider theme={darkTheme}>
<Sidebar />
</ThemeProvider>
</div>
Main
内任何位置的任何样式化组件现在都是浅色主题,Sidebar
内任何位置的任何样式组件都将是黑暗主题。它们根据应用程序的哪个区域进行调整,您无需做任何事情来实现它!
我建议您查看我们的docs about theming,因为样式组件的构建非常考虑到这一点。
在样式组件存在之前,JS中样式的一个主要难点在于,以前的库对样式的封装和共置非常好,但没有一个具有适当的主题支持。如果您想了解更多关于我们对现有库的其他痛点的建议,我建议您观看我发布样式组件的my talk at ReactNL。 (注意:样式组件的第一次出现是在~25分钟内,不要感到惊讶!)
答案 1 :(得分:1)
这个问题最初是因为要同时运行多个主题,但我个人想在运行时动态切换一个 单个主题 对于整个应用程序。
这是我的实现方式:(我将使用TypeScript并在此处进行钩子。对于普通JavaScript,只需删除type
,as
和interface
):>
为防万一,我还在所有块代码的顶部都包含了所有导入内容。
我们定义了theme.ts
文件
//theme.ts
import baseStyled, { ThemedStyledInterface } from 'styled-components';
export const lightTheme = {
all: {
borderRadius: '0.5rem',
},
main: {
color: '#FAFAFA',
textColor: '#212121',
bodyColor: '#FFF',
},
secondary: {
color: '#757575',
},
};
// Force both themes to be consistent!
export const darkTheme: Theme = {
// Make properties the same on both!
all: { ...lightTheme.all },
main: {
color: '#212121',
textColor: '#FAFAFA',
bodyColor: '#424242',
},
secondary: {
color: '#616161',
},
};
export type Theme = typeof lightTheme;
export const styled = baseStyled as ThemedStyledInterface<Theme>;
然后在主条目中,在本例中为App.tsx
,我们在将要使用该主题的每个组件之前定义<ThemeProvider>
。
// app.tsx
import React, { memo, Suspense, lazy, useState } from 'react';
import { Router } from '@reach/router';
// The header component that switches the styles.
import Header from './components/header';
// Personal component
import { Loading } from './components';
import { ThemeProvider } from 'styled-components';
// Bring either the lightTheme, or darkTheme, whichever you want to make the default
import { lightTheme } from './components/styles/theme';
// Own code.
const Home = lazy(() => import('./views/home'));
const BestSeller = lazy(() => import('./views/best-seller'));
/**
* Where the React APP main layout resides:
*/
function App() {
// Here we set the default theme of the app. In this case,
// we are setting the lightTheme. If you want the dark, import the `darkTheme` object.
const [theme, setTheme] = useState(lightTheme);
return (
<Suspense fallback={<Loading />}>
<ThemeProvider theme={theme}>
<React.Fragment>
{/* We pass the setTheme function (lift state up) to the Header */}
<Header setTheme={setTheme} />
<Router>
<Home path="/" />
<BestSeller path="/:listNameEncoded" />
</Router>
</React.Fragment>
</ThemeProvider>
</Suspense>
);
}
export default memo(App);
然后在header.tsx中,将setTheme传递给组件(提升状态):
// app.tsx
import React, { memo, useState } from 'react';
import styled, { ThemedStyledInterface } from 'styled-components';
import { Theme, lightTheme, darkTheme } from '../styles/theme';
// We have nice autocomplete functionality
const Nav = styled.nav`
background-color: ${props => props.theme.colors.primary};
`;
// We define the props that will receive the setTheme
type HeaderProps = {
setTheme: React.Dispatch<React.SetStateAction<Theme>>;
};
function Header(props:
function setLightTheme() {
props.setTheme(lightTheme);
}
function setDarkTheme() {
props.setTheme(darkTheme);
}
// We then set the light or dark theme according to what we want.
return (
<Nav>
<h1>Book App</h1>
<button onClick={setLightTheme}>Light </button>
<button onClick={setDarkTheme}> Dark </button>
</Nav>
);
}
export default memo(Header);
答案 2 :(得分:0)
以下是对我有用的东西:
import * as React from 'react';
import { connect } from 'react-redux';
import { getStateField } from 'app/redux/reducers/recordings';
import { lightTheme, darkTheme, ThemeProvider as SCThemeProvider } from 'app/utils/theme';
import { GlobalStyle } from 'app/utils/globalStyles';
interface ThemeProviderProps {
children: JSX.Element;
isLightMode?: boolean;
}
const ThemeProvider = ({ children, isLightMode }: ThemeProviderProps) => {
return (
<SCThemeProvider theme={isLightMode ? lightTheme : darkTheme}>
<React.Fragment>
{children}
<GlobalStyle />
</React.Fragment>
</SCThemeProvider>
);
};
export const ConnectedThemeProvider = connect((state) => ({
isLightMode: getStateField('isLightMode', state)
}))(ThemeProvider);