You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

custom-layout.md 2.3 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # Creating a custom layout
  2. **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.
  3. 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.
  4. <br>
  5. For example, if you wanted to create a custom layout that only displayed operations, you could define an `OperationsLayout`:
  6. ```js
  7. import React from "react"
  8. // Create the layout component
  9. class OperationsLayout extends React.Component {
  10. render() {
  11. const {
  12. getComponent
  13. } = this.props
  14. const Operations = getComponent("operations", true)
  15. return (
  16. <div>
  17. <Operations />
  18. </div>
  19. )
  20. }
  21. }
  22. // Create the plugin that provides our layout component
  23. const OperationsLayoutPlugin = () => {
  24. return {
  25. components: {
  26. OperationsLayout: OperationsLayout
  27. }
  28. }
  29. }
  30. // Provide the plugin to Swagger-UI, and select OperationsLayout
  31. // as the layout for Swagger-UI
  32. SwaggerUI({
  33. url: "https://petstore.swagger.io/v2/swagger.json",
  34. plugins: [ OperationsLayoutPlugin ],
  35. layout: "OperationsLayout"
  36. })
  37. ```
  38. ### Augmenting the default layout
  39. 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:
  40. ```js
  41. import React from "react"
  42. // Create the layout component
  43. class AugmentingLayout extends React.Component {
  44. render() {
  45. const {
  46. getComponent
  47. } = this.props
  48. const BaseLayout = getComponent("BaseLayout", true)
  49. return (
  50. <div>
  51. <div className="myCustomHeader">
  52. <h1>I have a custom header above Swagger-UI!</h1>
  53. </div>
  54. <BaseLayout />
  55. </div>
  56. )
  57. }
  58. }
  59. // Create the plugin that provides our layout component
  60. const AugmentingLayoutPlugin = () => {
  61. return {
  62. components: {
  63. AugmentingLayout: AugmentingLayout
  64. }
  65. }
  66. }
  67. // Provide the plugin to Swagger-UI, and select AugmentingLayout
  68. // as the layout for Swagger-UI
  69. SwaggerUI({
  70. url: "https://petstore.swagger.io/v2/swagger.json",
  71. plugins: [ AugmentingLayoutPlugin ],
  72. layout: "AugmentingLayout"
  73. })
  74. ```