vue3-enter-to-tab 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Aarón J. Montes
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
package/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # vue3-enter-to-tab
2
+
3
+ [![npm package][npm-img]][npm-url]
4
+ [![Build Status][build-img]][build-url]
5
+
6
+ TODO description
7
+
8
+ ## Credits
9
+
10
+ https://github.com/ajomuch92/vue-enter-to-tab
11
+
12
+ ## Install
13
+
14
+ NPM:
15
+
16
+ ```bash
17
+ npm i --save vue3-enter-to-tab
18
+ yarn add vue3-enter-to-tab
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ ```ts
24
+ import { useEnterToTab } from 'vue3-enter-to-tab'
25
+
26
+ myPackage('hello')
27
+ //=> 'hello from my package'
28
+ ```
29
+
30
+ **Note**: This directive doesn't work on textarea elements.
31
+
32
+ Any contribution is welcome. Visit my web page [here](https://www.ajomuch92.site/#/).
33
+
34
+
package/lib/index.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ import { MaybeRefOrGetter } from 'vue';
2
+ type HTMLElementWithPrevent = HTMLElement & {
3
+ preventEnterTab?: boolean;
4
+ };
5
+ export declare const useEnterToTab: (element: MaybeRefOrGetter<HTMLElement | null | undefined>, { autoClickButton }?: {
6
+ autoClickButton?: boolean | undefined;
7
+ }) => {
8
+ isEnterToTabEnabled: import("vue").Ref<boolean>;
9
+ vPreventEnterTab: {
10
+ beforeMount: (el: HTMLElementWithPrevent) => boolean;
11
+ };
12
+ setEnterToTabEnabled: (value: boolean) => void;
13
+ };
14
+ export {};
package/lib/index.js ADDED
@@ -0,0 +1,53 @@
1
+ import { useEventListener } from '@vueuse/core';
2
+ import { ref, toValue, nextTick } from 'vue';
3
+ export const useEnterToTab = (element, { autoClickButton = true } = {}) => {
4
+ const isEnterToTabEnabled = ref(true);
5
+ useEventListener(element, 'keydown', async (e) => {
6
+ const { ctrlKey, code, altKey, shiftKey } = e;
7
+ const target = e.target;
8
+ if ((code === 'Enter' || code === 'NumpadEnter') &&
9
+ !ctrlKey &&
10
+ !altKey &&
11
+ !shiftKey &&
12
+ target &&
13
+ target.tagName.toLowerCase() !== 'textarea' &&
14
+ isEnterToTabEnabled &&
15
+ !target.preventEnterTab) {
16
+ e.preventDefault();
17
+ const elementValue = toValue(element);
18
+ if (elementValue === null || elementValue === undefined) {
19
+ console.warn('cant convert enter to tab, element is null');
20
+ return;
21
+ }
22
+ const allElementsQuery = elementValue.querySelectorAll('input, button, a, textarea, select, audio, video, [contenteditable]');
23
+ const allElements = [];
24
+ allElementsQuery.forEach((e) => {
25
+ const r = e;
26
+ if (!r.disabled && !r.hidden && r.offsetParent && !r.readOnly && r.tabIndex >= 0) {
27
+ allElements.push(r);
28
+ }
29
+ });
30
+ const currentIndex = [...allElements].indexOf(target);
31
+ const targetIndex = (currentIndex + 1) % allElements.length;
32
+ const nextElement = allElements[targetIndex];
33
+ nextElement.focus();
34
+ // if the next element is a button, click on it instead of just focusing. otherwise user has to double enter for a button to activate
35
+ if (autoClickButton && nextElement.tagName.toLowerCase() === 'button') {
36
+ // wait so that any changes from unfocusing of old element are considered
37
+ await nextTick();
38
+ nextElement.click();
39
+ }
40
+ }
41
+ });
42
+ const setEnterToTabEnabled = (value) => {
43
+ isEnterToTabEnabled.value = value;
44
+ };
45
+ const vPreventEnterTab = {
46
+ beforeMount: (el) => (el.preventEnterTab = true),
47
+ };
48
+ return {
49
+ isEnterToTabEnabled,
50
+ vPreventEnterTab,
51
+ setEnterToTabEnabled,
52
+ };
53
+ };
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "vue3-enter-to-tab",
3
+ "version": "0.0.1",
4
+ "description": "",
5
+ "main": "./lib/index.js",
6
+ "files": [
7
+ "lib/**/*"
8
+ ],
9
+ "scripts": {
10
+ "build": "tsc --project tsconfig.json",
11
+ "typecheck": "tsc --noEmit"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/l3d00m/vue3-enter-to-tab.git"
16
+ },
17
+ "license": "MIT",
18
+ "author": {
19
+ "name": "l3d00m",
20
+ "email": "l3d00m@users.noreply.github.com",
21
+ "url": "https://github.com/l3d00m"
22
+ },
23
+ "engines": {
24
+ "node": ">=16.0"
25
+ },
26
+ "keywords": [],
27
+ "bugs": {
28
+ "url": "https://github.com/l3d00m/vue3-enter-to-tab/issues"
29
+ },
30
+ "homepage": "https://github.com/l3d00m/vue3-enter-to-tab#readme",
31
+ "devDependencies": {
32
+ "@types/node": "20.8.2",
33
+ "prettier": "3.0.3",
34
+ "ts-node": "10.9.1",
35
+ "typescript": "5.2.2"
36
+ },
37
+ "packageManager": "yarn@3.6.4",
38
+ "dependencies": {
39
+ "@vueuse/core": ">9.x",
40
+ "vue": ">=3.3 <4"
41
+ }
42
+ }