Actions
Iridium actions are points on your application's page(s) where a user can interact with your server (and sometimes the client) to perform certain operations.
The most common form of actions are buttons that perform a network request to the server to run a user defined callback with some functionality that will, as an example, launch a process, delete a record, log out the current user, etc.
Generally, you'll be creating actions in one of three places:
- Table Actions
- These are actions that interact directly with your table, or a row on your table.
- Page Actions
- These include header actions on your pages. An example might be a Generate Report action on a model's view page
- User Menu Actions
- These actions allow users to interact with their specific account in their user menu.
Routing
Actions with a defined callback are registered with your panel's mux with a unique route based on where the action is defined in your app.
For example, a header action called Generate Report on a page called Appointments in your Admin panel will register a route like /admin/appointments/header-actions/generate-report with your mux. This is done for you, but it's important to remind you that the action name must be unqiue for that page's header actions. As it goes with elsewhere in Iridium.
Each action registered to a panel is wrapped in the overall panel's middleware as well.
Common methods
Callback
The Callback method provides a location to write a user-defined function to execute server-side logic when a user clicks your button.
The displayed action button will call its associated action route when clicked, and Iridium will generate the relevant context and inject it for you into your callback on the server.
Action("generate_report").
Callback(func (ctx *context.PanelPage) {
reports.GenerateMonthly()
})Hidden
The Hidden method allows you to hide the action from displaying on your page:
// static
Action("my-action").
Hidden()
// callback
Action("my-action").
HiddenFn(func (ctx context.PanelPage) bool {
return false
})Route is still registered!
Keep in mind that hidden does not un-register your action's route. This means skilled users could still trigger your action even if it is visually hidden from them.
Make sure to secure your actions if you feel it necessary. See our auth for Iridium's own authorization methods.
Disabled
The Disabled method will disable the button on your form, preventing user interaction.
// static
Action("my-action").
Disabled()
// callback
Action("my-action").
DisabledFn(func (ctx context.PanelContext) bool {
return false
})Route is still registered!
Keep in mind that Disabled does not un-register your action's route. This means skilled users could still trigger your action even if it is visually disabled for them.
Make sure to secure your actions if you feel it necessary. See our auth for Iridium's own authorization methods.
Requires Confirmation
The RequiresConfirmation message will display a modal asking the user to confirm their choice prior to sending the action's request to the server.
// static
Action("my-action").
RequireConfirmation()
// callback
Action("my-action").
RequiresConfirmationFn(func (ctx context.PanelPage) bool {
return true
})Confirmation Title
Requires Confirmation must be enabled.
The confirmation title allows you to define a custom title to display in your confirmation modal:
// static
Action("my-action").
RequiresConfirmation().
ConfirmationTitle("Delete record?")
// callback
Action("my-action").
RequiresConfirmation().
ConfirmationTitleFn(func (ctx context.PanelPage) string {
if time.Now().Hour() >= 17 {
return "Delete record? It's getting late..."
}
return "Delete record?"
})Confirmation Message
Requires Confirmation must be enabled.
The confirmation message allows you to define a custom message to display in your confirmation modal:
// static
Action("my-action").
RequiresConfirmation().
ConfirmationMessage("Hey! You really sure you wanna do this?")
// callback
Action("my-action").
RequiresConfirmation().
ConfirmationMessageFn(func (ctx context.PanelPage) string {
if time.Now().Hour() >= 17 {
return "Hey it's past working hours. Clock out friend!"
}
return "This action cannot be undone"
})Label
The Label method allows you to define a label for your action's button:
// static
Action("my-action").
Label("Run an action")
// callback
Action("my-action").
LabelFn(func (ctx context.PanelPage) string {
return "Run an action"
})Icon
The Icon method allows you to specify a prefix icon for you action's button:
// static
Action("my-action").
Icon(icons.Anvil)
// callback
Action("my-action").
IconFn(func (ctx context.PanelPage) *icon.Icon {
return icons.Anvil
})Shortcut
The Shortcut method allows you to define an alpine shortcut/keyboard-event to trigger your action.
See more about alpine shortcuts/keyboard-events here.
// static
Action("my-action").
Shortcut("Ctrl+K")
// callback
Action("my-action").
ShortcutFn(func (ctx context.PanelContext) string {
return "Cmd+Shift+P"
})Hide Shortcut
The HideShortcut method will not display the shortcut as part of the label of your button.
// static
Action("my-action").
HideShortcut()
// callback
Action("my-action").
HideShortcutFn(func (ctx context.PanelPage) bool {
return true
})Style
The Style method allows you to specify a style for your action button.
The available styles are:
- Primary
- Secondary
- Destructive
- Outlink
- Ghost
- Link
Action("my-action").
Primary(). // Default
Secondary().
Destructive().
Outline().
Ghost().
Link().
//# You can also directly define the style based on our style enum #//
// static
Action("my-action").
Style(config.ButtonStyleOutline)
// callback
Action("my-action").
StyleFn(func (ctx context.PanelPage) config.ButtonStyle {
return config.ButtonStyleOutline
})Size
The Size method allows you to specify the size for your action button.
The available sizes are:
- Small
- Medium
- Large
Action("my-action").
Small().
Medium(). // Default
Large()
//# You can also directly define the size based on our size enum #//
// static
Action("my-action").
Size(config.ButtonSizeSmall)
// callback
Action("my-action").
SizeFn(func (ctx context.PanelPage) config.ButtonStyle {
return config.ButtonSizeLarge
})Attributes / Client side
You can apply attributes to the actions <button/> tag directly using the Attributes method.
Client Side
Using attributes without a defined Callback is how you can define a client-side action. You can use alpine.js attributes to interact with other components on the page (example), or fire off your own client side JS methods.
// static
Action("my-action").
Attributes(map[string]any{
"@click": "alert('Hey you clicked me!')",
})
// callback
Action("my-action").
Label("Visit documentation").
AttributesFn(func (ctx context.PanelPage) map[string]any{
// Prefer server-side redirects if possible.
// It's your application however!
return map[string]any{
"@click": "window.location.href='https://iridiumgo.dev'",
}
})