Plugin Development Guide

Docs home

Forge Select is built around a small plugin architecture so behavior can be extended without forking the core. A plugin is a plain object with lifecycle hooks that Forge Select calls at the right time.

Plugin interface

interface ForgeSelectPlugin {
  name: string;
  onInit?(select: ForgeSelect): void;
  onOpen?(select: ForgeSelect): void;
  onClose?(select: ForgeSelect): void;
  onDestroy?(select: ForgeSelect): void;
}

All hooks are optional — implement only the ones your plugin needs.

Registering a plugin

import ForgeSelect from "forge-select";
import clearOnEscape from "./clear-on-escape-plugin";

new ForgeSelect("#country", {
  plugins: [clearOnEscape],
});

Plugins run in the order they're listed. Multiple instances can share the same plugin object as long as the plugin doesn't keep instance-specific state on itself (keep instance state on select or in a closure per call).

Example: a complete plugin

A plugin that clears the selection when the user presses Escape while the dropdown is closed:

// clear-on-escape-plugin.js
export default {
  name: "clear-on-escape",

  onInit(select) {
    this._handler = (event) => {
      if (event.key === "Escape") {
        select.setValue(null);
      }
    };
    select.el.addEventListener("keydown", this._handler);
  },

  onDestroy(select) {
    select.el.removeEventListener("keydown", this._handler);
  },
};

Guidelines for authoring plugins

See also