Repository navigation
refactor: clean up code by moving repeated DOM and focus logic into dom_utils - #93
Open
balajis-qb wants to merge 2 commits into
Open
balajis-qb wants to merge 2 commits into
balajis-qb wants to merge 2 commits into
Conversation
…calls index.tsx duplicated the same "narrow container to Element, then narrow the query result to HTMLElement" boilerplate in three places, and tab_loop.tsx did an untyped Array.prototype.slice.call over a raw querySelectorAll. Add findElement/findElements in dom_utils.ts (production-safe counterparts to the test-only safeQuerySelector helpers) and use them at all four call sites.
day.tsx and week_number.tsx each had their own copy of "is anything focused?" and "is focus already on another cell of this calendar?", with matching comments - the kind of copy-paste that drifts apart over time. time.tsx repeated the same previous/next sibling focus block twice. Add isNothingFocused, isFocusOnClassInside and focusSibling to dom_utils and use them in those three components. No behaviour change.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Code cleanup: move DOM and focus logic that was duplicated in several files into one shared helper file,
src/dom_utils.ts.Problem
What was repeated, and where:
querySelector, make sure the result is anHTMLElement"index.tsx(3 places),tab_loop.tsxday.tsx,week_number.tsxday.tsx,week_number.tsxtime.tsx(twice)Changes
New file
src/dom_utils.tswith five small helpers:findElement/findElements: safe versions ofquerySelector/querySelectorAll. They returnnullor[]if the container is missing, and only return realHTMLElements.isNothingFocused: true when focus is on the page body.isFocusOnClassInside: true when the focused element is inside a container and has a given class.focusSibling: moves focus to the previous or next sibling, and says whether one existed.Files that now use the helpers (all become shorter):
src/index.tsx: three rawquerySelector/querySelectorAllcalls replaced.src/tab_loop.tsx:getTabChildrenusesfindElements; the long focusable-element type is now a namedFocusableElementtype.src/day.tsx,src/week_number.tsx: useisNothingFocusedandisFocusOnClassInside.src/time.tsx: arrow-key handling usesfocusSibling.Tests:
src/test/dom_utils.test.tscovers every helper, including edge cases (null container, SVG elements, no sibling, non-element sibling). The existing test suite passes unchanged.Small detail: in
time.tsx,preventDefault()on arrow keys now runs right after the focus move instead of right before it. The result is the same.Contribution checklist