The lightning web components are now device-aware. You can detect the device
form factor using the standard @salesforce/client/formFactor
module in the LWC framework.
To use this module you need to import it like below.
import FORM_FACTOR from '@salesforce/client/formFactor'
The FORM_FACTOR
can have below values based on the type of the
device.
Large
- If the device is a desktop.Medium
- If the device is a tablet.Small
- A phone client
How to use the form factor in your component?
Use Case
Sample Code
formFactorDemo.js
import { LightningElement } from 'lwc'; import FORM_FACTOR from '@salesforce/client/formFactor'; export default class FormFactorDemo extends LightningElement { connectedCallback() { console.log('The device form factor is: ' + FORM_FACTOR); } get message (){ switch(FORM_FACTOR) { case 'Large': return 'You are on desktop'; case 'Medium': return 'You are on tablet'; case 'Small': return 'You are on mobile'; default: } } }
formFactorDemo.html
<template> <lightning-card title="Form Factor Demo"> <div class="slds-var-p-around_medium"> <div>Message : {message}</div> </div> </lightning-card> </template>
Thanks a lot, it was of great help
ReplyDeleteYou are welcome!!
Delete