Introducing React Video Banner Web Part

Published May 06 2021 04:18 AM 1,321 Views
Contributor

In this post, we will see how easy it is to build a cool looking video banner web part using SPFx with the help of PnP reusable property pane controls. First of all, the requirements: 

  • The web part should look like a banner on top of pages, with a video playing
  • Ability to change the overlay text on this video
  • Control the banner area size

This is what the PnP React Video Banner web part offers, once you add the web part to your page, you will be able to select a video (using the file picker control from the PnP Reusable Property Pane Controls), you will be able to modify the text overlay and its color, control the height of the video area and lastly (at least for now) modify the brightness: 

 

  derhallim_0-1620249069072.png

 

 These settings would provide you with a web part that looks like this: 

 

 derhallim_4-1620249695924.png

 

 

Now let's have a look at the code itself which is pretty simple. The file structure can be seen below:

 

                                                                

 derhallim_1-1620252146944.png

 

 

The web part has only one functional component named: VideoBackground that accepts the following properties as defined in the IVideoBackgroundProps.ts file: wpTitle (string), videoUrl (string), labelColor (string), brightness (number), height (number).

 

 derhallim_2-1620252225550.png

 

The video url and the label color are set with the help of PnP Property Pane Controls as shown below: 

 

 derhallim_0-1620252048668.png

 

 

Once the properties are setup on the VideoBackgroundWebPart.ts file, we pass them to the VideoBackground.tsx component. 

 

 derhallim_6-1620252844193.png

 

 
All good so far? Now in our functional component, we have an HTML video control where the source of the video is set to the videoUrl prop we pass and we play with the CSS filter prop to set the brightness by adding the value concatenated with % as we need a brightness percentage: 
 
 derhallim_3-1620252272134.png

 

 

 

Notice the ref attribute on the video control? The reason we have it is that we want to refresh the video whenever we change the video url. By default, when changing the properties, all values will be reflected except for the video url, it needs to be explicitly refreshed on the video control. So we get a reference to the video control using React's ref, where we define the "vRef" value on the functional component itself. 

 

We then use React's useEffect to check if the video url has changed or not, if it has changed, we call the load() function on the video reference itself: 

 

 derhallim_4-1620252332115.png

 

 
Some notable points about the CSS used is the div that's available just before the video control, it's aligned using an absolute position with a display of flex to position the h1 element inside of it. The video element itself has a CSS property object-fit: cover.
 
As demonstrated, with SPFx and the help of PnP Reusable Property Pane controls, building web parts using modern development techniques is very easy and isn't time consuming as some may think! Source code is available on Github along with other awesome web parts by the community, for more information about all the PnP offerings, checkout https://aka.ms/m365pnp 

#SharingIsCaring
 
 

 

%3CLINGO-SUB%20id%3D%22lingo-sub-2329002%22%20slang%3D%22en-US%22%3EIntroducing%20React%20Video%20Banner%20Web%20Part%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2329002%22%20slang%3D%22en-US%22%3E%3CP%3EIn%20this%20post%2C%20we%20will%20see%20how%20easy%20it%20is%20to%20build%20a%20cool%20looking%20video%20banner%20web%20part%20using%20SPFx%20with%20the%20help%20of%20PnP%20reusable%20property%20pane%20controls.%20First%20of%20all%2C%20the%20requirements%3A%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EThe%20web%20part%20should%20look%20like%20a%20banner%20on%20top%20of%20pages%2C%20with%20a%20video%20playing%3C%2FLI%3E%0A%3CLI%3EAbility%20to%20change%20the%20overlay%20text%20on%20this%20video%3C%2FLI%3E%0A%3CLI%3EControl%20the%20banner%20area%20size%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EThis%20is%20what%20the%20%3CA%20title%3D%22PnP%20React%20Video%20Banner%20web%20part%22%20href%3D%22https%3A%2F%2Fgithub.com%2Fpnp%2Fsp-dev-fx-webparts%2Ftree%2Fmain%2Fsamples%2Freact-video-banner%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3EPnP%20React%20Video%20Banner%20web%20part%3C%2FA%3E%20offers%2C%20once%20you%20add%20the%20web%20part%20to%20your%20page%2C%20you%20will%20be%20able%20to%20select%20a%20video%20(using%20the%20file%20picker%20control%20from%20the%20PnP%20Reusable%20Property%20Pane%20Controls)%2C%20you%20will%20be%20able%20to%20modify%20the%20text%20overlay%20and%20its%20color%2C%20control%20the%20height%20of%20the%20video%20area%20and%20lastly%20(at%20least%20for%20now)%20modify%20the%20brightness%3A%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-center%22%3E%26nbsp%3B%26nbsp%3B%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22derhallim_0-1620249069072.png%22%20style%3D%22width%3A%20370px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F278588i6FB5C67745410F64%2Fimage-dimensions%2F370x949%3Fv%3Dv2%22%20width%3D%22370%22%20height%3D%22949%22%20role%3D%22button%22%20title%3D%22derhallim_0-1620249069072.png%22%20alt%3D%22derhallim_0-1620249069072.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3BThese%20settings%20would%20provide%20you%20with%20a%20web%20part%20that%20looks%20like%20this%3A%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-center%22%3E%26nbsp%3B%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22derhallim_4-1620249695924.png%22%20style%3D%22width%3A%20711px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F278593i48E5C33042DFF8F7%2Fimage-dimensions%2F711x328%3Fv%3Dv2%22%20width%3D%22711%22%20height%3D%22328%22%20role%3D%22button%22%20title%3D%22derhallim_4-1620249695924.png%22%20alt%3D%22derhallim_4-1620249695924.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENow%20let's%20have%20a%20look%20at%20the%20code%20itself%20which%20is%20pretty%20simple.%20The%20file%20structure%20can%20be%20seen%20below%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-center%22%3E%26nbsp%3B%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22derhallim_1-1620252146944.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F278611i8ECF721FD5D79709%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22derhallim_1-1620252146944.png%22%20alt%3D%22derhallim_1-1620252146944.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-center%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20web%20part%20has%20only%20one%20functional%20component%20named%3A%20VideoBackground%20that%20accepts%20the%20following%20properties%20as%20defined%20in%20the%20IVideoBackgroundProps.ts%20file%3A%20wpTitle%20(string)%2C%20videoUrl%20(string)%2C%20labelColor%20(string)%2C%20brightness%20(number)%2C%20height%26nbsp%3B(number).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-center%22%3E%26nbsp%3B%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22derhallim_2-1620252225550.png%22%20style%3D%22width%3A%20547px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F278613iD29EFFD38175FB17%2Fimage-dimensions%2F547x213%3Fv%3Dv2%22%20width%3D%22547%22%20height%3D%22213%22%20role%3D%22button%22%20title%3D%22derhallim_2-1620252225550.png%22%20alt%3D%22derhallim_2-1620252225550.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-center%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20video%20url%20and%20the%20label%20color%20are%20set%20with%20the%20help%20of%20PnP%20Property%20Pane%20Controls%20as%20shown%20below%3A%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-center%22%3E%26nbsp%3B%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22derhallim_0-1620252048668.png%22%20style%3D%22width%3A%20598px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F278610i7A6449E9A53A3572%2Fimage-dimensions%2F598x193%3Fv%3Dv2%22%20width%3D%22598%22%20height%3D%22193%22%20role%3D%22button%22%20title%3D%22derhallim_0-1620252048668.png%22%20alt%3D%22derhallim_0-1620252048668.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOnce%20the%20properties%20are%20setup%20on%20the%20VideoBackgroundWebPart.ts%20file%2C%20we%20pass%20them%20to%20the%20VideoBackground.tsx%20component.%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-center%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%3E%0A%3CDIV%20class%3D%22lia-align-center%22%3E%26nbsp%3B%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22derhallim_6-1620252844193.png%22%20style%3D%22width%3A%20618px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F278618i283DB0E0FD026BF6%2Fimage-dimensions%2F618x231%3Fv%3Dv2%22%20width%3D%22618%22%20height%3D%22231%22%20role%3D%22button%22%20title%3D%22derhallim_6-1620252844193.png%22%20alt%3D%22derhallim_6-1620252844193.png%22%20%2F%3E%3C%2FSPAN%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3EAll%20good%20so%20far%3F%20Now%20in%20our%20functional%20component%2C%20we%20have%20an%20HTML%20video%20control%20where%20the%20source%20of%20the%20video%20is%20set%20to%20the%20videoUrl%20prop%20we%20pass%20and%20we%20play%20with%20the%20CSS%20filter%20prop%20to%20set%20the%20brightness%20by%20adding%20the%20value%20concatenated%20with%20%25%20as%20we%20need%20a%20brightness%20percentage%3A%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%20class%3D%22lia-align-center%22%3E%26nbsp%3B%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22derhallim_3-1620252272134.png%22%20style%3D%22width%3A%20614px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F278614i077CE3A59A69450A%2Fimage-dimensions%2F614x433%3Fv%3Dv2%22%20width%3D%22614%22%20height%3D%22433%22%20role%3D%22button%22%20title%3D%22derhallim_3-1620252272134.png%22%20alt%3D%22derhallim_3-1620252272134.png%22%20%2F%3E%3C%2FSPAN%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3C%2FDIV%3E%0A%3CDIV%3E%0A%3CDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENotice%20the%20ref%20attribute%20on%20the%20video%20control%3F%20The%20reason%20we%20have%20it%20is%20that%20we%20want%20to%20refresh%20the%20video%20whenever%20we%20change%20the%20video%20url.%20By%20default%2C%20when%20changing%20the%20properties%2C%20all%20values%20will%20be%20reflected%20except%20for%20the%20video%20url%2C%20it%20needs%20to%20be%20explicitly%20refreshed%20on%20the%20video%20control.%20So%20we%20get%20a%20reference%20to%20the%20video%20control%20using%20React's%20ref%2C%20where%20we%20define%20the%20%22vRef%22%20value%20on%20the%20functional%20component%20itself.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWe%20then%20use%20React's%20useEffect%20to%20check%20if%20the%20video%20url%20has%20changed%20or%20not%2C%20if%20it%20has%20changed%2C%20we%20call%20the%20load()%20function%20on%20the%20video%20reference%20itself%3A%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%3E%0A%3CDIV%20class%3D%22lia-align-center%22%3E%26nbsp%3B%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22derhallim_4-1620252332115.png%22%20style%3D%22width%3A%20573px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F278615iF2B5BE371816B5AB%2Fimage-dimensions%2F573x179%3Fv%3Dv2%22%20width%3D%22573%22%20height%3D%22179%22%20role%3D%22button%22%20title%3D%22derhallim_4-1620252332115.png%22%20alt%3D%22derhallim_4-1620252332115.png%22%20%2F%3E%3C%2FSPAN%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3ESome%20notable%20points%20about%20the%20CSS%20used%20is%20the%20div%20that's%20available%20just%20before%20the%20video%20control%2C%20it's%20aligned%20using%20an%20absolute%20position%20with%20a%20display%20of%20flex%20to%20position%20the%20h1%20element%20inside%20of%20it.%20The%20video%20element%20itself%20has%20a%20CSS%20property%20%3CEM%3Eobject-fit%3A%20cover.%3C%2FEM%3E%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3EAs%20demonstrated%2C%20with%20SPFx%20and%20the%20help%20of%20PnP%20Reusable%20Property%20Pane%20controls%2C%20building%20web%20parts%20using%20modern%20development%20techniques%20is%20very%20easy%20and%20isn't%20time%20consuming%20as%20some%20may%20think!%20Source%20code%20is%20available%20on%20Github%20along%20with%20other%20awesome%20web%20parts%20by%20the%20community%2C%20for%20more%20information%20about%20all%20the%20PnP%20offerings%2C%20checkout%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Faka.ms%2Fm365pnp%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Faka.ms%2Fm365pnp%3C%2FA%3E%26nbsp%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%23SharingIsCaring%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-2329002%22%20slang%3D%22en-US%22%3E%3CP%3EHow%20easy%20is%20it%20to%20build%20a%20cool%20looking%20web%20part%20using%20SPFx%20%26amp%3B%20PnP%20reusable%20controls%3F%3C%2FP%3E%3C%2FLINGO-TEASER%3E
Co-Authors
Version history
Last update:
‎May 05 2021 03:25 PM
Updated by: