Skip to main content

๐ŸŽจ Fullpage.js + Next.js + TypeScript๋กœ ๋ฉ‹์ง„ ์›นํŽ˜์ด์ง€ ๋งŒ๋“ค๊ธฐ

ยท 4 min read
Bundaberg Man
Maintainer of Docusaurus

Fullpage.js + Next.js + TypeScript๋กœ ๋ฉ‹์ง„ ์›นํŽ˜์ด์ง€ ๋งŒ๋“ค๊ธฐ ๐Ÿš€โ€‹

์•ˆ๋…•ํ•˜์„ธ์š”! ์˜ค๋Š˜์€ Fullpage.js, Next.js, ๊ทธ๋ฆฌ๊ณ  TypeScript๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜์—ฌ ๋ฉ‹์ง„ ์›น ํŽ˜์ด์ง€๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๊ณต์œ ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ธํ„ฐ๋„ท์—์„œ ์ด ์„ธ ๊ฐ€์ง€ ๊ธฐ์ˆ ์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ์ œ๋ฅผ ์ฐพ๊ธฐ ์–ด๋ ค์›Œ ์ด๋ ‡๊ฒŒ ๋ธ”๋กœ๊ทธ์— ์ž‘์„ฑํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ“š ์ฐธ๊ณ  ์ž๋ฃŒโ€‹

๐Ÿ› ๏ธ ์ค€๋น„๋ฌผโ€‹

๋จผ์ €, ํ”„๋กœ์ ํŠธ์— ํ•„์š”ํ•œ ํŒจํ‚ค์ง€๋“ค์„ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค:

npx create-next-app@latest --typescript
npm install react-bootstrap bootstrap
npm install @fullpage/react-fullpage

๐Ÿ“ฆ package.jsonโ€‹

{
"name": "fullpage_nextjs_typescript",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"license": "gpl-3.0",
"dependencies": {
"@fullpage/react-fullpage": "^0.1.38",
"@types/node": "18.15.11",
"@types/react": "18.0.34",
"@types/react-dom": "18.0.11",
"bootstrap": "^5.2.3",
"next": "13.3.0",
"react": "18.2.0",
"react-bootstrap": "^2.7.2",
"react-dom": "18.2.0",
"typescript": "5.0.4"
}
}

๐Ÿ’ป ๊ตฌํ˜„ํ•˜๊ธฐโ€‹

components/fullpageExample.tsx๋ผ๋Š” ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ณ  ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.

[์ด์ „ ์ฝ”๋“œ์™€ ๋™์ผํ•œ TypeScript ์ฝ”๋“œ ๋ธ”๋ก]

import React, { useState } from "react";
import ReactFullpage, { fullpageOptions } from "@fullpage/react-fullpage";
import { Navbar, Nav, Button } from "react-bootstrap";

interface Section {
text: string;
id?: number;
}

const originalColors = [
"blue",
"#0798ec",
"#fc6c7c",
"#435b71",
"orange",
"blue",
"purple",
"yellow",
];

type Credits = {
enabled?: boolean;
label?: string;
position?: "left" | "right";
};

const pluginWrapper = () => {
/*
* require('../static/fullpage.scrollHorizontally.min.js'); // Optional. Required when using the "scrollHorizontally" extension.
*/
};

const FullpageJsExample = () => {
const [sectionsColor, setSectionsColor] = useState([...originalColors]);
const [fullpages, setFullpages] = useState<Section[]>([
{
text: "Section 1",
},
{
text: "Section 2",
},
{
text: "Section 3",
},
]);

const onLeave = (origin: any, destination: any, direction: any) => {
console.log("onLeave", { origin, destination, direction });
};

const handleChangeColors = () => {
const newColors =
sectionsColor[0] === "yellow" ? [...originalColors] : ["yellow", "blue", "white"];
setSectionsColor(newColors);
};

const handleAddSection = () => {
setFullpages((prevFullpages) => [
...prevFullpages,
{
text: `section ${prevFullpages.length + 1}`,
id: Math.random(),
},
]);
};

const handleRemoveSection = () => {
setFullpages((prevFullpages) => {
const newPages = [...prevFullpages];
newPages.pop();
return newPages;
});
};

if (!fullpages.length) {
return null;
}

const Menu = () => (
<Navbar bg="light" expand="lg" fixed="top">
<Navbar.Brand href="#home" className="mx-2">
Fullpage.js + Next.js + Typescript
</Navbar.Brand>
<Navbar.Toggle aria-controls="basic-navbar-nav" />
<Navbar.Collapse id="basic-navbar-nav">
<Nav className="mr-auto">
<Nav.Item>
<Button onClick={handleAddSection} className="mr-2, mx-1">
ADD SECTION
</Button>
</Nav.Item>
<Nav.Item>
<Button onClick={handleRemoveSection} className="mr-2, mx-1">
REMOVE SECTION
</Button>
</Nav.Item>
<Nav.Item>
<Button onClick={handleChangeColors}>CHANGE SECTION</Button>
</Nav.Item>
</Nav>
</Navbar.Collapse>
</Navbar>
);

const credits: Credits = {
enabled: true,
label: "my custom",
position: "left",
};

return (
<div className="App">
<Menu />
<ReactFullpage
licenseKey={"OPEN-SOURCE-GPLV3-LICENSE"}
navigation
onLeave={onLeave}
sectionsColor={sectionsColor}
pluginWrapper={pluginWrapper}
debug={false}
credits={credits}
render={(comp: any) => (
<ReactFullpage.Wrapper>
{fullpages.map(({ text }) => (
<div key={text} className="section">
<h1>{text}</h1>
</div>
))}
</ReactFullpage.Wrapper>
)}
/>
</div>
);
};

export default FullpageJsExample;

์ฝ”๋“œ ์„ค๋ช…์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค:

  1. React ๋ฐ ํ•„์š”ํ•œ ๋ชจ๋“ˆ์„ import ํ•ฉ๋‹ˆ๋‹ค.
  2. ๊ฐ ์„น์…˜์˜ ์ •๋ณด์™€ ์ƒ‰์ƒ์„ ์ €์žฅํ•˜๋Š” ์ƒํƒœ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
  3. ์„น์…˜์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์‚ญ์ œํ•˜๊ฑฐ๋‚˜ ์ƒ‰์ƒ์„ ๋ณ€๊ฒฝํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.
  4. React-Bootstrap์„ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ๋‹จ ๋ฉ”๋‰ด๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.
  5. Fullpage.js์˜ ์˜ต์…˜์„ ์„ค์ •ํ•˜๊ณ , ๊ฐ ์„น์…˜์„ ๋ Œ๋”๋งํ•ฉ๋‹ˆ๋‹ค.

์‹คํ–‰โ€‹

์ด์ œ ํ„ฐ๋ฏธ๋„์—์„œ npm run dev ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ๊ฐœ๋ฐœ ์„œ๋ฒ„๋ฅผ ์‹คํ–‰์‹œํ‚ค์„ธ์š”. ์›น ๋ธŒ๋ผ์šฐ์ €์—์„œ http://localhost:3000์— ์ ‘์†ํ•˜๋ฉด ์ž‘์„ฑํ•œ ์›น ํŽ˜์ด์ง€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด์ƒ์œผ๋กœ Fullpage.js, Next.js, TypeScript๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์›น ํŽ˜์ด์ง€๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.