refactor: with styled-components

This commit is contained in:
Daniel Soares committed 2021-12-18 00:50:54 -03:00
1 parent 5138b4dfc2
commit 47d03b6d73
42 files changed
+845 -934

No files matched your search

+7 -8
View File
@@ -1,10 +1,9 @@
import Image from 'next/image'
import styled from 'styled-components'
import { styled } from '../../styles/stitches.config'
export const Wrapper = styled(Image, {
borderRadius: '9999px',
objectFit: 'cover',
width: '84px',
height: '84px'
})
export const Wrapper = styled(Image)`
border-radius: 9999px;
object-fit: cover;
width: 84px;
height: 84px;
`
+1 -19
View File
@@ -1,19 +1 @@
import type { ButtonHTMLAttributes, ReactNode } from 'react'
import type { ButtonVariantProps } from './styles'
import { Wrapper } from './styles'
type ButtonProps = ButtonHTMLAttributes<HTMLButtonElement> & {
children: ReactNode
colorSchema?: ButtonVariantProps['colorSchema']
styleSchema?: ButtonVariantProps['styleSchema']
size?: ButtonVariantProps['size']
font?: ButtonVariantProps['font']
outline?: boolean
}
export function Button({ children, ...rest }: ButtonProps) {
return <Wrapper {...rest}>{children}</Wrapper>
}
export type { ButtonVariantProps }
export { Button } from './styles'
+60 -206
View File
@@ -1,218 +1,72 @@
import { Colors, styled, theme, VariantProps } from '../../styles/stitches.config'
import { darken } from 'polished'
import styled from 'styled-components'
const colorSchemaFn = (
lightColor: Colors,
regularColor: Colors,
darkColor: Colors,
darkerColor: Colors
) => ({
background: theme.colors[regularColor],
borderColor: theme.colors[regularColor],
color: theme.colors[lightColor],
type ButtonProps = {
color?: string
background?: string
variant?: 'square' | 'rounded' | 'pill'
outlined?: boolean
font?: string
size?: 'small' | 'medium'
}
'&:hover': {
background: theme.colors[darkColor],
borderColor: theme.colors[darkColor]
},
export const Button = styled.button.attrs((props: ButtonProps) => ({
color: props.color || '#f8fafc',
background: props.background || '#e11d48',
variant: props.variant || 'rounded',
outlined: props.outlined || false,
font: props.font || 'inherit',
size: props.size || 'medium'
}))<ButtonProps>`
display: flex;
align-items: center;
justify-content: center;
width: 100%;
padding: 0 1rem;
font-family: ${({ font }) => font};
color: ${({ color }) => color};
background: ${({ background }) => background};
border-width: 2.5px;
border-style: solid;
font-weight: 500;
cursor: pointer;
text-decoration: none;
transition: all 0.25s ease;
'&:active': {
background: theme.colors[darkerColor],
borderColor: theme.colors[darkerColor]
&:hover {
background: ${({ background }) => darken(0.1, background)};
transform: translateY(-0.15rem);
}
})
const outlineVariantFn = (color: Colors) => ({
color: theme.colors[color],
'&:hover': {
background: 'rgba(0, 0, 0, 0.05)'
},
'&:active': {
background: 'rgba(0, 0, 0, 0.1)'
&:active {
background: ${({ background }) => darken(0.2, background)};
}
})
export const Wrapper = styled('button', {
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
width: '100%',
padding: '0 1rem',
borderWidth: '2.5px',
borderStyle: 'solid',
fontWeight: 500,
cursor: 'pointer',
transition: 'all 0.25s ease-in-out',
${({ variant }) => variant === 'square' && `border-radius: 0;`}
'&:hover': {
transform: 'translateY(-0.15rem)'
},
${({ variant }) => variant === 'rounded' && `border-radius: 0.5rem;`}
variants: {
colorSchema: {
slate: colorSchemaFn('slate50', 'slate600', 'slate700', 'slate800'),
red: colorSchemaFn('red50', 'red600', 'red700', 'red800'),
orange: colorSchemaFn('orange50', 'orange600', 'orange700', 'orange800'),
amber: colorSchemaFn('amber50', 'amber600', 'amber700', 'amber800'),
yellow: colorSchemaFn('yellow50', 'yellow600', 'yellow700', 'yellow800'),
lime: colorSchemaFn('lime50', 'lime600', 'lime700', 'lime800'),
green: colorSchemaFn('green50', 'green600', 'green700', 'green800'),
emerald: colorSchemaFn('emerald50', 'emerald600', 'emerald700', 'emerald800'),
teal: colorSchemaFn('teal50', 'teal600', 'teal700', 'teal800'),
cyan: colorSchemaFn('cyan50', 'cyan600', 'cyan700', 'cyan800'),
sky: colorSchemaFn('sky50', 'sky600', 'sky700', 'sky800'),
blue: colorSchemaFn('blue50', 'blue600', 'blue700', 'blue800'),
indigo: colorSchemaFn('indigo50', 'indigo600', 'indigo700', 'indigo800'),
violet: colorSchemaFn('violet50', 'violet600', 'violet700', 'violet800'),
purple: colorSchemaFn('purple50', 'purple600', 'purple700', 'purple800'),
fuchsia: colorSchemaFn('fuchsia50', 'fuchsia600', 'fuchsia700', 'fuchsia800'),
pink: colorSchemaFn('pink50', 'pink600', 'pink700', 'pink800'),
rose: colorSchemaFn('rose50', 'rose600', 'rose700', 'rose800')
},
${({ variant }) => variant === 'pill' && `border-radius: 9999px;`}
styleSchema: {
square: {
borderRadius: '0'
},
rounded: {
borderRadius: '0.5rem'
},
pill: {
borderRadius: '9999px'
}
},
${({ size }) =>
size === 'small' &&
`
height: 2rem;
font-size: 0.875rem;
`}
size: {
small: {
height: '2rem',
fontSize: '0.875rem'
},
medium: {
height: '3rem',
fontSize: '1rem'
}
},
${({ size }) =>
size === 'medium' &&
`
height: 3rem;
font-size: 1rem;
`}
font: {
baloo: {
fontFamily: theme.fonts.baloo
},
montserrat: {
fontFamily: theme.fonts.montserrat
},
roboto: {
fontFamily: theme.fonts.roboto
}
},
outline: {
true: {
background: 'transparent'
}
}
},
compoundVariants: [
{
colorSchema: 'slate',
outline: true,
css: outlineVariantFn('slate600')
},
{
colorSchema: 'red',
outline: true,
css: outlineVariantFn('red600')
},
{
colorSchema: 'orange',
outline: true,
css: outlineVariantFn('orange600')
},
{
colorSchema: 'amber',
outline: true,
css: outlineVariantFn('amber600')
},
{
colorSchema: 'yellow',
outline: true,
css: outlineVariantFn('yellow600')
},
{
colorSchema: 'lime',
outline: true,
css: outlineVariantFn('lime600')
},
{
colorSchema: 'green',
outline: true,
css: outlineVariantFn('green600')
},
{
colorSchema: 'emerald',
outline: true,
css: outlineVariantFn('emerald600')
},
{
colorSchema: 'teal',
outline: true,
css: outlineVariantFn('teal600')
},
{
colorSchema: 'cyan',
outline: true,
css: outlineVariantFn('cyan600')
},
{
colorSchema: 'sky',
outline: true,
css: outlineVariantFn('sky600')
},
{
colorSchema: 'blue',
outline: true,
css: outlineVariantFn('blue600')
},
{
colorSchema: 'indigo',
outline: true,
css: outlineVariantFn('indigo600')
},
{
colorSchema: 'violet',
outline: true,
css: outlineVariantFn('violet600')
},
{
colorSchema: 'purple',
outline: true,
css: outlineVariantFn('purple600')
},
{
colorSchema: 'fuchsia',
outline: true,
css: outlineVariantFn('fuchsia600')
},
{
colorSchema: 'pink',
outline: true,
css: outlineVariantFn('pink600')
},
{
colorSchema: 'rose',
outline: true,
css: outlineVariantFn('rose600')
}
],
defaultVariants: {
colorSchema: 'teal',
styleSchema: 'pill',
size: 'medium',
font: 'roboto',
outline: false
}
})
export type ButtonVariantProps = VariantProps<typeof Wrapper>
${({ outlined, color }) =>
outlined &&
`
background: transparent;
color: ${color};
border-color: ${color};
`}
`
@@ -1,18 +1,17 @@
import { useData } from '../../../hooks/useData'
import type { ButtonVariantProps } from '../../Button'
export function ButtonsStyleSelect() {
const { data, setData } = useData()
return (
<select
value={data.settings.buttonsSchema as string}
value={data.settings.buttonsSchema}
onChange={e =>
setData({
...data,
settings: {
...data.settings,
buttonsSchema: e.target.value as ButtonVariantProps['styleSchema']
buttonsSchema: e.target.value as 'square' | 'rounded' | 'pill'
}
})
}
+35
View File
@@ -0,0 +1,35 @@
import { BlockPicker } from 'react-color'
import { useData } from '../../../hooks/useData'
import { colors } from '../../../styles/colors'
type ColorPickerProps = {
prop: 'texts' | 'socialLinks' | 'background' | 'buttonLinks'
}
export function ColorPicker({ prop }: ColorPickerProps) {
const { data, setData } = useData()
const handleChange = (color: string) => {
setData({
...data,
settings: {
...data.settings,
colors: {
...data.settings.colors,
[prop]: color
}
}
})
}
return (
<BlockPicker
color={data.settings.colors[prop]}
colors={colors}
triangle="hide"
width="100%"
onChangeComplete={color => handleChange(color.hex)}
/>
)
}
-88
View File
@@ -1,88 +0,0 @@
import { useEffect, useState } from 'react'
import { useData } from '../../../hooks/useData'
import type { Colors } from '../../../styles/stitches.config'
import { theme } from '../../../styles/stitches.config'
import { Color, SelectButton, Wrapper } from './styles'
type ColorSelectProps = {
prop: 'texts' | 'socialLinks' | 'background' | 'buttonLinks'
}
export function ColorSelect({ prop }: ColorSelectProps) {
const { data, setData } = useData()
const [activeColor, setActiveColor] = useState(data.settings.colors[prop] as string)
useEffect(() => {
setActiveColor(data.settings.colors[prop] as string)
}, [data, prop])
const allColors = Object.keys(theme.colors) as Colors[]
const colorsSchema = [
'slate',
'red',
'orange',
'amber',
'yellow',
'lime',
'green',
'emerald',
'teal',
'cyan',
'sky',
'blue',
'indigo',
'violet',
'purple',
'fuchsia',
'pink',
'rose'
]
const colors = {
texts: allColors,
socialLinks: colorsSchema,
background: allColors,
buttonLinks: colorsSchema
}[prop]
const backgroundColor = (color: string) => {
if (prop === 'background' || prop === 'texts') {
return theme.colors[color as Colors]
}
return theme.colors[`${color}600` as Colors]
}
const handleChange = (color: string) => {
setActiveColor(color)
setData({
...data,
settings: {
...data.settings,
colors: {
...data.settings.colors,
[prop]: color
}
}
})
}
return (
<Wrapper>
{colors.map(color => (
<SelectButton
key={color}
active={color === activeColor}
onClick={() => handleChange(color)}
>
<Color
css={{
backgroundColor: backgroundColor(color)
}}
/>
</SelectButton>
))}
</Wrapper>
)
}
-33
View File
@@ -1,33 +0,0 @@
import { styled, theme } from '../../../styles/stitches.config'
export const Wrapper = styled('div', {
display: 'grid',
gridTemplateColumns: 'repeat(10, 1fr)',
gridGap: '0.5rem'
})
export const SelectButton = styled('button', {
width: '100%',
height: '3rem',
padding: '0.25rem',
borderRadius: '0.2rem',
borderWidth: '3px',
borderStyle: 'solid',
borderColor: theme.colors.slate200,
background: 'transparent',
cursor: 'pointer',
variants: {
active: {
true: {
borderColor: theme.colors.sky500
}
}
}
})
export const Color = styled('div', {
width: '100%',
height: '100%',
borderRadius: '0.25rem'
})
+25 -10
View File
@@ -1,19 +1,34 @@
import { ColorSelect } from '../ColorSelect'
import { ColorPicker } from '../ColorPicker'
export function ColorsSettings() {
return (
<div>
<h1>Texts</h1>
<ColorSelect prop="texts" />
<div
style={{
display: 'flex',
gap: '1rem',
flexWrap: 'wrap',
flex: 1
}}
>
<div>
<h1>Texts</h1>
<ColorPicker prop="texts" />
</div>
<h1>Icons</h1>
<ColorSelect prop="socialLinks" />
<div>
<h1>Icons</h1>
<ColorPicker prop="socialLinks" />
</div>
<h1>Buttons</h1>
<ColorSelect prop="buttonLinks" />
<div>
<h1>Buttons</h1>
<ColorPicker prop="buttonLinks" />
</div>
<h1>Background</h1>
<ColorSelect prop="background" />
<div>
<h1>Background</h1>
<ColorPicker prop="background" />
</div>
</div>
)
}
+4 -5
View File
@@ -1,5 +1,4 @@
import { useData } from '../../../hooks/useData'
import type { Fonts } from '../../../styles/stitches.config'
export function FontsSelect() {
const { data, setData } = useData()
@@ -12,14 +11,14 @@ export function FontsSelect() {
...data,
settings: {
...data.settings,
font: e.target.value as Fonts
font: e.target.value
}
})
}
>
<option value="baloo">Baloo</option>
<option value="montserrat">Montserrat</option>
<option value="roboto">Roboto</option>
<option value="'Baloo Bhaijaan 2', sans-serif">Baloo</option>
<option value="'Montserrat', sans-serif">Montserrat</option>
<option value="'Roboto', sans-serif">Roboto</option>
</select>
)
}
+5 -11
View File
@@ -40,23 +40,17 @@ export function SaveChangesAlert({
setData(initialData)
}
return (
<Wrapper show={hasUnsavedChanges}>
return hasUnsavedChanges ? (
<Wrapper>
You have unsaved changes.
<ButtonsGroup>
<Button
size="small"
styleSchema="square"
colorSchema="rose"
outline
onClick={() => handleCancel()}
>
<Button size="small" outlined onClick={() => handleCancel()}>
Cancel
</Button>
<Button size="small" styleSchema="square" colorSchema="slate" onClick={() => handleSave()}>
<Button size="small" onClick={() => handleSave()}>
Save
</Button>
</ButtonsGroup>
</Wrapper>
)
) : null
}
+17 -36
View File
@@ -1,38 +1,19 @@
import { styled, theme } from '../../../styles/stitches.config'
import styled from 'styled-components'
export const Wrapper = styled('div', {
position: 'fixed',
top: 0,
left: 0,
width: '50%',
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
padding: '1rem',
background: theme.colors.sky200,
color: theme.colors.slate700,
fontFamily: theme.fonts.roboto,
fontSize: '0.875rem',
lineHeight: 1,
transition: 'all 0.25s ease-in-out',
export const Wrapper = styled.div`
position: fixed;
top: 0;
left: 0;
width: 50%;
display: flex;
justify-content: space-between;
align-items: center;
padding: 1rem;
font-size: 0.875rem;
line-height: 1;
`
variants: {
show: {
true: {
opacity: 1
},
false: {
opacity: 0
}
}
},
defaultVariant: {
show: false
}
})
export const ButtonsGroup = styled('div', {
display: 'flex',
gap: '1rem'
})
export const ButtonsGroup = styled.div`
display: flex;
gap: 1rem;
`
+10 -8
View File
@@ -1,8 +1,8 @@
import { isEqual } from 'lodash'
import { useEffect, useState } from 'react'
import Collapsible from 'react-collapsible'
import { useData } from '../../hooks/useData'
import type { Data } from '../../types/Data'
import { getLocalStorageData, setLocalStorageData } from '../../utils/localStorage'
import { Home } from '../Home'
import { ColorsSettings } from './ColorsSettings'
@@ -10,15 +10,12 @@ import { DataSettings } from './DataSettings'
import { SaveChangesAlert } from './SaveChangesAlert'
import { Content, Preview, Wrapper } from './styles'
type DashProps = {
initialData: Data
}
export function Dash({ initialData }: DashProps) {
export function Dash() {
const { data, setData } = useData()
const [isLoading, setIsLoading] = useState(true)
const [hasUnsavedChanges, setHasUnsavedChanges] = useState(false)
const [activeTab, setActiveTab] = useState<'data' | 'colors'>('data')
const initialData = data
useEffect(() => {
if (isLoading) {
@@ -53,13 +50,18 @@ export function Dash({ initialData }: DashProps) {
/>
<h1>Dash </h1>
{tabs.map(tab => (
<button key={tab.value} onClick={() => setActiveTab(tab.value as 'data' | 'colors')}>
{tabs.map((tab, i) => (
<button key={i} onClick={() => setActiveTab(tab.value as 'data' | 'colors')}>
{tab.label}
</button>
))}
{body[activeTab]}
<Collapsible trigger="Start here">
<p>This is the collapsible content. It can be any element or React component you like.</p>
<p>It can even be another Collapsible component. Check out the next section!</p>
</Collapsible>
</Content>
<Preview>
<Home />
+23 -25
View File
@@ -1,30 +1,28 @@
import { styled, theme } from '../../styles/stitches.config'
import styled from 'styled-components'
export const Wrapper = styled('main', {
display: 'grid',
gridTemplateColumns: '1fr 1fr',
position: 'fixed',
inset: 0
})
export const Wrapper = styled.main`
display: grid;
grid-template-columns: 1fr 1fr;
position: fixed;
inset: 0;
`
export const Content = styled('div', {
display: 'flex',
flexDirection: 'column',
gap: '1rem',
padding: '5rem 1rem 1rem',
fontFamily: theme.fonts.roboto,
color: theme.colors.slate900,
background: theme.colors.slate50,
overflowY: 'scroll'
})
export const Content = styled.div`
display: flex;
flex-direction: column;
gap: 1rem;
padding: 5rem 1rem 1rem;
font-family: 'Roboto', sans-serif;
overflow-y: scroll;
`
export const Preview = styled('div', {
position: 'relative',
overflowY: 'scroll',
export const Preview = styled.div`
position: relative;
overflow-y: scroll;
'&::after': {
content: '',
position: 'absolute',
inset: 0
&::after {
content: '';
position: absolute;
inset: 0;
}
})
`
+10 -23
View File
@@ -1,7 +1,4 @@
import Head from 'next/head'
import { useData } from '../../hooks/useData'
import { theme } from '../../styles/stitches.config'
import { Avatar } from '../Avatar'
import { Button } from '../Button'
import { SocialLinks } from '../SocialLinks'
@@ -9,34 +6,24 @@ import { Content, Description, Name, Wrapper } from './styles'
export const Home = () => {
const { data } = useData()
const { colors, buttonsSchema, outline, font, name, description } = data.settings
const { colors, buttonsSchema, font, name, description } = data.settings
return (
<Wrapper
css={{
color: theme.colors[colors.texts],
fontFamily: theme.fonts[font]
}}
>
<Head>
<style
dangerouslySetInnerHTML={{
__html: `body { background: ${theme.colors[colors.background]} }`
}}
/>
</Head>
<Wrapper color={colors.texts} font={font}>
<Content>
<Avatar />
<Name>{name}</Name>
<SocialLinks />
<Description>{description}</Description>
{data.buttonLinks?.map(link => (
{data.buttonLinks?.map((link, i) => (
<Button
colorSchema={colors.buttonLinks}
styleSchema={buttonsSchema}
outline={outline}
font={font}
key={link.href}
as="a"
href={link.href}
rel="noopener noreferrer"
target="_blank"
// key={link.id}
key={i}
variant={buttonsSchema}
>
{link.label}
</Button>
+36 -26
View File
@@ -1,30 +1,40 @@
import { styled } from '../../styles/stitches.config'
import styled from 'styled-components'
export const Wrapper = styled('main', {
width: '100%',
height: '100vh',
transition: 'all 0.25s ease-in-out'
})
type Props = {
color?: string
font?: string
}
export const Content = styled('div', {
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
gap: '1rem',
margin: '0 auto',
maxWidth: '640px',
padding: '2rem 1rem'
})
export const Wrapper = styled.main.attrs((props: Props) => ({
color: props.color || '#e11d48',
font: props.font || 'inherit'
}))<Props>`
color: ${({ color }) => color};
font-family: ${({ font }) => font};
width: 100%;
height: 100vh;
transition: all 0.25s ease;
`
export const Name = styled('h1', {
fontSize: '2rem',
fontWeight: 500,
margin: 0
})
export const Content = styled.div`
display: flex;
flex-direction: column;
align-items: center;
gap: 1rem;
margin: 0 auto;
max-width: 640px;
padding: 2rem 1rem;
`
export const Description = styled('p', {
fontSize: '1rem',
textAlign: 'center',
fontWeight: 400,
lineHeight: '1.5'
})
export const Name = styled.h1`
font-size: 2rem;
font-weight: 500;
margin: 0;
`
export const Description = styled.p`
font-size: 1rem;
text-align: center;
font-weight: 400;
line-height: 1.5;
`
+1
View File
@@ -0,0 +1 @@
export { Link } from './styles'
-20
View File
@@ -1,20 +0,0 @@
import NextLink from 'next/link'
import type { AnchorHTMLAttributes } from 'react'
import type { LinkVariantProps } from './styles'
import { Wrapper } from './styles'
export type LinkProps = AnchorHTMLAttributes<HTMLAnchorElement> & {
href: string
theme?: LinkVariantProps['theme']
}
export function Link({ href, theme, ...rest }: LinkProps) {
return (
<NextLink href={href} passHref>
<Wrapper theme={theme} {...rest} />
</NextLink>
)
}
export type { LinkVariantProps }
+16 -41
View File
@@ -1,47 +1,22 @@
import { Colors, styled, theme, VariantProps } from '../../styles/stitches.config'
import { darken } from 'polished'
import styled from 'styled-components'
const themeVariantFn = (regularColor: Colors, darkColor: Colors, darkerColor: Colors) => ({
color: theme.colors[regularColor],
type LinkProps = {
color?: string
}
'&:hover': {
color: theme.colors[darkColor]
},
export const Link = styled.a.attrs((props: LinkProps) => ({
color: props.color || '#e11d48'
}))<LinkProps>`
text-decoration: none;
cursor: pointer;
transition: color 0.25s ease;
'&:active': {
color: theme.colors[darkerColor]
&:hover {
color: ${({ color }) => darken(0.1, color)};
}
})
export const Wrapper = styled('a', {
textDecoration: 'none',
transition: 'color 0.25s ease',
cursor: 'pointer',
variants: {
theme: {
slate: themeVariantFn('slate600', 'slate700', 'slate800'),
orange: themeVariantFn('orange600', 'orange700', 'orange800'),
amber: themeVariantFn('amber600', 'amber700', 'amber800'),
yellow: themeVariantFn('yellow600', 'yellow700', 'yellow800'),
lime: themeVariantFn('lime600', 'lime700', 'lime800'),
green: themeVariantFn('green600', 'green700', 'green800'),
emerald: themeVariantFn('emerald600', 'emerald700', 'emerald800'),
teal: themeVariantFn('teal600', 'teal700', 'teal800'),
cyan: themeVariantFn('cyan600', 'cyan700', 'cyan800'),
sky: themeVariantFn('sky600', 'sky700', 'sky800'),
blue: themeVariantFn('blue600', 'blue700', 'blue800'),
indigo: themeVariantFn('indigo600', 'indigo700', 'indigo800'),
violet: themeVariantFn('violet600', 'violet700', 'violet800'),
purple: themeVariantFn('purple600', 'purple700', 'purple800'),
fuchsia: themeVariantFn('fuchsia600', 'fuchsia700', 'fuchsia800'),
pink: themeVariantFn('pink600', 'pink700', 'pink800'),
rose: themeVariantFn('rose600', 'rose700', 'rose800')
}
},
defaultVariants: {
theme: 'teal'
&:active {
color: ${({ color }) => darken(0.2, color)};
}
})
export type LinkVariantProps = VariantProps<typeof Wrapper>
`
+3 -3
View File
@@ -25,16 +25,16 @@ export function SocialLinks() {
return (
<Wrapper>
{data.socialLinks?.map(({ label, href }) => {
{data.socialLinks?.map(({ label, href }, i) => {
const Icon: IconType = Icons[label as keyof typeof Icons]
return (
<SocialItem key={href}>
<SocialItem key={i}>
<Link
href={href}
aria-label={label}
rel="noopener noreferrer"
target="_blank"
theme={data.settings.colors.socialLinks}
color={data.settings.colors.socialLinks}
>
<Icon size={22} />
</Link>
+18 -18
View File
@@ -1,22 +1,22 @@
import { styled } from '../../styles/stitches.config'
import styled from 'styled-components'
export const Wrapper = styled('ul', {
display: 'inline-flex',
listStyle: 'none'
})
export const Wrapper = styled.div`
display: inline-flex;
list-style: none;
`
export const SocialItem = styled('li', {
transition: 'transform 0.2s ease-in-out',
export const SocialItem = styled.li`
transition: transform 0.25s ease;
'&:not(:last-child)': {
marginRight: '1rem'
},
'&:hover': {
transform: 'scale(1.1)'
},
'&:active': {
transform: 'scale(0.95)'
&:not(:last-child) {
margin-right: 1rem;
}
})
&:hover {
transform: scale(1.1);
}
&:active {
transform: scale(0.95);
}
`
+4 -4
View File
@@ -1,7 +1,6 @@
import type { Dispatch, ReactNode, SetStateAction } from 'React'
import { Dispatch, ReactNode, SetStateAction } from 'react'
import { createContext, useState } from 'react'
import initialData from '../../temp/data.json'
import type { Data } from '../types/Data'
type DataContextType = {
@@ -11,12 +10,13 @@ type DataContextType = {
type DataProviderProps = {
children: ReactNode
initialData: Data
}
export const DataContext = createContext({} as DataContextType)
export const DataProvider = ({ children }: DataProviderProps) => {
const [data, setData] = useState<Data>(initialData as Data)
export const DataProvider = ({ children, initialData }: DataProviderProps) => {
const [data, setData] = useState<Data>(initialData)
return <DataContext.Provider value={{ data, setData }}>{children}</DataContext.Provider>
}
-15
View File
@@ -1,15 +0,0 @@
import useSWR from 'swr'
import { fetcher } from '../services/api'
import type { Data } from '../types/Data'
import { useData } from './useData'
export function useFetchData(fallbackData: Data) {
const { setData } = useData()
const { data } = useSWR('data', fetcher, {
refreshInterval: 1000,
fallbackData
})
setData(data)
}
+5 -4
View File
@@ -1,13 +1,14 @@
import type { AppProps } from 'next/app'
import { DataProvider } from '../contexts/DataContext'
import { globalStyles } from '../styles/globals'
import { Layout } from '../styles/layout'
export default function App({ Component, pageProps }: AppProps) {
globalStyles()
return (
<DataProvider>
<Component {...pageProps} />
<DataProvider initialData={pageProps.initialData}>
<Layout>
<Component {...pageProps} />
</Layout>
</DataProvider>
)
}
-3
View File
@@ -1,7 +1,5 @@
import NextDocument, { Head, Html, Main, NextScript } from 'next/document'
import { getCssText } from '../styles/stitches.config'
export default class Document extends NextDocument {
render() {
return (
@@ -13,7 +11,6 @@ export default class Document extends NextDocument {
href="https://fonts.googleapis.com/css2?family=Baloo+Bhaijaan+2:wght@400;500&family=Montserrat:wght@400;500&family=Roboto:wght@400;500&display=swap"
rel="stylesheet"
/>
<style id="stitches" dangerouslySetInnerHTML={{ __html: getCssText() }} />
</Head>
<body>
<Main />
+2 -2
View File
@@ -3,7 +3,7 @@ import type { NextApiRequest, NextApiResponse } from 'next'
import nc from 'next-connect'
import { prisma } from '../../services/prisma'
import { authMiddleware } from '../../utils/authMiddleware'
// import { authMiddleware } from '../../utils/authMiddleware'
import { ExceptionError } from '../../utils/error'
const handler = nc<NextApiRequest, NextApiResponse>({
@@ -29,7 +29,6 @@ const handler = nc<NextApiRequest, NextApiResponse>({
}
}
})
.use(authMiddleware)
.get(async (_req, res) => {
const response = await prisma.data.findUnique({
where: { id: 1 }
@@ -41,6 +40,7 @@ const handler = nc<NextApiRequest, NextApiResponse>({
res.status(200).json(response?.data)
})
// .use(authMiddleware)
.put(async (req, res) => {
const { data } = req.body
+2 -7
View File
@@ -2,14 +2,9 @@ import { GetServerSideProps } from 'next'
import { Dash } from '../components/Dash'
import { fetchData } from '../services/fetchData'
import type { Data } from '../types/Data'
type DashPageProps = {
initialData: Data
}
export default function DashPage({ initialData }: DashPageProps) {
return <Dash initialData={initialData} />
export default function DashPage() {
return <Dash />
}
export const getServerSideProps: GetServerSideProps = async () => {
+3 -10
View File
@@ -1,24 +1,17 @@
import { GetStaticProps } from 'next'
import { Home } from '../components/Home'
import { useFetchData } from '../hooks/useFetchData'
import { fetchData } from '../services/fetchData'
import type { Data } from '../types/Data'
type HomePageProps = {
data: Data
}
export default function HomePage({ data }: HomePageProps) {
useFetchData(data)
export default function HomePage() {
return <Home />
}
export const getStaticProps: GetStaticProps = async () => {
const data = await fetchData()
const initialData = await fetchData()
return {
props: { data },
props: { initialData },
revalidate: 1
}
}
+88 -5
View File
@@ -1,7 +1,90 @@
import axios from 'axios'
import axios, { AxiosError } from 'axios'
import { GetServerSidePropsContext } from 'next'
import router from 'next/router'
import { parseCookies } from 'nookies'
export const api = axios.create({
baseURL: '/api'
})
import { destroyCookies } from '../utils/destroyCookies'
import { setCookies } from '../utils/setCookies'
export const fetcher = (url: string) => api.get(url).then(({ data }) => data)
let isRefreshing = false
let failedRequestsQueued: any[] = []
export function setupAPIClient(ctx: GetServerSidePropsContext | undefined = undefined) {
let cookies = parseCookies(ctx)
const authorization = cookies.accessToken
? { headers: { authorization: `Bearer ${cookies.accessToken}` } }
: {}
const api = axios.create({
baseURL: '/api',
...authorization
})
api.interceptors.response.use(
response => {
return response
},
(error: AxiosError) => {
if (error.response?.status === 401) {
cookies = parseCookies(ctx)
const { refreshToken } = cookies
const originalConfig = error.config
if (!isRefreshing) {
isRefreshing = true
api
.post('session/refresh-token', { refreshToken })
.then(response => {
const { accessToken, refreshToken } = response.data
setCookies({
ctx,
accessToken,
refreshToken
})
failedRequestsQueued.forEach(request => request.onSuccess(accessToken))
failedRequestsQueued = []
})
.catch(error => {
failedRequestsQueued.forEach(request => request.onFailure(error))
failedRequestsQueued = []
destroyCookies(ctx)
if (process.browser) {
router.push('/auth')
}
})
.finally(() => {
isRefreshing = false
})
return new Promise((resolve, reject) => {
failedRequestsQueued.push({
onSuccess: (token: string) => {
if (originalConfig.headers) {
originalConfig.headers['authorization'] = `Bearer ${token}`
}
resolve(api(originalConfig))
},
onFailure: (error: AxiosError) => {
reject(error)
}
})
})
}
}
return Promise.reject(error)
}
)
return api
}
export const api = setupAPIClient()
+54
View File
@@ -0,0 +1,54 @@
export const colors = [
'#f8fafc',
'#cbd5e1',
'#475569',
'#0f172a',
'#fca5a5',
'#dc2626',
'#7f1d1d',
'#fdba74',
'#ea580c',
'#7c2d12',
'#fcd34d',
'#d97706',
'#78350f',
'#fde047',
'#ca8a04',
'#713f12',
'#bef264',
'#65a30d',
'#365314',
'#86efac',
'#16a34a',
'#14532d',
'#6ee7b7',
'#059669',
'#064e3b',
'#5eead4',
'#0d9488',
'#134e4a',
'#67e8f9',
'#0891b2',
'#164e63',
'#93c5fd',
'#2563eb',
'#1e3a8a',
'#a5b4fc',
'#4f46e5',
'#312e81',
'#c4b5fd',
'#7c3aed',
'#4c1d95',
'#d8b4fe',
'#9333ea',
'#581c87',
'#f0abfc',
'#c026d3',
'#701a75',
'#f9a8d4',
'#db2777',
'#831843',
'#fda4af',
'#e11d48',
'#881337'
]
-12
View File
@@ -1,12 +0,0 @@
import { globalCss } from './stitches.config'
export const globalStyles = globalCss({
'*': {
margin: 0,
padding: 0,
boxSizing: 'border-box'
},
body: {
transition: 'all 0.25s ease-in-out'
}
})
+39
View File
@@ -0,0 +1,39 @@
import { ReactNode } from 'react'
import { createGlobalStyle } from 'styled-components'
import { useData } from '../hooks/useData'
import { Data } from '../types/Data'
type GlobalStyleProps = {
data: Data
}
type LayoutProps = {
children: ReactNode
}
const GlobalStyles = createGlobalStyle<GlobalStyleProps>`
* {
margin: 0;
padding: 0;
box-sizing: border-box;
transition: all 0.25s ease;
}
body {
font-family: "Roboto", sans-serif;
color: #0f172a;
background: ${({ data }) => data.settings.colors.background};
}
`
export function Layout({ children }: LayoutProps) {
const { data } = useData()
return (
<>
<GlobalStyles data={data} />
{children}
</>
)
}
-224
View File
@@ -1,224 +0,0 @@
import type { VariantProps } from '@stitches/react'
import { createStitches } from '@stitches/react'
export const { css, styled, globalCss, theme, keyframes, getCssText } = createStitches({
theme: {
fonts: {
baloo: "'Baloo Bhaijaan 2', cursive",
montserrat: "'Montserrat', sans-serif",
roboto: "'Roboto', sans-serif"
},
colors: {
slate50: '#f8fafc',
slate100: '#f1f5f9',
slate200: '#e2e8f0',
slate300: '#cbd5e1',
slate400: '#94a3b8',
slate500: '#64748b',
slate600: '#475569',
slate700: '#334155',
slate800: '#1e293b',
slate900: '#0f172a',
red50: '#fef2f2',
red100: '#fee2e2',
red200: '#fecaca',
red300: '#fca5a5',
red400: '#f87171',
red500: '#ef4444',
red600: '#dc2626',
red700: '#b91c1c',
red800: '#991b1b',
red900: '#7f1d1d',
orange50: '#fff7ed',
orange100: '#ffedd5',
orange200: '#fed7aa',
orange300: '#fdba74',
orange400: '#fb923c',
orange500: '#f97316',
orange600: '#ea580c',
orange700: '#c2410c',
orange800: '#9a3412',
orange900: '#7c2d12',
amber50: '#fffbeb',
amber100: '#fef3c7',
amber200: '#fde68a',
amber300: '#fcd34d',
amber400: '#fbbf24',
amber500: '#f59e0b',
amber600: '#d97706',
amber700: '#b45309',
amber800: '#92400e',
amber900: '#78350f',
yellow50: '#fefce8',
yellow100: '#fef9c3',
yellow200: '#fef08a',
yellow300: '#fde047',
yellow400: '#facc15',
yellow500: '#eab308',
yellow600: '#ca8a04',
yellow700: '#a16207',
yellow800: '#854d0e',
yellow900: '#713f12',
lime50: '#f7fee7',
lime100: '#ecfccb',
lime200: '#d9f99d',
lime300: '#bef264',
lime400: '#a3e635',
lime500: '#84cc16',
lime600: '#65a30d',
lime700: '#4d7c0f',
lime800: '#3f6212',
lime900: '#365314',
green50: '#f0fdf4',
green100: '#dcfce7',
green200: '#bbf7d0',
green300: '#86efac',
green400: '#4ade80',
green500: '#22c55e',
green600: '#16a34a',
green700: '#15803d',
green800: '#166534',
green900: '#14532d',
emerald50: '#ecfdf5',
emerald100: '#d1fae5',
emerald200: '#a7f3d0',
emerald300: '#6ee7b7',
emerald400: '#34d399',
emerald500: '#10b981',
emerald600: '#059669',
emerald700: '#047857',
emerald800: '#065f46',
emerald900: '#064e3b',
teal50: '#f0fdfa',
teal100: '#ccfbf1',
teal200: '#99f6e4',
teal300: '#5eead4',
teal400: '#2dd4bf',
teal500: '#14b8a6',
teal600: '#0d9488',
teal700: '#0f766e',
teal800: '#115e59',
teal900: '#134e4a',
cyan50: '#ecfeff',
cyan100: '#cffafe',
cyan200: '#a5f3fc',
cyan300: '#67e8f9',
cyan400: '#22d3ee',
cyan500: '#06b6d4',
cyan600: '#0891b2',
cyan700: '#0e7490',
cyan800: '#155e75',
cyan900: '#164e63',
sky50: '#f0f9ff',
sky100: '#e0f2fe',
sky200: '#bae6fd',
sky300: '#7dd3fc',
sky400: '#38bdf8',
sky500: '#0ea5e9',
sky600: '#0284c7',
sky700: '#0369a1',
sky800: '#075985',
sky900: '#0c4a6e',
blue50: '#eff6ff',
blue100: '#dbeafe',
blue200: '#bfdbfe',
blue300: '#93c5fd',
blue400: '#60a5fa',
blue500: '#3b82f6',
blue600: '#2563eb',
blue700: '#1d4ed8',
blue800: '#1e40af',
blue900: '#1e3a8a',
indigo50: '#eef2ff',
indigo100: '#e0e7ff',
indigo200: '#c7d2fe',
indigo300: '#a5b4fc',
indigo400: '#818cf8',
indigo500: '#6366f1',
indigo600: '#4f46e5',
indigo700: '#4338ca',
indigo800: '#3730a3',
indigo900: '#312e81',
violet50: '#f5f3ff',
violet100: '#ede9fe',
violet200: '#ddd6fe',
violet300: '#c4b5fd',
violet400: '#a78bfa',
violet500: '#8b5cf6',
violet600: '#7c3aed',
violet700: '#6d28d9',
violet800: '#5b21b6',
violet900: '#4c1d95',
purple50: '#faf5ff',
purple100: '#f3e8ff',
purple200: '#e9d5ff',
purple300: '#d8b4fe',
purple400: '#c084fc',
purple500: '#a855f7',
purple600: '#9333ea',
purple700: '#7e22ce',
purple800: '#6b21a8',
purple900: '#581c87',
fuchsia50: '#fdf4ff',
fuchsia100: '#fae8ff',
fuchsia200: '#f5d0fe',
fuchsia300: '#f0abfc',
fuchsia400: '#e879f9',
fuchsia500: '#d946ef',
fuchsia600: '#c026d3',
fuchsia700: '#a21caf',
fuchsia800: '#86198f',
fuchsia900: '#701a75',
pink50: '#fdf2f8',
pink100: '#fce7f3',
pink200: '#fbcfe8',
pink300: '#f9a8d4',
pink400: '#f472b6',
pink500: '#ec4899',
pink600: '#db2777',
pink700: '#be185d',
pink800: '#9d174d',
pink900: '#831843',
rose50: '#fff1f2',
rose100: '#ffe4e6',
rose200: '#fecdd3',
rose300: '#fda4af',
rose400: '#fb7185',
rose500: '#f43f5e',
rose600: '#e11d48',
rose700: '#be123c',
rose800: '#9f1239',
rose900: '#881337'
}
},
utils: {
inset: (value: number | string) => ({
top: value,
right: value,
left: value,
bottom: value
})
}
})
type Colors = keyof typeof theme.colors
type Fonts = keyof typeof theme.fonts
export type { Colors, Fonts, VariantProps }
+7 -10
View File
@@ -1,20 +1,16 @@
import type { ButtonVariantProps } from '../components/Button'
import type { LinkVariantProps } from '../components/Link'
import type { Colors, Fonts } from '../styles/stitches.config'
export type Data = {
settings: {
avatar: string
name: string
description: string
font: Fonts
buttonsSchema: ButtonVariantProps['styleSchema']
font: string
buttonsSchema: 'square' | 'rounded' | 'pill'
outline: boolean
colors: {
texts: Colors
socialLinks: LinkVariantProps['theme']
buttonLinks: ButtonVariantProps['colorSchema']
background: Colors
texts: string
socialLinks: string
buttonLinks: string
background: string
}
}
socialLinks: {
@@ -22,6 +18,7 @@ export type Data = {
href: string
}[]
buttonLinks: {
id: string
label: string
href: string
}[]
+12
View File
@@ -0,0 +1,12 @@
import { GetServerSidePropsContext } from 'next'
import { destroyCookie } from 'nookies'
export function destroyCookies(ctx: GetServerSidePropsContext | undefined = undefined) {
destroyCookie(ctx, 'accessToken', {
path: '/'
})
destroyCookie(ctx, 'refreshToken', {
path: '/'
})
}
+25
View File
@@ -0,0 +1,25 @@
import { GetServerSidePropsContext } from 'next'
import { setCookie } from 'nookies'
import { api } from '../services/api'
type SetCookiesProps = {
ctx?: GetServerSidePropsContext
accessToken: string
refreshToken: string
}
export function setCookies({ ctx = undefined, accessToken, refreshToken }: SetCookiesProps) {
// @ts-ignore
api.defaults.headers['Authorization'] = `Bearer ${accessToken}`
setCookie(ctx, 'accessToken', accessToken, {
maxAge: 60 * 60 * 24 * 30, // 30 days
path: '/'
})
setCookie(ctx, 'refreshToken', refreshToken, {
maxAge: 60 * 60 * 24 * 30, // 30 days
path: '/'
})
}