1- import { type MouseEvent , type KeyboardEvent , useCallback , useRef , useState } from 'react' ;
1+ import {
2+ type MouseEvent ,
3+ type KeyboardEvent ,
4+ useCallback ,
5+ useRef ,
6+ useState ,
7+ useEffect ,
8+ } from 'react' ;
29import clsx from 'clsx' ;
310
411import styled , { keyframes } from 'styled-components' ;
@@ -31,6 +38,7 @@ import { GroupUpdateMessage } from './GroupUpdateMessage';
3138import { CallNotification } from './notification-bubble/CallNotification' ;
3239import { InteractionNotification } from './InteractionNotification' ;
3340import { MessageRequestResponse } from './MessageRequestResponse' ;
41+ import { useFocusScope , useIsInScope } from '../../../../state/focus' ;
3442
3543export type GenericReadableMessageSelectorProps = Pick <
3644 MessageRenderingProps ,
@@ -44,6 +52,7 @@ const highlightedMessageAnimation = keyframes`
4452const StyledReadableMessage = styled . div < {
4553 selected : boolean ;
4654 $isDetailView : boolean ;
55+ $focusedKeyboard : boolean ;
4756} > `
4857 display: flex;
4958 align-items: center;
@@ -59,9 +68,12 @@ const StyledReadableMessage = styled.div<{
5968 margin-top: var(--margins-xs);
6069 }
6170
62- &:focus-visible {
71+ ${ props =>
72+ props . $focusedKeyboard
73+ ? `&:focus-visible {
6374 background-color: var(--conversation-tab-background-selected-color);
64- }
75+ }`
76+ : '' }
6577` ;
6678
6779function getMessageComponent ( messageType : UIMessageType ) {
@@ -104,8 +116,10 @@ export const GenericReadableMessage = ({ messageId }: WithMessageId) => {
104116
105117 const ref = useRef < HTMLDivElement > ( null ) ;
106118 const pointerDownRef = useRef ( false ) ;
107- const keyboardFocusedRef = useRef ( false ) ;
108119 const [ triggerPosition , setTriggerPosition ] = useState < PopoverTriggerPosition | null > ( null ) ;
120+ const isInFocusScope = useIsInScope ( { scope : 'message' , scopeId : messageId } ) ;
121+ const { focusedMessageId } = useFocusScope ( ) ;
122+ const isAnotherMessageFocused = focusedMessageId && ! isInFocusScope ;
109123
110124 const getMessageContainerTriggerPosition = ( ) : PopoverTriggerPosition | null => {
111125 if ( ! ref . current ) {
@@ -180,6 +194,12 @@ export const GenericReadableMessage = ({ messageId }: WithMessageId) => {
180194
181195 const messageType = useMessageType ( messageId ) ;
182196
197+ useEffect ( ( ) => {
198+ if ( isAnotherMessageFocused ) {
199+ setTriggerPosition ( null ) ;
200+ }
201+ } , [ isAnotherMessageFocused ] ) ;
202+
183203 if ( ! convoId || ! messageId || ! messageType ) {
184204 return null ;
185205 }
@@ -200,23 +220,16 @@ export const GenericReadableMessage = ({ messageId }: WithMessageId) => {
200220 onContextMenu = { handleContextMenu }
201221 key = { `readable-message-${ messageId } ` }
202222 onKeyDown = { onKeyDown }
223+ $focusedKeyboard = { ! pointerDownRef . current }
203224 tabIndex = { 0 }
204225 onPointerDown = { ( ) => {
205226 pointerDownRef . current = true ;
206227 } }
207228 onFocus = { ( ) => {
208- if ( ! pointerDownRef . current ) {
209- keyboardFocusedRef . current = true ;
210- onFocus ( ) ;
211- }
229+ onFocus ( ) ;
212230 pointerDownRef . current = false ;
213231 } }
214- onBlur = { ( ) => {
215- if ( keyboardFocusedRef . current ) {
216- keyboardFocusedRef . current = false ;
217- onBlur ( ) ;
218- }
219- } }
232+ onBlur = { onBlur }
220233 >
221234 < CmpToRender
222235 contextMenuId = { ctxMenuID }
0 commit comments