4. The theme script
The theme script
Seven small, self-contained modules, each there because a component in the content needs it.
The JavaScript follows the modular pattern of reference build A. Each module runs in strict mode and is idempotent, and one shared helper sits alongside them. A shared content watcher re-runs the modules whenever Canvas re-renders part of a page, so components added after the first load are wired up too. Global listeners are registered once, guarded against the theme loading twice.
The script binds to the same component classes the course content is authored with, verified against the full page inventory, so it works against real pages with no find-and-replace.
| # | Module | What it does |
|---|---|---|
| 1 | H5P resizer | Sizes H5P frames from their height messages, checking the sender's origin and clamping the height |
| 2 | Image lightbox | Turns opted-in images into keyboard-operable zoom buttons that open a true modal dialog |
| 3 | Transcript modal | Opens the transcript in a focus-managed dialog, and stops Canvas opening its own plain dialog |
| 4 | Card navigation | Makes linked cards work with Enter and Space, without hijacking controls nested inside |
| 5 | Page banner | Rebuilds the banner Canvas strips on save, and writes the page title into the overlay |
| 6 | Video label cleanup | Removes the stray "[Video]" text Canvas adds beside video embeds |
| 7 | Glossary modal | Turns glossary terms into accessible buttons that open their definition in a dialog |
Consolidated from the reference build
Reference build A carried two banner systems: one with a partner prefix and one generic. The content here only ever uses the generic one, so the duplicate was removed and its title-overlay logic folded into the single system that remains.
Note
Where the brand lives
Class names that bind the script to markup are kept exactly as the content authors them, with the wcag- prefix. Brand identity lives in the stylesheet's colour tokens instead, which is the right place for it.
Explore
Deliberately left out
Learning outcomes hidden in a pop-up
Reference build B hard-coded around 330 lines of course-specific outcomes inside the theme script and showed them in a modal. That ties content to presentation, makes the file course-specific, and hides outcomes behind a click. Outcomes are authored on the page instead.
Forcing links to open in new tabs
New windows break the back button and disorient assistive-technology users. Links open in the same tab unless an author sets a target explicitly.
Loading the whole Google Fonts library
Reference build B imported hundreds of font families on every page, a serious performance problem. This theme loads only the two families it uses.
A script for accordions
Accordions are native details and summary elements, which work with a keyboard and screen reader with no script at all. One less moving part.
Carousels, tabs, slideshows and read-more toggles
None of them appear in the course content, so none of them are in the script.
Check your knowledge
Key terms
C
- Content watcher
- One shared observer that re-runs every module whenever Canvas changes the page, so components added after the first load still work.
D
- Design token
- A named value, such as a colour, stored once as a CSS custom property and reused everywhere, so a retheme is a single-location edit.
F
- Focus trap
- Keeping the Tab key inside an open dialog, so keyboard users can't move to the page behind it until they close it.
I
- Idempotent
- Safe to run more than once, with no duplicated effects.
- Inert
- The state of the page behind an open dialog: it can't be focused or clicked, and is hidden from screen readers.
P
- Progressive enhancement
- Authoring content so it works without the script, then letting the script improve it. If the script fails, nothing is lost.
Return to the course home page.
