1- import React , { useCallback , useMemo } from 'react' ;
1+ import React , { useCallback , useMemo , useRef } from 'react' ;
22import type * as CSS from 'csstype' ;
33import { HsvaColor , hsvaToHslaString } from '@uiw/color-convert' ;
44import Interactive , { type Interaction } from '@uiw/react-drag-event-interactive' ;
@@ -25,17 +25,40 @@ const Saturation = React.forwardRef<HTMLDivElement, SaturationProps>((props, ref
2525 position : 'relative' ,
2626 } ;
2727
28- const handleChange = ( interaction : Interaction , event : MouseEvent | TouchEvent ) => {
29- onChange &&
30- hsva &&
31- onChange ( {
32- h : hsva . h ,
33- s : interaction . left * 100 ,
34- v : ( 1 - interaction . top ) * 100 ,
35- a : hsva . a ,
36- // source: 'hsv',
37- } ) ;
38- } ;
28+ const containerRef = useRef < HTMLDivElement | null > ( null ) ;
29+
30+ // Combine external ref with internal ref
31+ const combinedRef = useCallback (
32+ ( node : HTMLDivElement | null ) => {
33+ containerRef . current = node ;
34+ if ( typeof ref === 'function' ) {
35+ ref ( node ) ;
36+ } else if ( ref && 'current' in ref ) {
37+ ( ref as React . MutableRefObject < HTMLDivElement | null > ) . current = node ;
38+ }
39+ } ,
40+ [ ref ] ,
41+ ) ;
42+
43+ const handleChange = useCallback (
44+ ( interaction : Interaction , event : MouseEvent | TouchEvent ) => {
45+ onChange &&
46+ hsva &&
47+ onChange ( {
48+ h : hsva . h ,
49+ s : interaction . left * 100 ,
50+ v : ( 1 - interaction . top ) * 100 ,
51+ a : hsva . a ,
52+ // source: 'hsv',
53+ } ) ;
54+ // Ensure the component maintains focus after drag interaction
55+ const element = containerRef . current ;
56+ if ( element ) {
57+ element . focus ( ) ;
58+ }
59+ } ,
60+ [ hsva , onChange ] ,
61+ ) ;
3962
4063 const handleKeyDown = useCallback (
4164 ( event : React . KeyboardEvent < HTMLDivElement > ) => {
@@ -112,7 +135,7 @@ const Saturation = React.forwardRef<HTMLDivElement, SaturationProps>((props, ref
112135 ...containerStyle ,
113136 outline : 'none' ,
114137 } }
115- ref = { ref }
138+ ref = { combinedRef }
116139 onMove = { handleChange }
117140 onDown = { handleChange }
118141 onKeyDown = { handleKeyDown }
0 commit comments