{"id":16616183,"url":"https://github.com/kidonng/animation-observer","last_synced_at":"2025-10-29T19:30:43.574Z","repository":{"id":50696208,"uuid":"519726925","full_name":"kidonng/animation-observer","owner":"kidonng","description":"Lightweight module to monitor DOM elements matching a CSS selector","archived":false,"fork":false,"pushed_at":"2023-07-18T09:38:27.000Z","size":1174,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-09T01:37:01.928Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://npm.im/animation-observer","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kidonng.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2022-07-31T09:05:27.000Z","updated_at":"2024-04-25T06:30:41.000Z","dependencies_parsed_at":"2024-09-25T06:51:06.718Z","dependency_job_id":"eebcd042-63bc-4950-a1ec-9f5dd4f5cd46","html_url":"https://github.com/kidonng/animation-observer","commit_stats":{"total_commits":36,"total_committers":2,"mean_commits":18.0,"dds":0.2222222222222222,"last_synced_commit":"0de39f4624ade9b3337f140047b6939ddc3d894e"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kidonng%2Fanimation-observer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kidonng%2Fanimation-observer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kidonng%2Fanimation-observer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kidonng%2Fanimation-observer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kidonng","download_url":"https://codeload.github.com/kidonng/animation-observer/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238874447,"owners_count":19545192,"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":[],"created_at":"2024-10-12T02:12:09.722Z","updated_at":"2025-10-29T19:30:38.243Z","avatar_url":"https://github.com/kidonng.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# animation-observer\n\n\u003e Lightweight module to monitor DOM elements matching a CSS selector\n\n## Install\n\n```sh\nnpm install animation-observer\n```\n\n## Usage\n\n```js\nimport {observe} from 'animation-observer'\n\nobserve('div', (element) =\u003e {\n\tconsole.log(element.id, 'just slid into the DOM.')\n})\n```\n\n## API\n\n```ts\ndeclare function observe(\n\tselector: string | string[],\n\tinitialize: (element: Element) =\u003e void,\n\toptions?: {\n\t\tevent?: 'start' | 'end' | 'cancel'\n\t\tduration?: string\n\t\tsignal?: AbortSignal\n\t\tname?: string\n\t},\n): AbortController\n```\n\n## Recipes\n\n### Stop the observer\n\nYou can pass a [`AbortSignal`](https://developer.mozilla.org/docs/Web/API/AbortSignal) to the `signal` option.\n\n\u003c!-- prettier-ignore --\u003e\n```js\nconst controller = new AbortController()\n\nobserve('img', () =\u003e {\n\tconsole.log('An image just showed up!')\n}, {\n\tsignal: controller.signal,\n})\n\ncontroller.abort()\n```\n\n### Listen to different events\n\nBy default, the function listens to the [`animationstart`](https://developer.mozilla.org/docs/Web/API/Element/animationstart_event) event, which triggers when a matching element [\"appears\"](#caveats).\n\nYou can listen to a different `event` in the options. The most prominent usage is to check element [\"disappears\"](#caveats):\n\n\u003c!-- prettier-ignore --\u003e\n```js\nobserve('div', (element) =\u003e {\n\tconsole.log(element.id, 'left the party.')\n}, {\n\tevent: 'cancel', // `animationcancel`\n})\n```\n\nOr execute a function after a `duration`:\n\n\u003c!-- prettier-ignore --\u003e\n```js\nobserve('input:focus', () =\u003e {\n\talert('Please fill in your answer quickly.')\n}, {\n\tevent: 'end', // `animationend`\n\tduration: '10s',\n})\n```\n\n`duration` has different meaning depending on `event`:\n\n- For `start`, it does nothing\n- For `end`, it is a delay\n- For `cancel`, it is a timeout (default: `9999s` a.k.a \"infinity\")\n\n### Caveats\n\n- By \"appearing\", it means `animationstart` and `animationend` events are fired when the element meets the following conditions:\n  - Element matches the selector\n  - Element is not `display: none` (`visibility: hidden` is fine)\n- By \"disappearing\", it means `animationcancel` event is fired when one of the following happens:\n  - Element no longer matches the selector (may still be in the DOM)\n  - Element is removed from the DOM\n  - Element becomes `display: none`\n\n### Custom name\n\nBy default, the function generates a random class name using [`crypto.randomUUID()`](https://developer.mozilla.org/docs/Web/API/Crypto/randomUUID).\n\nYou can specify a custom `name` in the options:\n\n\u003c!-- prettier-ignore --\u003e\n```js\nobserve('[href=\"https://www.random.org/\"]', () =\u003e {\n\tconsole.log('True randomness™️')\n}, {\n\tname: Math.random().toString(36).slice(2),\n})\n```\n\n## Browser support\n\nThis module uses CSS [`@layer`](https://developer.mozilla.org/docs/Web/CSS/@layer) to avoid conflicting with existing styles, which is supported since:\n\n- Chrome \u0026 Edge 99\n- Firefox 97\n- Safari 15.4\n\n## Credits\n\nThe first prototype is inspired by [@fregante](https://github.com/refined-github/refined-github/issues/5874#issuecomment-1200341987).\n\nSupport for multiple listeners matching the same element in [v2.1.0](https://github.com/kidonng/animation-observer/releases/tag/v2.1.0) is adapted from [Refined GitHub](https://github.com/refined-github/refined-github/pull/5886/files#diff-4512860ff8f9959a9b0b03bf0fc10f6bf704930bb48148d04c6a70ff8d500629).\n\n## See Also\n\n- [selector-observer](https://github.com/josh/selector-observer)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkidonng%2Fanimation-observer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkidonng%2Fanimation-observer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkidonng%2Fanimation-observer/lists"}