2017-12-04 16:42:20 -08:00
---
2018-05-18 11:51:22 -04:00
title: Creating Custom Previews
2019-03-17 18:49:55 +02:00
weight: 50
group: customization
2017-12-04 16:42:20 -08:00
---
2017-11-27 14:22:11 -05:00
The NetlifyCMS exposes a `window.CMS` global object that you can use to register custom widgets, previews and editor plugins. The available customization methods are:
2018-08-14 11:33:13 -06:00
* **registerPreviewStyle:** Register a custom stylesheet to use on the preview pane.
* **registerPreviewTemplate:** Registers a template for a collection.
2017-11-27 14:22:11 -05:00
### React Components inline interaction
NetlifyCMS is a collection of React components and exposes two constructs globally to allow you to create components inline: ‘ createClass’ and ‘ h’ (alias for React.createElement).
## `registerPreviewStyle`
Register a custom stylesheet to use on the preview pane.
```js
CMS.registerPreviewStyle(file);
```
**Params:**
2018-08-14 11:33:13 -06:00
* **file:** css file path
2017-11-27 14:22:11 -05:00
**Example:**
2017-12-09 03:28:57 -05:00
2017-11-27 14:22:11 -05:00
```html
// index.html
2018-07-27 11:08:17 -04:00
< script src = "https://unpkg.com/netlify-cms@^2.0.0/dist/netlify-cms.js" > < / script >
2017-11-27 14:22:11 -05:00
< script >
CMS.registerPreviewStyle("/example.css");
< / script >
```
2017-12-09 03:28:57 -05:00
2017-11-27 14:22:11 -05:00
```css
/* example.css */
html,
body {
color: #444 ;
font-size: 14px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
}
body {
padding: 20px;
}
```
## `registerPreviewTemplate`
2018-08-28 12:12:56 -07:00
Registers a template for a folder collection or an individual file in a file collection.
2017-11-27 14:22:11 -05:00
2018-08-28 12:12:56 -07:00
`CMS.registerPreviewTemplate(name, react_component);`
2017-11-27 14:22:11 -05:00
**Params:**
2018-08-28 12:12:56 -07:00
* name: The name of the collection (or file for file collections) which this preview component will be used for.
* Folder collections: Use the name of the collection
* File collections: Use the name of the file
2020-08-16 02:50:32 -07:00
* react_component: A React component that renders the collection data. Six props will be passed to your component during render:
2018-08-14 11:33:13 -06:00
* entry: Immutable collection containing the entry data.
* widgetFor: Returns the appropriate widget preview component for a given field.
* [widgetsFor ](#lists-and-objects ): Returns an array of objects with widgets and associated field data. For use with list and object type entries.
* getAsset: Returns the correct filePath or in-memory preview for uploaded images.
2017-11-27 14:22:11 -05:00
**Example:**
2017-12-19 13:59:30 -05:00
2017-11-27 14:22:11 -05:00
```html
2018-07-27 11:08:17 -04:00
< script src = "https://unpkg.com/netlify-cms@^2.0.0/dist/netlify-cms.js" > < / script >
2017-11-27 14:22:11 -05:00
< script >
2020-01-13 17:28:01 +02:00
var PostPreview = createClass({
render: function() {
var entry = this.props.entry;
2020-02-13 02:12:36 +02:00
var image = entry.getIn(['data', 'image']);
var bg = this.props.getAsset(image);
return h('div', {},
2020-01-13 17:28:01 +02:00
h('h1', {}, entry.getIn(['data', 'title'])),
2020-02-13 02:12:36 +02:00
h('img', {src: bg.toString()}),
h('div', {"className": "text"}, this.props.widgetFor('body'))
2020-01-13 17:28:01 +02:00
);
2020-02-13 02:12:36 +02:00
}
2020-01-13 17:28:01 +02:00
});
2020-02-13 02:12:36 +02:00
CMS.registerPreviewTemplate("posts", PostPreview);
2017-11-27 14:22:11 -05:00
< / script >
```
2020-08-16 02:50:32 -07:00
* document: The preview pane iframe's [document instance ](https://github.com/ryanseddon/react-frame-component/tree/9f8f06e1d3fc40da7122f0a57c62f7dec306e6cb#accessing-the-iframes-window-and-document ).
* window: The preview pane iframe's [window instance ](https://github.com/ryanseddon/react-frame-component/tree/9f8f06e1d3fc40da7122f0a57c62f7dec306e6cb#accessing-the-iframes-window-and-document ).
2017-11-27 14:22:11 -05:00
### Lists and Objects
2018-08-20 16:37:10 -04:00
The API for accessing the individual fields of list- and object-type entries is similar to the API for accessing fields in standard entries, but there are a few key differences. Access to these nested fields is facilitated through the `widgetsFor` function, which is passed to the preview template component during render.
**Note** : as is often the case with the NetlifyCMS API, arrays and objects are created with Immutable.js. If some of the methods that we use are unfamiliar, such as `getIn` , check out [their docs ](https://facebook.github.io/immutable-js/docs/#/ ) to get a better understanding.
2017-11-27 14:22:11 -05:00
**List Example:**
```html
< script >
var AuthorsPreview = createClass({
// For list fields, the widgetFor function returns an array of objects
// that you can map over in your template. If our field is a list of
// authors containing two entries, with fields `name` and `description` ,
// the return value of `widgetsFor` would look like this:
//
// [{
// data: { name: 'Mathias', description: 'Co-Founder'},
// widgets: { name: (< WidgetComponent > ), description: (WidgetComponent>)}
// },
// {
// data: { name: 'Chris', description: 'Co-Founder'},
// widgets: { name: (< WidgetComponent > ), description: (WidgetComponent>)}
// }]
//
// Templating would look something like this:
2017-12-09 03:28:57 -05:00
2017-11-27 14:22:11 -05:00
render: function() {
return h('div', {},
2017-12-09 03:28:57 -05:00
2017-11-27 14:22:11 -05:00
// This is a static header that would only be rendered once for the entire list
h('h1', {}, 'Authors'),
2017-12-09 03:28:57 -05:00
2017-11-27 14:22:11 -05:00
// Here we provide a simple mapping function that will be applied to each
// object in the array of authors
this.props.widgetsFor('authors').map(function(author, index) {
return h('div', {key: index},
h('hr', {}),
h('strong', {}, author.getIn(['data', 'name'])),
author.getIn(['widgets', 'description'])
);
})
);
}
});
2017-12-09 03:28:57 -05:00
2017-11-27 14:22:11 -05:00
CMS.registerPreviewTemplate("authors", AuthorsPreview);
< / script >
```
**Object Example:**
```html
< script >
var GeneralPreview = createClass({
// Object fields are simpler than lists - instead of `widgetsFor` returning
// an array of objects, it returns a single object. Accessing the shape of
// that object is the same as the shape of objects returned for list fields:
//
// {
// data: { front_limit: 0, author: 'Chris' },
// widgets: { front_limit: (< WidgetComponent > ), author: (WidgetComponent>)}
// }
render: function() {
var entry = this.props.entry;
var title = entry.getIn(['data', 'site_title']);
var posts = entry.getIn(['data', 'posts']);
2017-12-09 03:28:57 -05:00
2017-11-27 14:22:11 -05:00
return h('div', {},
h('h1', {}, title),
h('dl', {},
h('dt', {}, 'Posts on Frontpage'),
h('dd', {}, this.props.widgetsFor('posts').getIn(['widgets', 'front_limit']) || 0),
2017-12-09 03:28:57 -05:00
2017-11-27 14:22:11 -05:00
h('dt', {}, 'Default Author'),
h('dd', {}, this.props.widgetsFor('posts').getIn(['data', 'author']) || 'None'),
)
);
}
});
2017-12-09 03:28:57 -05:00
2017-11-27 14:22:11 -05:00
CMS.registerPreviewTemplate("general", GeneralPreview);
< / script >
```
### Accessing Metadata
2019-04-16 14:15:10 +02:00
Preview Components also receive an additional prop: `fieldsMetaData` . It contains aditional information (besides the plain textual value of each field) that can be useful for preview purposes. For example, the Relation widget passes the whole selected relation data in `fieldsMetaData` .
2017-11-27 14:22:11 -05:00
```js
export default class ArticlePreview extends React.Component {
render() {
2018-08-14 11:33:13 -06:00
const {entry, fieldsMetaData} = this.props;
2017-11-27 14:22:11 -05:00
const author = fieldsMetaData.getIn(['authors', data.author]);
2017-12-09 03:28:57 -05:00
2018-08-14 11:33:13 -06:00
return < article > < h2 > { entry.getIn(['data', 'title']) }< / h2 >
{author & & < AuthorBio author = {author.toJS()}/ > }
< / article >
2017-11-27 14:22:11 -05:00
}
}
2020-02-13 02:12:36 +02:00
```