Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Attractive.js custom actions can start as a short js: expression, grow into a registered function, and become a class with a run() method when the behavior needs more structure. The registration pattern remains Attractive.activate({ addActions: ... }) for functions and classes. The main trade-off: the compact js: shortcut uses new Function(), so it requires unsafe-eval in the site’s Content Security Policy.

Choose an implementation form that fits the behavior

The Rails Designer author’s custom-actions guide presents three ways to add behavior. Use the smallest form that stays clear and reusable; promote the logic as it gains a name, structure, or helper needs.

Form Best fit Main consideration
js: expression A short, one-off expression Compact, but uses new Function() and requires unsafe-eval in the CSP.
Registered function A named behavior with modest logic that may be reused Receives the element and a context object; register it through addActions.
Class with run() A larger behavior that benefits from instance structure or helpers Uses the same registration configuration; the optional Action base class adds helpers.

Use js: only for a small expression

The guide describes js: as a way to evaluate a JavaScript expression on the element, using Attractive.js’s normal action pipeline. That means the expression can participate in configured events and attributes such as data-debounce and data-delay. It is intended for behavior too small to justify a named action.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

There is a CSP consequence: the author says the implementation uses new Function(), which requires the unsafe-eval directive. If your site’s Content Security Policy disallows unsafe-eval, do not use this shortcut; register a function or class instead. The guide is describing the implementation, not offering an independent security assessment.

Promote reusable logic to a registered function

When behavior deserves a name but does not need a class, define a function and register it with Attractive.activate. For example, a sharing action might accept the triggering element and the context’s dataset:

function sharePage(element, { dataset }) {
  // Use element and dataset to implement the sharing behavior.
}

Attractive.activate({
  addActions: { sharePage }
});

According to the guide, the function receives the element plus a context object that can include value, target, targets, event, and dataset. Use the context values relevant to the action rather than reaching for a class solely to access them.

Use a class when the action needs structure

For behavior that has grown beyond a compact function, the guide shows a class with a run() method, still registered through addActions. An instance receives element and options. The optional Action base class provides value, dataset, and a dispatchEvent helper.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The author’s Reorder example uses that helper to emit a reorder:done event carrying IDs. This is useful when an action needs to report a result to other code, rather than only changing the triggering element. The guide does not establish that every action should extend Action; treat the base class as an optional source of helpers.

Organize actions and handle lifecycle events

Keep a growing set easy to find

For several actions, the author suggests storing each in its own file, importing them into a barrel module, and passing the resulting object to Attractive.activate({ addActions: actions }). This is an organizational option, not a required project convention.

Use hooks for shared handling

The guide describes three hooks: beforeAction runs before each action and can cancel it by returning false; afterAction runs after a successful action; and onError handles an action that throws. It also says a failure does not stop the rest of a chained action from continuing, and mentions Attractive.onError as a global fallback suitable for monitoring integration. These are claims from the author’s implementation guide, not independently tested behavior.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Keep related APIs separate from the custom-action choice

The same guide mentions optional keyboard support for event keys and global hotkeys, integration with native browser constraint validation, a shared JSON reactive store with text bindings, and Attractive Element, a custom-element base class that scopes actions and targets. These are related capabilities; none is a prerequisite for choosing between an expression, function, or class.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A separate overview published on 10 September 2026 described Attractive.js 1.0.0 as a pre-release at that time, and discussed @action/@target syntax and bundled actions. That is publication-time context, not confirmation of the project’s release status on 10 October 2026. The API descriptions here likewise reflect the Rails Designer author’s guide; current project documentation was not independently verified.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.