Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keystonejs: customize Keystonejs default Admin UI theme

I try to customize Keystonejs Admin UI theme: change primary colors, etc.

So I'm going to override keystone.less which is located in node_modules:

.
|____node_modules
| |____keystone
| | |____public
| | | |____styles
| | | | |____keystone.less

|____public
| |____styles
| | |____keystone.less // This will overwrite the UI

New keystone.less content:

// Elemental
// ------------------------------

@import "@{elementalPath}/less/elemental.less";


// KEYSTONE VARIABLES
@import "../../node_modules/keystone/admin/public/styles/variables.less";

// KEYSTONE AUTH
@import "../../node_modules/keystone/admin/public/styles/auth.less";

// KEYSTONE SETUP
@import "../../node_modules/keystone/admin/public/styles/keystone/animation.less";
@import "../../node_modules/keystone/admin/public/styles/keystone/base.less";
@import "../../node_modules/keystone/admin/public/styles/keystone/dashboard.less";
@import "../../node_modules/keystone/admin/public/styles/keystone/forms.less";
@import "../../node_modules/keystone/admin/public/styles/keystone/navigation.less";
@import "../../node_modules/keystone/admin/public/styles/keystone/tables.less";
@import "../../node_modules/keystone/admin/public/styles/keystone/utils.less";

@import "../../node_modules/keystone/admin/public/styles/keystone/list.less";
@import "../../node_modules/keystone/admin/public/styles/keystone/list-dropzone.less";
@import "../../node_modules/keystone/admin/public/styles/keystone/item.less";
@import "../../node_modules/keystone/admin/public/styles/keystone/toolbar.less";

// COMPONENTS
@import "../../node_modules/keystone/admin/public/styles/keystone/wysiwyg.less";
@import "../../node_modules/keystone/admin/public/styles/keystone/popout.less";


// REACT FIELDS
@import "../../node_modules/keystone/admin/public/styles/react/react.less";


@gray-base:              #000;
@gray-darker:            lighten(@gray-base, 13.5%); // #222
@gray-dark:              lighten(@gray-base, 20%);   // #333
@gray:                   lighten(@gray-base, 33.5%); // #555
@gray-light:             lighten(@gray-base, 46.7%); // #777
@gray-lighter:           lighten(@gray-base, 93.5%); // #eee

@brand-primary:         darken(#d326f6, 6.5%); // #337ab7
@brand-success:         #5cb85c;
@brand-info:            #d326f6;
@brand-warning:         #f0ad4e;
@brand-danger:          #d9534f;

@navbar-default-color:  green;

.primary-navbar {
  background-color: #d326f6;
  color: #d326f6;
  padding-bottom: .5em;
  padding-top: .5em;
}

I added less dependency in package.json:

"less": "2.7.2"

As described in doc:

If you want Keystone to automatically compile .less files into .css files, set this value to the same path as the static option. When this option is set, any requests to a .css or .min.css file will first check for a .less file with the same name, and if one is found, the css file will be generated.

So I added option to keystone.init:

'less': ['public', 'public-app'],

But when I build and deploy application I cannot see any changes in keystone.min.css

No changes in styles.

What have I missed?

like image 346
Bo. Avatar asked Dec 06 '17 16:12

Bo.


2 Answers

For my solution I did following:

I added: extensible theme.less file:

|____public
| |____styles
| | |____theme.less 

theme.less: content:

@import "../../node_modules/keystone/admin/public/styles/keystone.less";

@brand-primary:         darken(#d326f6, 6.5%); // #337ab7
@brand-success:         #5cb85c;
@brand-info:            #d326f6;
@brand-warning:         #f0ad4e;
@brand-danger:          #d9534f;

@link-color: #9c27b0;

.primary-navbar {
  background-color: #9c27b0;
  color: white;
  padding-bottom: .5em;
  padding-top: .5em;
}

Add postinstall script in package.json:

"scripts": {
    "postinstall": "node scripts/admin-ui.js"
  } 

Script will add import theme.less string to node_modules/.../keystone.less

Script content:

const fs = require('fs');
const buff = fs.readFileSync('node_modules/keystone/admin/public/styles/keystone.less');
const content = buff.toString();

const styleLink = '@import "../../../../../public/styles/theme.less";';

if (content.includes(styleLink)) {
    return;
}

const newContent = content + '\n' + styleLink;
fs.writeFileSync('node_modules/keystone/admin/public/styles/keystone.less', newContent);

It will be utomatically on each npm i

So by this way styles will be overriden.

like image 187
Bo. Avatar answered Oct 16 '22 03:10

Bo.


You can apply your custom styles in ./keystone.js file:

keystone.set('adminui custom styles', './public/styles/keystone.less');
like image 4
Ihor Sakailiuk Avatar answered Oct 16 '22 02:10

Ihor Sakailiuk