Jam*_*ley 10 themes reactjs styled-components
我在我的React应用程序中使用样式组件,并希望使用动态主题.有些区域会使用我的黑暗主题,有些会使用光线.因为样式化组件必须在它们所使用的组件之外声明,所以我们如何动态地传递主题?
mxs*_*tbr 37
这正是ThemeProvider组件的用途!
样式化组件theme在插入函数时可以访问特殊道具:
const Button = styled.button`
background: ${props => props.theme.primary};
`
Run Code Online (Sandbox Code Playgroud)
该<Button />组件现在将动态响应由a定义的主题ThemeProvider.你如何定义一个主题?将任何对象传递给以下的theme道具ThemeProvider:
const theme = {
primary: 'palevioletred',
};
<ThemeProvider theme={theme}>
<Button>I'm now palevioletred!</Button>
</ThemeProvider>
Run Code Online (Sandbox Code Playgroud)
我们为您的样式组件提供主题context,这意味着无论组件和ThemeProvider之间有多少组件或DOM节点,它仍将完全相同:
const theme = {
primary: 'palevioletred',
};
<ThemeProvider theme={theme}>
<div>
<SidebarContainer>
<Sidebar>
<Button>I'm still palevioletred!</Button>
</Sidebar>
</SidebarContainer>
</div>
</ThemeProvider>
Run Code Online (Sandbox Code Playgroud)
这意味着您可以将整个应用程序包装在一个单独的应用程序中ThemeProvider,并且所有样式化的组件都将获得该主题.您可以动态交换该属性以在明暗主题之间切换!
您可以根据需要ThemeProvider在应用中添加尽可能少的内容.大多数应用程序只需要一个包装整个应用程序,但要让你的应用程序的一部分是浅色主题和其他一些黑暗主题,你只需将它们包装在两个ThemeProvider具有不同主题的s中:
const darkTheme = {
primary: 'black',
};
const lightTheme = {
primary: 'white',
};
<div>
<ThemeProvider theme={lightTheme}>
<Main />
</ThemeProvider>
<ThemeProvider theme={darkTheme}>
<Sidebar />
</ThemeProvider>
</div>
Run Code Online (Sandbox Code Playgroud)
任何内部任何风格的组件Main现在都是浅色主题,任何内部任何风格的组件都Sidebar将是黑暗主题.它们根据应用程序的哪个区域进行调整,您无需做任何事情来实现它!
我鼓励你查看我们关于主题的文档,因为样式组件在构建时考虑到了这一点.
在样式组件存在之前,JS中样式的一大难点是,以前的库对样式进行了封装和托管,但没有一个具有适当的主题支持.如果你想了解更多关于我们在现有库中遇到的其他痛点,我建议你在ReactNL上观看我的演讲,在那里我发布了样式组件.(注意:样式组件的第一次出现是在~25分钟内,不要感到惊讶!)
Jos*_*e A 13
虽然这个问题最初是为了让多个主题同时运行,但我个人希望在运行时为整个应用程序动态切换一个主题。
我是这样实现的:(我将在这里使用 TypeScript 和钩子。对于纯 JavaScript,只需删除types 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>;
Run Code Online (Sandbox Code Playgroud)
然后在我们的主条目中,在这种情况下,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);
Run Code Online (Sandbox Code Playgroud)
在 header.tsx 中,我们将 setTheme 传递给组件(提升状态):
// header.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);
Run Code Online (Sandbox Code Playgroud)