Skip to content

Commit

Permalink
add code, remove unused stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
l3d00m committed Oct 6, 2023
1 parent fd7c38e commit 32c3f5a
Show file tree
Hide file tree
Showing 19 changed files with 415 additions and 8,544 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[*.{js,jsx,ts,tsx,vue}]
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
end_of_line = lf
charset = utf-8
insert_final_newline = true
max_line_length = 100

28 changes: 0 additions & 28 deletions .github/ISSUE_TEMPLATE/bug_report.md

This file was deleted.

34 changes: 0 additions & 34 deletions .github/ISSUE_TEMPLATE/feature_request.md

This file was deleted.

42 changes: 0 additions & 42 deletions .github/PULL_REQUEST_TEMPLATE.md

This file was deleted.

21 changes: 0 additions & 21 deletions .github/workflows/codeql-analysis.yml

This file was deleted.

9 changes: 0 additions & 9 deletions .github/workflows/pr.yml

This file was deleted.

16 changes: 0 additions & 16 deletions .github/workflows/release.yml

This file was deleted.

1 change: 0 additions & 1 deletion .husky/.gitignore

This file was deleted.

4 changes: 0 additions & 4 deletions .husky/pre-commit

This file was deleted.

4 changes: 0 additions & 4 deletions .husky/prepare-commit-msg

This file was deleted.

2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.yarn
.yarnrc.yml
7 changes: 7 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"semi": false,
"singleQuote": true,
"jsxSingleQuote": true,
"bracketSameLine": false,
"htmlWhitespaceSensitivity": "ignore"
}
52 changes: 0 additions & 52 deletions .vscode/launch.json

This file was deleted.

6 changes: 0 additions & 6 deletions .vscode/settings.json

This file was deleted.

3 changes: 2 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 Ryan Sonshine
Copyright (c) 2021 Aarón J. Montes

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -19,3 +19,4 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@
},
"homepage": "https://github.com/l3d00m/vue3-enter-to-tab#readme",
"devDependencies": {
"@types/node": "^20.8.2",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
"@types/node": "20.8.2",
"prettier": "3.0.3",
"ts-node": "10.9.1",
"typescript": "5.2.2"
},
"packageManager": "[email protected]"
"packageManager": "[email protected]",
"dependencies": {
"@vueuse/core": ">9.x",
"vue": ">=3.3 <4"
}
}
74 changes: 73 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,73 @@
export const myPackage = (taco = ''): string => `${taco} from my package`;
import { useEventListener } from '@vueuse/core'
import { MaybeRefOrGetter, ref, toValue, nextTick } from 'vue'

// source from https://github.com/ajomuch92/vue-enter-to-tab
// it has been modified to 1. to auto click a button if the next element is a button
// 2. to use the composable API
// 3. to use vueuse with useEventListener for automatically removing event listeners

type HTMLElementWithPrevent = HTMLElement & { preventEnterTab?: boolean }

export const useEnterToTab = (
element: MaybeRefOrGetter<HTMLElement | null | undefined>,
{ autoClickButton = true } = {},
) => {
const isEnterToTabEnabled = ref(true)

useEventListener(element, 'keydown', async (e: KeyboardEvent) => {
const { ctrlKey, code, altKey, shiftKey } = e
const target = e.target as HTMLElementWithPrevent
if (
(code === 'Enter' || code === 'NumpadEnter') &&
!ctrlKey &&
!altKey &&
!shiftKey &&
target &&
target.tagName.toLowerCase() !== 'textarea' &&
isEnterToTabEnabled &&
!target.preventEnterTab
) {
e.preventDefault()
const elementValue = toValue(element)
if (elementValue === null || elementValue === undefined) {
console.warn('cant convert enter to tab, element is null')
return
}
const allElementsQuery = elementValue.querySelectorAll(
'input, button, a, textarea, select, audio, video, [contenteditable]',
)

const allElements: HTMLElement[] = []
allElementsQuery.forEach((e) => {
const r = e as HTMLInputElement
if (!r.disabled && !r.hidden && r.offsetParent && !r.readOnly && r.tabIndex >= 0) {
allElements.push(r)
}
})
const currentIndex = [...allElements].indexOf(target)
const targetIndex = (currentIndex + 1) % allElements.length
const nextElement = allElements[targetIndex]
nextElement.focus()
// 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
if (autoClickButton && nextElement.tagName.toLowerCase() === 'button') {
// wait so that any changes from unfocusing of old element are considered
await nextTick()
nextElement.click()
}
}
})

const setEnterToTabEnabled = (value: boolean) => {
isEnterToTabEnabled.value = value
}

const vPreventEnterTab = {
beforeMount: (el: HTMLElementWithPrevent) => (el.preventEnterTab = true),
}

return {
isEnterToTabEnabled,
vPreventEnterTab,
setEnterToTabEnabled,
}
}
8 changes: 6 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "Node",
"declaration": true,
"outDir": "./lib/",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true
"skipLibCheck": true,
"allowSyntheticDefaultImports": true,
"isolatedModules": true
},
"include": ["src/**/*.ts"]
"include": ["src/**/*.ts"],
"exclude": ["node_modules"]
}
Loading

0 comments on commit 32c3f5a

Please sign in to comment.