Skip to content

Commit

Permalink
chore: focus trap pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
segunadebayo committed Sep 12, 2024
1 parent 18fc76c commit 13eef3e
Show file tree
Hide file tree
Showing 14 changed files with 160 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/pretty-rabbits-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@zag-js/focus-trap": patch
---

Initial release
1 change: 1 addition & 0 deletions examples/lit-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@zag-js/file-upload": "workspace:*",
"@zag-js/file-utils": "workspace:*",
"@zag-js/floating-panel": "workspace:*",
"@zag-js/focus-trap": "workspace:*",
"@zag-js/focus-visible": "workspace:*",
"@zag-js/form-utils": "workspace:*",
"@zag-js/highlight-word": "workspace:*",
Expand Down
1 change: 1 addition & 0 deletions examples/next-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@zag-js/file-upload": "workspace:*",
"@zag-js/file-utils": "workspace:*",
"@zag-js/floating-panel": "workspace:*",
"@zag-js/focus-trap": "workspace:*",
"@zag-js/focus-visible": "workspace:*",
"@zag-js/form-utils": "workspace:*",
"@zag-js/highlight-word": "workspace:*",
Expand Down
1 change: 1 addition & 0 deletions examples/nuxt-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@zag-js/file-upload": "workspace:*",
"@zag-js/file-utils": "workspace:*",
"@zag-js/floating-panel": "workspace:*",
"@zag-js/focus-trap": "workspace:*",
"@zag-js/focus-visible": "workspace:*",
"@zag-js/form-utils": "workspace:*",
"@zag-js/highlight-word": "workspace:*",
Expand Down
1 change: 1 addition & 0 deletions examples/preact-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@zag-js/file-upload": "workspace:*",
"@zag-js/file-utils": "workspace:*",
"@zag-js/floating-panel": "workspace:*",
"@zag-js/focus-trap": "workspace:*",
"@zag-js/focus-visible": "workspace:*",
"@zag-js/form-utils": "workspace:*",
"@zag-js/highlight-word": "workspace:*",
Expand Down
1 change: 1 addition & 0 deletions examples/react-19/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@zag-js/file-upload": "workspace:*",
"@zag-js/file-utils": "workspace:*",
"@zag-js/floating-panel": "workspace:*",
"@zag-js/focus-trap": "workspace:*",
"@zag-js/focus-visible": "workspace:*",
"@zag-js/form-utils": "workspace:*",
"@zag-js/highlight-word": "workspace:*",
Expand Down
3 changes: 2 additions & 1 deletion examples/solid-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@zag-js/file-upload": "workspace:*",
"@zag-js/file-utils": "workspace:*",
"@zag-js/floating-panel": "workspace:*",
"@zag-js/focus-trap": "workspace:*",
"@zag-js/focus-visible": "workspace:*",
"@zag-js/form-utils": "workspace:*",
"@zag-js/highlight-word": "workspace:*",
Expand Down Expand Up @@ -94,4 +95,4 @@
"engines": {
"node": ">=18"
}
}
}
1 change: 1 addition & 0 deletions examples/svelte-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@zag-js/file-upload": "workspace:*",
"@zag-js/file-utils": "workspace:*",
"@zag-js/floating-panel": "workspace:*",
"@zag-js/focus-trap": "workspace:*",
"@zag-js/focus-visible": "workspace:*",
"@zag-js/form-utils": "workspace:*",
"@zag-js/highlight-word": "workspace:*",
Expand Down
1 change: 1 addition & 0 deletions examples/vanilla-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@zag-js/file-upload": "workspace:*",
"@zag-js/file-utils": "workspace:*",
"@zag-js/floating-panel": "workspace:*",
"@zag-js/focus-trap": "workspace:*",
"@zag-js/focus-visible": "workspace:*",
"@zag-js/form-utils": "workspace:*",
"@zag-js/highlight-word": "workspace:*",
Expand Down
22 changes: 22 additions & 0 deletions packages/utilities/focus-trap/README.md
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).
40 changes: 40 additions & 0 deletions packages/utilities/focus-trap/package.json
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"
}
}
31 changes: 31 additions & 0 deletions packages/utilities/focus-trap/src/index.ts
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()
}
}
7 changes: 7 additions & 0 deletions packages/utilities/focus-trap/tsconfig.json
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"
}
}
55 changes: 46 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 13eef3e

Please sign in to comment.