I want to recreate this Underline Effect from this Codepen with React and Typescript
The Codepen: https://codepen.io/krakruhahah/pen/jOzwXww
I think my issue in the code down below is the interface, I started to declare my types but still it does not recognize them. It says they are any. But max is declared as number but shows up as any still. I am unsure why. The functions are described as comments.
tsx:
import React from 'react';
import Typography from '@mui/material/Typography';
import { Box } from '@mui/material';
interface Props {
max: number;
}
const styles = {
body: {
width: "80%",
margin: "10vw auto",
},
heading: {
fontFamily: "Playfair Display, serif",
fontSize: "10vw",
},
"subheading": {
fontFamily: "Open Sans, sans-serif",
fontSize: "1em",
lineHeight: "1.5",
},
"@media screen and (min-width: 40em)": {
body: {
width: "50%",
},
heading:{
fontSize: "6vw",
},
subheading: {
fontSize: "1.75vw",
}
},
"underline--magical": {
backgroundImage: "linear-gradient(120deg, #84fab0 0%, #8fd3f4 100%)",
backgroundRepeat: "no-repeat",
backgroundSize: "100% 0.2em",
backgroundPosition: "0 88%",
transition: "backgroundSize 0.25s ease-in",
"&:hover": {
backgroundSize: "100% 88%",
},
},
};
function Effect(props: Props) {
// VARIABLES
const magicalUnderlines = Array.from(document.querySelectorAll('.underline--magical'));
const gradientAPI = 'https://gist.githubusercontent.com/wking-io/3e116c0e5675c8bcad8b5a6dc6ca5344/raw/4e783ce3ad0bcd98811c6531e40256b8feeb8fc8/gradient.json';
// HELPER FUNCTIONS
// 1. Get random number in range. Used to get random index from array.
const randNumInRange = max => Math.floor(Math.random() * (max - 1));
// 2. Merge two separate array values at the same index to
// be the same value in new array.
const mergeArrays = (arrOne, arrTwo) => arrOne
.map((item, i) => `${item} ${arrTwo[i]}`)
.join(', ');
// 3. Curried function to add a background to array of elms
const addBackground = (elms) => (color) => {
elms.forEach(el => {
el.style.backgroundImage = color;
});
}
// 4. Function to get data from API
const getData = async(url): Promise<XMLHttpRequest> => {
const response = await fetch(url);
const data = await response.json();
return data.data;
}
// 5. Partial Application of addBackground to always apply
// background to the magicalUnderlines constant
const addBackgroundToUnderlines = addBackground(magicalUnderlines);
// GRADIENT FUNCTIONS
// 1. Build CSS formatted linear-gradient from API data
const buildGradient = (obj) => `linear-gradient(${obj.direction}, ${mergeArrays(obj.colors, obj.positions)})`;
// 2. Get single gradient from data pulled in array and
// apply single gradient to a callback function
const applyGradient = async(url, callback): Promise<XMLHttpRequest> => {
const data = await getData(url);
const gradient = buildGradient(data[randNumInRange(data.length)]);
callback(gradient);
}
// RESULT
applyGradient(gradientAPI, addBackgroundToUnderlines);
return (
<Box>
<Typography sx={styles.heading}>
Look At This <span style={styles['underline--magical']}>Pretty</span> Underline
</Typography>
<Typography sx={styles.subheading}>
Wow this one is super incredibly cool, and this{' '}
<span style={styles['underline--magical']}>one is on Multiple Lines!</span> I wish I had found this like thirty
projects ago when I was representing the lollipop guild.
</Typography>
</Box>
);
}
export { Effect };
from Customized Underline with gradient API in React
No comments:
Post a Comment