{"id":20806686,"url":"https://github.com/fed/modal-focus-trap","last_synced_at":"2026-04-23T06:33:10.165Z","repository":{"id":57300281,"uuid":"216215556","full_name":"fed/modal-focus-trap","owner":"fed","description":"Simple modal focus trap implementation","archived":false,"fork":false,"pushed_at":"2020-04-07T06:30:48.000Z","size":326,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-12-26T18:50:13.445Z","etag":null,"topics":["a11y","focus-trap","keyboard-navigation","modal"],"latest_commit_sha":null,"homepage":"https://npm.im/modal-focus-trap","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fed.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}},"created_at":"2019-10-19T14:03:48.000Z","updated_at":"2022-10-05T05:33:29.000Z","dependencies_parsed_at":"2022-09-07T15:21:04.180Z","dependency_job_id":null,"html_url":"https://github.com/fed/modal-focus-trap","commit_stats":null,"previous_names":["fknussel/modal-focus-trap"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/fed/modal-focus-trap","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fed%2Fmodal-focus-trap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fed%2Fmodal-focus-trap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fed%2Fmodal-focus-trap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fed%2Fmodal-focus-trap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fed","download_url":"https://codeload.github.com/fed/modal-focus-trap/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fed%2Fmodal-focus-trap/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32169657,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-23T02:19:40.750Z","status":"ssl_error","status_checked_at":"2026-04-23T02:17:55.737Z","response_time":53,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["a11y","focus-trap","keyboard-navigation","modal"],"created_at":"2024-11-17T19:24:08.570Z","updated_at":"2026-04-23T06:33:10.143Z","avatar_url":"https://github.com/fed.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# modal-focus-trap\n\n[![Build Status](https://travis-ci.org/fknussel/modal-focus-trap.svg?branch=master)](https://travis-ci.org/fknussel/modal-focus-trap)\n[![Downloads](https://img.shields.io/npm/dm/modal-focus-trap)](https://npm.im/modal-focus-trap)\n[![Version](https://img.shields.io/npm/v/modal-focus-trap)](https://npm.im/modal-focus-trap)\n[![License](https://img.shields.io/npm/l/modal-focus-trap)](https://opensource.org/licenses/MIT)\n\nThis is a very simple and lightweight helper function that makes it easier to set up a focus trap in a modal window. This implementation is not library specific and can therefore be used with VanillaJS, React or any other library/framework.\n\n## How to use\n\nFirst import the helper function. You don't necessarily need to name it `focusTrap`, you can use any name you want.\n\n```js\nimport focusTrap from 'modal-focus-trap';\n```\n\nSet up the focus trap by passing in an `HTMLElement` which is your modal element. The function returns a callback you can use later on to disengage the focus trap. Cache this callback somewhere in your code, you will need it later.\n\n```js\nconst modalElement = document.querySelector('.modal');\nlet restoreFocus = focusTrap(modalElement);\n```\n\nAfter closing the modal, you need to disengage the focus trap and restore the focus to the previously focused element by invoking the callback:\n\n```js\nif (restoreFocus) {\n    restoreFocus();\n    restoreFocus = null;\n}\n```\n\n## Using with React\n\nYou can also create a hook to use this helper with React:\n\n```js\nimport { useRef, useEffect } from 'react';\nimport focusTrap from 'modal-focus-trap';\n\nexport default function useFocusTrap() {\n    const ref = useRef(null);\n\n    // Create on didMount.\n    useEffect(() =\u003e {\n        const destroy = focusTrap(ref.current);\n\n        // Destroy on willUnmount.\n        return destroy;\n    }, []);\n\n    return ref;\n}\n```\n\nSee this [sample project](https://codesandbox.io/s/react-focus-trap-demo-bzitn) for more info.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffed%2Fmodal-focus-trap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffed%2Fmodal-focus-trap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffed%2Fmodal-focus-trap/lists"}