How to remove the injected CSS Resource in GWT? - gwt

I want to remove the injected CSSResource in GWT application.
I used the following code MyClass.INSTANCE.ensureInjected();
I want the above CSSResource for a particular page only. So the remaining pages should be work as per the actual css/theme.
Once I inject this then its applicable for the whole application. How can I overcome this?

You can inject your css bundle using directly StyleInjector utility class, instead of the ensureInjected() method
Then you will have a reference of the injected element which you can remove when you want.
// Equivalent to MyClass.INSTANCE.ensureInjected()
StyleElement e = StyleInjector.injectStylesheet(MyClass.INSTANCE.css().getText());
// Remove the injected css element
e.removeFromParent();

Theoretically you could try to remove the injected style block from the DOM, but this would be quite difficult (and maybe not very reliable).
Much better to organize your 'special' CSS style sheet in a different way:
Turn selectors like
.some {
color: green;
}
.other {
color: red;
}
into
.special .some {
color: green;
}
.special .other {
color: red;
}
and then add/remove the 'special' class e.g. to/from your body element to activate/deactivate the special styles.

If you have embedded the same GWT application in more than 1 page and you want a different behavior based on the given page, you can for example call the
MyClass.INSTANCE.ensureInjected();
if a bootstrap parameter is set.
In the host page, set the parameter, like YourGwtApp.nocahe.js?css=inject and read it as it's explained here
In the onLoadMethod, call the ensureInjected accordingly to your bootstrap parameter.

Related

Difference between Angular property and Custom CSS Properties in Ionic

Could you give an example of usage of CSS Custom Properties
--ion-grid-padding
--ion-grid-padding-lg
with Angular.
Specifically, I do not see a thin difference between size-sm and --ion-grid-padding-lg?
I do not understand where and in what file I should use custom CSS properties (also, how to use them properly).
You can define your custom css properties in the file variable.css, for example:
--my-custom-opacity: 0.8;
In the css file associated with your page, or in global.css, you can access this property using:
.my-css-class {
opacity: var(--my-custom-opacity);
}
Your html elements will be attributed an opacity of 0.8.
If you need to update this property to change on a single page (and stay the same elsewhere), you can update it using the host tag:
:host {
--my-custom-opacity: 0.6;
}
In that case, your elements will be attributed an opacity of 0.6 on this page only.
This is an easy way to apply css values at multiple places in your code and update when necessary.
Hope that helps!

CSS from code? How to use CSS decoration directly from Vala?

How to use CSS customization directly from Vala? Not like in this example with file. Let's say I want the button to turn red by clicking on it, without using an external css file, as this action is too simple to create a css file with a single field.
I mean smth like this:
label.set_styleSheet("font-size: 17px;")
You still have to create a CssProvider, like in the code you linked to:
var screen = this.get_screen ();
var css_provider = new Gtk.CssProvider();
You can call load_from_data () instead of load_from_path () to load it from a string in memory instead of a file:
https://valadoc.org/gtk+-3.0/Gtk.CssProvider.load_from_data.html
css_provider.load_from_data(".my_class { font-size: 17px; }");
Gtk.StyleContext.add_provider_for_screen(screen, css_provider, Gtk.STYLE_PROVIDER_PRIORITY_USER);
When the CSS provider has loaded the custom styles to be used, you can manipulate every Gtk+ widget with get_style_context ().
The style context has methods to add, remove and query a class, etc.
https://valadoc.org/gtk+-3.0/Gtk.StyleContext.html
label.get_style_context().add_class("my_class");
Since you only have to setup the style provider once, I don't think it is too much overhead.
For anyone who is reading this I have posted both examples with and without file to Git https://gitlab.com/gavr123456789/vala-css-examples/tree/master

Parse and rewrite CSS with Electron and ReactJS

I'm writing a desktop application with Electron and ReactJS that edits CSS files. I need to scan the CSS looking for a class selector, and then clear the following declaration block and add some new properties.
The tricky part is matching the class in the selector. I need the class to be the actual target (not a parent), but there might be multiple comma-separated selectors, and so I need to check all of them. For example, in this file I'm searching for the containerApp class:
.section-main .section-right , .menu .container-manu , .containerApp .container-nav {
background: black;
}
.section-main2 .section-left , .menu .container-manu , .section-group-d .containerApp {
background: red;
}
The first block doesn't match because .containerApp is only mentioned as the parent of the real target, .container-nav. The second block does match, and I would want to remove the background: red; and replace it with something else.
What's the best way to go about doing this CSS matching and rewriting?

GWT StackLayoutPanel: how to change header background color

I want to change stacklayoutpanel header back ground color using css and I tried everything.
.gwt-StackLayoutPanel .gwt-StackLayoutPanelHeader .gwt-StackLayoutPanelContent .gwt-StackLayoutPanelItem {
color: red;
border:red;
border-color: red;
background:red;
background-color:red;
}
But only changed the text color and I don't want that. Please can you explain how can I do that?
StackLayoutPanel wraps hour header widget/text to an internal class named Header, which is not publicly accessible. One approach is to override default clean.css .gwt-StackLayoutPanel .gwt-StackLayoutPanelHeader styles by copying it to your own css file, then appending !important to styles you want to change.
However, better and cleaner solution is to do the following:
// add/insert your item first
myStackLayoutPanel.add(widget, header, size);
// retrieve the Header internal widget (AFTER ADDING!)
Widget internHeader = header.getParent();
// replace default style
internHeader.setStyleName("my_custom_style");
If you don't like using class css styles, you may alternatively do something like:
... same as above
// reset the default style
internHeader.setStyleName("");
// then add your styles programmatically
Style style = internHeader.getElement().getStyle();
style.setBackgroundColor();
etc.
It is important to retrieve the internal header widget after call to add/insert!
Your CSS style is incorrect. It's trying to target classes with the following hierarchy:
.gwt-StackLayoutPanel
.gwt-StackLayoutPanelHeader
.gwt-StackLayoutPanelContent
.gwt-StackLayoutPanelItem
Which is completely incorrect. If you want ALL elements with those classes to have the same background color, you would write your CSS rule like this:
.gwt-StackLayoutPanel,
.gwt-StackLayoutPanelHeader,
.gwt-StackLayoutPanelContent,
.gwt-StackLayoutPanelItem
{
background-color: red;
}
You better create your own css file based on gwt's default and make changes there. You also need to exclude gwt default css from your_module.gwt.xml and put there your newly created

Modify Style from GWT

I executed but only FF and chrome moves the textarea 0px from top and 0px from left but in IE
textarea is in default position.
Here is my code:
public class MyGWT implements EntryPoint {
TextArea ta= TextArea.wrap(DOM.getElementById("t"));
public void onModuleLoad() {
ta.getElement().setAttribute("style", "position:absolute;top:0px;left:0px;");
}
}
is there any bug or how can i change style attribute programmatically from GWT ??
Don't set style via setAttribute. In JavaScript the style attribute is actually an array. Thus depending on how smart the browser is and understands you want to set the style attributes it will work by setting style or won't work.
You should set style attributes individually via getElement().getStyle().setProperty(). Or use the specific methods, like: ta.getElement().getStyle().setPosition(Position.ABSOLUTE) or via the setProperty method: ta.getElement().getStyle().setProperty("position", "absolute"). And the same for the 2 other properties. See the Style class for what specific methods are supported.
What about using style classes? I mean, you can write a style class like this:
.someClass {
position:absolute;
top:0px;
left:0px;
}
And then add the style class to the TextArea
ta.addStyleName("someClass");
It will help you to write a more concise code, without inline styling that could be difficult to maintain.