Using the Firefox Addon SDK, I am creating a toolbar with several buttons and I want to create a mouseover effect for the buttons.
At first I thought to use a mouseover event, but then I would have to create a mouseout
event to return it to normal, so I figured the best way would be to use css
In my old XUL version of my addon I was able to attach the stylesheet by linking to it in the XUL code and just add css for my #buttonID
, which worked perfectly.
But how do I add the css stylesheet for my toolbar using the Addon SDK?
Here's what I've tried so far (which does not produce any errors), but I think this is just for content; if this is correct, then I'm not sure how to bind to the element:
const { browserWindows } = require("sdk/windows");
const { loadSheet } = require("sdk/stylesheet/utils");
//This is how to load an external stylesheet
for(let w of browserWindows){
loadSheet(viewFor(w), "./myStyleSheet.css","author" );
}
I've also tried this:
var Style = require("sdk/stylesheet/style").Style;
let myStyle = Style({source:'./myStyleSheet.css'});
for(let w of browserWindows){
attachTo(myStyle, viewFor(w))
};
And this:
var { attach, detach } = require('sdk/content/mod');
const { browserWindows } = require("sdk/windows");
var { Style } = require('sdk/stylesheet/style');
var stylesheet = Style({
uri: self.data.url('myStyleSheet.css')
});
for(let w of browserWindows){
attach(stylesheet, viewFor(w))
};
And here is my css:
#myButton:hover{list-style-image(url("./icon-16b.png")!important; }
This goes in the javascript file:
And here is the css for that
There are several gotchas here.
id
for the button because they get completely removed - only lowercase letters and hyphens are allowed.content
in the url for the stylesheet file (in the loadSheet function call) you will also need to create achrome.manifest
in the root of your addon folder, and put this in it:content spadmintoolbar data/
where "data" is the name of a real directory in the root folder. I needed adata/
folder so I could load icons for the button declarations, but you need to declare your virtual directories inchrome.manifest
whichjpm init
does not do for you.How to get the element id for your css file:
The easy way to get the
id
for your button element for use in an external style sheet is by testing your addon and then using the browser-toolbox's inspector to locate the element, whence you can fetch the id from the outputted code.However, if you want to figure it yourself, try this formula.
[button-class]
= the sdk class for the button. An Action Button becomesaction-button
[mybuttonid]
= theid
you gave the button in the sdk button declaration[myaddonname]
= thename
you gave the addon in it'spackage.json
file.[strippedaddonid]
= take theid
you assigned the addon in thepackage.json
file, and remove any @ symbol or dots and change it to all lowercase.Now put it all together (don't include the square brackets):
An example:
action-button--myaddonsomewherecom-mybutton
Really simple isn't it?!
credit for the stylesheet attach code goes to
mconley
using this code, you should be able to use the mouse over or hover to change how it looks.
Tested this in Browser Toolbox:
The commented part allows to load from a stylesheet file in the addon
data
directory.Notice that you need to import SDK loader to use it in the toolbox. When in an SDK addon, just use
require
directly.NB: there is a difference in spelling:
self.data.url
vs{ uri }
See self/data documentation.
NB2: SDK uses a custom widget ID scheme for toggle and action buttons so your button ID might not be what you expect:
OR