Skip to content

Feat/compt 31 dom event hooks#8

Merged
a-elkhiraooui-ciscode merged 2 commits intodevelopfrom
feat/COMPT-31-dom-event-hooks
Mar 30, 2026
Merged

Feat/compt 31 dom event hooks#8
a-elkhiraooui-ciscode merged 2 commits intodevelopfrom
feat/COMPT-31-dom-event-hooks

Conversation

@a-elkhiraooui-ciscode
Copy link
Copy Markdown
Contributor

Summary

  • What does this PR change?

Why

  • Why is this change needed?

Checklist

  • Added/updated tests (if behavior changed)
  • npm run lint passes
  • npm run typecheck passes
  • npm test passes
  • npm run build passes
  • Added a changeset (npx changeset) if this affects consumers

Notes

  • Anything reviewers should pay attention to?

…eIntersectionObserver

- useMediaQuery(query): tracks matchMedia via useSyncExternalStore, SSR-safe (server snapshot false)
- useWindowSize(): returns {width, height}, debounced 100ms on resize, SSR-safe ({0,0})
- useClickOutside(ref, handler): fires on mousedown/touchstart outside ref; handler via ref pattern
- useIntersectionObserver(ref, options?): IntersectionObserverEntry|null, disconnects on unmount
- All listeners registered in useEffect with cleanup return
- All SSR-safe: typeof window === undefined guards
- Zero runtime dependencies
- tsc --noEmit passes, lint passes (0 warnings), 26/26 tests pass, coverage >= 95%
- All four exported from src/hooks/index.ts -> src/index.ts
- Changeset added, copilot-instructions.md updated for epic COMPT-2
- remove accidental duplicated useMediaQuery suite block
- extract shared viewport setup in useWindowSize tests
- extract shared mount helper in useClickOutside tests
- keep behavior coverage unchanged
Copilot AI review requested due to automatic review settings March 30, 2026 13:22
@a-elkhiraooui-ciscode a-elkhiraooui-ciscode merged commit 788fe7e into develop Mar 30, 2026
5 checks passed
@sonarqubecloud
Copy link
Copy Markdown

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new “DOM & Events” hook group to HooksKit (COMPT-31), expanding the library’s hook surface area alongside accompanying tests, docs, and a changeset.

Changes:

  • Introduces four new hooks: useMediaQuery, useWindowSize, useClickOutside, useIntersectionObserver.
  • Adds Vitest + React Testing Library coverage for each new hook.
  • Updates the hooks barrel export list, project instructions, and adds a minor-version changeset.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/hooks/useWindowSize.ts Adds debounced resize-based window size hook (and a helper).
src/hooks/useWindowSize.test.ts Tests initial size + debounced resize behavior + cleanup.
src/hooks/useMediaQuery.ts Adds matchMedia-backed query hook using useSyncExternalStore.
src/hooks/useMediaQuery.test.ts Tests initial match, updates, and unmount cleanup.
src/hooks/useIntersectionObserver.ts Adds IntersectionObserver-backed hook for latest entry.
src/hooks/useIntersectionObserver.test.ts Tests entry updates, observe/disconnect behavior.
src/hooks/useClickOutside.ts Adds outside-click/touch handler hook with stale-closure avoidance.
src/hooks/useClickOutside.test.ts Tests outside vs inside interactions and cleanup.
src/hooks/index.ts Exports the new hooks from the hooks barrel.
.github/instructions/copilot-instructions.md Updates docs to include COMPT-31 hooks and library naming/structure.
.changeset/COMPT-31-dom-event-hooks.md Declares a minor release and documents the new hooks.

height: number;
}

export function getWindowSize(): WindowSize {
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getWindowSize is exported from this module, which makes it part of the public API via the hook barrel exports. If the intent is to expose only hooks, make this helper const/non-exported (or move it to an internal util) to avoid an unintended API surface and future breaking-change constraints.

Suggested change
export function getWindowSize(): WindowSize {
function getWindowSize(): WindowSize {

Copilot uses AI. Check for mistakes.
Comment on lines +8 to +21
const optionsRef = useRef(options);

useEffect(() => {
if (typeof window === 'undefined' || !ref.current) return;

const observer = new IntersectionObserver(([newEntry]) => {
if (newEntry) setEntry(newEntry);
}, optionsRef.current);

observer.observe(ref.current);
return () => {
observer.disconnect();
};
}, [ref]);
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The effect captures the initial options and observes only the initial ref.current. If options change or the ref starts pointing to a different element after mount, the observer won’t be recreated/re-attached, so the hook can return stale results. Consider tracking the observed element (e.g., const node = ref.current) and including node + options in the effect dependencies (or updating optionsRef.current when options changes).

Suggested change
const optionsRef = useRef(options);
useEffect(() => {
if (typeof window === 'undefined' || !ref.current) return;
const observer = new IntersectionObserver(([newEntry]) => {
if (newEntry) setEntry(newEntry);
}, optionsRef.current);
observer.observe(ref.current);
return () => {
observer.disconnect();
};
}, [ref]);
useEffect(() => {
if (typeof window === 'undefined') return;
const node = ref.current;
if (!node) return;
const observer = new IntersectionObserver(([newEntry]) => {
if (newEntry) setEntry(newEntry);
}, options);
observer.observe(node);
return () => {
observer.disconnect();
};
}, [ref.current, options]);

Copilot uses AI. Check for mistakes.
Comment on lines +11 to +16
if (typeof window === 'undefined' || !ref.current) return;

const observer = new IntersectionObserver(([newEntry]) => {
if (newEntry) setEntry(newEntry);
}, optionsRef.current);

Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new IntersectionObserver(...) will throw in environments where the API is unavailable (older browsers, some test/embedded webviews). Add a feature-detection guard (e.g., check typeof IntersectionObserver !== 'undefined') and return null / no-op when unsupported to keep the hook safe to import/use.

Copilot uses AI. Check for mistakes.

useEffect(() => {
handlerRef.current = handler;
});
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This effect runs after every render because it has no dependency array. Since it only needs to update the ref when handler changes, add [handler] as the dependency list to avoid unnecessary effect executions.

Suggested change
});
}, [handler]);

Copilot uses AI. Check for mistakes.
Comment on lines +6 to +11
if (typeof window === 'undefined') return () => undefined;
const mql = window.matchMedia(query);
mql.addEventListener('change', callback);
return () => mql.removeEventListener('change', callback);
},
() => (typeof window !== 'undefined' ? window.matchMedia(query).matches : false),
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

window.matchMedia isn’t available in all browser-like environments (and is undefined in some DOM shims unless polyfilled). As written, calling useMediaQuery will throw if matchMedia is missing. Consider guarding for typeof window === 'undefined' || typeof window.matchMedia !== 'function' and falling back to false with a no-op subscription.

Suggested change
if (typeof window === 'undefined') return () => undefined;
const mql = window.matchMedia(query);
mql.addEventListener('change', callback);
return () => mql.removeEventListener('change', callback);
},
() => (typeof window !== 'undefined' ? window.matchMedia(query).matches : false),
if (typeof window === 'undefined' || typeof window.matchMedia !== 'function') {
return () => undefined;
}
const mql = window.matchMedia(query);
mql.addEventListener('change', callback);
return () => mql.removeEventListener('change', callback);
},
() =>
typeof window !== 'undefined' && typeof window.matchMedia === 'function'
? window.matchMedia(query).matches
: false,

Copilot uses AI. Check for mistakes.
Zaiidmo added a commit that referenced this pull request Apr 7, 2026
* initiated dev environment

* ops: updated SOnar name

* ops (ci): standardize publish validation and dependabot across all packages

- Replace git tag --list strategy with package.json-driven tag validation
  in all 16 publish workflows; use git rev-parse to verify the exact tag
  exists rather than guessing the latest repo-wide tag
- Update error guidance to reflect feat/** → develop → master flow
- Standardize dependabot to npm-only, grouped, monthly cadence across
  all 16 packages; remove github-actions ecosystem updates
- Add missing dependabot.yml to AuthKit-UI, ChartKit-UI, HealthKit,
  HooksKit, paymentkit, StorageKit

* chore(COMPT-138): apply repository-wide prettier baseline (#4)

- Run prettier --write across repository
- No logic/behavior changes
- Unblocks CI PR Validation format gate for feature PRs
- verify passes: lint + typecheck + tests

* Feat/compt 30 state storage hooks (#6)

* feat(COMPT-30): add useDebounce, useLocalStorage, useSessionStorage hooks

- useDebounce<T>(value, delay): returns debounced value, resets timer on value/delay change
- useLocalStorage<T>(key, initial): syncs with localStorage, SSR-safe, JSON serialization
- useSessionStorage<T>(key, initial): same pattern for sessionStorage
- Shared storage.ts helper with readStorageValue/writeStorageValue (SSR guard + parse fallback)
- All three exported from src/hooks/index.ts -> src/index.ts
- Full test coverage: timer reset, JSON sync, parse error fallback, SSR guard
- tsc --noEmit passes, lint passes (0 warnings), 13/13 tests pass

* chore(COMPT-30): add changeset, update copilot-instructions, fix husky pre-commit

- .changeset/COMPT-30-state-storage-hooks.md: minor bump summary for 0.0.1 release
- .github/instructions/copilot-instructions.md: updated to HooksKit package identity,
  real src structure with COMPT-30 hooks marked, COMPT-XX branch naming convention
- .husky/pre-commit: removed deprecated husky v9 shebang lines (breaks in v10)

* Feat/compt 31 dom event hooks (#8)

* feat(COMPT-31): add useMediaQuery, useWindowSize, useClickOutside, useIntersectionObserver

- useMediaQuery(query): tracks matchMedia via useSyncExternalStore, SSR-safe (server snapshot false)
- useWindowSize(): returns {width, height}, debounced 100ms on resize, SSR-safe ({0,0})
- useClickOutside(ref, handler): fires on mousedown/touchstart outside ref; handler via ref pattern
- useIntersectionObserver(ref, options?): IntersectionObserverEntry|null, disconnects on unmount
- All listeners registered in useEffect with cleanup return
- All SSR-safe: typeof window === undefined guards
- Zero runtime dependencies
- tsc --noEmit passes, lint passes (0 warnings), 26/26 tests pass, coverage >= 95%
- All four exported from src/hooks/index.ts -> src/index.ts
- Changeset added, copilot-instructions.md updated for epic COMPT-2

* test(COMPT-31): reduce duplicated test blocks for Sonar quality gate

- remove accidental duplicated useMediaQuery suite block
- extract shared viewport setup in useWindowSize tests
- extract shared mount helper in useClickOutside tests
- keep behavior coverage unchanged

* Feat/compt 32 async lifecycle hooks (#9)

* feat(COMPT-32): add usePrevious, useToggle, useInterval, useTimeout, useIsFirstRender

- usePrevious<T>(value): previous render value via state-derivation; undefined on first render
- useToggle(initial?): boolean toggle with stable useCallback reference
- useInterval(callback, delay|null): fires at cadence, stops on null, latest callback via ref
- useTimeout(callback, delay|null): fires once, cancels on null or unmount, latest callback via ref
- useIsFirstRender(): true only on first render (scoped eslint-disable for intentional ref access)
- All timer cleanup in useEffect return — StrictMode safe
- Zero runtime deps; tsc --noEmit passes, lint passes, 25/25 tests, hooks coverage >= 98%
- All five exported from src/hooks/index.ts -> src/index.ts
- Changeset added, copilot-instructions.md updated with all three COMPT groups complete

* refactor(COMPT-32): move hook tests to src/hooks/__tests__/

- Moved all 5 hook test files from src/hooks/ to src/hooks/__tests__/
- Updated relative imports from ./hook to ../hook
- No logic changes; all 25 tests still pass

* test(COMPT-33): consolidate all hook tests into src/hooks/__tests__/ (#10)

- Move useDebounce, useLocalStorage, useSessionStorage, useMediaQuery,
  useWindowSize, useClickOutside, useIntersectionObserver tests to __tests__/
- Update relative imports from ./ to ../
- All 12 hooks covered: fake timers for debounce/interval/timeout/windowSize,
  mock matchMedia, mock IntersectionObserver, storage parse-error guards
- 55 tests passing, 97.44% line coverage (>= 85% AC)

* security: added CODEOWNER file for branches security

* ops: updated release check workflow

* ops: updated relese check workflow#

* feat(COMPT-34): README, v0.1.0 version bump, publish preparation (#11)

- Full README with installation, SSR note, 12 hook examples with types
- Remove __hooks_placeholder from hooks barrel
- Fix COMPT-30 changeset package name (reactts-developerkit -> hooks-kit)
- Apply changeset version bump: 0.0.0 -> 0.1.0
- Remove duplicate root-level test files (already in __tests__/)
- Update copilot-instructions.md with COMPT-34 status

Co-authored-by: a-elkhiraooui-ciscode <a.elkhiraoui@ciscod.com>

* ci: update release check workflow

* ops: updated release check jobs.

* v0.1.0

* fix(ci): correct sonar test directory and inclusion patterns

---------

Co-authored-by: Zaiidmo <zaiidmoumnii@gmail.com>
Co-authored-by: Zaiid Moumni <141942826+Zaiidmo@users.noreply.github.com>
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.

2 participants