-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add styled components theme provider
- Loading branch information
1 parent
2d2f6d1
commit 517e52b
Showing
15 changed files
with
240 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
/** @type {import('next').NextConfig} */ | ||
const nextConfig = { | ||
output: 'export', // Outputs a Single-Page Application (SPA). | ||
distDir: './build', // Changes the build output directory to `./dist`. | ||
output: 'export', // Outputs a Single-Page Application (SPA). | ||
distDir: './build', // Changes the build output directory to `./dist`. | ||
compiler: { | ||
styledComponents: true, | ||
}, | ||
} | ||
|
||
export default nextConfig | ||
export default nextConfig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import 'styled-components' | ||
import { defaultTheme } from '../styles/themes/default' | ||
import { colors } from '@sapuris-ui/tokens' | ||
|
||
export type Colors = typeof colors & { | ||
background500: string | ||
background600: string | ||
text500: string | ||
} | ||
|
||
type ITheme = typeof defaultTheme & { | ||
colors: Colors | ||
} | ||
|
||
declare module 'styled-components' { | ||
export interface DefaultTheme extends ITheme {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use client' | ||
|
||
import React, { ButtonHTMLAttributes } from 'react' | ||
import * as S from './styled' | ||
|
||
export type ButtonProps = S.ButtonContainerProps & | ||
ButtonHTMLAttributes<HTMLButtonElement> | ||
|
||
export const Button = React.forwardRef< | ||
HTMLButtonElement, | ||
React.PropsWithChildren<ButtonProps> | ||
>(({ children, ...props }: ButtonProps, forwardedRef) => ( | ||
<S.ButtonContainer {...props} ref={forwardedRef}> | ||
{children} | ||
</S.ButtonContainer> | ||
)) | ||
Button.displayName = 'Button' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import styled, { DefaultTheme, css } from 'styled-components' | ||
import { ButtonProps } from '.' | ||
|
||
export interface ButtonContainerProps { | ||
$color?: 'primary' | 'danger' | ||
$outline?: 'filled' | 'outlined' | ||
} | ||
|
||
const BUTTON_COLORS = ( | ||
theme: DefaultTheme, | ||
color: ButtonContainerProps['$color'] = 'primary', | ||
) => | ||
({ | ||
primary: theme.colors.sapuris500, | ||
danger: theme.colors.wine500, | ||
})[color] | ||
|
||
const variantStyles = ( | ||
theme: DefaultTheme, | ||
outline: ButtonContainerProps['$outline'] = 'filled', | ||
color: ButtonContainerProps['$color'] = 'primary', | ||
) => | ||
({ | ||
filled: css` | ||
color: ${theme.colors.white}; | ||
background: ${BUTTON_COLORS(theme, color)}; | ||
border: none; | ||
&:hover { | ||
filter: brightness(85%); | ||
} | ||
`, | ||
outlined: css` | ||
color: ${BUTTON_COLORS(theme, color)}; | ||
background: transparent; | ||
border: 1px solid ${BUTTON_COLORS(theme, color)}; | ||
&:hover { | ||
color: ${theme.colors.white}; | ||
background: ${BUTTON_COLORS(theme, color)}; | ||
} | ||
`, | ||
})[outline] | ||
|
||
export const ButtonContainer = styled.button<ButtonProps>` | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
gap: 8px; | ||
border-radius: ${(props) => props.theme.radii.md}; | ||
transition: all 0.2s ease; | ||
cursor: pointer; | ||
padding: 1.6rem; | ||
font-weight: bold; | ||
&:disabled { | ||
opacity: 0.7; | ||
cursor: not-allowed; | ||
} | ||
//variants | ||
${({ theme, $outline, $color }) => variantStyles(theme, $outline, $color)} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use client' | ||
|
||
import React, { useState } from 'react' | ||
import { useServerInsertedHTML } from 'next/navigation' | ||
import { ServerStyleSheet, StyleSheetManager } from 'styled-components' | ||
|
||
export default function StyledComponentsRegistry({ | ||
children, | ||
}: { | ||
children: React.ReactNode | ||
}) { | ||
// Only create stylesheet once with lazy initial state | ||
// x-ref: https://reactjs.org/docs/hooks-reference.html#lazy-initial-state | ||
const [styledComponentsStyleSheet] = useState(() => new ServerStyleSheet()) | ||
|
||
useServerInsertedHTML(() => { | ||
const styles = styledComponentsStyleSheet.getStyleElement() | ||
styledComponentsStyleSheet.instance.clearTag() | ||
return <>{styles}</> | ||
}) | ||
|
||
if (typeof window !== 'undefined') return <>{children}</> | ||
|
||
return ( | ||
<StyleSheetManager sheet={styledComponentsStyleSheet.instance}> | ||
{children} | ||
</StyleSheetManager> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use client' | ||
|
||
import { theme } from '@/styles/themes/default' | ||
import StyledComponentsRegistry from './registry' | ||
import { ThemeProvider } from 'styled-components' | ||
|
||
export function ThemeWrapper({ children }: { children: React.ReactNode }) { | ||
return ( | ||
<StyledComponentsRegistry> | ||
<ThemeProvider theme={theme.light}>{children}</ThemeProvider> | ||
</StyledComponentsRegistry> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { createGlobalStyle } from 'styled-components' | ||
|
||
export const GlobalStyle = createGlobalStyle` | ||
*{ | ||
margin: 0; | ||
padding: 0; | ||
box-sizing: border-box; | ||
} | ||
:focus{ | ||
outline: 0; | ||
} | ||
body { | ||
background-color: ${({ theme }) => theme.colors.background600}; | ||
color: ${({ theme }) => theme.colors.white}; | ||
-webkit-font-smoothing: antialiased; | ||
font-size: 1.6rem; | ||
} | ||
body, input, textarea, button{ | ||
font-weight: 400; | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Colors } from '@/@types/styled' | ||
import { colors } from '@sapuris-ui/tokens' | ||
|
||
export const darkTheme: Colors = { | ||
...colors, | ||
background500: '', | ||
background600: '', | ||
text500: '', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { | ||
fontSizes, | ||
fontWeights, | ||
fonts, | ||
lineHeights, | ||
radii, | ||
space, | ||
} from '@sapuris-ui/tokens' | ||
import { darkTheme } from './dark' | ||
import { lightTheme } from './light' | ||
|
||
export const defaultTheme = { | ||
fontSizes, | ||
fontWeights, | ||
fonts, | ||
lineHeights, | ||
radii, | ||
space, | ||
} as const | ||
|
||
export const theme = { | ||
light: { | ||
colors: lightTheme, | ||
...defaultTheme, | ||
}, | ||
dark: { | ||
colors: darkTheme, | ||
...defaultTheme, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Colors } from '@/@types/styled' | ||
import { colors } from '@sapuris-ui/tokens' | ||
|
||
export const lightTheme: Colors = { | ||
...colors, | ||
'background-500': '', | ||
'background-600': '', | ||
'text-500': '', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters