11import { generateDiffFile , DiffFile } from "@git-diff-view/file" ;
2- import { Button , ButtonGroup , FloatingIndicator , Group , LoadingOverlay , Tooltip } from "@mantine/core" ;
2+ import { Button , ButtonGroup , Card , FloatingIndicator , Group , LoadingOverlay , Popover , Tooltip } from "@mantine/core" ;
33import { useDisclosure } from "@mantine/hooks" ;
44import { IconCode , IconPlayerPlay , IconRefresh , IconBrandReact , IconBrandVue } from "@tabler/icons-react" ;
5- import { useState , startTransition , useCallback } from "react" ;
5+ import { useState , startTransition , useCallback , forwardRef } from "react" ;
66
77import { useDiffHighlighter } from "../hooks/useDiffHighlighter" ;
88
@@ -27,6 +27,14 @@ export const MainContentDiffExample = () => {
2727
2828 const [ platform , setPlatform ] = useState < "react" | "vue" > ( "react" ) ;
2929
30+ const [ showCode , setShowCode ] = useState ( true ) ;
31+
32+ const closeShowCode = ( ) => setShowCode ( false ) ;
33+
34+ const [ showPlatform , setShowPlatform ] = useState ( true ) ;
35+
36+ const closePlatform = ( ) => setShowPlatform ( false ) ;
37+
3038 const [ loading , setLoading ] = useState ( false ) ;
3139
3240 const refreshFile = ( ) => {
@@ -46,22 +54,68 @@ export const MainContentDiffExample = () => {
4654
4755 const [ controlsRefs , setControlsRefs ] = useState < Record < string , HTMLButtonElement | null > > ( { } ) ;
4856
57+ const Element = forwardRef < HTMLButtonElement , any > (
58+ useCallback (
59+ ( { onClick, ...props } , ref ) => (
60+ < Tooltip label = "show the code" >
61+ < Button
62+ variant = "light"
63+ size = "compact-sm"
64+ onClick = { ( e ) => {
65+ onClick ?.( e ) ;
66+ open ( ) ;
67+ } }
68+ color = "gray"
69+ ref = { useCallback ( ( node : HTMLButtonElement ) => {
70+ if ( typeof ref === "function" ) ref ( node ) ;
71+ else if ( ref ) ref . current = node ;
72+ setControlsRefs ( ( l ) => ( { ...l , code : node } ) ) ;
73+ } , [ ] ) }
74+ { ...props }
75+ >
76+ < IconCode className = "w-[1.2em]" />
77+ </ Button >
78+ </ Tooltip >
79+ ) ,
80+ [ ]
81+ )
82+ ) ;
83+
84+ const PlatformSwitchElement = forwardRef < HTMLButtonElement , any > ( ( { onClick, ...props } , ref ) => (
85+ < Tooltip label = "switch the platform" >
86+ < Button
87+ variant = "light"
88+ size = "compact-sm"
89+ ref = { ref }
90+ onClick = { ( e ) => {
91+ onClick ?.( e ) ;
92+ setPlatform ( platform === "react" ? "vue" : "react" ) ;
93+ } }
94+ { ...props }
95+ >
96+ { platform === "react" ? < IconBrandReact className = "w-[1.2em]" /> : < IconBrandVue className = "w-[1.2em]" /> }
97+ </ Button >
98+ </ Tooltip >
99+ ) ) ;
100+
49101 return (
50102 < >
51103 < Group className = "fixed right-6 top-2 z-10" >
52104 < div ref = { setRootRef } >
53105 < ButtonGroup >
54- < Tooltip label = "show the code" >
55- < Button
56- variant = "light"
57- size = "compact-sm"
58- onClick = { open }
59- color = "gray"
60- ref = { useCallback ( ( node : HTMLButtonElement ) => setControlsRefs ( ( l ) => ( { ...l , code : node } ) ) , [ ] ) }
61- >
62- < IconCode className = "w-[1.2em]" />
63- </ Button >
64- </ Tooltip >
106+ < Popover withArrow opened = { showCode } onClose = { closeShowCode } closeOnClickOutside = { false } >
107+ < Popover . Target >
108+ < Element onClick = { closeShowCode } />
109+ </ Popover . Target >
110+ < Popover . Dropdown className = "animate-float" >
111+ < Card >
112+ < Card . Section > Click to show the code ✨✨</ Card . Section >
113+ </ Card >
114+ < Button size = "compact-sm" className = "float-right" onClick = { closeShowCode } >
115+ Close
116+ </ Button >
117+ </ Popover . Dropdown >
118+ </ Popover >
65119 < Tooltip label = "show the preview" >
66120 < Button
67121 variant = "light"
@@ -89,15 +143,19 @@ export const MainContentDiffExample = () => {
89143 </ Button >
90144 </ Tooltip >
91145 ) : (
92- < Tooltip label = "switch the platform" >
93- < Button
94- variant = "light"
95- size = "compact-sm"
96- onClick = { ( ) => setPlatform ( platform === "react" ? "vue" : "react" ) }
97- >
98- { platform === "react" ? < IconBrandReact className = "w-[1.2em]" /> : < IconBrandVue className = "w-[1.2em]" /> }
99- </ Button >
100- </ Tooltip >
146+ < Popover withArrow opened = { showPlatform } onClose = { closePlatform } closeOnClickOutside = { false } >
147+ < Popover . Target >
148+ < PlatformSwitchElement onClick = { closePlatform } />
149+ </ Popover . Target >
150+ < Popover . Dropdown className = "animate-float" >
151+ < Card >
152+ < Card . Section > Click to show the other platform code ✨✨</ Card . Section >
153+ </ Card >
154+ < Button size = "compact-sm" className = "float-right" onClick = { closePlatform } >
155+ Close
156+ </ Button >
157+ </ Popover . Dropdown >
158+ </ Popover >
101159 ) }
102160 </ Group >
103161
0 commit comments