React Awesome Slider in SPFX

In this article, we will learn about how to integrate the React Awesome Slider plugin in SPF and dynamically load images for Carousel from a SharePoint Document Library

Introduction 
 
As per official documentation, the react-awesome-slider is a 60 fps, extendable, highly customizable, and production-ready React Component that renders a media (image/video) gallery slider/carousel. 
 
Open a command prompt. Create a directory for the SPFx solution.
md spfx-React-Slider
 
Navigate to the above-created directory.
cd spfx-React-Slider
 
Run the Yeoman SharePoint Generator to create the solution. 
yo @microsoft/sharepoint
 
Solution Name
 
Hit Enter to have the default name (spfx-pnp-DropDown in this case) or type in any other name for your solution.
Selected choice - Hit Enter
 
Target for the component
 
Here, we can select the target environment where we are planning to deploy the client web part, i.e., SharePoint Online or SharePoint OnPremise (SharePoint 2016 onwards).
Selected choice - SharePoint Online only (latest)
 
Place of files
 
We may choose to use the same folder or create a subfolder for our solution.
Selected choice - Same folder
 
Deployment option
 
Selecting Y will allow the app to be deployed instantly to all sites and be accessible everywhere.
Selected choice - N (install on each site explicitly)
 
Permissions to access web APIs
 
Choose if the components in the solution require permission to access web APIs that are unique and not shared with other components in the tenant.
Selected choice - N (solution contains unique permissions)
 
Type of client-side component to create
 
We can choose to create a client-side web part or an extension. Choose the web part option.
Selected choice - WebPart
 
Web part name
 
Hit Enter to select the default name or type in any other name.
Selected choice - SpfxReactAwesomeSlider
 
Web part description
 
Hit Enter to select the default description or type in any other value.
 
Framework to use
 
Select any JavaScript framework to develop the component. Available choices are - No JavaScript Framework, React, and Knockout.
Selected choice - React
 
The Yeoman generator will perform a scaffolding process to generate the solution. The scaffolding process will take a significant amount of time.
Once the scaffolding process is completed, lock down the version of project dependencies by running the below command:
npm shrinkwrap
 
In the command prompt, type below command to open the solution in the code editor of your choice.
code .
 
NPM Packages Used,
On the command prompt, run below command.
  1. npm i @pnp/logging @pnp/common @pnp/odata @pnp/sp --save
for Pollyfills
  1. npm install --save @pnp/polyfill-ie11
for AwesomeSlider
  1. npm install --save react-awesome-slider  
in SpfxReactAwesomeSlider.tsx
 
  1. import * as React from 'react';  
  2. import styles from './SpfxReactAwesomeSlider.module.scss';  
  3. import {sp} from '@pnp/sp';  
  4. import { ISpfxReactAwesomeSliderProps } from './ISpfxReactAwesomeSliderProps';  
  5. import AwesomeSlider from 'react-awesome-slider';  
  6. import 'react-awesome-slider/dist/styles.css';  
  7. /*import CaptionedStyles from 'react-awesome-slider/src/components/captioned/styles.scss';*/  
  8. /*import 'react-awesome-slider/dist/custom-animations/scale-out-animation.css'; 
  9. import 'react-awesome-slider/dist/custom-animations/cube-animation.css'; 
  10. import 'react-awesome-slider/dist/custom-animations/fall-animation.css'; 
  11. import 'react-awesome-slider/dist/custom-animations/fold-out-animation.css'; 
  12. import 'react-awesome-slider/dist/custom-animations/open-animation.css';*/  
  13. import withAutoplay from 'react-awesome-slider/dist/autoplay';  
  14. const AutoplaySlider = withAutoplay(AwesomeSlider);  
  15. export interface Iurl {  
  16.   //File: Array<urlproperties>;  
  17.   File: urlproperties;  
  18.   
  19. }  
  20. export interface urlproperties {  
  21.   ServerRelativeUrl: string;  
  22.    
  23. }  
  24. interface IPnpstate {    
  25.   MYPnpFile:Iurl[];  
  26. }   
  27. export default class SpfxReactAwesomeSlider extends React.Component<ISpfxReactAwesomeSliderProps,IPnpstate> {  
  28.   constructor(props: ISpfxReactAwesomeSliderProps, state: IPnpstate) {  
  29.     super(props);  
  30.     this.state = {  
  31.       MYPnpFile: []  
  32.     };  
  33.    
  34.   }  
  35.  public componentDidMount(){  

  36.     this._getpnpfile()  
  37.     .then((response: Iurl[]) => {  
  38.       console.log(response);  
  39.       console.log(response[0].File.ServerRelativeUrl);  
  40.       this.setState({ MYPnpFile: response });  
  41.     });  
  42.   }  
  43.   private _getpnpfile() {  
  44.       
  45.    /* sp.web.lists.getByTitle("PnpLibrary") 
  46.   .items.expand("Folder", "File").get() 
  47.     .then((items: any[]): void => { 
  48.     console.log(items); 
  49.     }, (error: any): void => { 
  50.  
  51.     });*/  
  52.     return sp.web.lists.getByTitle("PnpLibrary")  
  53.     .items.expand("Folder""File").get();  
  54.   
  55.      
  56.   }   
  57.   public render(): React.ReactElement<ISpfxReactAwesomeSliderProps> {  
  58.     return (  
  59.       <div  className={ styles.spfxReactAwesomeSlider }>  
  60.         <div className={ styles.container }>  
  61.         { this.state.MYPnpFile.length>0 && <Firstcarousel bindoutput={this.state.MYPnpFile} />}  
  62.   
  63.   <br></br>  
  64.   { this.state.MYPnpFile.length>0 && <Secondcarousel bindoutput={this.state.MYPnpFile} />}  
  65.         </div>  
  66.       </div>  
  67.     );  
  68.   }  
  69. }  
  70.   
  71.   
  72. /* ... */  
  73. const Firstcarousel = (props) => {  
  74.  //var abc= (document.getElementById("textBox2") as HTMLInputElement).value ;  
  75.  const Bindediv = props.bindoutput.map((Outfile,index) =>  
  76.  <div key={index}  data-src={Outfile.File.ServerRelativeUrl}>  
  77.  </div>  
  78. );  
  79.  const myfirstslider = (  
  80.    <AwesomeSlider>  
  81. {Bindediv}  
  82.      </AwesomeSlider>  
  83.  );  
  84.  return (  
  85.    <div>  
  86.      {myfirstslider}  
  87.    </div>  
  88.  );  
  89. };  
  90.   
  91.   const Secondcarousel = (myprops) => {  
  92.     const Bindedcontent = myprops.bindoutput.map((Outfile,index) =>  
  93.     <div key={index}  data-src={Outfile.File.ServerRelativeUrl}>  
  94.     </div>  
  95.   );  
  96.     const mysecondslider = (  
  97.       <AutoplaySlider  
  98.       play={true}  
  99.       cancelOnInteraction={false// should stop playing on user interaction  
  100.       interval={2000}  
  101.     >  
  102.   {Bindedcontent}  
  103.         </AutoplaySlider>  
  104.     );  
  105.     return (  
  106.       <div>  
  107.         {mysecondslider}  
  108.       </div>  
  109.     );  
  110.     };  
  111.   
  112.     /* document.getElementById("Btn").addEventListener("click", function(){ 
  113.         (<HTMLInputElement><any>document.getElementById("textBox2")).value=(<HTMLInputElement><any>document.getElementById("textBox1")).value; 
  114.        (<HTMLInputElement>document.getElementById(elementId)).value; 
  115.       }); 
  116.  */  
Here, I am using the document library name "PnpLibrary" 
 
The following code is for fetching images from the Document Library 
  1. sp.web.lists.getByTitle("PnpLibrary")  
  2.  .items.expand("Folder""File").get()  
  3.    .then((items: any[]): void => {  
  4.    console.log(items);  
  5.    }, (error: any): void => {  
  6.   
  7.    });  
 
For basic carousel, use the below CSS import
  1. import 'react-awesome-slider/dist/styles.css';  
For Scale-out animation carousel use the below CSS import 
  1. import 'react-awesome-slider/dist/custom-animations/scale-out-animation.css';  
For Fold-out animation carousel use the below CSS import
  1. import 'react-awesome-slider/dist/custom-animations/fold-out-animation.css';  
For Cube animation carousel use the below CSS import
  1. import 'react-awesome-slider/dist/custom-animations/cube-animation.css';  
For Open animation carousel use the below CSS import  
  1. import 'react-awesome-slider/dist/custom-animations/open-animation.css';  
For Fall animation carousel use the below CSS import  
  1. import 'react-awesome-slider/dist/custom-animations/fall-animation.css';  
Note: use anyone on the CSS as per requirement to prevent Override,
 
AutoplaySlider is important from different package, use the following lines:
  1. import withAutoplay from 'react-awesome-slider/dist/autoplay';  
  2. const AutoplaySlider = withAutoplay(AwesomeSlider);  
 Autoslider implemented by:
  1.   <AutoplaySlider  
  2.     play={true}  
  3.     cancelOnInteraction={false}  
  4.     interval={3000}>  
  5.     <div data-src="/image-0.jpg" />  
  6.     <div data-src="/image-1.jpg" />  
  7.     <div data-src="/image-2.jpg" />  
  8.     <div data-src="/image-3.jpg" />  
  9.   </AutoplaySlider>  
 Whereas other above mentioned sliders are implemented via: 
  1.   <AwesomeSlider  
  2.     <div data-src="/image-0.png" />  
  3.     <div data-src="/image-1.png" />  
  4.     <div data-src="/image-2.jpg" />  
  5.   </AwesomeSlider>
Sample Output
 
Here, I stuck with the CSS module parameter as per the documentation. I used a different way to implement the different types sliders using CSS import methods. Captioned images and smooth lettering are pending from my side (since I receive some error, I hope to solve and update it very soon in my upcoming blogs).
 
 
I hope this helps someone. If anyone solves the captioned images and smooth lettering, please comment in the comment section. Happy Coding! :)