Unverified Commit eb9a500d authored by rodrigo's avatar rodrigo Committed by GitHub

[Examples] updated styled-components (#37856)

### Example

- added declaration file types for the used theme [Create a declarations file](https://styled-components.com/docs/api#create-a-declarations-file)
- added custom styled component that wraps an `<a>` tag like the next js docs as example [Next link DOCS](https://nextjs.org/docs/api-reference/next/link#if-the-child-is-a-custom-component-that-wraps-an-a-tag)
- changed the ui page like the `create-next-app` examples

### Dependencies

- updated deps to latest
- added types for SC  => @types/styled-components
- removed react-is, unused in this example

 [Example deploy](https://stylednextexample.vercel.app/)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [x] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
parent c8666a17
import styled from 'styled-components'
import Link from 'next/link'
const FlexContainer = styled.div`
display: flex;
align-items: center;
justify-content: center;
flex-flow: column wrap;
max-width: 800px;
margin-top: 3rem;
`
const Card = styled.div`
padding: 1.5rem;
color: inherit;
text-decoration: none;
border: 1px solid black;
border-radius: 10px;
transition: color 0.15s ease, border-color 0.15s ease;
width: 100%;
&:hover,
:focus,
:active {
color: #0070f3;
border-color: #0070f3;
}
`
const StyledA = styled.a`
margin: 0 0 1rem 0;
font-size: 1.5rem;
`
const StyledLink = ({ href, name }) => (
<Link href={href} passHref>
<StyledA>{name}</StyledA>
</Link>
)
export default function Cards() {
return (
<FlexContainer>
<Card>
<StyledLink href="/about" name="About Page &rarr;" />
</Card>
</FlexContainer>
)
}
import { createGlobalStyle } from 'styled-components'
const GlobalStyle = createGlobalStyle`
html,
body {
color: ${({ theme }) => theme.colors.primary};
padding: 0;
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen,
Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif;
}
a {
color: inherit;
text-decoration: none;
}
* {
box-sizing: border-box;
}
`
export default GlobalStyle
import styled from 'styled-components'
const Container = styled.div`
padding: 0 0.5rem;
display: flex;
flex-flow: column nowrap;
justify-content: center;
align-items: center;
height: 100vh;
min-height: 100vh;
`
const Main = styled.main`
padding: 5rem 0;
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
`
const Title = styled.h1`
margin: 0;
line-height: 1.15;
font-size: 4rem;
text-align: center;
text-decoration: none;
a {
color: ${({ theme }) => theme.colors.secondary};
text-decoration: none;
&:hover,
:focus,
:active {
text-decoration: underline;
}
}
`
const Description = styled.p`
text-align: center;
line-height: 1.5;
font-size: 1.5rem;
`
const CodeTag = styled.code`
background: #fafafa;
border-radius: 5px;
margin: 0 0.75rem;
padding: 0.75rem;
font-size: 1.1rem;
font-family: Menlo, Monaco, Lucida Console, Liberation Mono, DejaVu Sans Mono,
Bitstream Vera Sans Mono, Courier New, monospace;
`
export { Container, Main, Title, Description, CodeTag }
...@@ -7,15 +7,15 @@ ...@@ -7,15 +7,15 @@
}, },
"dependencies": { "dependencies": {
"next": "latest", "next": "latest",
"react": "^18.1.0", "react": "^18.2.0",
"react-dom": "^18.1.0", "react-dom": "^18.2.0",
"react-is": "^18.1.0",
"styled-components": "^5.3.5" "styled-components": "^5.3.5"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^17.0.35", "@types/node": "^18.0.0",
"@types/react": "^18.0.9", "@types/react": "^18.0.14",
"@types/react-dom": "^18.0.5", "@types/react-dom": "^18.0.5",
"typescript": "^4.7.2" "@types/styled-components": "^5.1.25",
"typescript": "^4.7.4"
} }
} }
import { createGlobalStyle, ThemeProvider } from 'styled-components' import type { AppProps } from 'next/app'
import { ThemeProvider, DefaultTheme } from 'styled-components'
import GlobalStyle from '../components/globalstyles'
const GlobalStyle = createGlobalStyle` const theme: DefaultTheme = {
body {
margin: 0;
padding: 0;
box-sizing: border-box;
}
`
const theme = {
colors: { colors: {
primary: '#0070f3', primary: '#111',
secondary: '#0070f3',
}, },
} }
export default function App({ Component, pageProps }) { export default function App({ Component, pageProps }: AppProps) {
return ( return (
<> <>
<GlobalStyle />
<ThemeProvider theme={theme}> <ThemeProvider theme={theme}>
<GlobalStyle />
<Component {...pageProps} /> <Component {...pageProps} />
</ThemeProvider> </ThemeProvider>
</> </>
......
import Link from 'next/link'
import { Container, Main, Title, Description } from '../components/sharedstyles'
export default function About() {
return (
<Container>
<Main>
<Title>About Page</Title>
<Description>
<Link href="/">
<a>&larr; Go Back</a>
</Link>
</Description>
</Main>
</Container>
)
}
import styled from 'styled-components' import Head from 'next/head'
import {
const Title = styled.h1` Container,
font-size: 50px; Main,
color: ${({ theme }) => theme.colors.primary}; Title,
` Description,
CodeTag,
} from '../components/sharedstyles'
import Cards from '../components/cards'
export default function Home() { export default function Home() {
return <Title>My page</Title> return (
<Container>
<Head>
<title>Create Next App</title>
<meta name="description" content="Generated by create next app" />
<link rel="icon" href="/favicon.ico" />
</Head>
<Main>
<Title>
Welcome to <a href="https://nextjs.org">Next.js!</a>
</Title>
<Description>
Get started by editing
<CodeTag>pages/index.tsx</CodeTag>
</Description>
<Cards />
</Main>
</Container>
)
} }
import 'styled-components'
declare module 'styled-components' {
export interface DefaultTheme {
colors: {
primary: string
secondary: string
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment