@@ -13,16 +13,24 @@ import useExampleContext from "./hooks/useExampleContext";
1313import SplitPane , { Pane } from 'split-pane-react' ;
1414import 'split-pane-react/esm/themes/default.css' ;
1515
16- const ExampleDetail = ( { examplesUrl} ) => {
17-
16+ const ExampleDetail = ( { examplesUrl, examples, refresh} ) => {
1817 const { id} = useParams ( ) ;
19-
2018 const { exampleHtml, loadExample, setExampleHtml} = useExampleContext ( ) ;
21-
2219 const [ sizes , setSizes ] = useState ( [ '44%' , 'auto' ] ) ;
23-
24- // Fixing mouse pointer evetns when dragging panels
2520 const [ drag , setDrag ] = useState ( false ) ;
21+ const [ showCode , setShowCode ] = useState ( window . innerWidth > 768 ) ;
22+ const [ isMobile , setIsMobile ] = useState ( window . innerWidth <= 768 ) ;
23+
24+ useEffect ( ( ) => {
25+ const handleResize = ( ) => {
26+ const mobile = window . innerWidth <= 768 ;
27+ setIsMobile ( mobile ) ;
28+ setShowCode ( ! mobile ) ;
29+ } ;
30+
31+ window . addEventListener ( 'resize' , handleResize ) ;
32+ return ( ) => window . removeEventListener ( 'resize' , handleResize ) ;
33+ } , [ ] ) ;
2634
2735 useEffect ( ( ) => {
2836 let _id = id || 'baseLayers' ;
@@ -49,33 +57,98 @@ const ExampleDetail = ({examplesUrl}) => {
4957 setDrag ( false )
5058 }
5159
52- return ( < >
53- < div className = "og-examples__panel" >
54- < SplitPane
55- split = 'vertical'
56- sizes = { sizes }
57- onChange = { setSizes }
58- onDragStart = { handleDragStart }
59- onDragEnd = { handleDragEnd }
60- >
61- < Pane >
62- < Editor examplesUrl = { examplesUrl } onRun = { handleRun } onRaw = { handleRaw } code = { exampleHtml }
63- id = { id } />
64- </ Pane >
65- < Frame examplesUrl = { examplesUrl } code = { exampleHtml } id = { id }
66- style = { { "pointerEvents" : drag ? "none" : "" } } />
67- </ SplitPane >
60+ const toggleCode = ( ) => {
61+ setShowCode ( ! showCode ) ;
62+ }
63+
64+ if ( isMobile ) {
65+ return (
66+ < div className = "og-examples__mobile-layout" >
67+ < div className = "og-examples__frame-container" >
68+ { showCode && (
69+ < button
70+ className = "code-toggle-btn"
71+ onClick = { toggleCode }
72+ >
73+ Hide Code
74+ </ button >
75+ ) }
76+ { showCode ? (
77+ < SplitPane
78+ split = 'vertical'
79+ sizes = { sizes }
80+ onChange = { setSizes }
81+ onDragStart = { handleDragStart }
82+ onDragEnd = { handleDragEnd }
83+ >
84+ < Pane >
85+ < Editor examplesUrl = { examplesUrl } onRun = { handleRun } onRaw = { handleRaw } code = { exampleHtml }
86+ id = { id } />
87+ </ Pane >
88+ < Frame examplesUrl = { examplesUrl } code = { exampleHtml } id = { id }
89+ style = { { "pointerEvents" : drag ? "none" : "" } } />
90+ </ SplitPane >
91+ ) : (
92+ < >
93+ < Frame examplesUrl = { examplesUrl } code = { exampleHtml } id = { id }
94+ style = { { "pointerEvents" : drag ? "none" : "" } } />
95+ < button
96+ className = "code-toggle-btn"
97+ onClick = { toggleCode }
98+ >
99+ Show Code
100+ </ button >
101+ </ >
102+ ) }
103+ </ div >
104+ < div className = "og-examples__list-container" >
105+ < List
106+ examples = { examples }
107+ onClick = { ( id ) => {
108+ if ( window . location . pathname . endsWith ( id ) ) {
109+ refresh ( ) ;
110+ }
111+ } }
112+ />
113+ </ div >
68114 </ div >
69- </ >
70- )
115+ ) ;
116+ }
117+
118+ return (
119+ < div className = "og-examples__panel" >
120+ < SplitPane
121+ split = 'vertical'
122+ sizes = { sizes }
123+ onChange = { setSizes }
124+ onDragStart = { handleDragStart }
125+ onDragEnd = { handleDragEnd }
126+ >
127+ < Pane >
128+ < Editor examplesUrl = { examplesUrl } onRun = { handleRun } onRaw = { handleRaw } code = { exampleHtml }
129+ id = { id } />
130+ </ Pane >
131+ < Frame examplesUrl = { examplesUrl } code = { exampleHtml } id = { id }
132+ style = { { "pointerEvents" : drag ? "none" : "" } } />
133+ </ SplitPane >
134+ </ div >
135+ ) ;
71136} ;
72137
73138function App ( ) {
74-
75139 const { refresh} = useExampleContext ( ) ;
76-
77140 const [ examples , setExamples ] = useState ( [ ] ) ;
78141 const [ examplesUrl , setExamplesUrl ] = useState ( '' ) ;
142+ const [ isMobile , setIsMobile ] = useState ( window . innerWidth <= 768 ) ;
143+
144+ useEffect ( ( ) => {
145+ const handleResize = ( ) => {
146+ setIsMobile ( window . innerWidth <= 768 ) ;
147+ } ;
148+
149+ window . addEventListener ( 'resize' , handleResize ) ;
150+ return ( ) => window . removeEventListener ( 'resize' , handleResize ) ;
151+ } , [ ] ) ;
79152
80153 const fetchData = useCallback ( async ( ) => {
81154 try {
@@ -99,17 +172,21 @@ function App() {
99172 fetchConfig ( ) ;
100173 } , [ fetchConfig ] ) ;
101174
102- return ( < Router >
103- < List examples = { examples } onClick = { ( id ) => {
104- if ( window . location . pathname . endsWith ( id ) ) {
105- refresh ( ) ;
106- }
107- } } />
108- < Routes >
109- < Route path = "/" element = { < ExampleDetail examplesUrl = { examplesUrl } /> } />
110- < Route path = "/examples/:id" element = { < ExampleDetail examplesUrl = { examplesUrl } /> } />
111- </ Routes >
112- </ Router > ) ;
175+ return (
176+ < Router >
177+ { ! isMobile && (
178+ < List examples = { examples } onClick = { ( id ) => {
179+ if ( window . location . pathname . endsWith ( id ) ) {
180+ refresh ( ) ;
181+ }
182+ } } />
183+ ) }
184+ < Routes >
185+ < Route path = "/" element = { < ExampleDetail examplesUrl = { examplesUrl } examples = { examples } refresh = { refresh } /> } />
186+ < Route path = "/examples/:id" element = { < ExampleDetail examplesUrl = { examplesUrl } examples = { examples } refresh = { refresh } /> } />
187+ </ Routes >
188+ </ Router >
189+ ) ;
113190}
114191
115192export default App ;
0 commit comments