User blog:TK-999/Styling PortableInfoboxes with Javascript

Wikia is currently working on a new portable infobox system, intended to streamline viewing experiences for desktop and mobile users alike. One significant caveat of this feature is that it is more complicated to customize than traditional infobox templates, which impacts user-defined customization. This blog post aims to guide admins on constructing a template–JS combination that will permit seamless application of user-defined options, such as color schemes.

Templates
The principal issue to address is making the user's parameters available to Javascript; HTML 5 data- attributes are an excellent solution for this. On the Star Wars Fanon Wiki, we have the &#123;&#123;CustomInfoboxColors}} template for this purpose, which looks like this: This allows users to specify hexadecimal color codes as template arguments, which are then made available on the element as invisible data attributes.

Javascript
Now it's time to use Javascript to obtain and utilize the user-defined data. Since site JS is loaded in the  element, it gets executed immediately, without waiting for the page to be rendered, so no weird styling hiccups or abrupt changes should occur. To ensure that the styling is applied seamlessly, it's best to use native Javascript instead of the jQuery library; vanilla operations are significantly faster than jQuery's.

Here is the JS we use at Star Wars Fanon to apply user-defined colorization from the above template:

Summary
Although CSS is a powerful tool for working with the new infoboxes, especially when creating their basic themes, Javascript also has its fair share of use cases, especially when you have to work with user-defined data, or need to dynamically apply styling that would otherwise require a disproportionate amount of CSS rules. An example of the latter is applying borders to infoboxes on Monobook: this way, you don't have to create separate CSS rules for each infobox theme.

Please add your thoughts below. If you wish to comment on the feature itself, please use the dedicated forum thread instead.