-
-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
18fc76c
commit 13eef3e
Showing
14 changed files
with
160 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@zag-js/focus-trap": patch | ||
--- | ||
|
||
Initial release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# @zag-js/focus-trap | ||
|
||
Focus trap utility | ||
|
||
## Installation | ||
|
||
```sh | ||
yarn add @zag-js/focus-trap | ||
# or | ||
npm i @zag-js/focus-trap | ||
``` | ||
|
||
## Contribution | ||
|
||
Yes please! See the | ||
[contributing guidelines](https://github.com/chakra-ui/zag/blob/main/CONTRIBUTING.md) | ||
for details. | ||
|
||
## Licence | ||
|
||
This project is licensed under the terms of the | ||
[MIT license](https://github.com/chakra-ui/zag/blob/main/LICENSE). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ | ||
"name": "@zag-js/focus-trap", | ||
"version": "0.0.0", | ||
"description": "Focus trap utility", | ||
"keywords": [ | ||
"js", | ||
"utils", | ||
"focus-trap" | ||
], | ||
"author": "Segun Adebayo <[email protected]>", | ||
"homepage": "https://github.com/chakra-ui/zag#readme", | ||
"license": "MIT", | ||
"main": "src/index.ts", | ||
"repository": "https://github.com/chakra-ui/zag/tree/main/packages/utilities/focus-trap", | ||
"sideEffects": false, | ||
"files": [ | ||
"dist/**/*" | ||
], | ||
"scripts": { | ||
"build": "tsup", | ||
"lint": "eslint src", | ||
"typecheck": "tsc --noEmit", | ||
"prepack": "clean-package", | ||
"postpack": "clean-package restore" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/chakra-ui/zag/issues" | ||
}, | ||
"clean-package": "../../../clean-package.config.json", | ||
"dependencies": { | ||
"@zag-js/dom-query": "workspace:*", | ||
"focus-trap": "7.5.4" | ||
}, | ||
"devDependencies": { | ||
"clean-package": "2.2.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { nextTick, getDocument } from "@zag-js/dom-query" | ||
import { createFocusTrap, type FocusTrap, type Options } from "focus-trap" | ||
|
||
type ElementOrGetter = HTMLElement | null | (() => HTMLElement | null) | ||
|
||
export interface TrapFocusOptions extends Omit<Options, "document"> {} | ||
|
||
export function trapFocus(el: ElementOrGetter, options: TrapFocusOptions = {}) { | ||
let trap: FocusTrap | undefined | ||
nextTick(() => { | ||
const contentEl = typeof el === "function" ? el() : el | ||
if (!contentEl) return | ||
trap = createFocusTrap(contentEl, { | ||
escapeDeactivates: false, | ||
allowOutsideClick: true, | ||
preventScroll: true, | ||
returnFocusOnDeactivate: true, | ||
document: getDocument(contentEl), | ||
fallbackFocus: contentEl, | ||
...options, | ||
}) | ||
|
||
try { | ||
trap.activate() | ||
} catch {} | ||
}) | ||
|
||
return function destroy() { | ||
trap?.deactivate() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": "../../../tsconfig.json", | ||
"include": ["src"], | ||
"compilerOptions": { | ||
"tsBuildInfoFile": "node_modules/.cache/.tsbuildinfo" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.