How to determine web part size with SPFx v1.12

Published 03-23-2021 03:55 PM 1,394 Views
Senior Member

Context

You might know that this new SPFx v1.12 feature has an old story behind it. In the past, developers were getting web part's width by unsupported ways like DOM classes or attributes. It was working pretty well until Microsoft decided to update SharePoint Online DOM in October last year and broke a lot of custom developments. As Microsoft says, DOM is not and API and you should avoid taking any dependencies in it but until now, nothing was officially released to solve this issue correctly. It's now done, SPFx v1.12 adds a width property and an onAfterResize() event to determine the width of your web part.

 

Note: In this article, I'm using a web part project with SPFx v.1.12 and React framework.

 

Determine web part size

In your web part TS file, you can add the onAfterResize() method to get notified when the web part is resized (for example when you resize your window):

 

  protected onAfterResize(newWidth: number) {
    console.log("New web part width: " + newWidth);
  }

 

This new method is documented as below:

 

This API is invoked when the web part container dom element width is changed, e.g. when the browser window is resized and when the property pane is toggled open/closed.

@param newWidth — Width (in pixels) of the container for the web part after the resize event.

@remarks
Web parts should utilize this method to perform operations such as potentially re-rendering components based on the new available width for the web part.

@virtual

 

Then you can use the width property as a prop for your React component:

 

  public render(): void {
    const element: React.ReactElement<IDemoWebPartWidthProps> = React.createElement(
      DemoWebPartWidth,
      {
        description: this.properties.description,
        webPartWidth: this.width
      }
    );

    ReactDom.render(element, this.domElement);
  }

 

This new property is documented as below:

 

This propery returns the width of the container for the web part.

@returns — Width (in pixels) of the container for the web part.

@remarks
Web parts should utilize this property to perform operations such as any conditional styling of components based on the initial available width for the web part.

@internalRemarks
This function retrieves web part's key to get stored section width from cache. If cache key does not exist in cache it will calculate and store the width before returning.

In the case where getWebPartCacheKey is not passed down, it will go through the original workflow to caculate web part width.

 

And you can display it in your component render() method:

 

<p className={ styles.description }>Web part width = { this.props.webPartWidth }</p>

 

Final result of this demo web part:

web-part-width.png

 

This a great add to SPFx and allows developers to re-render components when the web part is resized :thumbs_up:

 

Happy coding everyone!

 

Resources

Determine the rendered web part width | Microsoft Docs

SharePoint Framework enterprise guidance | Microsoft Docs

 

%3CLINGO-SUB%20id%3D%22lingo-sub-2230898%22%20slang%3D%22en-US%22%3EHow%20to%20determine%20web%20part%20size%20with%20SPFx%20v1.12%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2230898%22%20slang%3D%22en-US%22%3E%3CH2%20id%3D%22toc-hId--497531179%22%20id%3D%22toc-hId--497555014%22%3EContext%3C%2FH2%3E%0A%3CP%3EYou%20might%20know%20that%20this%20new%20SPFx%20v1.12%20feature%20has%20an%20old%20story%20behind%20it.%20In%20the%20past%2C%20developers%20were%20getting%20web%20part's%20width%20by%20unsupported%20ways%20like%20DOM%20classes%20or%20attributes.%20It%20was%20working%20pretty%20well%20until%20Microsoft%20decided%20to%20update%20SharePoint%20Online%20DOM%20in%20October%20last%20year%20and%20broke%20a%20lot%20of%20custom%20developments.%20As%20Microsoft%20says%2C%20DOM%20is%20not%20and%20API%20and%20you%20should%20avoid%20taking%20any%20dependencies%20in%20it%20but%20until%20now%2C%20nothing%20was%20officially%20released%20to%20solve%20this%20issue%20correctly.%20It's%20now%20done%2C%20SPFx%20v1.12%20adds%20a%20%3CSTRONG%3Ewidth%3C%2FSTRONG%3E%20property%20and%20an%26nbsp%3B%3CSPAN%3E%3CSTRONG%3EonAfterResize()%3C%2FSTRONG%3E%20event%20to%20determine%20the%20width%20of%20your%20web%20part.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSTRONG%3ENote%3A%3C%2FSTRONG%3E%20In%20this%20article%2C%20I'm%20using%20a%20web%20part%20project%20with%20SPFx%20v.1.12%20and%20React%20framework.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1989981654%22%20id%3D%22toc-hId-1989957819%22%3EDetermine%20web%20part%20size%3C%2FH2%3E%0A%3CP%3EIn%20your%20web%20part%20TS%20file%2C%20you%20can%20add%20the%20%3CSTRONG%3EonAfterResize()%3C%2FSTRONG%3E%20method%20to%20get%20notified%20when%20the%20web%20part%20is%20resized%20(for%20example%20when%20you%20resize%20your%20window)%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-javascript%22%3E%3CCODE%3E%20%20protected%20onAfterResize(newWidth%3A%20number)%20%7B%0A%20%20%20%20console.log(%22New%20web%20part%20width%3A%20%22%20%2B%20newWidth)%3B%0A%20%20%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20new%20method%20is%20documented%20as%20below%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-javascript%22%3E%3CCODE%3EThis%20API%20is%20invoked%20when%20the%20web%20part%20container%20dom%20element%20width%20is%20changed%2C%20e.g.%20when%20the%20browser%20window%20is%20resized%20and%20when%20the%20property%20pane%20is%20toggled%20open%2Fclosed.%0A%0A%40param%20newWidth%20%E2%80%94%20Width%20(in%20pixels)%20of%20the%20container%20for%20the%20web%20part%20after%20the%20resize%20event.%0A%0A%40remarks%0AWeb%20parts%20should%20utilize%20this%20method%20to%20perform%20operations%20such%20as%20potentially%20re-rendering%20components%20based%20on%20the%20new%20available%20width%20for%20the%20web%20part.%0A%0A%40virtual%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThen%20you%20can%20use%20the%20%3CSTRONG%3Ewidth%3C%2FSTRONG%3E%20property%20as%20a%20prop%20for%20your%20React%20component%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-javascript%22%3E%3CCODE%3E%20%20public%20render()%3A%20void%20%7B%0A%20%20%20%20const%20element%3A%20React.ReactElement%3CIDEMOWEBPARTWIDTHPROPS%3E%20%3D%20React.createElement(%0A%20%20%20%20%20%20DemoWebPartWidth%2C%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20description%3A%20this.properties.description%2C%0A%20%20%20%20%20%20%20%20webPartWidth%3A%20this.width%0A%20%20%20%20%20%20%7D%0A%20%20%20%20)%3B%0A%0A%20%20%20%20ReactDom.render(element%2C%20this.domElement)%3B%0A%20%20%7D%3C%2FIDEMOWEBPARTWIDTHPROPS%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20new%20property%20is%20documented%20as%20below%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-javascript%22%3E%3CCODE%3EThis%20propery%20returns%20the%20width%20of%20the%20container%20for%20the%20web%20part.%0A%0A%40returns%20%E2%80%94%20Width%20(in%20pixels)%20of%20the%20container%20for%20the%20web%20part.%0A%0A%40remarks%0AWeb%20parts%20should%20utilize%20this%20property%20to%20perform%20operations%20such%20as%20any%20conditional%20styling%20of%20components%20based%20on%20the%20initial%20available%20width%20for%20the%20web%20part.%0A%0A%40internalRemarks%0AThis%20function%20retrieves%20web%20part's%20key%20to%20get%20stored%20section%20width%20from%20cache.%20If%20cache%20key%20does%20not%20exist%20in%20cache%20it%20will%20calculate%20and%20store%20the%20width%20before%20returning.%0A%0AIn%20the%20case%20where%20getWebPartCacheKey%20is%20not%20passed%20down%2C%20it%20will%20go%20through%20the%20original%20workflow%20to%20caculate%20web%20part%20width.%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAnd%20you%20can%20display%20it%20in%20your%20component%20render()%20method%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-javascript%22%3E%3CCODE%3E%3CP%20classname%3D%22%7B%22%20styles.description%3D%22%22%3EWeb%20part%20width%20%3D%20%7B%20this.props.webPartWidth%20%7D%3C%2FP%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFinal%20result%20of%20this%20demo%20web%20part%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22web-part-width.png%22%20style%3D%22width%3A%20946px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F266517i5217B524DA12E0F2%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22web-part-width.png%22%20alt%3D%22web-part-width.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20a%20great%20add%20to%20SPFx%20and%20allows%20developers%20to%20re-render%20components%20when%20the%20web%20part%20is%20resized%26nbsp%3B%3Athumbs_up%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHappy%20coding%20everyone!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-182527191%22%20id%3D%22toc-hId-182503356%22%3EResources%3C%2FH2%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsharepoint%2Fdev%2Fspfx%2Fweb-parts%2Fbasics%2Fdetermine-web-part-width%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EDetermine%20the%20rendered%20web%20part%20width%20%7C%20Microsoft%20Docs%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsharepoint%2Fdev%2Fspfx%2Fenterprise-guidance%23in-perspective-sharepoint-framework-in-the-broader-sharepoint-platform%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3ESharePoint%20Framework%20enterprise%20guidance%20%7C%20Microsoft%20Docs%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-2230898%22%20slang%3D%22en-US%22%3E%3CP%3EYou%20might%20know%20that%20this%20new%20SPFx%20v1.12%20feature%20has%20an%20old%20story%20behind%20it.%20In%20the%20past%2C%20developers%20were%20getting%20web%20part's%20width%20by%20unsupported%20ways%20like%20DOM%20classes%20or%20attributes.%20It%20was%20working%20pretty%20well%20until%20Microsoft%20decided%20to%20update%20SharePoint%20Online%20DOM%20in%20October%20last%20year%20and%20broke%20a%20lot%20of%20custom%20developments.%20As%20Microsoft%20says%2C%20DOM%20is%20not%20and%20API%20and%20you%20should%20avoid%20taking%20any%20dependencies%20in%20it%20but%20until%20now%2C%20nothing%20was%20officially%20released%20to%20solve%20this%20issue%20correctly.%20It's%20now%20done%2C%20SPFx%20v1.12%20adds%20a%20%3CSTRONG%3Ewidth%3C%2FSTRONG%3E%20property%20and%20an%26nbsp%3B%3CSPAN%3E%3CSTRONG%3EonAfterResize()%3C%2FSTRONG%3E%20event%20to%20determine%20the%20width%20of%20your%20web%20part.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2230898%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EHow%20to%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Co-Authors
Version history
Last update:
‎Mar 23 2021 02:14 PM
Updated by: