Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/common/hooks/useCapsLock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,41 @@

import { useCallback, useEffect, useState } from 'react';

type CapsLockEvent = {
getModifierState?: (key: string) => boolean;
nativeEvent?: { getModifierState?: (key: string) => boolean };
key?: string;
};

const getCapsLockState = (event?: CapsLockEvent | null): boolean =>
event?.getModifierState?.('CapsLock') ??
event?.nativeEvent?.getModifierState?.('CapsLock') ??
false;

/**
* Tracks Caps Lock state via window-level keyboard events.
* Also exposes syncFromMouseEvent so callers can re-read the real OS state
* from a MouseEvent (e.g. onMouseDown on the input) — MouseEvent.getModifierState
* is reliable even after tab switches, unlike FocusEvent which lacks it.
* from a MouseEvent (e.g. onMouseDown on the input).
*
* Optional-chained: autofill, password managers, and some browser/extension
* events fire keydown/mousedown without getModifierState — calling it raw throws.
*/
export const useCapsLock = () => {
const [capsLockOn, setCapsLockOn] = useState(false);

const syncFromMouseEvent = useCallback((event: MouseEvent) => {
setCapsLockOn(event.getModifierState('CapsLock'));
const syncFromMouseEvent = useCallback((event: CapsLockEvent) => {
setCapsLockOn(getCapsLockState(event));
}, []);

useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
setCapsLockOn(event.getModifierState('CapsLock'));
setCapsLockOn(getCapsLockState(event));
};

// keydown fires before the OS flips the CapsLock modifier; keyup reads the new state.
const handleKeyUp = (event: KeyboardEvent) => {
if (event.key === 'CapsLock') {
setCapsLockOn(event.getModifierState('CapsLock'));
setCapsLockOn(getCapsLockState(event));
}
};

Expand Down
1 change: 1 addition & 0 deletions src/components/fieldText/fieldText.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
align-items: center;
justify-content: center;
margin-right: 12px;
cursor: default;
}

.caps-lock-tooltip-content {
Expand Down
9 changes: 6 additions & 3 deletions src/components/fieldText/fieldText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -176,21 +176,24 @@ export const FieldText = forwardRef<HTMLInputElement, FieldTextProps>(
disabled={disabled}
id={inputId}
onChange={onChange}
{...rest}
onFocus={onFocusHandler}
onBlur={onBlurHandler}
onMouseDown={(e) => {
if (capsLockMessage) syncFromMouseEvent(e.nativeEvent);
if (capsLockMessage) {
syncFromMouseEvent(e.nativeEvent ?? e);
}
onMouseDown?.(e);
}}
{...rest}
/>
{showCapsLock && (
<Tooltip
content={capsLockMessage}
wrapperClassName={cx('caps-lock-tooltip-wrapper')}
contentClassName={cx('caps-lock-tooltip-content')}
wrapperTabIndex={-1}
>
<span className={cx('caps-lock-icon')}>
<span className={cx('caps-lock-icon')} onMouseDown={(e) => e.preventDefault()}>
<CapsLockIcon />
</span>
</Tooltip>
Expand Down
Loading