Best practice for React. Hover or mouseEnter/mouseLeave? - css

I'm learning React with styled components and I have a button with a simple hover-effect.
This hover-effect can be created in the styled components file with &:hover, and in my jsx file, I can use onMouseEnter. Both will work.
I have searched google to find what is considered as best practice, but no luck, so I try here. Anyone?

It's very simple choice.
If you need change styles then use ":hover". You can pass props to styled component for change styles in depend of some condition.
If you need realize some logic on hover then use event handler for onMouseLeave, onMouseEnter, onClick and other events.
Use event handler for realize some logic. As example when you have some element and you handle "onMouseLeave", in mouseleave event handler you change something, from what depend other components. For this need use onMouseLeave.
I recommend use styles for simple styling things. Don't use javascript for simple styling thing as example for make blue color on hover, for that simple thing use CSS.

Related

How do I "inject" a CSS-class into a lit-element?

lit-element completely encapsulates CSS and the only way to style components is via custom CSS variables.
When using tailwindcss all styles are applied via classes, and I currently don't see a way to inject those classes from the outside world.
What I would like to achieve is to make the custom lit-component completely unaware of tailwind. It should only do the most basic styling but leave the customisation up to the user of the component.
The only solution I see right now is to provide the classes via a property and then apply them using classMap. But I don't know where users would like to apply those classes and adding them to each element is unfeasible (and unmaintainable). In addition, I have my doubts that tailwind would even work in that case due to the style-encapsulation.
It sounds like you want your users to be able add classes to specific parts within the custom element you authored?
If it works with what your component's trying to do, the best way to achieve that would be to place slots in your component and have the user provide the element to fill those slots as children to your component. That way the user directly controls what classes they want to put on it and the styling will apply as the children would be part of the light DOM.
As you've said, providing classes via property would be clunky API and styling won't apply unless you forego using shadow DOM by overriding createRenderRoot which is not recommended.
CSS custom properties are not the only way to allow users to style parts of your component as you can also add part attributes letting the user use ::part() pseudo-element to style them. If your users can write CSS instead of providing tailwind classes, that would be the way to give users some control of styling your component. See https://developer.mozilla.org/en-US/docs/Web/CSS/::part

Using react; workflow method for overloading css

I am new to the React javascript framework and have a question about styling using CSS.
Using jQuery, my old workflow was to pick an element on the screen, inspect it in Chrome, note the selector/s that triggered the styling, change the element styling in the browser, and then save it to css/sass etc. If the widget had a hover-state I could make the element visible to see what it looked like.
However using React, and especially for components that someone else has coded, where the component does a 'pop-up' etc, I can't manipulate the DOM to see the component because it is removed from the DOM before I can inspect it.
Now of course I could read the external library code, work out how it works, but CSS inheritance means it would take some time to work out exactly is happening and this seems to be slower than what I was doing before - especially for a simple change.
So my question is, what is the preferred workflow for overloading the CSS for DOM elements that are removed before they can be inspected?

How to to customize GWT components style?

I'm developing a multi-module application using GWT 2.5.1. I'm not using any GWT theme. I want to customize the style for some of the GWT widgets, for example Button and CheckBox.
I see two solutions:
Write a CSS file loaded in the application (link in the HTML page). The CSS will contain CSS rules using GWT defined names, like .gwt-Button for buttons and .gwt-CheckBox, .gwt-CheckBox-disabled for checkboxes. This solution don't takes the advantage of CSS optimizations made by the GWT compiler.
Use a CssResource and set the style name each time I use a Button or a Checkbox. This solution will take advantage of CSS optimizations but it requires to set the style name every time I create a new Widget.
There are other solutions? Which is the correct one?
You can put those styles in a CssResource as well.
Just put #external on top of those styles in your css file, and you are good to go.
For example:
#external gwt-DatePicker;
.gwt-DatePicker {
...
}
Hope it helps.
Other solution: Button is html element button and Checkbox an html element input[type=checkbox]. So you could set styles on those elements and use css selectors for specific states. i.e. button:disabled. That way you won't have to set style names, or don't have lots of extra style names and use cleaner css.
You could subclass whatever widgets you want to style (e.g. MyButton), and have your subclass either just add a style name to each widget that gets created, or do the styling inline using calls to this.setWidth(), this.getElement().getStyle.setXXX.
Also, what optimizations does the GWT compiler perform on CSS? I know that it will obfuscate style names to avoid collisions, but I'm not sure CSS is even able to be optimized?
I would personally use emanuele's solution, but just to offer an alternative: you can use a widget's getElement() method to access style names directly, so if you really want to, you can override the style names with ones you created. This gets rather difficult, however, with larger widgets and panels that have multiple styles.

Prevent CSS classes from being overridden

I have a project that uses JQuery-UI. I recently found formee, which is a nice framework for building forms. One annoyance is that the formee submit button styling overrides the jquery-ui themed button style. How can I get all of the formee goodness but keep my jquery-ui button style? I realize I can edit the formee CSS to remove the button style, but I'm hoping to avoid that.
You can, as you said yourself, remove the offending CSS, which is what I would recommend.
Alternatively you can give the jQuery UI css classes that apply on the relevant button more specificity than the formee classes. This would be second best solution.
As a last case you could add !important; behind all CSS attributes in the classes for jQuery UI... I really wouldn't recommend this.
use this to bring front:
#your-div-id{
position:relative;
z-index:999999;
}

Cocoa WebKit - detecting CSS rollover/hover

I have a WebView displaying a HTML page, linking to a CSS file.
The CSS file makes use of the pseudoclasses :active and :hover for rollover effects.
Q. How can I tell, in my WebView, when :active and :hover have been called? I would like to intercept these calls and act on them programmatically within Cocoa.
Thanks.
:active and :hover aren't calls; they're CSS pseudo-classes, for use in CSS selectors. You use them in CSS to select elements to style. They're adjectives, not verbs.
Try adding JavaScript event handlers to the elements instead. You can use a WebScriptObject to project some of your Cocoa methods into the JavaScript space for use from the event handlers.
I'm looking in the 2.2 SDK docs, and I don't see anything in the UIWebViewDelegate protocol. The best hope of accomplishing this maybe the instance method in UIWebView called stringByEvaluatingJavaScriptFromString. Unfortunately that would probably imply some kind of polling, as there seems to be no way to define an Objective-C method that JavaScript could call back on for such an event. I might be wrong, but I don't think it can be done (in 2.2).

Resources