Introduction
PRIMEREACT UI framework that has over 80 React UI Components with top-notch quality to help you implement all your UI requirements in style.
Galleria Advanced, Galleria can be extended further to implement complex requirements.
Preconditions
- Javascript
- Basic knowledge of React js
- Node.js
- V.S. Code,Visual Studio
We cover the below things,
- Create React application
- Installation of Primeface
- How to apply steps of Primeface in React js
Step 1
Run the below command to create React js
npx create-react-app prime-app
cd prime-app
npm start
Step 2
Run the below command for installing PrimeReact
npm install primereact primeicons
Create the files according to the below image
Step 3
Add the below code in the App.js
import 'primeicons/primeicons.css';
import 'primereact/resources/themes/lara-light-indigo/theme.css';
import 'primereact/resources/primereact.css';
import 'primeflex/primeflex.css';
import './App.css';
import ReactDOM from 'react-dom';
import React, { useRef, useEffect, useState } from 'react';
import { classNames } from 'primereact/utils';
import { PhotoService } from './PhotosService';
import { Button } from 'primereact/button';
import { Galleria } from 'primereact/galleria';
function App() {
const [images, setImages] = useState(null);
const [activeIndex, setActiveIndex] = useState(0);
const [showThumbnails, setShowThumbnails] = useState(false);
const [isAutoPlayActive, setAutoPlayActive] = useState(true);
const [isFullScreen, setFullScreen] = useState(false);
const galleriaService = new PhotoService();
const galleria = useRef(null)
const responsiveOptions = [
{
breakpoint: '1024px',
numVisible: 5
},
{
breakpoint: '960px',
numVisible: 4
},
{
breakpoint: '768px',
numVisible: 3
},
{
breakpoint: '560px',
numVisible: 1
}
];
useEffect(() => {
debugger
let data1 = galleriaService.getImages();
setImages(data1); bindDocumentListeners();
// galleriaService.getImages().then(data => setImages(data));
// bindDocumentListeners();
return () => unbindDocumentListeners();
},[]) // eslint-disable-line react-hooks/exhaustive-deps
useEffect(() => {
setAutoPlayActive(galleria.current.isAutoPlayActive())
},[isAutoPlayActive]);
const onThumbnailChange = (event) => {
setActiveIndex(event.index)
}
const onItemChange = (event) => {
setActiveIndex(event.index)
}
const toggleFullScreen = () => {
if (isFullScreen) {
closeFullScreen();
}
else {
openFullScreen();
}
}
const onFullScreenChange = () => {
setFullScreen(prevState => !prevState )
}
const openFullScreen = () => {
let elem = document.querySelector('.custom-galleria');
if (elem.requestFullscreen) {
elem.requestFullscreen();
}
else if (elem.mozRequestFullScreen) { /* Firefox */
elem.mozRequestFullScreen();
}
else if (elem.webkitRequestFullscreen) { /* Chrome, Safari & Opera */
elem.webkitRequestFullscreen();
}
else if (elem.msRequestFullscreen) { /* IE/Edge */
elem.msRequestFullscreen();
}
}
const closeFullScreen = () => {
if (document.exitFullscreen) {
document.exitFullscreen();
}
else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
}
else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen();
}
else if (document.msExitFullscreen) {
document.msExitFullscreen();
}
}
const bindDocumentListeners = () => {
document.addEventListener("fullscreenchange", onFullScreenChange);
document.addEventListener("mozfullscreenchange", onFullScreenChange);
document.addEventListener("webkitfullscreenchange", onFullScreenChange);
document.addEventListener("msfullscreenchange", onFullScreenChange);
}
const unbindDocumentListeners = () => {
document.removeEventListener("fullscreenchange", onFullScreenChange);
document.removeEventListener("mozfullscreenchange", onFullScreenChange);
document.removeEventListener("webkitfullscreenchange", onFullScreenChange);
document.removeEventListener("msfullscreenchange", onFullScreenChange);
}
const thumbnailTemplate = (item) => {
return (
<div className="grid grid-nogutter justify-content-center">
<img src={item.thumbnailImageSrc} onError={(e) => e.target.src='https://www.primefaces.org/wp-content/uploads/2020/05/placeholder.png'} alt={item.alt} style={{ display: 'block' }} />
</div>
);
}
const itemTemplate = (item) => {
debugger
if (isFullScreen) {
debugger
return <img src={`https://www.primefaces.org/primereact/${item.itemImageSrc}`} onError={(e) => e.target.src='https://www.primefaces.org/wp-content/uploads/2020/05/placeholder.png'} alt={item.alt} />
}
return <img src={`https://www.primefaces.org/primereact/${item.itemImageSrc}`} onError={(e) => e.target.src='https://www.primefaces.org/wp-content/uploads/2020/05/placeholder.png'} alt={item.alt} style={{ width: '100%', display: 'block' }} />
}
const renderFooter = () => {
let autoPlayClassName = classNames('pi', {
'pi-play': !isAutoPlayActive,
'pi-pause': isAutoPlayActive
});
let fullScreenClassName = classNames('pi', {
'pi-window-maximize': !isFullScreen,
'pi-window-minimize': isFullScreen
});
return (
<div className="custom-galleria-footer">
<Button icon="pi pi-list" onClick={() => setShowThumbnails(prevState => !prevState)} />
<Button icon={autoPlayClassName} onClick={() => {
if (!isAutoPlayActive) {
galleria.current.startSlideShow();
setAutoPlayActive(true)
}
else {
galleria.current.stopSlideShow();
setAutoPlayActive(false)
}
}} />
{
images && (
<span className="title-container">
<span>{activeIndex + 1}/{images.length}</span>
<span className="title">{images[activeIndex].title}</span>
<span>{images[activeIndex].alt}</span>
</span>
)
}
<Button icon={fullScreenClassName} onClick={() => toggleFullScreen()} className="fullscreen-button" />
</div>
);
}
const footer = renderFooter();
const galleriaClassName = classNames('custom-galleria', {
'fullscreen': isFullScreen
});
return (
<div>
<div className="galleria-demo">
<div className="card">
<Galleria ref={galleria} value={images} activeIndex={activeIndex} onItemChange={onItemChange}
showThumbnails={showThumbnails} showItemNavigators showItemNavigatorsOnHover
numVisible={5} circular autoPlay transitionInterval={3000} responsiveOptions={responsiveOptions}
item={itemTemplate} thumbnail={thumbnailTemplate} footer={footer}
style={{ maxWidth: '640px' }} className={galleriaClassName} />
</div>
</div>
</div>
);
}
export default App;
Step 4
Add the below code in index,html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!-- PrimeReact -->
<link rel="stylesheet" href="https://unpkg.com/primeicons/primeicons.css" />
<link rel="stylesheet" href="https://unpkg.com/primereact/resources/themes/lara-light-indigo/theme.css" />
<link rel="stylesheet" href="https://unpkg.com/primereact/resources/primereact.min.css" />
<link rel="stylesheet" href="https://unpkg.com/[email protected]/primeflex.min.css" />
<!-- Dependencies -->
<script src="https://unpkg.com/react/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom/umd/react-dom.production.min.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/react-transition-group.js"></script>
<!-- Demo -->
<script src="https://unpkg.com/primereact/core/core.min.js"></script>
<script src="https://unpkg.com/primereact/slidemenu/slidemenu.min.js"></script>
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<script src="https://unpkg.com/primereact/core/core.min.js"></script>
<script src="https://unpkg.com/primereact/slidemenu/slidemenu.min.js"></script>
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
Step 5
Add the below code in App.css
.App {
text - align: center;
}.App - logo {
height: 40 vmin;
pointer - events: none;
}
@media(prefers - reduced - motion: no - preference) {
.App - logo {
animation: App - logo - spin infinite 20 s linear;
}
}.App - header {
background - color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}
.App-link {
color: # 61 dafb;
}
@keyframes App - logo - spin {
from {
transform: rotate(0 deg);
}
to {
transform: rotate(360 deg);
}
}
/* GalleriaAdvancedDemo.css */
.galleria - demo.custom - galleria.fullscreen {
display: flex;
flex - direction: column;
}.galleria - demo.custom - galleria.fullscreen.p - galleria - content {
flex - grow: 1;
justify - content: center;
}.galleria - demo.custom - galleria.p - galleria - content {
position: relative;
}.galleria - demo.custom - galleria.p - galleria - thumbnail - wrapper {
position: absolute;
bottom: 0;
left: 0;
width: 100 % ;
}.galleria - demo.custom - galleria.p - galleria - thumbnail - items - container {
width: 100 % ;
}.galleria - demo.custom - galleria.custom - galleria - footer {
display: flex;
align - items: center;
background - color: rgba(0, 0, 0, .9);
color: #fff;
}.galleria - demo.custom - galleria.custom - galleria - footer > button {
background - color: transparent;
color: #fff;
border: 0 none;
border - radius: 0;
margin: 0.2 rem 0;
}.galleria - demo.custom - galleria.custom - galleria - footer > button.fullscreen - button {
margin - left: auto;
}.galleria - demo.custom - galleria.custom - galleria - footer > button: hover {
background - color: rgba(255, 255, 255, 0.1);
}.galleria - demo.custom - galleria.title - container > span {
font - size: 0.9 rem;
padding - left: 0.829 rem;
}.galleria - demo.custom - galleria.title - container > span.title {
font - weight: bold;
}
Step 6
Add the following code in package.json,
{
"name": "prime-demo",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.3.0",
"@testing-library/user-event": "^13.5.0",
"primeicons": "^5.0.0",
"primereact": "^8.1.1",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-redux": "^8.0.2",
"react-scripts": "^2.1.3",
"redux": "^4.2.0",
"web-vitals": "^2.1.4",
"primeflex": "^3.1.0",
"react-transition-group": "^4.4.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": ["react-app", "react-app/jest"]
},
"browserslist": {
"production": [">0.2%", "not dead", "not op_mini all"],
"development": ["last 1 chrome version", "last 1 firefox version", "last 1 safari version"]
}
}
Step 7
Add below code in Photos.json
[{
"itemImageSrc": "images/galleria/galleria1.jpg",
"thumbnailImageSrc": "images/galleria/galleria1s.jpg",
"alt": "Description for Image 1",
"title": "Title 1"
}, {
"itemImageSrc": "images/galleria/galleria2.jpg",
"thumbnailImageSrc": "images/galleria/galleria2s.jpg",
"alt": "Description for Image 2",
"title": "Title 2"
}, {
"itemImageSrc": "images/galleria/galleria3.jpg",
"thumbnailImageSrc": "images/galleria/galleria3s.jpg",
"alt": "Description for Image 3",
"title": "Title 3"
}, {
"itemImageSrc": "images/galleria/galleria4.jpg",
"thumbnailImageSrc": "images/galleria/galleria4s.jpg",
"alt": "Description for Image 4",
"title": "Title 4"
}, {
"itemImageSrc": "images/galleria/galleria5.jpg",
"thumbnailImageSrc": "images/galleria/galleria5s.jpg",
"alt": "Description for Image 5",
"title": "Title 5"
}, {
"itemImageSrc": "images/galleria/galleria6.jpg",
"thumbnailImageSrc": "images/galleria/galleria6s.jpg",
"alt": "Description for Image 6",
"title": "Title 6"
}, {
"itemImageSrc": "images/galleria/galleria7.jpg",
"thumbnailImageSrc": "images/galleria/galleria7s.jpg",
"alt": "Description for Image 7",
"title": "Title 7"
}, {
"itemImageSrc": "images/galleria/galleria8.jpg",
"thumbnailImageSrc": "images/galleria/galleria8s.jpg",
"alt": "Description for Image 8",
"title": "Title 8"
}, {
"itemImageSrc": "images/galleria/galleria9.jpg",
"thumbnailImageSrc": "images/galleria/galleria9s.jpg",
"alt": "Description for Image 9",
"title": "Title 9"
}, {
"itemImageSrc": "images/galleria/galleria10.jpg",
"thumbnailImageSrc": "images/galleria/galleria10s.jpg",
"alt": "Description for Image 10",
"title": "Title 10"
}, {
"itemImageSrc": "images/galleria/galleria11.jpg",
"thumbnailImageSrc": "images/galleria/galleria11s.jpg",
"alt": "Description for Image 11",
"title": "Title 11"
}, {
"itemImageSrc": "images/galleria/galleria12.jpg",
"thumbnailImageSrc": "images/galleria/galleria12s.jpg",
"alt": "Description for Image 12",
"title": "Title 12"
}, {
"itemImageSrc": "images/galleria/galleria13.jpg",
"thumbnailImageSrc": "images/galleria/galleria13s.jpg",
"alt": "Description for Image 13",
"title": "Title 13"
}, {
"itemImageSrc": "images/galleria/galleria14.jpg",
"thumbnailImageSrc": "images/galleria/galleria14s.jpg",
"alt": "Description for Image 14",
"title": "Title 14"
}, {
"itemImageSrc": "images/galleria/galleria15.jpg",
"thumbnailImageSrc": "images/galleria/galleria15s.jpg",
"alt": "Description for Image 15",
"title": "Title 15"
}]
Step 8
Add below code in PhotoService.js
import datas from './Photos.json';
export class PhotoService {
getImages() {
const results1 = [];
datas.map((datas) => {
results1.push(datas)
});
return results1;
}
}
Step 9
Run the following command
npm i
npm start
Summary
In this article, we learned how to create react project, set PrimeReact UI, and create React js PrimeReact UI Component Galleria Advanced and PrimeReact UI components.