Hero `ParallaxBackground`
Introduction
Section titled โIntroductionโThe second custom component we are going to look at is the ParallaxBackground
hero component.
This component generates a full width hero image with an optional call-to-action overlay.
It is further enhanced with a parallax effect when users scroll up or down
Installing an NPM package
Section titled โInstalling an NPM packageโStart by opening the component file in question at app/components/organisms/Hero/ParallaxBackground.tsx
.
If you look at the import
statement at the top of the file, you will see a red underline under the react-scroll-parallax
package.
Hover your mouse over the package name to see the error message:
This error message indicates that the react-scroll-parallax
NPM package is not installed in your project.
If you ever see an error message like this on an import
, it means you need to install the package into your project. This is done using the npm install
command, followed by the package name.
Installing react-scroll-parallax
Section titled โInstalling react-scroll-parallaxโ-
Stop your server (if it is running) by clicking into the terminal panel and typing CTRL + C (or CMD + C on Mac)
-
Next type the following command in your terminal and hit enter:
npm install react-scroll-parallax
After a few seconds, you should see a message like this:
With the package installed successfully, we can begin to use the ParallaxBackground
component in our project.
Wrapper component
Section titled โWrapper componentโThe react-scroll-parallax
package requires that our application is wrapped in a ParallaxProvider
component. This allows the package to track the parallax effect across multiple areas of the app.
To do this, we need to wrap the entire application in a ParallaxProvider
component in the app/root.tsx
file.
-
Firstly, open
app/root.tsx
. -
Next,
import
theParallaxProvider
at the top of yourapp/root.tsx
file:
12 collapsed lines
import { type LinksFunction } from '@remix-run/node'import { Link, useLoaderData } from '@remix-run/react'import { AuthenticityTokenProvider } from 'remix-utils/csrf/react'import { HoneypotProvider } from 'remix-utils/honeypot/react'import Document from '~/components/shared-layout/Document'import { useNonce } from '~/utils/nonce-provider.ts'import rootLinkElements from '~/utils/providers/rootLinkElements'import { loader } from './__root.server.tsx'import FooterMenuRight from './components/organisms/Footer/FooterMenuRight'import HeaderWithSearch from './components/organisms/HeaderWithSearch'import HeroCallToAction from './components/organisms/Hero/HeroCallToAction.tsx'import ThemeSwitch from './components/shared-layout/ThemeSwitch.tsx'import useTheme from './hooks/useTheme.tsx'import heroImage from '~/assets/jpg/sample-hero.jpg'import { Button } from './components/atoms/Button.tsx'import { ParallaxProvider } from 'react-scroll-parallax'
- Next, wrap all the JSX that is being returned from the
App
component inside theParallaxProvider
component like so:
export default function App() { const data = useLoaderData<typeof loader>() const theme = useTheme() const nonce = useNonce()
return ( <ParallaxProvider> <Document nonce={nonce} theme={theme}> <div className="flex h-screen flex-col justify-between"> <HeaderWithSearch />
34 collapsed lines
<div className="flex-1"> <main className="grid h-full"> <HeroCallToAction image={heroImage} imageRight={true} hasBackgroundColour={true} > <div className="flex h-full flex-1 flex-col justify-between p-16"> <div className="flex flex-col gap-8"> <h2 className="text-h2">Welcome to Epic News</h2> <p className="text-lg"> Keep up to date with the latest tech news. </p> </div> <Button asChild variant="default" size="lg"> <Link to="/signup">Sign up</Link> </Button> </div> </HeroCallToAction>
<ParallaxBackground image={heroImage} title="Epic News" logo={logo} />
<div className="container"> <p className="text-base text-gray-600 md:text-lg lg:text-xl"> Welcome to Epic News, where the latest developments in tech are found. </p> </div> </main> </div>
<div className="container flex justify-between pb-5"> <ThemeSwitch userPreference={data.requestInfo.userPrefs.theme} /> </div> <FooterMenuRight /> </div> </Document> </ParallaxProvider> )}
Using the ParallaxBackground
component
Section titled โUsing the ParallaxBackground componentโThe ParallaxBackground
component is now ready to be used in the app.
We just need to import and configure it.
With your two hero sections in place, decide which you like best.
Do you prefer the โHero call to actionโ or the โParallax Backgroundโ?
Decide on your favourite, and delete the other from app/root.tsx
. Remember you can always customise your choice further if you wish.
Experimentation
Section titled โExperimentationโThere are lots of other custom components in the app/components
directory that you can explore and use in your site.
Documentation for each can be found in the README_COMPONENTS.md
at the root of your project.
Try experimenting with these components to see how they work and how you can use them in your site.
Why not have a go at building your own custom component?