Craftcms-claude-skills craft-garnish
Garnish — Craft CMS's built-in JavaScript UI toolkit for the control panel. Covers the full Garnish surface: class system (Garnish.Base.extend, init, setSettings, addListener, on/off/trigger, destroy), UI widgets (Modal, HUD, DisclosureMenu, MenuBtn, CustomSelect, ContextMenu, Select), drag system (BaseDrag, DragSort, DragDrop, DragMove), form widgets (NiceText, CheckboxSelect, MixedInput, MultiFunctionBtn), utilities (key constants, ARIA helpers, focus management), and Craft integration (GarnishAsset, webpack externals, Craft.* class pattern). Triggers on: Garnish.Base.extend, Garnish.Modal, Garnish.HUD, Garnish.DragSort, Garnish.Select, Garnish.DisclosureMenu, Garnish.MenuBtn, Garnish.CustomSelect, addListener, removeListener, removeAllListeners, Garnish.ESC_KEY, Garnish.RETURN_KEY, activate event, textchange event, UiLayerManager, registerShortcut, trapFocusWithin, garnishjs, GarnishAsset, CpAsset, CP JavaScript, control panel JS, drag and drop, sortable, modal dialog, HUD popover, disclosure menu, menu button, Craft.CP, Craft.Slideout, Craft.ElementEditor, onSortChange, onOptionSelect, onSelectionChange, aria-modal, focus trap, keyboard navigation CP, this.base(), window.Garnish, expose-loader, CP memory leak, event listener cleanup, jQuery .on() in CP, selection interface, multi-select grid. Always use when writing, editing, or reviewing JavaScript that runs in the Craft CMS control panel — including plugin CP assets, custom field type JS, element index JS, CP webpack config, or code that imports garnishjs or references window.Garnish. Also trigger for Craft CP accessibility, keyboard interactions, drag-sort behavior, or CP JS memory issues. Do NOT trigger for front-end JavaScript (Alpine, Vue, htmx) or Twig templates.
git clone https://github.com/michtio/craftcms-claude-skills
T=$(mktemp -d) && git clone --depth=1 https://github.com/michtio/craftcms-claude-skills "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/craft-garnish" ~/.claude/skills/michtio-craftcms-claude-skills-craft-garnish && rm -rf "$T"
skills/craft-garnish/SKILL.mdGarnish — Craft CMS Control Panel JavaScript Toolkit
Reference for Garnish, Craft CMS's built-in JavaScript UI framework. Covers the class system, UI widgets, drag interactions, form components, accessibility helpers, and integration with Craft's CP.
This skill is scoped to Garnish itself — the JavaScript library at
src/web/assets/garnish/. For PHP-side plugin development (elements, controllers, services), see the craftcms skill. For CP template markup that Garnish widgets attach to, see the craftcms skill's cp.md reference.
Companion Skills — Load When Needed
— Load when the task involves PHP asset bundle classes, plugin architecture, or CP template markup that Garnish widgets attach to. Skip for pure JavaScript refactoring, Garnish API questions, or JS-only tasks.craftcms
— Load only when editing PHP files (asset bundle classes, controllers that register JS). Skip for pure JS work.craft-php-guidelines
Documentation
- Garnish source:
in the Craft CMS repositorysrc/web/assets/garnish/src/ - No official external documentation exists — this skill IS the documentation.
Use
WebFetch on Craft's class reference (https://docs.craftcms.com/api/v5/) when looking up PHP-side asset bundle registration.
Common Pitfalls (Cross-Cutting)
- Using jQuery
directly instead of.on()
— listeners added via jQuery won't auto-clean onthis.addListener()
, causing memory leaks.destroy() - Forgetting
when overridingthis.base()
— parent cleanup (listener removal, event teardown) gets skipped.destroy() - Using
instead ofclick
event on non-activate
elements —<button>
handles both click and keyboard (Space/Enter), making the UI accessible.activate - Fighting
by binding ESC directly — useUiLayerManager
so escape routes through the layer stack correctly.Garnish.uiLayerManager.registerShortcut(Garnish.ESC_KEY, callback) - Magic key code numbers instead of
,Garnish.ESC_KEY
, etc. — constants are self-documenting and consistent.Garnish.RETURN_KEY - Instantiating Garnish widgets before the DOM is ready — Garnish requires jQuery and all dependencies loaded first; in plugin assets, rely on
dependency chain.CpAsset - Not calling
when removing widgets — orphaned listeners accumulate, especially in slideouts and live preview where DOM is repeatedly created/destroyed.destroy() - Importing Garnish into webpack bundles instead of using the external —
resolves toimport Garnish from 'garnishjs'
via webpack externals; bundling it duplicates 134KB.window.Garnish - Using deprecated
instead ofGarnish.Menu
—Garnish.CustomSelect
is an alias kept for BC only.Menu - Using deprecated
orGarnish.escManager
instead ofGarnish.shortcutManager
— the newer manager provides layer-aware keyboard routing that respects the modal/menu stack.Garnish.uiLayerManager
Reference Files
Read the relevant reference file(s) for your task. Multiple files often apply together.
Task examples:
- "Create a modal dialog in a plugin's CP JS" → read
+class-system.mdui-widgets.md - "Add drag-to-reorder to a custom field type" → read
+drag-system.mdclass-system.md - "Build a custom CP widget class" → read
+class-system.mdintegration.md - "Add a disclosure menu to a CP template" → read
+ui-widgets.mdintegration.md - "Handle keyboard events in CP JavaScript" → read
+utilities.mdclass-system.md - "Create an inline editor HUD" → read
(HUD section)ui-widgets.md - "Make a selection interface for elements" → read
(Select section)ui-widgets.md - "Set up a plugin's webpack config for Garnish" → read
integration.md - "Custom element index class isn't loading" → read
(Element Index JS Loading)integration.md - "Load element index JS with Vite" → read
(Element Index JS Loading — Vite doesn't work for element index classes)integration.md - "Add ARIA attributes to a custom modal" → read
(ARIA & Focus section)utilities.md - "Understand how Craft.CP extends Garnish" → read
+integration.mdclass-system.md - "Build a multi-state submit button" → read
(Form Widgets section)integration.md - "Add auto-growing textarea behavior" → read
(Form Widgets section)integration.md
| Reference | Scope |
|---|---|
| Garnish.Base, inheritance (extend/init/base), events (on/off/trigger), listeners (addListener/removeListener), settings, namespacing, enable/disable, destroy lifecycle |
| Modal, HUD, DisclosureMenu, MenuBtn, SelectMenu, CustomSelect, ContextMenu, Select — constructor args, settings/defaults, methods, events, ARIA behavior |
| BaseDrag, Drag, DragSort, DragDrop, DragMove — class hierarchy, settings/defaults, events, helper system, insertion points, scroll handling |
| Garnish namespace object, key constants, custom jQuery events (activate, textchange, resize), ARIA/focus management, geometry/hit testing, animation, form helpers, detection |
| GarnishAsset PHP bundle, webpack externals, loading sequence, Craft.* class pattern, Twig JS blocks, form widgets (NiceText, CheckboxSelect, MultiFunctionBtn, MixedInput) |