@cocreate/actions

中文文档
2.0.2发布于 2026/11/07MITCommonJS近 30 天下载 3381

Chain multiple component executions to generate your desired logic, when one action is complete next one will start. The sequence goes until all actions have been completed. Vanilla javascript, easily configured using HTML5 attributes and/or JavaScript AP

原始使用文档
文档目录27 节
  1. Documentation
  2. Table of Contents
  3. Features
  4. Installation
  5. CDN
  6. Usage
  7. Chain Executions
  8. Pass Parameters
  9. Chain Across Modules
  10. Delay and Wait
  11. How it Works
  12. Anatomy of an Action Chain
  13. Sequencing and Completion
  14. Control Actions
  15. Element Method Actions
  16. Registered Actions
  17. Targeting Elements
  18. JavaScript API
  19. Register an Action
  20. Register Several at Once
  21. Abort a Chain
  22. Behavior Notes
  23. Announcements
  24. Roadmap
  25. How to Contribute
  26. About
  27. License

@cocreate/actions

A sequential execution pipeline for the browser, configured entirely through HTML5 attributes. A single actions attribute chains any number of component executions together: each action runs, signals completion, and only then does the next one begin. Actions are contributed by every CoCreate module, so saving a record, calling a third-party API, rendering a template, waiting on an event, and following a link compose into one declarative sequence without writing JavaScript.


Documentation

For a complete guide and working demo refer to the documentation.

Table of Contents


Features

  • Declarative Sequencing: Chain any number of executions in a single attribute, running strictly in order.
  • Asynchronous Awareness: Each action signals its own completion, so database writes, network requests, and renders finish before the next step starts.
  • Universal Registration: Any module can contribute an action by name, making its behavior available to markup across the entire application.
  • Element Method Invocation: Call methods directly on queried elements, awaiting any promises they return.
  • Flow Control: Wait for events, delay execution, trigger other chains, submit forms, and toggle button state as first-class steps.
  • Abort Semantics: An action can halt the remainder of the chain when a step fails or a condition is not met.
  • Relational Targeting: Action parameters accept the full CoCreate selector grammar, including $parent, $next, $closest, and $document.
  • Automatic Binding: Elements added to the DOM later are bound automatically, so rendered templates behave like static markup.

Installation

npm install @cocreate/actions
yarn add @cocreate/actions

CDN

<script src="https://cdn.CoCreateJS.com/actions/latest/CoCreate-actions.min.js"></script>

Usage

Chain Executions

Actions run left to right. Each one waits for the previous to complete.

<button actions="save, reset">Save</button>

Pass Parameters

A parameter is supplied in parentheses and is usually a selector.

<button actions="save, action($document #refresh-list)">Save</button>

Chain Across Modules

Because every module registers its actions by name, a single chain can persist a record, call an API, and update the interface.

<button actions="save, stripe.customers.create, render, reset">
    Create customer
</button>

Delay and Wait

<button actions="save, timeout(1000), action($document #notify)">Save</button>

How it Works

  1. A click listener is bound to every [actions] element, including elements inserted later.
  2. When triggered, the attribute is split on commas into an ordered list of staged actions.
  3. Each entry is parsed into a name and optional parenthesized parameters. A name containing a dot is split into an action name and a method, so stripe.customers.create invokes the stripe action with the method customers.create.
  4. The first action runs. The pipeline attaches a one-time listener for that action's completion event before invoking it.
  5. When the completion event fires, the next action starts. This repeats until the chain is exhausted or aborted.
  6. Once every action has completed, the pipeline performs a native form submission if a real form action is present, then follows any link on the element.

Anatomy of an Action Chain

<button actions="save, timeout(500), stripe.customers.create">Go</button>
PartMeaning
saveA registered action, resolved by name
,Sequential separator, the next action waits for the previous
timeout(500)An action with a parameter
stripe.customers.createAction stripe invoked with method customers.create

Sequencing and Completion

An action is considered finished when its completion event fires on the element. Registration declares that event, and it defaults to the action's own name.

Actions.init({
    name: "save",
    endEvent: "saved",
    callback: (action) => { /* … */ }
});

The callback receives everything needed to do its work and to report back:

PropertyDescription
elementThe element carrying the actions attribute
formThe closest ancestor form, when present
nameResolved action name
methodMethod portion of a dotted action name
paramsParameter string, when supplied
endEventCompletion event the pipeline is listening for
end(detail)Dispatches the completion event, advancing the chain
continue()Advances the chain directly
abort()Halts the chain, skipping all remaining actions

An action that neither dispatches its completion event nor calls continue() or abort() leaves the chain waiting, which is the intended behavior for steps that resolve later.

Actions written with a dotted name receive a unique completion event per run, so concurrent chains using the same provider never resolve one another.


Control Actions

These are always available and require no registration.

ActionParameterBehavior
timeout(ms)millisecondsPauses the chain for the given duration
event(name)event nameWaits for that event to fire on the element
action(selector)selectorClicks the matching elements, triggering their own chains
submitnoneDispatches a cancelable submit on the closest form and waits for a submitted event
disablednoneToggles the disabled attribute on the closest button
<button actions="disabled, save, disabled">Save</button>
<button actions="save, event(uploaded), render">Save and wait for upload</button>

Element Method Actions

When a name is not registered and is not a control action, the pipeline treats it as a method call on the queried elements. Every match that exposes a function of that name has it invoked, and any returned promises are awaited before the chain continues.

<button actions="read($document #profile)">Reload profile</button>

This calls read() on #profile. The same mechanism covers renderValue, getValue, and any method a module adds to an element, which makes the pipeline extensible without registering anything.

A method action requires a parameter to select its targets. submit and disabled are the only control actions that operate without one.


Registered Actions

Modules register their actions on import. Commonly available names include:

ActionCompletion EventProvided By
savesaved@cocreate/elements
deletedeleted@cocreate/elements
resetreset@cocreate/elements
renderrender@cocreate/render
click, toggle, hover, input, change, focus, blur, and every other event prefix<name>End@cocreate/events
Any provider declared with api="name"per run@cocreate/api

Because event prefixes are registered as actions, a binding can be invoked as a step rather than by user interaction:

<button actions="save, click($document #panel)">Save and open panel</button>

Targeting Elements

Parameters accept standard CSS selectors plus the CoCreate relational operators, and a trailing [] selects every match rather than the first.

OperatorResolves To
$documentThe owning document
$parentThe parent element
$nextThe next element sibling
$previousThe previous element sibling
$closest <selector>Nearest matching ancestor
$frameThe frame element of the current document
$topThe top-level document
<button actions="save, action($closest .card .refresh[])">Save</button>

JavaScript API

Register an Action

import Actions from "@cocreate/actions";

Actions.init({
    name: "notify",
    endEvent: "notified",
    callback: async (action) => {
        await sendNotification(action.params);
        action.end();
    }
});

Register Several at Once

init() accepts an array, and name may itself be an array of aliases.

Actions.init([
    { name: ["publish", "release"], endEvent: "published", callback: publish },
    { name: "archive", endEvent: "archived", callback: archive }
]);

Registration is idempotent: a name that is already registered is left untouched, so the first module to claim a name owns it.

Abort a Chain

Actions.init({
    name: "validate",
    callback: (action) => {
        if (!action.form.checkValidity()) return action.abort();
        action.end();
    }
});
<button actions="validate, save, reset">Submit</button>

Behavior Notes

  • Chains are triggered by click. Elements without a native click target can still be driven from another chain using action(selector).
  • The pipeline resolves the nearest [actions] ancestor, so icons and spans inside a button trigger the button's chain.
  • Default click behavior is prevented while a chain runs. A form whose action is a real URL submits natively instead.
  • After the final action, a submit button inside a form with a real action URL submits the form, and any href, target, or state_to on the element is followed.
  • Aborting stops every remaining action in the chain, including the trailing submit and link behavior.

Announcements

All updates to this library are documented in our CHANGELOG and releases. You may also subscribe to email for releases and breaking changes.


Roadmap

Upcoming enhancements include:

  • Conditional actions and branching
  • Parallel action groups
  • Error handling and retry policies
  • Named chains reusable across elements
  • Development mode tracing of chain execution

If you are interested in the future direction of this project, please take a look at our open issues and pull requests. We would love to hear your feedback.


How to Contribute

We encourage contribution to our libraries (you might even score some nifty swag), please see our CONTRIBUTING guide for details.

We want this library to be community-driven, and CoCreate led. We need your help to realize this goal. To help make sure we are building the right things in the right order, we ask that you create issues and pull requests or merely upvote or comment on existing issues or pull requests.

We appreciate your continued support, thank you!


About

CoCreate-actions is guided and supported by the CoCreate Developer Experience Team.

Please Email the Developer Experience Team here in case of any queries.

CoCreate-actions is maintained and funded by CoCreate. The names and logos for CoCreate are trademarks of CoCreate, LLC.


License

The MIT License (MIT)

版本与发布记录

@cocreate/actions 共发布 181 个版本,首次发布于 2021/01/23;过去 12 个月发布了 10 个版本。

版本发布日期距今
2.0.2latest2026/11/073 天前
2.0.12026/11/064 天前
2.0.02026/11/037 天前
1.27.02026/10/2120 天前
1.24.02026/08/013 个月前
1.23.02026/07/293 个月前

关于 @cocreate/actions 的常见问题

@cocreate/actions 是什么?

Chain multiple component executions to generate your desired logic, when one action is complete next one will start. The sequence goes until all actions have been completed. Vanilla javascript, easily configured using HTML5 attributes and/or JavaScript AP

如何安装 @cocreate/actions?

在项目目录运行 npm install @cocreate/actions,或使用 pnpm add / yarn add / bun add。

@cocreate/actions 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/cocreate__actions 是否存在。

@cocreate/actions 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@cocreate/actions 可以商用吗?

@cocreate/actions 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@cocreate/actions 的下载量有多少?

根据 npm 官方下载统计,@cocreate/actions 在 2026/10/10 – 2026/11/08 这 30 天内被下载 3,381 次,日均约 113 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cocreate/actions 还在维护吗?

最新版本 2.0.2 发布于 2026/11/07(3 天前);至今共发布 181 个版本,其中过去 12 个月发布了 10 个;首次发布于 2021/01/23。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。