This is a solution to the GitHub user search app challenge on Frontend Mentor. Frontend Mentor challenges help you improve your coding skills by building realistic projects.
Users should be able to:
- View the optimal layout for the app depending on their device's screen size
- See hover states for all interactive elements on the page
- Search for GitHub users by their username
- See relevant user information based on their search
- Switch between light and dark themes
- Bonus: Have the correct color scheme chosen for them based on their computer preferences. Hint: Research
prefers-color-scheme
in CSS.
- Solution URL: GitHub
- Live Site URL: GitHub Pages
- Semantic HTML5 markup
- CSS custom properties
- Flexbox
- CSS Grid
- Sass
- Mobile-first workflow
- React - JS library
- Intersection Observer API
There are many ways of adding classes in React conditionally. A great resource detailing them is provided in Useful resources section. One of the said methods is using an array and join()
method:
function myComponent() {
const classes = []
if (isError) {
classes.push('error')
} else if (isWarining) {
classes.push('warning')
} else if (isInfo) {
classes.push('info')
} else {
const indexOfError = searchBarClasses.indexOf('error')
const indexOfWarning = searchBarClasses.indexOf('warning')
const indexOfInfo = searchBarClasses.indexOf('info')
searchBarClasses.splice(indexOfError - 1, 1)
searchBarClasses.splice(indexOfWarning - 1, 1)
searchBarClasses.splice(indexOfInfo - 1, 1)
}
return (
<article className={`base-style` + ' ' + classes.join(' ')}>
<h2>This is my component</h2>
<p>
Here is some info aobut it. This component was created using React. The
classes it uses are added conditionally with an array and join() method
</p>
</article>
)
}
I decided to use an empty array and have some classes for my component by default. This is because the ones I was using were for basic styling, so whole thing looked really ugly for a split second while loading when the said classes were missing. Yet you can have some items in the array if appropriate. While the article I've learnt this from doesn't really show that, you also need to make sure to remove the classes, when they're no longer needed.
- React
- Intersection Observer
- Sass
- Mastering React Add Class Conditionally: A Comprehensive Guide - amazing article about conditionally adding classes in React
- Intersection Observer using React - This helped me correctly use Intersection Observer API in React.
- Building Skeleton Screens with CSS Custom Properties - This article explains how to build a skeleton screen using gradients and custom properties.
- Frontend Mentor - @ania221b