Created
November 8, 2020 06:30
-
-
Save SagnikPradhan/98da1c56621e5fdb752f8b1240a8c4e2 to your computer and use it in GitHub Desktop.
Component for all text elements
This file contains hidden or 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
import Head from "next/head"; | |
type TextType = | |
| "h1" | |
| "h2" | |
| "h3" | |
| "caps" | |
| "emphasis" | |
| "strong-emphasis" | |
| "low-emphasis"; | |
export const Text: React.FC<{ | |
type?: TextType; | |
}> = ({ type, children }) => ( | |
<> | |
<Head> | |
<link | |
href="https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:ital,wght@0,400;0,500;1,400&family=Source+Serif+Pro:wght@600&display=swap" | |
rel="stylesheet" | |
/> | |
</Head> | |
{getText(type, children)} | |
<style global jsx>{` | |
:root { | |
font-size: 18px; | |
} | |
`}</style> | |
</> | |
); | |
function getText(type: TextType | undefined, children: React.ReactNode) { | |
switch (type) { | |
case "h1": | |
case "h2": | |
case "h3": | |
const Head = type; | |
const size = 5 - Number(type.slice(1)); | |
return ( | |
<Head className="head"> | |
{children} | |
<style jsx>{` | |
.head { | |
margin: 1rem 0; | |
font-family: "Source Serif Pro", serif; | |
font-size: ${size}rem; | |
font-weight: 500; | |
} | |
`}</style> | |
</Head> | |
); | |
case "emphasis": | |
return <i className="emphasis">{children}</i>; | |
case "strong-emphasis": | |
return ( | |
<b className="strong-emphasis"> | |
{children} | |
<style jsx>{` | |
.strong-emphasis { | |
font-weight: 500; | |
} | |
`}</style> | |
</b> | |
); | |
case "low-emphasis": | |
return ( | |
<span className="low-emphasis"> | |
{children} | |
<style jsx>{` | |
.low-emphasis { | |
color: rgba(0, 0, 0, 0.8); | |
} | |
`}</style> | |
</span> | |
); | |
case "caps": | |
return ( | |
<span className="caps"> | |
{children} | |
<style jsx>{` | |
.caps { | |
text-transform: uppercase; | |
letter-spacing: 0.3ch; | |
} | |
`}</style> | |
</span> | |
); | |
default: | |
return ( | |
<p className="paragraph"> | |
{children} | |
<style jsx>{` | |
.paragraph { | |
font-family: "IBM Plex Sans", sans-serif; | |
font-size: 1rem; | |
font-weight: 400; | |
line-height: 1.6; | |
margin: 1em; | |
} | |
`}</style> | |
</p> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment