Skip to content

refactor: clean up code by moving repeated DOM and focus logic into dom_utils - #93

Open
balajis-qb wants to merge 2 commits into
mainfrom
refactor/extract-safe-dom-query-helper
Open

balajis-qb wants to merge 2 commits into
mainfrom
refactor/extract-safe-dom-query-helper

Conversation

@balajis-qb

Copy link
Copy Markdown
Collaborator

Summary

Code cleanup: move DOM and focus logic that was duplicated in several files into one shared helper file, src/dom_utils.ts.

Problem

  • The same small checks were written out by hand in several components. Some copies even had matching comments.
  • Repeated code is harder to read, and the copies slowly drift apart. Then one copy gets a fix or a bug that the others don't.
  • Moving each check into one named, tested helper makes the components shorter and easier to read, and gives each check a single place to fix.

What was repeated, and where:

Repeated logic Where it was copied
"Make sure the container is an element, run querySelector, make sure the result is an HTMLElement" index.tsx (3 places), tab_loop.tsx
"Is anything focused?" day.tsx, week_number.tsx
"Is focus already on another cell of this calendar?" day.tsx, week_number.tsx
"Focus the previous / next sibling" time.tsx (twice)

Changes

New file src/dom_utils.ts with five small helpers:

  • findElement / findElements: safe versions of querySelector / querySelectorAll. They return null or [] if the container is missing, and only return real HTMLElements.
  • 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 raw querySelector / querySelectorAll calls replaced.
  • src/tab_loop.tsx: getTabChildren uses findElements; the long focusable-element type is now a named FocusableElement type.
  • src/day.tsx, src/week_number.tsx: use isNothingFocused and isFocusOnClassInside.
  • src/time.tsx: arrow-key handling uses focusSibling.

Tests: src/test/dom_utils.test.ts covers 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

  • I have followed the contributing guidelines.
  • I have added sufficient test coverage for my changes.
  • I have formatted my code with Prettier and checked for linting issues with ESLint for code readability.

…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant