import{_ as i,o as a,c as n,ae as t}from"./chunks/framework.C8AWLET_.js";const o=JSON.parse('{"title":"Creating Custom Plugins","description":"","frontmatter":{},"headers":[],"relativePath":"plugins/custom.md","filePath":"plugins/custom.md"}'),l={name:"plugins/custom.md"};function h(p,s,e,k,r,d){return a(),n("div",null,[...s[0]||(s[0]=[t(`
There are two main ways to expose a plugin's functionality: Static/Manual Imports (cleaner for large projects) or Global/Automatic Window Injection (easier for quick scripts and global helpers).
A plugin is a standard JavaScript function. By convention, if a plugin adds a global helper or component, it should be prefixed with an underscore (_).
// plugins/my-utils.js
export const MyUtils = ($) => {
// 1. Attach to the SigPro instance
$.capitalize = (str) => str.charAt(0).toUpperCase() + str.slice(1);
// 2. Attach to the Window (Global access)
window._hello = (name) => div(\`Hello, \${$.capitalize(name)}!\`);
// 3. You can also return values if needed
return { version: '1.0.0' };
};This approach keeps your global namespace clean. You import the logic only where you need it, but the plugin still initializes the core $ extensions.
// main.js
import { $ } from 'sigpro';
import { MyUtils } from './plugins/my-utils.js';
$.plugin(MyUtils);
// App.js
export default () => {
const name = "sigpro";
// $.capitalize was added by the plugin
return h1($.capitalize(name));
};If your plugin defines global tags (like _button or _hello), you should attach them to the window object inside the plugin function. This makes them available everywhere without imports.
// plugins/theme.js
export const Theme = ($) => {
const $dark = $(false);
window._themeToggle = () => button({
onclick: () => $dark(v => !v),
class: () => $dark() ? 'bg-black text-white' : 'bg-white text-black'
}, "Toggle Mode");
};
// main.js
$.plugin(Theme).then(() => {
// _themeToggle is now a global function
$.mount(App);
});If your plugin needs to load external data or scripts before the app starts, make it async. SigPro will wait for it.
export const ConfigLoader = async ($) => {
const res = await fetch('/config.json');
const config = await res.json();
$.config = config; // Attach loaded config to SigPro
};
// Usage
$.plugin(ConfigLoader).then(() => {
console.log("Config loaded:", $.config);
$.mount(App);
});| Rule | Description |
|---|---|
| Prefixing | Use _ for UI components (_modal) and $. for logic ($.fetch). |
| Idempotency | Ensure calling $.plugin(MyPlugin) twice doesn't break the app. |
| Encapsulation | Use the $ instance passed as an argument rather than importing it again inside the plugin. |
| Reactivity | Always use $(...) for internal state so the app stays reactive. |
Custom plugins don't require extra packages, but ensure your build tool (Vite/Bun) is configured to handle the module imports.
npm install sigpropnpm add sigproyarn add sigprobun add sigpro