1

I'm having an issue with using custom theme. I'm using Spring Boot + Vaadin 8 and IntelliJ Idea + Gradle. I've created a file: src/main/webapp/VAADIN/themes/mytheme/styles.css

@import "../valo/styles.css";
.my-split { background: #a983ff; }

I've added @Theme("mytheme") to my UI class.

I've set the style name on my component: gridsSplitPane.addStyleName("my-split");

And now when I run my app it seems that the mytheme gets loaded because the background is changed correctly but everything else has no CSS styling - as if the import didn't work: @import "../valo/styles.css"

What am I missing here?

I've seen some information that a theme needs to be compiled somehow. Can anyone help me to compile it with Gradle + IntelliJ Idea? Is it also required for plain css customizations like mine?

When I use @Theme("valo") instead - everything is loaded correctly, so I'm guessing that all the Valo theme files are loaded correctly from: vaadin-themes-8.1.0.jar!/VAADIN/themes/valo/...

build.gradle

buildscript {
    ext { springBootVersion = '1.5.6.RELEASE' }
    repositories { mavenCentral() }
    dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") }
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'

version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories { mavenCentral() }

ext { vaadinVersion = '8.1.0' }

dependencies {
    compile('com.vaadin:vaadin-spring-boot-starter')

    compile("org.springframework.boot:spring-boot-starter-logging")
    compile('org.springframework.boot:spring-boot-starter-web')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

dependencyManagement {
    imports { mavenBom "com.vaadin:vaadin-bom:${vaadinVersion}" }
}

AppMain.java

package example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class AppMain {
    public static void main(String[] args) {
        SpringApplication.run(AppMain.class, args);
    }
}

MyUI.java

package example;

import com.vaadin.annotations.Theme;
import com.vaadin.server.VaadinRequest;
import com.vaadin.spring.annotation.SpringUI;
import com.vaadin.ui.Label;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;

@Theme("mytheme")
@SpringUI(path = "/ui")
public class MyUI extends UI {
    @Override
    protected void init(VaadinRequest request) {
        Label label = new Label("Customizing theme.");
        label.setStyleName("my-label");

        VerticalLayout root = new VerticalLayout();
        root.addComponent(label);
        root.setSizeFull();
        setContent(root);
    }
}
alwi
  • 431
  • 6
  • 21
  • Have you recompiled the themes? In IntelliJ, open the Maven Projects panel, open 'Vaadin' and run 'vaadin:compile-theme'. Then run your project again. – Thibstars Sep 12 '17 at 09:14
  • I'm using Gradle instead of Maven and cannot find any useful information how to proceed with that using Gradle... – alwi Sep 12 '17 at 09:22
  • I'm sorry, I missed the Gradle part. There must be a solution. Could you maybe share your build.gradle setup? – Thibstars Sep 12 '17 at 09:27
  • 1
    I've updated the post with some example files that can be used to reproduce. – alwi Sep 12 '17 at 09:46
  • 1
    I've been looking around a bit, but couldn't really find a clear solution on how to compile the theme using Gradle. However, there is a command line solution that you could try. Official Vaadin doc: https://vaadin.com/docs/-/part/framework/themes/themes-compiling.html – Thibstars Sep 12 '17 at 10:21
  • I've tried creating SCSS and compiling it but no luck. Although the `styles.css` file was generated successfully, the whole page still displays as if there was no styling for it. I used command: `java -cp vaadin-sass-compiler-0.9.13.jar;vaadin-themes-8.1.0.jar;. com.vaadin.sass.SassCompiler src/main/webapp/VAADIN/themes/mytheme/styles.scss src/main/webapp/VAADIN/themes/mytheme/styles.css` and utilized example from vaadin documentation for `styles.scss`, containing just: `@import "../valo/styles.scss"; @mixin mytheme { @include valo; }` – alwi Sep 12 '17 at 12:10
  • As far as I know (I have a gradle project running with custom theme) you do not need to compile themes. This is done by Vaadin at runtime if you use scss files. – Steffen Harbich Sep 13 '17 at 08:57
  • What you're saying is in line with the documentation however If I don't compile SCSS and want to rely on on-the-fly generation by simply putting `styles.scss` in proper location (as mentioned earlier in post & comment), then it is not working too and I'm getting standard error output: `Sep 13, 2017 9:08:22 AM org.atmosphere.util.IOUtils guestRawServletPath WARNING: More than one Servlet Mapping defined. WebSocket may not work org.apache.catalina.core.ApplicationServletRegistration`. Could you please try to compile and run my example files and see what happens? Are you using Spring too? – alwi Sep 13 '17 at 09:00
  • Although the documentation says about "Plain old CSS themes", I wonder how this could work because the compiled Valo CSS styles are all prefixed with ".valo" selector which I think will never be set as style class to the main div element when switching to other themes like "mytheme". – Steffen Harbich Sep 13 '17 at 09:06
  • The log you posted is just a warning. Maybe try the default context path (not "/ui") and removing other dependencies than Vaadin, just to narrow the error. – Steffen Harbich Sep 13 '17 at 09:08
  • Can you post the compiled SCSS result CSS? – Steffen Harbich Sep 13 '17 at 09:09
  • I got it working with your example with some modification, I will post it as answer. – Steffen Harbich Sep 13 '17 at 09:28
  • Thanks a lot! It works. The mentioned warning appears when I add `com.vaadin:vaadin-push` dependency. I still have no clue how to fix it... – alwi Sep 13 '17 at 10:36
  • 1
    You are welcome. Please open a new question for that issue if you need help. I did not work with Vaadin push so far. – Steffen Harbich Sep 13 '17 at 11:17
  • I've done that. If anybody can help here I'd appreciate it since I'm still pretty much evaluating Vaadin and Spring in tandem and just going through tutorials, see here: https://stackoverflow.com/questions/46113772/vaadin-spring-boot-errors-cannot-enhance-configuration-and-more-than-one – alwi Sep 13 '17 at 19:52

2 Answers2

2

Here a quick tutorial

"How to use a custom theme with Vaadin 8 and SPRING boot application"

  1. Create a Vaadin Application with Spring BOOT, sbt, SCALA and Vaadin8

  2. Create a new Vaadin application using maven archetype

Maven -> [x] Create from archetype -> com.vaadin:vaadin-archetype-application -> vaadin-archetype-application:8.0.0

crate vaadin app from archetype

  1. Copy vaadin custom theme from maven archetype app to your SPRING Vaadin 8 app:

Copy entire VAADIN directory (src/main/webapp/VAADIN/themes/mytheme)

to

src/main/resources/VAADIN/themes/mytheme

copy vaadin directory

  1. Change

@Theme("mytheme") public class MyUI extends UI { }

  1. [OPTIONAL] Change the name of the theme to your liking

Rename:

VAADIN/themes/mytheme/mytheme.scss -> othertheme.scss
VAADIN/themes/mytheme/mytheme.scss -> change mixin name @mixin othertheme { ... }
VAADIN/themes/mytheme/styles.scss -> change @import "othertheme.scss" | .othertheme{} | @include othertheme;

More info in these screencasts:

video Introduction to Vaadin - Your first application with Framework 8

video Community Answer: Creating new Vaadin Themes in Spring Applications

1

You were close to get it working. To use a custom SCSS theme, put the following in the styles.scss:

@import "../valo/valo";

.mytheme {
    @include valo;
}

The basic difference with the content you mentioned in the comments is the way to include Valo in your theme.

EDIT

Your original question was about CSS theme. As far as I can see, CSS themes shouldn't work with including Valo because most styles are prefixed with .valo CSS selector and switching to your custom theme will add the CSS class mytheme instead of default valo to the main div element. I even tested to add the valo CSS class in the final HTML (Firefox inspector) which then shows styles as expected. Feels like a Vaadin bug. So, I recommend using SCSS themes which also have more features than CSS.

Steffen Harbich
  • 2,639
  • 2
  • 37
  • 71