Skip to content

Kirby 6.0.0

Panel drawers

As part of your Panel area extension, you can also define custom drawers for the Panel as well as extend or modify the Panel's default drawers. Drawers work like dialogs, but slide in from the side and have room for longer forms.

Drawer definition

Drawers are defined as a part of areas. The key of a drawer is its name and, unless you set a pattern, also its route. load is called when the drawer opens and returns the drawer component and its props, submit is called when the drawer is submitted:

/site/plugins/todos/index.php
<?php

Kirby::plugin(
  name: 'yourname/todos',
  extends: [
    'areas' => [
      'todos' => fn () => [
        // ... other code for the custom Panel area
        'drawers' => [
          'todo.update' => [
            'pattern' => 'todos/(:any)/update',
            'load'    => fn (string $id) => [
              'component' => 'k-form-drawer',
              'props'     => [
                'title'  => 'Edit todo',
                'icon'   => 'check',
                'fields' => [
                  'text'  => ['label' => 'Todo', 'type' => 'text'],
                  'notes' => ['label' => 'Notes', 'type' => 'textarea']
                ],
                'value'  => todo($id)
              ]
            ],
            'submit'  => function (string $id) {
              // save the todo
              return true;
            }
          ]
        ]
      ]
    ]
  ]
);

submit returns the same results as for dialogs, e.g. true to close the drawer.

Drawer components

The Panel comes with these drawer components:

Component Usage
k-form-drawer For drawers with form fields (fields, value)
k-text-drawer For drawers with a text

Both accept a title, an icon, options and an empty text for when there's nothing to show. The text of a text drawer is escaped unless it is trusted HTML, e.g. an HtmlString object. Your own drawer components work the same way: return their name as component and their props in props.

Drawer objects

Instead of an array, load can return a FormDrawer or TextDrawer object from the Kirby\Panel\Ui\Drawer namespace. All named arguments become props:

use Kirby\Panel\Ui\Drawer\FormDrawer;

'load' => fn (string $id) => new FormDrawer(
  title: 'Edit todo',
  fields: [
    'text' => ['label' => 'Todo', 'type' => 'text']
  ],
  value: todo($id)
)

For your own drawer components, you can create your own drawer classes as well, by extending Kirby\Panel\Ui\Drawer or one of the classes above.

Controller classes

Instead of load and submit closures, you can pass a controller class as action. It extends Kirby\Panel\Controller\DrawerController and works like a dialog controller: load() returns the drawer, submit() handles the submission, and a static factory() method receives the placeholders of the route pattern.

'drawers' => [
  'todo.update' => [
    'pattern' => 'todos/(:any)/update',
    'action'  => TodoUpdateDrawerController::class
  ]
]

Extending drawers

You can replace or extend the Panel's core drawers like core dialogs. Kirby merges your definition into the core definition: load and submit closures take precedence over the core controller, action replaces it and false removes the drawer. To fall back to the core behavior, call the core controller, e.g. UserSecurityDrawerController::factory($id)->load().

You find the names of all core drawers in the area definitions in /kirby/config/areas and their controllers in /kirby/src/Panel/Controller/Drawer.