<h1id="creating-a-custom-layout">Creating a custom layout</h1>
<p><strong>Layouts</strong> are a special type of component that Swagger-UI uses as the root component for the entire application. You can define custom layouts in order to have high-level control over what ends up on the page.</p>
<p>By default, Swagger-UI uses <code>BaseLayout</code>, which is built into the application. You can specify a different layout to be used by passing the layout's name as the <code>layout</code> parameter to Swagger-UI. Be sure to provide your custom layout as a component to Swagger-UI.</p>
<p><br></p>
<p>For example, if you wanted to create a custom layout that only displayed operations, you could define an <code>OperationsLayout</code>:</p>
<h3id="augmenting-the-default-layout">Augmenting the default layout</h3>
<p>If you'd like to build around the <code>BaseLayout</code> instead of replacing it, you can pull the <code>BaseLayout</code> into your custom layout and use it:</p>
<spanclass="hljs-tag"><<spanclass="hljs-name">h1</span>></span>I have a custom header above Swagger-UI!<spanclass="hljs-tag"></<spanclass="hljs-name">h1</span>></span>
gitbook.page.hasChanged({"page":{"title":"Creating a Custom Layout","level":"3.2","depth":1,"next":{"title":"Plugin API","level":"3.3","depth":1,"path":"customization/plugin-api.md","ref":"customization/plugin-api.md","articles":[]},"previous":{"title":"Overview","level":"3.1","depth":1,"path":"customization/overview.md","ref":"customization/overview.md","articles":[]},"dir":"neutral"},"config":{"plugins":["livereload"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Swagger-UI","gitbook":"*"},"file":{"path":"customization/custom-layout.md","mtime":"2017-10-05T23:29:46.881Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2017-10-06T04:20:18.617Z"},"basePath":"..","book":{"language":""}});
gitbook.page.hasChanged({"page":{"title":"Creating a custom layout","level":"3.2","depth":1,"next":{"title":"Plugin API","level":"3.3","depth":1,"path":"customization/plugin-api.md","ref":"customization/plugin-api.md","articles":[]},"previous":{"title":"Overview","level":"3.1","depth":1,"path":"customization/overview.md","ref":"customization/overview.md","articles":[]},"dir":"ltr"},"config":{"plugins":["livereload"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Swagger-UI","gitbook":"*"},"file":{"path":"customization/custom-layout.md","mtime":"2017-10-06T21:59:49.674Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2017-10-06T04:20:18.617Z"},"basePath":"..","book":{"language":""}});
<p>In the following documentation, we won't take the time to define the fundamentals covered in the resources above.</p>
<blockquote>
<p><strong>Note</strong>: Some of the examples in this section contain JSX, which is a syntax extension to JavaScript that is useful for writing React components.</p>
<p>If you don't want to set up a build pipeline capable of translating JSX to JavaScript, take a look at <ahref="https://reactjs.org/docs/react-without-jsx.html"target="_blank">React without JSX (reactjs.org)</a>.</p>
</blockquote>
<h3id="the-system">The System</h3>
<p>The <em>system</em> is the heart of the Swagger-UI application. At runtime, it's a JavaScript object that holds many things:</p>
<ul>
...
...
@@ -345,6 +349,12 @@
]
})
</code></pre>
<h3id="getcomponent">getComponent</h3>
<p><code>getComponent</code> is a helper function injected into every container component, which is used to get references to components provided by the plugin system.</p>
<p>All components should be loaded through <code>getComponent</code>, since it allows other plugins to modify the component. It is preferred over a conventional <code>import</code> statement.</p>
<p>Container components in Swagger-UI can be loaded by passing <code>true</code> as the second argument to <code>getComponent</code>, like so:</p>
</code></pre><p>This will map the current system as props to the component.</p>
</section>
...
...
@@ -377,7 +387,7 @@
</a>
<ahref="custom-layout.html"class="navigation navigation-next "aria-label="Next page: Creating a Custom Layout">
<ahref="custom-layout.html"class="navigation navigation-next "aria-label="Next page: Creating a custom layout">
<iclass="fa fa-angle-right"></i>
</a>
...
...
@@ -388,7 +398,7 @@
<script>
vargitbook=gitbook||[];
gitbook.push(function(){
gitbook.page.hasChanged({"page":{"title":"Overview","level":"3.1","depth":1,"next":{"title":"Creating a Custom Layout","level":"3.2","depth":1,"path":"customization/custom-layout.md","ref":"customization/custom-layout.md","articles":[]},"previous":{"title":"Version detection","level":"2.3","depth":1,"path":"usage/version-detection.md","ref":"usage/version-detection.md","articles":[]},"dir":"ltr"},"config":{"plugins":["livereload"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Swagger-UI","gitbook":"*"},"file":{"path":"customization/overview.md","mtime":"2017-10-06T07:15:00.038Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2017-10-06T04:20:18.617Z"},"basePath":"..","book":{"language":""}});
gitbook.page.hasChanged({"page":{"title":"Overview","level":"3.1","depth":1,"next":{"title":"Creating a custom layout","level":"3.2","depth":1,"path":"customization/custom-layout.md","ref":"customization/custom-layout.md","articles":[]},"previous":{"title":"Version detection","level":"2.3","depth":1,"path":"usage/version-detection.md","ref":"usage/version-detection.md","articles":[]},"dir":"ltr"},"config":{"plugins":["livereload"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Swagger-UI","gitbook":"*"},"file":{"path":"customization/overview.md","mtime":"2017-10-06T22:21:23.876Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2017-10-06T04:20:18.617Z"},"basePath":"..","book":{"language":""}});
<p>The Action interface enables the creation of new Redux action creators within a piece of state in the Swagger-UI system.</p>
<p>This action creator function will be bound to the <code>example</code> reducer dispatcher and exposed to container components as <code>exampleActions.updateFavoriteColor</code>.</p>
<p>For more information about the concept of actions in Redux, see the <ahref="http://redux.js.org/docs/basics/Actions.html"target="_blank">Redux Actions documentation</a>.</p>
<h5id="reducers">Reducers</h5>
<p>Reducers take a state (which is an Immutable map) and an action, and return a new state.</p>
<p>Reducers must be provided to the system under the name of the action type that they handle, in this case, <code>MYPLUGIN_UPDATE_SOMETHING</code>.</p>
<spanclass="hljs-comment">// we're updating the Immutable state object...</span>
<spanclass="hljs-comment">// we need to convert vanilla objects into an immutable type (fromJS)</span>
<spanclass="hljs-comment">// See immutable docs about how to modify the state</span>
<spanclass="hljs-comment">// PS: you're only working with the state under the namespace, in this case "example".</span>
<spanclass="hljs-comment">// So you can do what you want, without worrying about /other/ namespaces</span>
}
}
}
}
}
}
</code></pre>
<h5id="selectors">Selectors</h5>
<p>Selectors take any number of functions as arguments, and passes all results to the last function.</p>
<p>They're an easy way to keep logic for getting data out of state in one place, and is preferred over passing state data directly into components.</p>
<p>See <ahref="https://github.com/reactjs/reselect#createselectorinputselectors--inputselectors-resultfunc"target="_blank">Reselect: <code>createSelector</code></a> for more information.</p>
state => state.get(<spanclass="hljs-string">"something"</span>) <spanclass="hljs-comment">// return the whatever "something" points to</span>
)
}
}
}
}
}
</code></pre>
<h5id="components">Components</h5>
<p>You can provide a map of components to be integrated into the system.</p>
<p>Be mindful of the key names for the components you provide, as you'll need to use those names to refer to the components elsewhere.</p>
<p>Wrap Actions allow you to override the behavior of an action in the system.</p>
<p>This interface is very useful for building custom behavior on top of builtin actions.</p>
<p>A Wrap Action's first argument is <code>oriAction</code>, which is the action being wrapped. It is your responsibility to call the <code>oriAction</code> - if you don't, the original action will not fire!</p>
<p>Wrap Selectors allow you to override the behavior of a selector in the system.</p>
<p>They are function factories with the signature <code>(oriSelector, system) => (...args) => result</code>.</p>
<p>This interface is useful for controlling what data flows into components. We use this in the core code to disable selectors based on the API definition's version.</p>
NumberDisplay: ({ number }) =><spanclass="xml"><spanclass="hljs-tag"><<spanclass="hljs-name">span</span>></span>{number}<spanclass="hljs-tag"></<spanclass="hljs-name">span</span>></span></span>
<spanclass="hljs-tag"><<spanclass="hljs-name">h3</span>></span>Warning! Big number ahead.<spanclass="hljs-tag"></<spanclass="hljs-name">h3</span>></span>
**Layouts** are a special type of component that Swagger-UI uses as the root component for the entire application. You can define custom layouts in order to have high-level control over what ends up on the page.
By default, Swagger-UI uses `BaseLayout`, which is built into the application. You can specify a different layout to be used by passing the layout's name as the `layout` parameter to Swagger-UI. Be sure to provide your custom layout as a component to Swagger-UI.
<br>
For example, if you wanted to create a custom layout that only displayed operations, you could define an `OperationsLayout`:
```js
importReactfrom"react"
// Create the layout component
classOperationsLayoutextendsReact.Component{
render(){
const{
getComponent
}=this.props
constOperations=getComponent("Operations",true)
return{
<div>
<Operations/>
</div>
}
}
}
// Create the plugin that provides our layout component
constOperationsLayoutPlugin=function(){
return{
components:{
OperationsLayout:OperationsLayout
}
}
}
// Provide the plugin to Swagger-UI, and select OperationsLayout
// as the layout for Swagger-UI
SwaggerUI({
url:"http://petstore.swagger.io/v2/swagger.json",
plugins:[OperationsLayoutPlugin],
layout:"OperationsLayout"
})
```
### Augmenting the default layout
If you'd like to build around the `BaseLayout` instead of replacing it, you can pull the `BaseLayout` into your custom layout and use it:
```js
importReactfrom"react"
// Create the layout component
classAugmentingLayoutextendsReact.Component{
render(){
const{
getComponent
}=this.props
constBaseLayout=getComponent("BaseLayout",true)
return{
<div>
<divclassName="myCustomHeader">
<h1>IhaveacustomheaderaboveSwagger-UI!</h1>
</div>
<BaseLayout/>
</div>
}
}
}
// Create the plugin that provides our layout component
constAugmentingLayoutPlugin=function(){
return{
components:{
AugmentingLayout:AugmentingLayout
}
}
}
// Provide the plugin to Swagger-UI, and select AugmentingLayout
In the following documentation, we won't take the time to define the fundamentals covered in the resources above.
> **Note**: Some of the examples in this section contain JSX, which is a syntax extension to JavaScript that is useful for writing React components.
>
> If you don't want to set up a build pipeline capable of translating JSX to JavaScript, take a look at [React without JSX (reactjs.org)](https://reactjs.org/docs/react-without-jsx.html).
### The System
The _system_ is the heart of the Swagger-UI application. At runtime, it's a JavaScript object that holds many things:
...
...
@@ -51,3 +55,17 @@ SwaggerUI({
]
})
```
### getComponent
`getComponent` is a helper function injected into every container component, which is used to get references to components provided by the plugin system.
All components should be loaded through `getComponent`, since it allows other plugins to modify the component. It is preferred over a conventional `import` statement.
Container components in Swagger-UI can be loaded by passing `true` as the second argument to `getComponent`, like so:
```
getComponent("ContainerComponentName", true)
```
This will map the current system as props to the component.
The Action interface enables the creation of new Redux action creators within a piece of state in the Swagger-UI system.
This action creator function will be bound to the `example` reducer dispatcher and exposed to container components as `exampleActions.updateFavoriteColor`.
...
...
@@ -79,14 +87,206 @@ For more information about the concept of actions in Redux, see the [Redux Actio
##### Reducers
Reducers take a state (which is an Immutable map) and an action, and return a new state.
Reducers must be provided to the system under the name of the action type that they handle, in this case, `MYPLUGIN_UPDATE_SOMETHING`.
// we need to convert vanilla objects into an immutable type (fromJS)
// See immutable docs about how to modify the state
// PS: you're only working with the state under the namespace, in this case "example".
// So you can do what you want, without worrying about /other/ namespaces
}
}
}
}
}
}
```
##### Selectors
Selectors take any number of functions as arguments, and passes all results to the last function.
They're an easy way to keep logic for getting data out of state in one place, and is preferred over passing state data directly into components.
See [Reselect: `createSelector`](https://github.com/reactjs/reselect#createselectorinputselectors--inputselectors-resultfunc) for more information.
```js
constMySelectorPlugin=function(system){
return{
statePlugins:{
myPlugin:{
selectors:{
something:createSelector(
state=>state.get("something")// return the whatever "something" points to
)
}
}
}
}
}
```
##### Components
You can provide a map of components to be integrated into the system.
Be mindful of the key names for the components you provide, as you'll need to use those names to refer to the components elsewhere.
```js
constMyComponentPlugin=function(system){
return{
components:{
// components can just be functions
HelloWorld:()=><h1>HelloWorld!</h1>
}
}
}
```
```js
// elsewhere
constHelloWorld=getComponent("HelloWorld")
```
##### Wrap-Actions
Wrap Actions allow you to override the behavior of an action in the system.
This interface is very useful for building custom behavior on top of builtin actions.
A Wrap Action's first argument is `oriAction`, which is the action being wrapped. It is your responsibility to call the `oriAction` - if you don't, the original action will not fire!
```js
constMySpecPlugin=function(system){
return{
statePlugins:{
spec:{
actions:{
updateSpec:(str)=>{
return{
type:"SPEC_UPDATE_SPEC",
payload:str
}
}
}
}
}
}
}
// this plugin allows you to watch changes to the spec that is in memory
constMyWrapActionPlugin=function(system){
return{
statePlugins:{
spec:{
wrapActions:{
updateSpec:function(oriAction,str){
doSomethingWithSpecValue(str)
returnoriAction(str)// don't forget!
}
}
}
}
}
}
```
##### Wrap-Selectors
Wrap Selectors allow you to override the behavior of a selector in the system.
They are function factories with the signature `(oriSelector, system) => (...args) => result`.
This interface is useful for controlling what data flows into components. We use this in the core code to disable selectors based on the API definition's version.
```js
import{createSelector}from'reselect'
constMySpecPlugin=function(system){
return{
statePlugins:{
spec:{
selectors:{
someData:createSelector(
state=>state.get("something")
)
}
}
}
}
}
constMyWrapSelectorsPlugin=function(system){
return{
statePlugins:{
spec:{
wrapSelectors:{
someData:(oriSelector,system)=>(...args)=>{
// you can do other things here...
// but let's just enable the default behavior
returnoriSelector(...args)
}
}
}
}
}
}
```
##### Wrap-Components
Wrap Components allow you to override a component registered within the system.
Wrap Components are function factories with the signature `(OriginalComponent, system) => props => ReactElement`.
```js
constMyNumberDisplayPlugin=function(system){
return{
components:{
NumberDisplay:({number})=><span>{number}</span>
}
}
}
constMyWrapComponentPlugin=function(system){
return{
wrapComponents:{
NumberDisplay:(Original,system)=>(props)=>{
if(props.number>10){
return<div>
<h3>Warning!Bignumberahead.</h3>
</div>
}else{
return<Original{...props}/>
}
}
}
}
}
```
##### fn
The fn interface allows you to add helper functions to the system for use elsewhere.