DEV Community

Cover image for Craft CMS 6 Beta for Plugin Developers: Four Control Panel Changes in beta.3
Bohdan Feniuk
Bohdan Feniuk

Posted on Originally published at b10k.nl

Craft CMS 6 Beta for Plugin Developers: Four Control Panel Changes in beta.3

My plugin passed its tests on Craft 6 beta.3. Then I clicked around, and some of its buttons did nothing.

Craft 6.0.0-beta.3 came out on 2 December. For site builders it's mostly faster indexes and a new footer in the control panel. For plugin developers it also brings a few changes to Craft's own web components, the <craft-button>, <craft-button-group> and friends that plugin UIs are now built from.

None of them throws an error. The console stays clean, PHPStan has nothing to say, and the tests pass. You only notice when you use the plugin. Here are the four I ran into while updating Component Guide, with what you see, why it happens and how to fix it. (For the code-level traps, see what breaks without an error in Craft 6.)

1. Small buttons in a group steal each other's clicks

What you see: a row of small icon buttons, like desktop, tablet and phone. The first and last work. The one in the middle is almost impossible to click: clicks on it go to its neighbour.

Why: every <craft-button> now has an invisible hit area of at least 44 × 44 px, drawn by its own :host::before and centred on the button. It's there for accessibility (WCAG 2.5.5), and on a normal button you never notice it. But a small icon-only button is narrower than 44 px. Put three of them side by side and their hit areas overlap. The last button in the row sits on top, so it takes the clicks meant for the one before it.

You can see it in DevTools: select the button and look for its ::before. It's larger than the button.

Fix: inside your own groups, make the hit area the size of the button. Page styles win over a component's :host styles, so this works from your plugin's CSS:

.my-plugin craft-button-group > craft-button::before {
    min-width: 0;
    min-height: 0;
}
Enter fullscreen mode Exit fullscreen mode

The group as a whole is still big enough to aim at. It also helps to build icon buttons the way Craft does in Live Preview: put a <craft-icon> inside the button instead of using the icon attribute, and give the button a width (inline-size: calc(var(--c-size-control-sm) * 1.25)). The buttons are then wider and look like Craft's own.

2. A button group clears your active state

What you see: you set active on the button the user picked, and it doesn't stick. The button lights up and goes dark again, or nothing seems selected at all.

Why: <craft-button-group> now manages selection itself, by value. The group has a value. Whenever a button's active changes, the group goes through its buttons and sets active on the one whose value matches its own, and removes it from the rest. A button without a value never matches, so whatever your script sets, the group takes away.

Fix: give each button a value, and set the group's value when the selection changes. Craft's own Live Preview does it like this:

<craft-button-group name="device" value="desktop" aria-label="Device">
    <craft-button value="desktop"><craft-icon name="desktop" label="Desktop"></craft-icon></craft-button>
    <craft-button value="tablet"><craft-icon name="custom-icons/tablet-even" label="Tablet"></craft-icon></craft-button>
    <craft-button value="phone"><craft-icon name="custom-icons/mobile-even" label="Phone"></craft-icon></craft-button>
</craft-button-group>
Enter fullscreen mode Exit fullscreen mode
group.value = 'tablet';
Enter fullscreen mode Exit fullscreen mode

The group then keeps active and aria-pressed in line by itself. A click on a button with a value also selects it and fires a change event on the group, so you can listen there instead of on each button.

3. The last element in a header is no longer the close button

What you see: a button your plugin adds next to the close button in Live Preview's editor pane ends up after the close button instead of before it.

Why: the close button is now a small Vue component that renders two elements: the <craft-button> with the ✕, and a <craft-tooltip> after it. Code that looks for "the last element in the header" finds the tooltip and puts its button between the two. And because Vue can re-create the close button later, a button that was placed correctly once can drift to the end again.

Fix: look for the last <craft-button> in the header instead of the last element, and check the position again whenever the DOM changes, not only when you first add your button:

const close = [...header.querySelectorAll(':scope > craft-button:not(.my-plugin-button)')].pop();
if (close && myButton.nextElementSibling !== close) {
    header.insertBefore(myButton, close);
}
Enter fullscreen mode Exit fullscreen mode

Run it from the same MutationObserver that adds your button in the first place. The same idea applies anywhere you insert something next to Craft's controls: look for the control itself, not for a position.

4. devMode is always off

What you see: debug output that your plugin shows in dev mode, like a stack trace for a template that failed to render, never appears on Craft 6, even with APP_DEBUG=true.

Why: in Craft 6, debug mode comes from Laravel's APP_DEBUG. The general config still has a devMode property, but Craft doesn't set it from APP_DEBUG. It stays false unless a project still sets it in its config. So Cms::config()->devMode is false, and code that checks it never takes the debug path. Craft itself checks app()->hasDebugModeEnabled().

This one isn't new in beta.3. I found it the same week, while fixing the things Craft's Yii2 Loose Ends utility had just started showing me in my plugin.

Fix:

// Before
if (Craft::$app->getConfig()->getGeneral()->devMode) { … }

// After
if (app()->hasDebugModeEnabled()) { … }
Enter fullscreen mode Exit fullscreen mode

In Twig, use the global devMode variable. On Craft 6 it already reads from APP_DEBUG.

How do you catch these?

None of them shows up in tests or static analysis. What worked for me:

  • Update a test site to each new beta and click through your plugin. Every page, every button, the entry editor and Live Preview. These changes only show when you use the UI.
  • Use Craft's components, the way Craft uses them. Most of my problems came from markup that worked on beta.2 but wasn't how Craft builds the same control. Live Preview's toolbar was the best reference: it has the same kind of buttons most plugins need.
  • Open Yii2 Loose Ends. Since beta.3 it also lists old Craft 5 calls in plugins installed from a local path repository, each with the file, the line and the replacement. It catches the PHP side; the four things above it can't see.
  • Update from the terminal, not from the control panel. During the beta the Yii2 adapter requires the exact same Craft version, and the control panel's updater only updates Craft itself, so it fails. composer update craftcms/cms craftcms/yii2-adapter -W updates both.

Quick answers

Will these change again before Craft 6.0?

Possibly. Craft 6 is still in beta, and Pixel & Tonic are still working on the control panel. That's why it pays to build with Craft's components and their attributes rather than around them: when the component changes, your UI changes with it.

Do these affect my plugin on Craft 5?

No. Craft 5's control panel doesn't use these components. If your plugin supports both, keep the Craft 6 markup in your Craft 6 version.

Why not override the 44 px hit area everywhere?

Because it's there for people who use a touch screen or have trouble aiming. Only remove it where buttons sit close enough to overlap, and keep the group itself easy to hit.


I'm Bohdan, a freelance Craft developer at b10k. I build Craft sites, take care of Craft CMS upgrades and make a few Craft plugins, all three on the Craft 6 beta. Porting a plugin or a site to Craft 6? Start with a Craft 6 readiness check, email me or find me on LinkedIn.

Top comments (0)