Features • Installation • Usage • API Reference • Accessibility (A11Y) • Theming • Examples
A fully accessible, themeable, and server-rendered dropdown component for Go web applications. Built with the templ library for seamless integration into Go-based frontends.
- Accessible by Design: Fully compliant with the WAI-ARIA Menu Button Pattern, including proper roles, focus management, and keyboard interactions.
- Zero Dependencies: Built with native Go and
templ— no external JavaScript or CSS required. - Highly Configurable: Customize behavior such as positioning, default open state, and alignment to fit your needs.
- Multiple Instances: Use several
gropdowncomponents on the same page, each with its own configuration and styling. - Themeable via CSS Variables: Easily style the component using CSS custom properties. Supports light and dark themes via the
data-themeattribute and allows for custom themes. - Versatile Items: Dropdown items can be buttons or links (
<a>). External links are automatically marked with an icon for better UX.
To install the Dropdown module, use the go get command:
go get github.com/indaco/gropdownEnsure your project is using Go Modules (it will have a go.mod file in its root if it already does).
Tip
refer to the Examples section.
Import the Dropdown module into your project:
import "github.com/indaco/gropdown"// Default options
dropdownConfig := gropdown.NewConfigBuilder().Build()Users can access each configuration option using the corresponding With method, such as gropdown.WithOpen(true) or gropdown.WithPosition(gropdown.Left).
| Option | Type | Default | Description |
|---|---|---|---|
Open |
bool | false |
indicates whether the dropdown menu is currently open. |
Placement |
Placement | Bottom |
indicates the position of the dropdown content relative to the button. Options: Top, Bottom, Left, and Right. |
Animation |
bool | true |
indicates whether the dropdown button should use animations on open and close. |
CloseOnOutsideClick |
bool | true |
indicates whether the dropdown should be closed when a click occurs outside of it. |
To allow configurations to be accessible at any level in the dropdown hierarchy and make each component on the same page function independently, gropdown makes use of the templ component context and the implicit ctx variable. You can read more about templ component context here.
In your function handler, create a configuration for gropdown and attach it to the request context passed into the handler function.
func HandleHome(w http.ResponseWriter, r *http.Request) {
dropdownConfig := gropdown.NewConfigBuilder().WithPlacement(gropdown.Top).Build()
configMap := gropdown.NewConfigMap()
configMap.Add("demo", dropdownConfig)
ctx := context.WithValue(r.Context(), gropdown.ConfigContextKey, configMap)
err := Page().Render(ctx, w)
if err != nil {
return
}
}In your templ file, defines the structure for the dropdown component.
Important
It is crucial to ensure that the value passed to gropdown.Root matches the one used when adding the gropdownConfig to the configMap as per step above. This ensures that multiple dropdowns on the same page function independently.
// Set the button label.
@gropdown.Root("demo") {
@gropdown.Button("Menu")
@gropdown.Content() {
@gropdown.Item("Profile",
gropdown.ItemOptions{
Href: "/profile",
Icon: profileIcon,
},
)
@gropdown.Item("Settings",
gropdown.ItemOptions{
Href: "/settings",
Icon: settingsIcon,
},
)
@gropdown.Divider()
@gropdown.Item("GitHub",
gropdown.ItemOptions{
Href: "https://github.com",
External: true,
Icon: globeIcon,
},
)
@gropdown.Divider()
@gropdown.Item("Button",
gropdown.ItemOptions{
Icon: clickIcon,
Attrs: templ.Attributes{"onclick": "alert('Hello gropdown');"},
},
)
}
}gropdown leverages the templ library's features, including CSS Components and JavaScript Templates, to encapsulate all necessary styling and functionality without relying on external dependencies.
GropdownCSS: it supplies the required CSS, encapsulating the visual design and layout specifics of the component.GropdownJS: it provides the JavaScript logic essential for dynamic behaviors such as displaying, keyboard navigation and interaction with the component.
To facilitate integration with Go's template/html standard library, gropdown includes a dedicated HTMLGenerator type to seamlessly integrate the component into web applications built with Go's html/template standard library.
There are methods acting as wrappers to the templ's templ.ToGoHTML, generate the necessary HTML to be embedded them into server-rendered pages:
GropdownCSSToGoHTML: render theGropdownCSScomponent into atemplate.HTMLvalue.GropdownJSToGoHTML: render theGropdownJScomponent into atemplate.HTMLvalue.
| Property | Type | Description |
|---|---|---|
id |
string | The unique identifier for the dropdown menu component. |
| Property | Type | Description |
|---|---|---|
label |
string | The text displayed for the dropdown menu button. |
icon |
ButtonIcon | The icon displayed next to the dropdown menu button. |
None
| Property | Type | Description |
|---|---|---|
label |
string | The text displayed for the dropdown menu item. |
opts |
ItemOptions | The options for configuring the behavior and appearance of the item. |
None
The dropdown component is designed to be accessible to screen readers and supports keyboard navigation according to the WAI-ARIA pattern for menu buttons.
Ensure that proper ARIA attributes are used to convey the state and role of the dropdown elements.
- Focusing the Dropdown:
- Use the
Tabkey to navigate to the dropdown button. PressingEnterorSpacewill open the dropdown menu.
- Use the
- Navigating within the Dropdown Menu:
- Use the
Arrowkeys to move focus between items within the dropdown menu. - Pressing
HomeorEndkeys will move focus to the first or last item respectively. - Use
A-Zora-zkeys to move focus to the next menu item with a label that starts with the typed character if such a menu item exists. Otherwise, focus does not move.
- Use the
- Selecting an Item:
- Press
Enterto select the currently focused item in the dropdown menu.
- Press
- Closing the Dropdown:
- Press
Escapeto close the dropdown and sets focus to the menu button.
- Press
Dropdown is themeable using CSS variables (prefix gdd) to customize the appearance according to your design.
By default, it supports both light and dark modes. In addition to the built-in modes, you can define your
own custom themes using the data-theme attribute. Simply add a data-theme attribute to the root element of your application
and define the corresponding CSS variables for your custom theme.
For a comprehensive list of CSS custom properties, along with their default values and descriptions, please consult the gropdown CSS custom Props document.
- with
a-h/templ - custom animations
- custom button icon
- with
template/html - icon only button
- multiple dropdowns
- positioning
- theming
Contributions are welcome! Feel free to open an issue or submit a pull request.
To set up a development environment for this repository, you can use devbox along with the provided devbox.json configuration file.
- Install devbox by following the instructions in the devbox documentation.
- Clone this repository to your local machine.
- Navigate to the root directory of the cloned repository.
- Run
devbox installto install all packages mentioned in thedevbox.jsonfile. - Run
devbox shellto start a new shell with access to the environment. - Once the devbox environment is set up, you can start developing, testing, and contributing to the repository.
Additionally, you can make use of the provided Makefile to run various tasks:
make build # The main build target
make examples # Process templ files in the _examples folder
make templ # Process TEMPL files
make test # Run go tests
make help # Print this help messageThis project is licensed under the MIT License - see the LICENSE file for details.
