How to Modify the StrongLoop's LoopBack Explorer CSS
Asked Answered
M

5

14

We're using Strongloop's LoopBack for our REST APIs and would like to modify the CSS for the LoopBack Explorer. However, it's not clear which CSS files are being used (LoopBack vs Swagger) and where they're located. I was not able to find specific documentation for this.

Maemaeander answered 21/1, 2015 at 17:34 Comment(0)
S
11

You can provide your own version of Swagger UI files via options.uiDirs.

  1. Edit your server/server.js and add this config option to the explorer:

    app.use(explorer(app, { uiDirs: path.resolve(__dirname, 'explorer') }));
    
  2. Copy the directory node_modules/loopback-explorer/public/css to server/explorer/css

  3. Customize the copied CSS files as you need.

You should lock loopback-explorer's major & minor version in your package.json. Newer versions of loopback-explorer may change the CSS in which case your customization may stop working.

Shennashensi answered 22/1, 2015 at 12:34 Comment(6)
Miroslav's answer is correct. I deleted mine since it's incorrect.Extent
I did this in server js file and add dir server/explorer/all public files app.use(explorer(app, { uiDirs: path.resolve(__dirname, 'explorer') })); Ui is changed but there is no API.?Lyre
@Lyre i am also getting same issue, how you resolved this issueHernadez
I get following error after adapting this change TypeError: app.use() requires a middleware functionTicino
@Think-Twice see github.com/strongloop/loopback-component-explorer/blob/master/…. You need to use explorer.routes() instead of explorer() now.Senegal
@MiroslavBajtoš Thanks for your reply. I have applied custom css styles as mentioned in my newly added answer under this questionTicino
T
13

You can modify more than just the css. And also, if you generate your Loopback application using slc loopback like I did, you'll find that your server/server.js doesn't look immediately in a way you can configure it like it's shown in the accepted answer.

Instead you can use server/component-config.json to instruct the loopback component explorer to use an alternative directory for static files for the swagger-ui. With the uiDirs configuration below I configured it to go look for static files in the server/explorer directory.

{
  "loopback-component-explorer": {
    "mountPath": "/explorer",
    "uiDirs": "server/explorer",
    "apiInfo": {
      "title": "My API",
      "description": "Description of my API"
    }
  }
}

* When using IISNode uiDirs has to be set to "explorer" otherwise it's "server/explorer" as per @phegde 's comment

In my server directory I created a index.html which is a copy from node_modules/loopback-component-explorer/public/index.html and also I created an images folder with a custom logo.

enter image description here

And finally, If you want to have custom css, copy node_modules/loopback-component-explorer/public/css/loopbackStyles.css into server/explorer/css/loopbackStyles.css

Tiliaceous answered 28/9, 2016 at 7:52 Comment(4)
With me it does. If you want help, do an analyses and ask a question on SO.Tiliaceous
if explorer folder is inside the server, the uiDirs must be "server/explorer". This was the fix.Privatdocent
@phegde I updated my answer to reflect that. uiDirs had to be "explorer" when using iisnode. Otherwise, it's what you said.Tiliaceous
Color has changed but I am unable to override the logo nameTicino
S
11

You can provide your own version of Swagger UI files via options.uiDirs.

  1. Edit your server/server.js and add this config option to the explorer:

    app.use(explorer(app, { uiDirs: path.resolve(__dirname, 'explorer') }));
    
  2. Copy the directory node_modules/loopback-explorer/public/css to server/explorer/css

  3. Customize the copied CSS files as you need.

You should lock loopback-explorer's major & minor version in your package.json. Newer versions of loopback-explorer may change the CSS in which case your customization may stop working.

Shennashensi answered 22/1, 2015 at 12:34 Comment(6)
Miroslav's answer is correct. I deleted mine since it's incorrect.Extent
I did this in server js file and add dir server/explorer/all public files app.use(explorer(app, { uiDirs: path.resolve(__dirname, 'explorer') })); Ui is changed but there is no API.?Lyre
@Lyre i am also getting same issue, how you resolved this issueHernadez
I get following error after adapting this change TypeError: app.use() requires a middleware functionTicino
@Think-Twice see github.com/strongloop/loopback-component-explorer/blob/master/…. You need to use explorer.routes() instead of explorer() now.Senegal
@MiroslavBajtoš Thanks for your reply. I have applied custom css styles as mentioned in my newly added answer under this questionTicino
O
0

If you didn't lock the loopback-explorer in package.json or if you started your application from the new release of loopback(v2.x), you have to make another change:

  1. If you generated your loopback app with the generator tool, edit server/component-config.json and change it to this:

    { "loopback-component-explorer": null }

2.Copy the directory node_modules/loopback-explorer/public/ to server/explorer/ as Miroslav said. If you copy the whole directory you can also change the index.html file.

  1. Edit server/server.js file and add this line: app.use('/explorer',explorer.routes(app, { uiDirs: path.resolve(__dirname, 'explorer') })); also you have to add the explorer module at the top of the file: var explorer = require('loopback-component-explorer');

4.Customize the ui of your explorer, all the necessary files are in server/explorer

Offcolor answered 17/9, 2016 at 9:2 Comment(0)
P
0

With the loopback-component-explorer the uiDirs defined in component-config.json should be added something like the below (which solved my issue).

"uiDirs": ["server/explorer"]

instead of

"uiDirs": "server/api-explorer",
Particolored answered 30/9, 2017 at 20:59 Comment(0)
T
0

I am able to apply custom css styles to loopback api explorer header.

Steps I followed as mentioned below

  1. Goto node_modules > loopback-component-explorer > public > css folder
  2. Copy loopbackStyles.css
  3. Create a new folder called explorer under server folder
  4. create css folder under explorer and paste the css file under css folder i.e., loopbackStyles.css
  5. Add below config to component-config.json file
{
  "loopback-component-explorer": {
    "mountPath": "/explorer",
    "generateOperationScopedModels": true,
    "uiDirs": "server/explorer"
  }
}

To change loopback header color I have just overrided backgroun-color with my own color in body #header css selector in loopbackStyles.css

To replace the default header logo name with our custom name. I have added following css styles in loopbackStyles.css

.swagger-ui-wrap #logo{
  display: none;
}
.swagger-ui-wrap:after { 
  content: "MyOwn API Explorer";
  color: #fff;
  font-weight: bold; 
}
Ticino answered 1/10, 2018 at 7:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.