3

I am currently trying to add custom style to a panel and the following code works, but in fact is really ugly and non reusable.

In which folder and file should I write the corresponding css class and what's the correct way to bind it to this component?.

Note that I'm trying to define a style for every component of this type.

Ext.define('MyProject.view.main.Main', {

  extend: 'Ext.container.Container',

  style: 'margin: 0; padding: 0; border: 0; text-align: center; background: #ecf0f1 url(resources/bg.jpg) no-repeat center 0;',

...

});
Alejandro Morán
  • 669
  • 1
  • 13
  • 36

3 Answers3

5

Here is an example of creating a Custom Component and Its Styling

MyApp/view/main/AppMenu.js

Ext.define('MyApp.view.main.AppMenu', {
    extend: 'Ext.Panel',
    xtype: 'app-menu',
    alias: 'widget.app-menu',
    config: {

        cls: 'app-menu'
    }
}

SCSS File, the path should match the Component Path. MyApp/sass/src/view/main/AppMenu.scss

.app-menu {
    background-color: #fafafa;
    .x-panel-inner {
        border: 0px !important;
    }
    .profile-info {
       font-size: 2em;
    }
}

You need build the app after changing the scss file using "sencha app build"

JChap
  • 1,421
  • 9
  • 23
  • Best answer so far! thank you!. How can I detect when I should use the !important clause or not? – Alejandro Morán Mar 11 '16 at 07:09
  • 1
    IMO, `!important` is a sign of laziness. Lee Boonstra, at SenchaCon RoadShow 2015, gave the rule of thumb that you shouldn't use the `!important` clause at all. If required, it is better to use nesting to reach precedence. More deeply nested (=special) entries have precedence over less deeply nested entries. E.g. `.x-btn-small.x-btn-default` takes precedence over `.app-menu`, but not over `.app-menu.x-btn-small.x-btn-default`. – Alexander Mar 11 '16 at 11:50
0

You can use a personal css class to add to your container, use the config cls.

cls: An optional extra CSS class that will be added to this component's Element. The value can be a string, a list of strings separated by spaces, or an array of strings. This can be useful for adding customized styles to the component or any of its children using standard CSS rules.

Defaults to: ''

Adding a personal css class you can overwrite css style properties that you're giving using the style config. If you need to set again these properties you can simply use the component css class again every time you need.

Generate a custom theme for your application or add to the used theme the css. To create a theme read here: https://docs.sencha.com/extjs/5.1/core_concepts/theming.html Same on Extjs 6

Example of cls: my css class uppercase simply set the text to uppercase

{
                    xtype: 'textfield',
                    validator: function(value) {
                        errMsg = "Campo obbligatorio, sostituibile da uno spazio";
                        return (value !== '') ? true : errMsg;
                    },
                    listeners: {
                        beforerender: 'onTextfieldFocus1',
                        change: 'onTextfieldChange'
                    },
                    anchor: '90%',
                    fieldLabel: 'Indirizzo',
                    name: 'Indirizzo',
                    fieldCls: 'x-form-field uppercase',
                    enableKeyEvents: true,
                    enforceMaxLength: true,
                    maxLength: 50
                }

so, to the component base cls x-form-field, i added the css class uppercase, simply extjs while Printing the component html content will assign to that two classes instead of one

In my case i used that css class to set to uppercase all the input fields of a single form, assigning to them the cls class

LellisMoon
  • 4,810
  • 2
  • 12
  • 24
  • I'm doing an override over the components to give them a custom style by overriding the package/local/myTheme/sass/src/, but I cannot override styles in the .scss file, that's what I mean. Otherwise, the use of "cls" property does not look very cool, you are doing modifications over the view in the code, isn't it? – Alejandro Morán Mar 10 '16 at 14:21
  • simply using cls you give to the component some css variables, obviously if already on the component there are some used variables the cls will remove them – LellisMoon Mar 10 '16 at 14:38
  • but only if the variables are the same – LellisMoon Mar 10 '16 at 14:38
  • or variables will be "merged" on the component – LellisMoon Mar 10 '16 at 14:38
  • about overriding the theme of your sass, i usually set a css class to use for the component, and if that's not working i put an !important. – LellisMoon Mar 10 '16 at 14:42
0

I do what I think Mr George is suggesting. If I want to style one component with CSS, I add cls to the component itself and use that as the selector in CSS. I like to use the xtype as the CSS classname

Ext.define('MyProject.view.main.Main', {
  xtype: 'myproject-main',    
  extend: 'Ext.container.Container',

  initComponent: function() {
    this.callParent();
    // This is better than cls on the prototype since it won't be overridden
    // If this class is extended or if the caller sets a `cls` config
    this.addClass(this.xtype); 
  }   
});

// CSS
.myproject-main {
  margin: 0;
  padding: 0;
  border: 0;
  text-align: center;
  background: #ecf0f1 url(resources/bg.jpg) no-repeat center 0;
}

.myproject-main-instance {
  color: red;
}

If you have additional styling per instance, you could follow the same pattern

var main1 = Ext.widget('myproject-main', {
    cls: 'myproject-main-instance'
});

See Extjs 5: how to make two panel with different style for further information

Community
  • 1
  • 1
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • Thank you, I think I have explained myself very bad, this is the opposite that I want, I want to design a component and give the common style for all the instances of that component, and my doubt is where that css should be placed (in the file structure). – Alejandro Morán Mar 10 '16 at 15:14
  • @AlejandroMorán The first example will make all instances of `MyProject.view.main.Main` look like `.myproject-main `, I'm not sure what you mean – Ruan Mendes Mar 10 '16 at 15:49