How AI is powering better personalization in e-commerce [with Vercel]. Sign up now

Mobile preview

Studio v2
This plugin only works with an older version of Sanity Studio (v2), which is deprecated.

The mobile preview plugin makes it possible to preview how a website looks on a mobile phone.

By Jesper Paulsen

v2 install command (deprecated)

npm install sanity-mobile-preview

sanity-mobile-preview

An NPM package written in React used to preview mobile devices. Especially helpful when used in combination with a CMS like sanity.

NPM JavaScript Style Guide

DEMO

Demo picture

Install

npm install --save sanity-mobile-preview

Usage

import SanityMobilePreview from 'sanity-mobile-preview'
import 'sanity-mobile-preview/dist/index.css?raw'

const Example = () => {
  return <SanityMobilePreview />
}

How to set up preview with Sanity

In order to set up mobile preview with Sanity, you first need to create a new view.

For simplicity, we will copy the predefined preview from IframePreview.js to a file called IframeMobilePreview.js

Add imports:

import SanityMobilePreview from 'sanity-mobile-preview'
import 'sanity-mobile-preview/dist/index.css?raw'

At line 47, replace

return (
  <div className={styles.componentWrapper}>
    <div className={styles.iframeContainer}>
      <iframe src={url} frameBorder={'0'} />
    </div>
  </div>
)

with

return (
  <div className={styles.componentWrapper}>
    <SanityMobilePreview>
      <div className={styles.iframeContainer}>
        <iframe src={url} frameBorder={'0'} />
      </div>
    </SanityMobilePreview>
  </div>
)

Then go to deskStructure.js.

Under getDefaultDocumentNode add the next snippet to the Schemes you want to add mobile preview to:

    S.view
        .component(IframeMobilePreview)
        .title('Mobile preview')
        .options({previewURL})
    ])

You should now be able to preview your website with a mobile view.

Customization

The SanityMobilePreview takes five optional arguments:

ArgumentValuesDefault
allowedDevicesAn array of MobileDevices. This will be the devices that are selectable in the dropdown menu.All devices
preSelectedDeviceA MobileDevice. The default device to be selected.'iphone-x'
preSelectedColorA device color. The default device color to selected.'black
showMenuA boolean. Controls the visibility of the dropdowns.true
preSelectedLandscapeA boolean. The default orientation of the devicesfalse

If you have specified a preSelectedDevice that is not specified in allowedDevices, it will default to the first device in allowedDevices.

If you have specified a preSelectedColor that is not applicable to the preSelectedDevice, the preSelectedColor will default to black.

Example

This example will load a silver iPad mini as default, and only allows iPhone X and iPad to be selected:

const App = () => {
  return (
    <MobileDevicePreview
      preSelectedDevice={'ipad'}
      preSelectedColor={'silver'}
      allowedDevices={['ipad', 'iphone-x']}
    >
      <div> This is a demo 🤠</div>
    </MobileDevicePreview>
  )
}

Types

MobileDevices = 'iphone-x' | 'note8' | 'iphone8' | 'iphone8plus' | 'iphone5s' | 'ipad' | 'nexus5'

MobileDeviceColors = 'silver' | 'black' | 'gold'

Devices

The different devices are:

DeviceIdReadable nameColors
iphone-xiPhone Xblack
note8Note 8black
iphone8iPhone 8black, silver, gold
iphone8plusiPhone 8 Plusblack, silver, gold
iphone5siPhone 5sblack, silver, gold
ipadiPad miniblack, silver
nexus5Nexus 5black

Credit

Marvel Devices
create-react-library

License

MIT