-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #447 from qonto/typescript-addon-migration
refactor: migrate addon to TypeScript
- Loading branch information
Showing
21 changed files
with
574 additions
and
366 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 |
---|---|---|
|
@@ -77,3 +77,35 @@ jobs: | |
- name: Run tests | ||
run: pnpm ember try:one ${{ matrix.ember-try-scenario }} --skip-cleanup | ||
working-directory: test-app | ||
|
||
typescript-compatibility: | ||
name: Type checking - ${{ matrix.typescript-scenario }} | ||
runs-on: ubuntu-latest | ||
|
||
needs: [test] | ||
|
||
strategy: | ||
fail-fast: false | ||
matrix: | ||
typescript-scenario: | ||
- [email protected] | ||
- [email protected] | ||
- typescript@next | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
- name: Install pnpm | ||
uses: wyvox/action-setup-pnpm@v3 | ||
with: | ||
pnpm-version: 8.5.1 | ||
node-version: 16.x | ||
args: "--frozen-lockfile" | ||
- name: Update TS version on addon package | ||
run: pnpm add -D ${{ matrix.typescript-scenario }} | ||
working-directory: ember-autofocus-modifier | ||
- name: Update TS version on test-app package | ||
run: pnpm add -D ${{ matrix.typescript-scenario }} | ||
working-directory: test-app | ||
- name: Type checking | ||
run: pnpm lint:types |
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 was deleted.
Oops, something went wrong.
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,80 @@ | ||
import { modifier } from "ember-modifier"; | ||
import { next } from "@ember/runloop"; | ||
|
||
const focusableElements = [ | ||
"BUTTON", | ||
"SUMMARY", | ||
"IFRAME", | ||
"INPUT", | ||
"SELECT", | ||
"TEXTAREA", | ||
] as const; | ||
|
||
const DEFAULT_SELECTOR = | ||
"input:not([disabled]):not([readonly]),textarea:not([disabled]):not([readonly])"; | ||
|
||
interface ModifierArgs { | ||
Element: HTMLElement; | ||
Args: { | ||
Positional: [string?] | undefined; // Optional selector | ||
Named: { | ||
disabled?: boolean; // Optional 'disabled' parameter | ||
}; | ||
}; | ||
} | ||
|
||
export default modifier<ModifierArgs>(function autofocus( | ||
element: HTMLElement, | ||
[selector] = [DEFAULT_SELECTOR], | ||
{ disabled } = { disabled: false }, | ||
) { | ||
if (disabled) { | ||
return; | ||
} | ||
|
||
const targetElement: HTMLElement = | ||
element.querySelector(selector ?? DEFAULT_SELECTOR) || element; | ||
const isChildElement = targetElement !== element; | ||
|
||
/** | ||
* Only applies to the element that {{autofocus}} is applied to. | ||
* opts-out if we're selecting a child element. | ||
*/ | ||
const shouldMoveFocus = | ||
!isChildElement && | ||
!focusableElements.some( | ||
(item) => | ||
element.tagName === item || | ||
element.isContentEditable || | ||
element.hasAttribute("aria-disabled") || | ||
element.hasAttribute("href") || | ||
element.hasAttribute("tabindex"), | ||
); | ||
|
||
/** | ||
* if {{autofocus}} is applied to a non-focusable element, | ||
* For A11y purposes, this is used to move focus to the non-focusable element. | ||
* This is helpful when new elements are inserted on to the screen (yet not focus-trapped), | ||
* and we want the tab-behavior to move "near" the inserted content. | ||
* | ||
* This still prevents the non-focusable element from being tabbed to, as non-focusable | ||
* elements are still not focusable. | ||
* | ||
* But this is a behavior we can use to help out screen readers and keyboard users alike to | ||
* more smoothly interact with newly-inserted content (without needing to focus an interactive-specifically | ||
* maybe the inserted contents are just buttons, for example). | ||
*/ | ||
if (shouldMoveFocus) { | ||
element.setAttribute("tabindex", "-1"); | ||
} | ||
|
||
next(function () { | ||
targetElement.focus(); | ||
}); | ||
|
||
return (): void => { | ||
if (shouldMoveFocus) { | ||
element.removeAttribute("tabindex"); | ||
} | ||
}; | ||
}); |
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 @@ | ||
import type autofocus from "./modifiers/autofocus"; | ||
|
||
export default interface Registry { | ||
autofocus: typeof autofocus; | ||
} |
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
Oops, something went wrong.