Use Case
Genkai is writing a plugin that has a list component and image buttons in her plugin. When the user hovers over the row she wants to change the background color. When the users mouses down she wants to change the color.
While I couldnāt agree more with this request, couldnāt you ā for that use-case ā use :hover in you CSS? That even feels a bit cleaner (to me) than doing it with JSā¦
That would help in some cases but in other cases I want to get the keyboard properties that come with mouse events.
For example, the user has the mouse over a row inside of a list (home made) and clicks it and then the user clicks another row. Did they they have the shift key down? We donāt know. If they did that means they are selecting multiple rows. If they didnāt that means they are selecting another row.
Also, CSS :hover is not going to give you an opportunity to run any code (it will only change styles). For example, if on mouse over you want to show an image preview in a thumbnail you need to be able to get a mouse over event to create that thumbnail and then remove it in a mouse out event.
Agreed, I just wanted that clarified since your stated use-case allows for this āpure CSSā solution and thereby ā in my opinion ā weakens this request I agree on whole-heartedly a little.
Kerri, whereās a good place to see a list of all the supported events? I tried searching the docs for pointerdown and mousedown and both had zero hits, so itās hard to see where we could tell that one event is supported and the other one isnāt.
I can confirm pointer events working and they have key states that work too! I can also confirm thereās no pointerdown or mousedown events. With the pointer events I can work around not having the pointerdown event but others may still need it.
@pklaschka Iād like to attempt your suggestion to set some styles on hover via CSS but I couldnāt find an example of how to add a class declaration.
Here is what I have so far:
var sheet = document.styleSheets[0];
sheet.addRule("row { padding-left: 20px }");
That gives me this error
[ts] Property āaddRuleā does not exist on type āStyleSheetā. [2339]
and
Error:TypeError: Cannot read property āaddRuleā of undefined
2nd Attempt:
var stylesheets = document.styleSheets;
var numberOfSheets = stylesheets.length;
var sheet;
if (numberOfSheets) {
sheet = stylesheets[numberOfSheets-1];
}
var style = document.createElement("style");
document.head.appendChild(style);
sheet = style.sheet;
var selector = "row";
var property = "padding-left";
var value = "20px";
value = property + ": " + value;
sheet.insertRule(selector + '{' + value + '}', sheet.cssRules.length);
[ts] Property āinsertRuleā does not exist on type āStyleSheetā. [2339]
TypeError: Cannot read property āinsertRuleā of undefined
3rd Attempt:
This seems to work to add new styles:
Yeah ā we donāt have a fully spec-compliant CSS parser, so using <style> tags is the way to go there.
As for pointerdown ā would have sworn we have it, but apparently not ā probably there, the simplest option is to use click (not sure if it will have the keyboard meta keys assigned or not). So the page Peter linked to is correct.
We should definitely support the down/up variations here in the future. Iām not sure mouse% events will ever happen, though, since weāre supporting touch-capable devices from the get-go, and donāt want to encourage plugin devs to use a feature that would only work with a mouse.