Dev Toolbar App API
Este conteúdo não está disponível em sua língua ainda.
The Astro Dev Toolbar App API allows you to create Astro Integrations that add apps to the Astro Dev Toolbar. This allows you to add new features and integrations with third-party services.
Toolbar app integration setup
Section titled Toolbar app integration setupIntegrations can add apps to the dev toolbar in the astro:config:setup
hook.
addDevToolbarApp()
Section titled addDevToolbarApp()A function available to the astro:config:setup
hook that adds dev toolbar apps. It takes an object with the following required properties to define the toolbar app: id
, name
, icon
, and entrypoint
.
A unique identifier for the app. This will be used to uniquely identify the app in hooks and events.
The name of the app. This will be shown to users whenever the app needs to be referenced using a human-readable name.
The icon used to display the app in the toolbar. This can either be an icon from the icon list, or a string containing the SVG markup of the icon.
entrypoint
Section titled entrypointThe path to the file that exports the dev toolbar app.
Adicionado em:
astro@5.0.0
Beta
The function also accepts a URL
as entrypoint
:
Structure of a Dev Toolbar App
Section titled Structure of a Dev Toolbar AppA Dev Toolbar App is a .js
or .ts
file that default exports an object using the defineToolbarApp()
function available from the astro/toolbar
module.
defineToolbarApp()
Section titled defineToolbarApp()
Adicionado em:
astro@4.7.0
A function that defines the logic of your toolbar app when it is loaded and toggled off.
This function takes an object with an init()
function that will be called when the dev toolbar app is loaded. It can also take a beforeTogglingOff()
function that will run when the toolbar app is clicked to toggle off its active status.
init()
Section titled init()Signature: init(canvas: ShadowRoot, app: ToolbarAppEventTarget, server: ToolbarServerHelpers) => void
Although not required, most apps will use this function to define the core behavior of the app. This function will be called only once when the app is loaded, which will either be when the browser is idle or when the user clicks on the app in the UI, depending on which one comes first.
The function receives three arguments to define your app logic: canvas
(to render elements to the screen), app
(to send and receive client-side events from the dev toolbar), and server
(to communicate with the server).
canvas
Section titled canvasA standard ShadowRoot that the app can use to render its UI. Elements can be created and added to the ShadowRoot using the standard DOM APIs.
Every app receives its own dedicated ShadowRoot for rendering its UI. Additionally, the parent element is positioned using position: absolute;
so the app UI will not affect the layout of an Astro page.
Adicionado em:
astro@4.7.0
A standard EventTarget
with a few additional helpers for client-side events that can be used to send and receive events from the Dev toolbar.
server
Section titled server
Adicionado em:
astro@4.7.0
An object that can be used to communicate with the server.
beforeTogglingOff()
Section titled beforeTogglingOff()Signature: beforeTogglingOff(canvas: ShadowRoot): boolean | void
astro@4.7.0
This optional function will be called when the user clicks on the app icon in the UI to toggle off the app. This function can be used, for example, to perform cleanup operations, or to ask the user for confirmation before toggling off the app.
If a falsy value is returned, the toggling off will be cancelled and the app will stay enabled.
canvas
Section titled canvasThe ShadowRoot of the app, can be used to render any UI needed before closing. Same as the canvas
argument of the init
function.
Client-side Events
Section titled Client-side EventsIn addition to the standard methods of an EventTarget
(addEventListener
, dispatchEvent
, removeEventListener
etc.), the app
object also has the following methods:
onToggled()
Section titled onToggled()Signature: onToggled(callback: (options: {state: boolean})) => void
astro@4.7.0
Registers a callback to be called when the user clicks on the app icon in the UI to toggle the app on or off.
onToolbarPlacementUpdated()
Section titled onToolbarPlacementUpdated()Signature: onToolbarPlacementUpdated(callback: (options: {placement: 'bottom-left' | 'bottom-center' | 'bottom-right'})) => void
astro@4.7.0
This event is fired when the user changes the placement of the Dev Toolbar. This can, for example, be used to reposition the app’s UI when the toolbar is moved.
toggleState()
Section titled toggleState()Signature: toggleState(options: {state: boolean}) => void
astro@4.7.0
Changes the state of the app. This can be used to enable or disable the app programmatically, for example, when the user clicks on a button in the app’s UI.
toggleNotification()
Section titled toggleNotification()Signature: toggleNotification(options: {state?: boolean, level?: 'error' | 'warning' | 'info'}) => void
astro@4.7.0
Toggles a notification on the app icon. This can be used to inform the user that the app requires attention, or remove the current notification.
state: boolean
Section titled state: booleanIndicates whether or not the app has a notification for the user. When true
, the app icon will be highlighted. Conversely, when false
, the highlight will be removed. If this property is not specified, true
will be assumed.
level: 'error' | 'warning' | 'info'
Section titled level: 'error' | 'warning' | 'info'Indicates the level of the notification. This will be used to determine the color and shape (dark pink circle, gold triangle, or blue square) of the highlight on the app icon. If this property is not specified, 'error'
will be assumed.
Client-Server Communication
Section titled Client-Server CommunicationUsing Vite’s methods for client-server communication, Dev Toolbar Apps and the server can communicate with each other. In order to facilitate sending and receiving custom messages, helper methods are provided for use both in your toolbar app (on the client) and in your integration (on the server).
On the client
Section titled On the clientIn your app, use the server
object on the init()
hook to send and receive messages to and from the server.
send()
Section titled send()Signature: send<T>(event: stringify, data: T) => void
astro@4.7.0
Sends data to the server from logic defined in your toolbar app.
When sending messages from the client to the server, it is good practice to prefix the event name with the app ID or other namespaces to avoid conflicts with other apps or other integrations that may be listening for messages.
Signature: on<T>(event: string, callback: (data: T) => void) => void
astro@4.7.0
Registers a callback to be called when the server sends a message with the specified event.
On the server
Section titled On the serverIn an integration, such as the integration that adds your toolbar app, use the astro:server:setup
hook to access the toolbar
object to send and receive messages to and from your apps.
send()
Section titled send()Signature: send<T>(event: string, data: T) => void
astro@4.7.0
Sends data to the client.
Signature: on<T>(event: string, callback: (data: T) => void) => void
astro@4.7.0
Registers a callback to be called when the client sends a message with the specified event.
onInitialized()
Section titled onInitialized()Signature: onInitialized(appId: string, callback: () => void) => void
astro@4.7.0
Registers a callback to be called when the app is initialized.
The built-in connection
event from Vite fires before Dev Toolbar apps are initialized and therefore cannot be used directly by apps. Use the onInitialized
method to ensure that the app is fully initialized before sending messages to it.
onAppToggled()
Section titled onAppToggled()Signature: onAppToggled(appId: string, callback: (options: {state: boolean}) => void) => void
astro@4.7.0
Registers a callback to be called when the user clicks on the app icon in the UI to toggle the app on or off.
Component Library
Section titled Component LibraryThe Dev Toolbar includes a set of web components that can be used to build apps with a consistent look and feel.
astro-dev-toolbar-window
Section titled astro-dev-toolbar-windowShows a window.
The slot of the component will be used as the content of the window.
When building a window using JavaScript, slotted content must go inside the light DOM of the component.
astro-dev-toolbar-button
Section titled astro-dev-toolbar-buttonShows a button.
The slot of the component will be used as the content of the button.
The size of the button (small
, medium
, large
).
button-style
Section titled button-styleThe style of the button (ghost
, outline
, purple
, gray
, red
, green
, yellow
, blue
). When using ghost
, the button itself is invisible and only the content of the button will be shown.
In JavaScript, set this property using the buttonStyle
property to avoid conflict with the native style
property.
button-border-radius
Section titled button-border-radius
Adicionado em:
astro@4.8.0
The border radius of the button (normal
, rounded
). When using rounded
, the button will have rounded corners and uniform padding on all sides.
In JavaScript, set this property using the buttonBorderRadius
property.
astro-dev-toolbar-badge
Section titled astro-dev-toolbar-badgeShows a badge.
The slot of the component will be used as the content of the badge.
The size of the badge (small
, large
).
badge-style
Section titled badge-styleThe style (color) of the badge (purple
, gray
, red
, green
, yellow
, blue
).
In JavaScript, set this property using the badgeStyle
property to avoid conflict with the native style
property.
astro-dev-toolbar-card
Section titled astro-dev-toolbar-cardShows a card. Specify an optional link
attribute to make the card act like an <a>
element.
When making a card using JavaScript, a clickAction
property can be specified to make the card act like a <button>
element.
The slot of the component will be used as the content of the card.
card-style
Section titled card-styleThe style of the card (purple
, gray
, red
, green
, yellow
, blue
). The color is only applied to the border of the card on hover.
In JavaScript, set this property using the cardStyle
.
astro-dev-toolbar-toggle
Section titled astro-dev-toolbar-toggleShows a toggle element, acting as a checkbox. This element internally is a simple wrapper around a native <input type="checkbox">
element. The checkbox element can be accessed using the input
property.
astro-dev-toolbar-radio-checkbox
Section titled astro-dev-toolbar-radio-checkbox
Adicionado em:
astro@4.8.0
Shows a radio checkbox. Similar to the astro-dev-toolbar-toggle
component, this element is a simple wrapper around a native <input type="radio">
element. The radio element can be accessed using the input
property.
toggle-style
Section titled toggle-styleThe style of the toggle (purple
, gray
, red
, green
, yellow
, blue
).
In JavaScript, set this property using the toggleStyle
property.
astro-dev-toolbar-highlight
Section titled astro-dev-toolbar-highlightCan be used to highlight an element on the page. In most cases, you’ll want to position and resize this element using the top
, left
, width
and height
CSS properties to match the element you want to highlight.
style
Section titled styleThe style of the highlight (purple
, gray
, red
, green
, yellow
, blue
).
An icon to show in the top right corner of the highlight.
astro-dev-toolbar-tooltip
Section titled astro-dev-toolbar-tooltipShows a tooltip with different sections. This component is set to display: none;
by default and can be made visible using a data-show="true"
attribute.
Sections are defined using the sections
property. This property is an array of objects with the following shape:
This component is often combined with the astro-dev-toolbar-highlight
component to show a tooltip when hovering a highlighted element:
astro-dev-toolbar-icon
Section titled astro-dev-toolbar-iconShows an icon. An icon from the icon list can be specified using the icon
attribute, or the SVG markup of an icon can be passed as a slot.
Icons
Section titled IconsCurrently, the following icons are available and can be used in any component that accepts an icon:
astro:logo
warning
arrow-down
bug
file-search
check-circle
gear
lightbulb
checkmark
dots-three
copy
All of the above icons have fill="currentColor"
set by default and will inherit their color from the parent element.