{"id":20129363,"url":"https://github.com/ryanmorr/action-observer","last_synced_at":"2025-04-09T16:10:17.103Z","repository":{"id":17076321,"uuid":"19841307","full_name":"ryanmorr/action-observer","owner":"ryanmorr","description":"Declarative event handling to capture unbound action events","archived":false,"fork":false,"pushed_at":"2024-10-21T16:20:17.000Z","size":526,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-09T16:09:54.594Z","etag":null,"topics":["action","event","javascript","observer","responsive"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ryanmorr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-05-16T01:47:20.000Z","updated_at":"2024-10-20T20:59:57.000Z","dependencies_parsed_at":"2024-11-13T20:45:29.323Z","dependency_job_id":null,"html_url":"https://github.com/ryanmorr/action-observer","commit_stats":{"total_commits":51,"total_committers":1,"mean_commits":51.0,"dds":0.0,"last_synced_commit":"a3e9a2f193a69055677b1395e540db6364e6f8f8"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryanmorr%2Faction-observer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryanmorr%2Faction-observer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryanmorr%2Faction-observer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryanmorr%2Faction-observer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ryanmorr","download_url":"https://codeload.github.com/ryanmorr/action-observer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248065283,"owners_count":21041871,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["action","event","javascript","observer","responsive"],"created_at":"2024-11-13T20:33:53.757Z","updated_at":"2025-04-09T16:10:17.082Z","avatar_url":"https://github.com/ryanmorr.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# action-observer\n\n[![Version Badge][version-image]][project-url]\n[![License][license-image]][license-url]\n[![Build Status][build-image]][build-url]\n\n\u003e Declarative event handling to capture unbound action events\n\n## Description\n\nAction observer is a solution for capturing user triggered action events (click, submit) as they bubble up to the document element. This is most useful as a means of maintaining responsiveness pre-initialization of JavaScript heavy apps, allowing action events to be handled immediately or queued for processing once the page has finished initializing. Please refer to the [blog post](http://www.ryanmorr.com/maintain-responsiveness-by-capturing-unbound-action-events) to read more.\n\n## Install\n\nDownload the [CJS](https://github.com/ryanmorr/action-observer/raw/master/dist/cjs/action-observer.js), [ESM](https://github.com/ryanmorr/action-observer/raw/master/dist/esm/action-observer.js), [UMD](https://github.com/ryanmorr/action-observer/raw/master/dist/umd/action-observer.js) versions or install via NPM:\n\n``` sh\nnpm install @ryanmorr/action-observer\n```\n\n## Usage\n\nFirst, you'll want to add the script to the `\u003chead\u003e` of your HTML. This is important because it must be loaded *before* the DOM has started to load and scripts have begun executing so that action events can be captured pre-initialization.\n\nNext, add an `action-observe` attribute to any element you wish to observe events on. Adding the `action-observe` attribute to a form will automatically observe submit events for that form. Otherwise, click events will be observed for any other type of element. The value of the `action-observe` attribute is used as the reference to that element in the JavaScript API for action observer:\n\n```html\n\u003c!-- Observe click events --\u003e\n\u003ca href=\"#\" action-observe=\"add\"\u003eAdd Item\u003c/a\u003e\n\n\u003c!-- Observe submit events --\u003e\n\u003cform method=\"GET\" action=\"#\" action-observe=\"search\"\u003e\n    \u003cinput type=\"search\" name=\"search\" /\u003e\n    \u003cbutton type=\"submit\"\u003eSubmit\u003c/button\u003e\n\u003c/form\u003e\n```\n\nThere are two different ways to approach implementation in JavaScript. The first option is to define a callback function to be called for each action event dispatched from the target element, including events dispatched before the handler was defined, allowing you to handle actions immediately:\n\n```javascript\nimport { observe } from '@ryanmorr/action-observer';\n\n// Observes the element(s) with the action-observe=\"add\" attribute\nobserve('add', (event, element) =\u003e {\n    // Handle the action immediately                  \n});\n```\n\nThe other option is to check if an action event was dispatched on a target element once your scripts have completed initializing to address the action afterwards:\n\n```javascript\nimport { getActions } from '@ryanmorr/action-observer';\n\n// Returns an array of all events dispatched from the action-observe=\"add\" attribute\nconst actions = getActions('add');\n\nactions.forEach(({event, element}) =\u003e {\n    // Handle each action after the fact (better late than never)    \n});\n```\n\nOnce your primary JavaScript has loaded, you can choose to stop observing specific elements or stop functionality entirely for all elements:\n\n```javascript\nimport { unobserve, disable } from '@ryanmorr/action-observer';\n\n// Once the application code has initialized the link, stop observing\nif (isInitialized('link')) {\n    unobserve('link');\n}\n\n// Once all initializing code is complete, disable all functionality\nif (isAppLoaded()){\n    disable();\n}\n```\n\n## License\n\nThis project is dedicated to the public domain as described by the [Unlicense](http://unlicense.org/).\n\n[project-url]: https://github.com/ryanmorr/action-observer\n[version-image]: https://img.shields.io/github/package-json/v/ryanmorr/action-observer?color=blue\u0026style=flat-square\n[build-url]: https://github.com/ryanmorr/action-observer/actions\n[build-image]: https://img.shields.io/github/actions/workflow/status/ryanmorr/action-observer/node.js.yml?style=flat-square\n[license-image]: https://img.shields.io/github/license/ryanmorr/action-observer?color=blue\u0026style=flat-square\n[license-url]: UNLICENSE\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryanmorr%2Faction-observer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fryanmorr%2Faction-observer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryanmorr%2Faction-observer/lists"}