@@ -26,71 +26,75 @@ export const AIMenuController = (props: {
2626 const blockId = aiMenuState === "closed" ? undefined : aiMenuState . blockId ;
2727
2828 const floatingUIOptions = useMemo < FloatingUIOptions > (
29- ( ) => ( {
30- ...props . floatingUIOptions ,
31- useFloatingOptions : {
32- open : aiMenuState !== "closed" ,
33- placement : "bottom" ,
34- middleware : [
35- offset ( 10 ) ,
36- flip ( ) ,
37- size ( {
38- apply ( { rects, elements } ) {
39- Object . assign ( elements . floating . style , {
40- width : `${ rects . reference . width } px` ,
41- } ) ;
42- } ,
43- } ) ,
44- ] ,
45- onOpenChange : ( open ) => {
46- if ( open || aiMenuState === "closed" ) {
47- return ;
48- }
29+ ( ) =>
30+ ( {
31+ ...props . floatingUIOptions ,
32+ useFloatingOptions : {
33+ open : aiMenuState !== "closed" ,
34+ placement : "bottom" ,
35+ middleware : [
36+ offset ( 10 ) ,
37+ flip ( ) ,
38+ size ( {
39+ apply ( { rects, elements } ) {
40+ Object . assign ( elements . floating . style , {
41+ width : `${ rects . reference . width } px` ,
42+ } ) ;
43+ } ,
44+ } ) ,
45+ ] ,
46+ onOpenChange : ( open ) => {
47+ if ( open || aiMenuState === "closed" ) {
48+ return ;
49+ }
4950
50- if ( aiMenuState . status === "user-input" ) {
51- ai . closeAIMenu ( ) ;
52- } else if (
53- aiMenuState . status === "user-reviewing" ||
54- aiMenuState . status === "error"
55- ) {
56- ai . rejectChanges ( ) ;
57- }
58- } ,
59- whileElementsMounted ( reference , floating , update ) {
60- return autoUpdate ( reference , floating , update , {
61- animationFrame : true ,
62- } ) ;
63- } ,
64- ...props . floatingUIOptions ?. useFloatingOptions ,
65- } ,
66- useDismissProps : {
67- enabled :
68- aiMenuState === "closed" || aiMenuState . status === "user-input" ,
69- // We should just be able to set `referencePress: true` instead of
70- // using this listener, but this doesn't seem to trigger.
71- // (probably because we don't assign the referenceProps to the reference element)
72- outsidePress : ( event ) => {
73- if ( event . target instanceof Element ) {
74- const blockElement = event . target . closest ( ".bn-block" ) ;
75- if (
76- blockElement &&
77- blockElement . getAttribute ( "data-id" ) === blockId
78- ) {
51+ if ( aiMenuState . status === "user-input" ) {
7952 ai . closeAIMenu ( ) ;
53+ } else if (
54+ aiMenuState . status === "user-reviewing" ||
55+ aiMenuState . status === "error"
56+ ) {
57+ ai . rejectChanges ( ) ;
58+ }
59+ } ,
60+ whileElementsMounted ( reference , floating , update ) {
61+ return autoUpdate ( reference , floating , update , {
62+ animationFrame : true ,
63+ } ) ;
64+ } ,
65+ ...props . floatingUIOptions ?. useFloatingOptions ,
66+ } ,
67+ useDismissProps : {
68+ enabled :
69+ aiMenuState === "closed" || aiMenuState . status === "user-input" ,
70+ // We should just be able to set `referencePress: true` instead of
71+ // using this listener, but this doesn't seem to trigger.
72+ // (probably because we don't assign the referenceProps to the reference element)
73+ outsidePress : ( event ) => {
74+ if ( event . target instanceof Element ) {
75+ const blockElement = event . target . closest ( ".bn-block" ) ;
76+ if (
77+ blockElement &&
78+ blockElement . getAttribute ( "data-id" ) === blockId
79+ ) {
80+ ai . closeAIMenu ( ) ;
81+ }
8082 }
81- }
8283
83- return true ;
84+ return true ;
85+ } ,
86+ ...props . floatingUIOptions ?. useDismissProps ,
8487 } ,
85- ... props . floatingUIOptions ?. useDismissProps ,
86- } ,
87- elementProps : {
88- style : {
89- zIndex : 100 ,
88+ elementProps : {
89+ style : {
90+ zIndex : 100 ,
91+ } ,
92+ ... props . floatingUIOptions ?. elementProps ,
9093 } ,
91- ...props . floatingUIOptions ?. elementProps ,
92- } ,
93- } ) ,
94+ // we use the focus manager instead of `autoFocus={true}` to prevent "page-scrolls-to-top-when-opening-the-floating-element"
95+ // see https://floating-ui.com/docs/floatingfocusmanager#page-scrolls-to-top-when-opening-the-floating-element
96+ focusManagerProps : { } ,
97+ } ) satisfies FloatingUIOptions ,
9498 [ ai , aiMenuState , blockId , props . floatingUIOptions ] ,
9599 ) ;
96100
0 commit comments