# Panel view buttons

- Since [5.0.0](https://github.com/getkirby/kirby/releases/tag/5.0.0)

****

Kirby allows your plugin to add custom view buttons to most views of the Panel views (e.g. page, site, file, user, language). These buttons can be added alongside the default buttons, such as the preview button or settings dropdown button.

<figure class="image"><a data-lightbox href="https://assets.getkirby.com/media/pages/docs/reference/plugins/extensions/panel-view-buttons/befb31cd4b-1750751812/buttons.png?width=780&amp;height=276&amp;enlarge=0"><img alt="" class="rounded" height="276" loading="lazy" sizes="auto" src="https://assets.getkirby.com/media/pages/docs/reference/plugins/extensions/panel-view-buttons/befb31cd4b-1750751812/buttons.png?width=780&amp;height=276&amp;enlarge=0" srcset="https://assets.getkirby.com/media/pages/docs/reference/plugins/extensions/panel-view-buttons/befb31cd4b-1750751812/buttons.png?width=462&amp;height=163&amp;enlarge=0 462w, https://assets.getkirby.com/media/pages/docs/reference/plugins/extensions/panel-view-buttons/befb31cd4b-1750751812/buttons.png?width=690&amp;height=244&amp;enlarge=0 690w, https://assets.getkirby.com/media/pages/docs/reference/plugins/extensions/panel-view-buttons/befb31cd4b-1750751812/buttons.png?width=780&amp;height=276&amp;enlarge=0 924w, https://assets.getkirby.com/media/pages/docs/reference/plugins/extensions/panel-view-buttons/befb31cd4b-1750751812/buttons.png?width=780&amp;height=276&amp;enlarge=0 1380w, https://assets.getkirby.com/media/pages/docs/reference/plugins/extensions/panel-view-buttons/befb31cd4b-1750751812/buttons.png?width=780&amp;height=276&amp;enlarge=0 1848w" width="780"></a></figure>

## Buttons in your Panel area

When you ship a custom view button as part of your plugin, you need to add them to a <a href="https://getkirby.com/docs/reference/plugins/extensions/panel-areas">Panel area extension</a>:

```php "site/plugins/your-plugin/index.php"
Kirby::plugin('custom/buttons', [
  'areas' => [
    'plugin-a' => function () {
      return [
        'buttons' => [
          'analytics' => function () {
            return [
              'icon'     => 'chart',
              'text'     => 'Analytics',
              'dropdown' => 'my/analytics/dropdown/route'
            ];
          }
        ]
      ];
    }
  ]
]);
```

Buttons are defined the same way as in the <a href="https://getkirby.com/docs/reference/system/options/panel/panel-view-buttons">`panel.viewButtons` config option</a>, incl. available button attributes or query support.

Buttons you add via your plugin extension can then be referenced <a href="https://getkirby.com/docs/reference/panel/blueprints/page#view-buttons">in a blueprint</a> or the <a href="https://getkirby.com/docs/reference/system/options/panel/panel-view-buttons#referencing-buttons">config option</a>.

## Use a `ViewButton` class

You can either return an array with the options or a `Kirby\Panel\Ui\Buttons\ViewButton` object. Especially when working on complex buttons, it can make sense to create your custom PHP class extending the <a class="type-link" href="https://getkirby.com/docs/reference/objects/panel-ui-buttons/view-button"><code class="type type-class">ViewButton</code></a> class to power your button:

```php "site/plugins/your-plugin/index.php"
class AnalyticsViewButton extends Kirby\Panel\Ui\Button\ViewButton
{
	// …
}

Kirby::plugin('custom/buttons', [
  'areas' => [
    'plugin-a' => function () {
      return [
        'buttons' => [
          'analytics' => fn () => new AnalyticsViewButton()
        ]
      ];
    }
  ]
]);
```

## Access objects in your closure

For some buttons, the context in which the button is displayed might be relevant. You can <a href="https://getkirby.com/docs/reference/system/options/panel/panel-view-buttons#using-the-current-context__access-current-object">access relevant objects passed as arguments to the callback function</a>:

```php "site/plugins/your-plugin/index.php"
'analytics' => fn (Kirby\Cms\Page $page) {
  return new AnalyticsViewButton($page);
}
```

## Custom Vue component

By default, custom view buttons are rendered with the `<k-view-button>` component.

If you need more interactivity or want to render a more complex UI, you can create your own Vue component:

```js "site/plugins/your-plugin/index.js"
panel.plugin("getkirby/custom-view-buttons", {
  viewButtons: {
    applause: {
      props: {
        praise: String
      },
      template: `
        <k-button
          icon="heart"
          variant="filled"
          theme="love"
          size="sm"
          @click="applause"
        >
          Applause
        </k-button>
      `,
      methods: {
        applause() {
          alert("👏 " + this.praise);
        },
      },
    },
  },
});
```

### Use a custom component

There are different ways, how you can specify your custom Vue component to be used:

1. Kirby will use the button name from your blueprint/config to look up a potential component. For a button named `applause`, Kirby will look if a `k-applause-view-button` is available.

2. You can explicitly specify what component to use. When you use the array notation, provide it as follows

```php
return [
	'component' => 'k-my-applause',
	'props'     => [
		'praise' => 'Good job!'
	]
];
```

When you are using a custom <a class="type-link" href="https://getkirby.com/docs/reference/objects/panel-ui-buttons/view-button"><code class="type type-class">ViewButton</code></a> class, the class defines the Vue component to be used.