Skip to main content

Documentation Index

Fetch the complete documentation index at: https://zenbulabs.mintlify.app/llms.txt

Use this file to discover all available pages before exploring further.

Advice lets a plugin wrap or replace a function or React component owned by another plugin, without modifying the original source. This API is inspired from Emacs, where defadvice is used to modify existing functions without editing their source.

Registering advice

Call this.advise(...) from inside a service:
import { Service } from "@zenbujs/core/runtime"

export class ChromeService extends Service.create({ key: "chrome" }) {
  evaluate() {
    this.setup("wrap-counter", () =>
      this.advise({
        view: "entrypoint",
        moduleId: "App.tsx",
        name: "Counter",
        type: "around",
        modulePath: "src/content/wrap-counter.tsx",
        exportName: "WrapCounter",
      }),
    )
  }
}
FieldMeaning
viewView type to apply the advice in. "*" for every view.
moduleIdSuffix of the source file that exports the target.
nameName of the export to advise.
typeOne of "replace", "before", "after", "around".
modulePathPath to your wrapper module, relative to the plugin root (or absolute).
exportNameNamed export inside the wrapper module.
Returns an unregister function. Use it as a setup cleanup.

Around advice

Receives the original as __original in props, making it the most flexible advice type.
src/content/wrap-counter.tsx
export function WrapCounter(props) {
  const Original = props.__original
  return (
    <div className="bordered">
      <Original {...props} />
    </div>
  )
}
Use this when you want to render the original but add structure around it or change its props.

Replace advice

Substitutes the export entirely. The wrapper is used in place of the original.
export function WrapCounter() {
  return <button>Replaced</button>
}

Before / After

before and after advice run extra logic around the original function.
// before: runs your function, then calls the original
export function beforeSave(args: { path: string }) {
  console.log("saving", args.path)
}

// after: calls the original, then runs your function with the result
export function afterSave(result: void, args: { path: string }) {
  console.log("saved", args.path)
}

Hot reloading

Adding, removing, or editing a this.advise(...) call reloads the affected views so the new advice takes effect. Edits inside the advice module itself hot-replace through Vite’s normal HMR.