Override styled components default theme

This commit is contained in:
Hazem Krimi
2021-01-07 14:45:14 +01:00
parent 0eaf23ad60
commit 065413eaf5
5 changed files with 80 additions and 24 deletions
+16
View File
@@ -0,0 +1,16 @@
import 'styled-components';
declare module 'styled-components' {
export interface DefaultTheme {
colors: {
dark: {
background: string;
text: string;
};
light: {
background: string;
text: string;
};
};
}
}
+26 -13
View File
@@ -1,8 +1,7 @@
import { FC, useContext } from 'react'; import { FC, useContext } from 'react';
import { DarkModeContext } from '../components/DarkMode'; import { DarkModeContext } from '../components/DarkMode';
import styled from 'styled-components'; import styled from 'styled-components';
interface Props {
export interface Props {
variant?: 'outline' | 'text' | 'action'; variant?: 'outline' | 'text' | 'action';
onClick?: () => void; onClick?: () => void;
dark?: boolean; dark?: boolean;
@@ -14,16 +13,18 @@ const Btn = styled.button<Props>`
cursor: pointer; cursor: pointer;
background: none; background: none;
outline: none; outline: none;
color: ${({ dark }) => (dark ? 'white' : 'black')}; color: ${({ dark, theme }) => (dark ? theme.colors.dark.text : theme.colors.light.text)};
padding: 1rem; padding: 1rem;
border: ${({ variant, dark }) => border: ${({ variant, dark, theme }) =>
variant === 'outline' ? `2px solid ${dark ? 'white' : 'black'}` : 'none'}; variant === 'outline'
? `2px solid ${dark ? theme.colors.dark.text : theme.colors.light.text}`
: 'none'};
font-weight: bold; font-weight: bold;
font-size: ${({ variant }) => (variant === 'outline' ? '1.05rem' : 'inherit')}; font-size: ${({ variant }) => (variant === 'outline' ? '1.05rem' : 'inherit')};
text-transform: ${({ variant }) => (variant === 'outline' ? 'uppercase' : 'inherit')}; text-transform: ${({ variant }) => (variant === 'outline' ? 'uppercase' : 'inherit')};
padding: ${({ variant }) => (variant === 'outline' ? '.5rem 1rem' : '0rem')}; padding: ${({ variant }) => (variant === 'outline' ? '.5rem 1rem' : '0rem')};
transition: color 300ms ease-in-out; transition: color 250ms ease-in-out;
z-index: 1; z-index: 1;
&::before { &::before {
@@ -34,21 +35,33 @@ const Btn = styled.button<Props>`
bottom: 0; bottom: 0;
right: 0; right: 0;
z-index: -1; z-index: -1;
background-color: ${({ variant, dark }) => background-color: ${({ variant, dark, theme }) =>
variant === 'outline' ? (dark ? 'white' : 'black') : 'inherit'}; variant === 'outline'
transition: transform 300ms ease-in-out; ? dark
? theme.colors.dark.text
: theme.colors.light.text
: 'inherit'};
transition: transform 250ms ease-in-out;
transform: scaleX(0); transform: scaleX(0);
transform-origin: left; transform-origin: left;
} }
&:hover { &:hover {
color: ${({ variant, dark }) => color: ${({ variant, dark, theme }) =>
variant === 'outline' ? (dark ? '#262626' : '#F9F9F9') : 'inherit'}; variant === 'outline'
? dark
? theme.colors.dark.background
: theme.colors.light.background
: 'inherit'};
} }
&:focus { &:focus {
color: ${({ variant, dark }) => color: ${({ variant, dark, theme }) =>
variant === 'outline' ? (dark ? '#262626' : '#F9F9F9') : 'inherit'}; variant === 'outline'
? dark
? theme.colors.dark.background
: theme.colors.light.background
: 'inherit'};
} }
&:hover::before { &:hover::before {
+5 -4
View File
@@ -1,8 +1,7 @@
import { FC, useContext } from 'react'; import { FC, useContext } from 'react';
import { createGlobalStyle } from 'styled-components'; import { createGlobalStyle } from 'styled-components';
import { DarkModeContext } from '../components/DarkMode'; import { DarkModeContext } from '../components/DarkMode';
interface Props {
export interface Props {
dark: boolean; dark: boolean;
} }
@@ -16,8 +15,10 @@ const Global = createGlobalStyle<Props>`
} }
body { body {
background: ${({ dark }) => (dark ? '#262626' : '#F9F9F9')}; background: ${({ dark, theme }) =>
color: ${({ dark }) => (dark ? 'white' : 'black')}; dark ? theme.colors.dark.background : theme.colors.light.background};
color: ${({ dark, theme }) => (dark ? theme.colors.dark.text : theme.colors.light.text)};
transition: color 250ms ease-in-out, background 250ms ease-in-out;
} }
`; `;
+21
View File
@@ -0,0 +1,21 @@
import { FC } from 'react';
import { ThemeProvider, DefaultTheme } from 'styled-components';
const Theme: FC = ({ children }) => {
const theme: DefaultTheme = {
colors: {
dark: {
background: '#262626',
text: 'white'
},
light: {
background: '#F9F9F9',
text: 'black'
}
}
};
return <ThemeProvider theme={theme}>{children}</ThemeProvider>;
};
export default Theme;
+12 -7
View File
@@ -4,10 +4,11 @@ import Nav from '../components/Nav';
import DarkMode from '../components/DarkMode'; import DarkMode from '../components/DarkMode';
import Container from '../components/Container'; import Container from '../components/Container';
import GlobalStyles from '../components/GlobalStyles'; import GlobalStyles from '../components/GlobalStyles';
import Theme from '../components/Theme';
const App = ({ Component, pageProps }: AppProps) => { const App = ({ Component, pageProps }: AppProps) => {
return ( return (
<DarkMode> <>
<Head> <Head>
<link rel='preconnect' href='https://fonts.gstatic.com' /> <link rel='preconnect' href='https://fonts.gstatic.com' />
<link <link
@@ -16,12 +17,16 @@ const App = ({ Component, pageProps }: AppProps) => {
/> />
<title>Hazem Krimi</title> <title>Hazem Krimi</title>
</Head> </Head>
<GlobalStyles /> <DarkMode>
<Container> <Theme>
<Nav /> <GlobalStyles />
<Component {...pageProps} /> <Container>
</Container> <Nav />
</DarkMode> <Component {...pageProps} />
</Container>
</Theme>
</DarkMode>
</>
); );
}; };