No description
Find a file
2023-01-10 09:33:02 +01:00
.github/workflows Update and rename doc-publish.yml to documentation.yml 2022-10-05 18:36:15 +02:00
__mocks__ add more tests 2022-10-05 09:35:18 -03:00
lib 📝 build doc support 2022-10-05 10:20:43 -03:00
src 🚨 fix linter warnings 2022-10-05 05:51:10 -03:00
tests add more tests 2022-10-05 09:35:18 -03:00
.eslintignore add more tests 2022-10-05 09:35:18 -03:00
.eslintrc.cjs add more tests 2022-10-05 09:35:18 -03:00
.gitignore 🎉 init 2022-10-03 16:10:24 -03:00
.npmignore 🙈 ignore some files into npm package 2022-10-05 09:47:13 -03:00
.prettierignore 🧑‍💻 add eslint and prettier 2022-10-05 05:34:46 -03:00
.prettierrc.yml 🧑‍💻 add eslint and prettier 2022-10-05 05:34:46 -03:00
index.html ♻️ use vite instead of rollup directly 2022-10-05 05:16:13 -03:00
LICENSE 🎉 init 2022-10-03 16:10:24 -03:00
package-lock.json Bump json5 from 1.0.1 to 1.0.2 2023-01-09 17:09:58 +00:00
package.json 📝 build doc support 2022-10-05 10:20:43 -03:00
postcss.config.js 🚨 fix linter warnings 2022-10-05 05:51:10 -03:00
README.md 📝 add development section 2022-10-05 12:33:00 -03:00
tailwind.config.js 🚨 fix linter warnings 2022-10-05 05:51:10 -03:00
tsconfig.doc.json 📝 build doc support 2022-10-05 10:20:43 -03:00
tsconfig.json 🚨 format code 2022-10-05 12:32:10 -03:00
tsconfig.node.json 📝 build doc support 2022-10-05 10:20:43 -03:00
typedoc.json 📝 build doc support 2022-10-05 10:20:43 -03:00
vite.config.doc.ts 📝 build doc support 2022-10-05 10:20:43 -03:00
vite.config.ts 🚨 fix linter warnings 2022-10-05 05:51:10 -03:00

@nextcloud/focus-trap

Wrapper over focus-trap library to have more than one instance activate at the same time.

Install

npm install @nextcloud/focus-trap -S
yarn add @nextcloud/focus-trap

The API is the same of focus-trap, just follow the doc.

import { createFocusTrap } from '@nextcloud/focus-trap';

Example

Vue.js v3

Check src/ to full example.

import type { ComputedRef, Ref } from 'vue';
import type { FocusTrap } from '@nextcloud/focus-trap';

import { watchEffect, onBeforeUnmount } from 'vue';
import { createFocusTrap } from '@nextcloud/focus-trap';

type Input = ComputedRef<boolean> | Ref<boolean>;

const useFocusTrap = (state: Input, el: Ref<HTMLElement | null>): void => {
  let focusTrap: FocusTrap | null = null;

  const onOpen = (): void => {
    if (el.value == null) {
      return;
    }

    focusTrap = createFocusTrap(el.value, {
      escapeDeactivates: false,
      allowOutsideClick: true,
    });

    focusTrap.activate();
  };

  const onClose = (): void => {
    if (focusTrap == null) {
      return;
    }

    focusTrap.deactivate();
    focusTrap = null;
  };

  watchEffect(() => {
    state.value
      ? requestAnimationFrame(onOpen)
      : requestAnimationFrame(onClose);
  });

  onBeforeUnmount(onClose);
};

export { useFocusTrap };

Development

This project is build on top of

Install dependencies

npm install

Run tests

# test single run
npm run test

# test with coverage
npm run test -- --coverage

# watch tests and code changes
npm run test:watch

Run demo app

npm run dev

Linter & Format

# apply prettier
npm run format

# run eslint
npm run lint

# apply fix to eslint errors
npm run lint:fix

# check lint and format
npm run check