add focus tool; time input: refactor away from bind:ref

This commit is contained in:
Elijah Duffy
2025-07-21 19:41:44 -07:00
parent e3c08b4247
commit 123594f828
3 changed files with 231 additions and 122 deletions

View File

@@ -69,3 +69,21 @@ export const getValue = (option: Option): string => {
return option.value;
}
};
/**
* targetMust returns the target of an event coerced to a particular type and
* throws an error if the target does not exist or is not connected.
*
* @returns The target element coerced to a particular type.
* @throws Will throw an error if the target is null or not connected to the DOM.
*/
export function targetMust<T extends HTMLElement>(event: Event): T {
const target = event.target as T | null;
if (!target) {
throw new Error('Event target is null');
}
if (!target.isConnected) {
throw new Error('Event target is not connected to the DOM');
}
return target;
}