@@ -10,7 +10,6 @@ import {
1010 PanResponder ,
1111 ScrollView ,
1212 View ,
13- type LayoutChangeEvent ,
1413 type LayoutRectangle ,
1514 type NativeTouchEvent ,
1615} from 'react-native' ;
@@ -63,6 +62,7 @@ export const Waveform = forwardRef<IWaveformRef, IWaveform>((props, ref) => {
6362 } = props as StaticWaveform & LiveWaveform ;
6463 const viewRef = useRef < View > ( null ) ;
6564 const scrollRef = useRef < ScrollView > ( null ) ;
65+ const isLayoutCalculated = useRef < boolean > ( false ) ;
6666 const [ waveform , setWaveform ] = useState < number [ ] > ( [ ] ) ;
6767 const [ viewLayout , setViewLayout ] = useState < LayoutRectangle | null > ( null ) ;
6868 const [ seekPosition , setSeekPosition ] = useState < NativeTouchEvent | null > (
@@ -403,13 +403,19 @@ export const Waveform = forwardRef<IWaveformRef, IWaveform>((props, ref) => {
403403 const getNumberOfSamples = floor (
404404 ( viewLayout ?. width ?? 0 ) / ( candleWidth + candleSpace )
405405 ) ;
406+
407+ // when orientation changes, the layout needs to be recalculated
408+ if ( viewLayout ?. x === 0 && viewLayout ?. y === 0 ) {
409+ isLayoutCalculated . current = false ;
410+ }
411+
406412 setNoOfSamples ( getNumberOfSamples ) ;
407413 if ( mode === 'static' ) {
408414 getAudioWaveFormForPath ( getNumberOfSamples ) ;
409415 }
410416 }
411417 // eslint-disable-next-line react-hooks/exhaustive-deps
412- } , [ viewLayout , mode , candleWidth , candleSpace ] ) ;
418+ } , [ viewLayout ?. width , mode , candleWidth , candleSpace ] ) ;
413419
414420 useEffect ( ( ) => {
415421 if ( ! isNil ( seekPosition ) ) {
@@ -516,8 +522,25 @@ export const Waveform = forwardRef<IWaveformRef, IWaveform>((props, ref) => {
516522 // eslint-disable-next-line react-hooks/exhaustive-deps
517523 } , [ panMoving ] ) ;
518524
525+ const calculateLayout = ( ) : void => {
526+ viewRef . current ?. measureInWindow ( ( x , y , width , height ) => {
527+ setViewLayout ( { x, y, width, height } ) ;
528+ if ( x !== 0 || y !== 0 ) {
529+ // found the position of view in window
530+ isLayoutCalculated . current = true ;
531+ }
532+ } ) ;
533+ } ;
534+
519535 const panResponder = useRef (
520536 PanResponder . create ( {
537+ onStartShouldSetPanResponder : ( ) => {
538+ if ( ! isLayoutCalculated . current ) {
539+ calculateLayout ( ) ;
540+ }
541+
542+ return true ;
543+ } ,
521544 onMoveShouldSetPanResponder : ( ) => true ,
522545 onPanResponderGrant : ( ) => {
523546 setPanMoving ( true ) ;
@@ -556,10 +579,7 @@ export const Waveform = forwardRef<IWaveformRef, IWaveform>((props, ref) => {
556579 < View
557580 ref = { viewRef }
558581 style = { styles . waveformInnerContainer }
559- onLayout = { ( event : LayoutChangeEvent ) => {
560- const { height, width, x, y } = event . nativeEvent . layout ;
561- setViewLayout ( { height, width, x, y } ) ;
562- } }
582+ onLayout = { calculateLayout }
563583 { ...( mode === 'static' ? panResponder . panHandlers : { } ) } >
564584 < ScrollView
565585 horizontal
0 commit comments