How can i apply CSS to components in VAADIN 8 - css

I am trying to apply CSS to Vaadin 8 component. I have followed this example and still unable to apply CSS. I understand that i can call the addStyleName method and i am able to apply the build in ValoTheme styles (for example ValoTheme.PANEL_BORDERLESS does make a button smaller), but my custom styles are ignored. I have tried defining my custom CSS rules in the following files:
/src/Main/webapp/VAADIN/themes/apptheme/styles.css
#import "../reindeer/styles.css";
.mystyle {
color: red;
background: #012345;
background-color: #012345;
}
Then in Java i create a button:
Button btn = new Button(" Test ");
btn.addStyleName("mystyle");
My custom style does not get applied to the button. I suspect that i am not defining CSS correctly. Please share your knowledge of how to do this correctly in Vaadin 8.

This is not related, but are you actually using a reindeer theme?
Otherwise, you should put you styles in your own theme file (the default one generated from an archetype is mytheme.scss)
It's suggested to leave styles.scss as it is. Also, it's mentioned in comments section of the file:
This file prefixes all rules with the theme name to avoid causing conflicts with other themes. The actual styles should be defined in mytheme.scss
If you want, you could add your styles there as well under
.mytheme {
#include addons;
#include mytheme;
.testStyle{
color: red;
background: #012345;
background-color: #012345;
}
}
While it works, I will still suggest to add them to your custom scss file (Based on your folder name it is apptheme.scss)
Mine mytheme.scss looks like this:
#import "../valo/valo.scss";
#mixin mytheme {
#include valo;
.testStyle{
color: red;
background: #012345;
background-color: #012345;
}
}
After styles are applied, button looks like this:
Style files are located under webapp/VAADIN/themes/mytheme

Related

How to change variable in bootstrap via app.scss

Currently I'm building a template that will be used by multiple people on different projects. So making this work instantly without changing things per project install is crutial.
For this instance I want to change the $spacer variable that is used for all the margings and paddings classes that Bootstrap offers. But I cant seem to figure out how to change the $spacer variable outside of the /node_modules. I have an own _variables.scss that creates variables for the theme but an !important or anything else wont work eventhough the custom _variables.scss is loaded later that the bootstrap from the node modules.
Is there a way to send a scss file to the node_modules file so it changes the variables from within? or is there a different way to overwrite a variable from the node modules?
I always work like this and no problem:
// File: my-bootstrap-and-styles.scss
// your overrides
$primary : #FEBC35;
// include bootstrap.scss
#import "../node_modules/bootstrap/scss/bootstrap";
// Then add your additional custom code here
.my-primary-div {
background: $primary;
border: 1px solid black;
}
// also don't forget to take advantage
// of bootstrap's variables and mixins
#include media-breakpoint-down(md) {
.my-class {
overflow-y: auto;
}
}

CSS Modules Breaking in Production with Create React App

I'm using css modules in a create-react-app project (React V17.0.2 / React-Scripts V4.0.3). All seems well in local but the styles break in production (hosted on netlify).
I believe the problem is that the css modules are not recognizing variables I've defined globally in plain css files. Here's an example of the set up I came up with:
index.css file imported at the top level index.js in my react project:
#import '../Global/ColorScheme.css';
body {
// body styles
}
a {
// global a tag styles
}
ColorScheme.css:
:root {
--green: #00b890;
--pink: #ef767a;
--brown: #554348;
--orange: #fb8f67;
}
Then some CSS module consuming global styles from ColorScheme.css..
SomeFile.module.css
.greenBox {
background-color: var(--green);
height: 500px;
width: 500px;
border: 1px solid #333;
}
Example Component
import React from 'react';
import styles from '../somePath/SomeFile.module.css';
export default function MyComponent() {
return <div className={styles.greenBox} />;
}
When I run it locally I will get a green box with height & width at 500px with a 1px solid black border around it. So all is working as expected. However the production build will show a 500px by 500px box with 1px solid black border but no background color. I'm guessing it's the disconnect is when a css module is using a variable defined in a plain css file. Maybe something with the way webpack compiles down a create-react-app for production build?
Does anyone have any ideas as to why this might be happening and any way I can get around it? I've seen instances of global variables in css modules but I'm trying to avoid importing the global styles in every single module.
I found the solution to my own problem and originally had that solution in the OP as an edit. Moving this to 'Answer my own question' so it's more clear if someone finds this issue in the future:
I found a work around by chance, but I don't understand the 'why' or 'how'. It seems like my custom CSS properties defined in :root were working, just not the ones I titled with color names (i.e. --navy, --green, --orange, or even --gray-scale-0). After running create-react-app's standard npm run build the produced main.css file would replace my css like this:
Some CSS Module Before Build
.someClass {
color: var(--green);
background-color: var(--gray-scale-0);
}
Same Class in Main.####.chunk.css After Build
.someClass {
color: var(green);
background-color: var(gray);
}
Changing my custom properties to something like --theme-orange or --theme-green works just fine. I'm still new to webpack and preprocessors so if anyone knows why this happens I'd love to know.
You should define the variable with $ and use it also with $ without any problem =>
$green : #00b890;
.greenBox {
background-color: $green;
}

Toggle scss variable file

I am trying to make a toggle between night mode and day mode only by changing colors. I have some base color variables inside my _colors.scss, and they are used all over my site. I use React to toggle a className between 'night-mode' and 'day-mode' at the first div of the project.
I have tried to wrap the variables in the mentioned class names, but the result is that no other files can access the variables. Therefore I was hoping for a solution where I can use a night-mode file and a day-time file and toggle between them.
As I see it now, the issue is that I can't wrap the $variables or the #import in a class name, which makes it difficult to know what mode is selected. I am looking for a solution that does not include jQuery (I have a variable globally stored that can be used for javascript reference if that ends up to be the best solution).
You can't toggle scss files at runtime, since they are already compiled to css.
I would go with CSS custom properties (sometimes called CSS variables) instead of pure Sass variables.
Example:
:root {
--background: lightblue;
}
.night-mode {
--background: darkblue;
}
.component {
background-color: var(--background);
}
Toggle the class .night-mode on with javascript depending on the time of day.
You may of course feed your CSS custom properties from Sass variables in your scss files:
$bg-day: lightblue;
$bg-night: darkblue;
:root {
--background: $bg-day;
}
.night-mode {
-- background: $bg-night;
}
.component {
background-color: var(--background);
}

Semantic UI - change component styles (override components css)

I'm trying to change the style of some semantic ui components. I've created a css file and I've been changing the parameters there. Then I noticed that all component css files (in Semantic UI folder) have the following code at the end:
/*******************************
Theme Overrides
*******************************/
/*******************************
Site Overrides
*******************************/
I've copy pasted the code from my css file to the bottom of the component css file but the styles were not updated/overrided. I tried to use !important but it didn't worked either.
I've tried something like this:
/*******************************
Theme Overrides
*******************************/
.ui.header {
border: solid red; !important
border-width: 2px; !important
}
I was expecting this element to get a red border around it
<div class="ui small header">This is a header</div>
Any idea why it didn't worked ?
You need to add your overriding CSS styles in src/site/elements/header.overrides file and run gulp build command.
If "gulp watch" is already running, then it will automatically run build task on detecting changes in the aforementioned file.
The src/site folder is present to override any style introduced by the selected theme.
Try including '!important' within ';' in your code like this,
.ui.header {
border: solid red !important;
border-width: 2px !important;
}
Another possible solution would be to nest all your application's less styling under a unique id, used at the root element of your app.
for example in my own case:
#__next {
.is-pink {
color: pink
}
.mb-70 {
margin-bottom: 70px !important;
}
}

All buttons hover styling in Twitter Bootstrap

I would like to change default styling for hover buttons (Bootstrap v3.2). By default it's becoming darker. Let's assume I would like to make it lighter.
I looked at Buttons Less variables but I don't see button hover styling option.
In SASS I can do:
#import "packages/stylesheets/bootstrap/bootstrap";
.btn {
&:hover {
background: yellow;
}
}
but it changes all button background to defined color (in this case yellow).
Is it possible to do it for all buttons?
The only solution I found id to do it this for each type of button this way but I hope it can be done for all buttons a bit simpler:
#import "packages/stylesheets/bootstrap/bootstrap";
.btn-primary {
&:hover {
background: lighten($btn-primary-bg, 5%);
}
}
.btn-success {
&:hover {
background: lighten($btn-success-bg, 5%);
}
}
While Marcin's solution will work, as he mentioned it is not very elegant as you will modify bootstrap's source code and you will loose all the edits once you update the base of bs.
What you need to do, in order to properly customize the button hover is, define a new file called for example "_buttons.scss" and include it in your main bootstrap/main file you are using (usually a file that include the base boostrap + the customized parts):
// Base bootstrap 3
#import "./vendor/bower_components/bootstrap-sass/assets/stylesheets/bootstrap.scss";
// My custom buttons
#import "_buttons.scss"
This way you are overriding default styles in order to have your look and feel of default bootstrap elements, and keeping base bootstrap file intact so you can update it without a fear you will overwrite your changes with next update.
And now, your part:
in the _buttons.scss define your hovers like you mentioned:
// My custom variation for btn-primary
.btn-primary {
&:hover {
background: lighten($btn-primary-bg, 5%);
}
}
If you want to do that on global level, just simply update the mixins that are in charge for buttons output in the override file, so that would be (if you follow your Bootstrap's default namespace, in your custom styles folder for example: myproject/assets/scss/mixins/_buttons.scss)
// My custom mixin for button-variant override file
// I only include the part i want to customize
// In this case, i want to customize background-color
#mixin button-variant($color, $background, $border) {
&:hover {
background-color: darken($background, 40%);
}
}
.. and include it into your main file
// Base bootstrap 3
#import "./vendor/bower_components/bootstrap-sass/assets/stylesheets/bootstrap.scss";
// My custom buttons
#import "_buttons.scss"
#import "mixins/_buttons.scss"
This way you are:
1) Able to update bootstrap base once the update is up
2) Able to keep your custom buttons styles in one file, if you decide to update it later, you can find it easily
3) Able to use same bootstrap base for different projects (if needed)
Hope it helps
Cheers
M.
The solution I found is not very elegant (you need to modify bootstrap file) but it works.
You need to go to mixins/_buttons.scss file and then change:
background-color: darken($background, 10%);
into
background-color: lighten($background, 10%);
It should do the job for all buttons.

Resources