1
+ /* eslint-disable react/prop-types */
1
2
import React , { Fragment , useEffect , useState , useRef } from 'react' ;
2
3
import Grid from '@material-ui/core/Grid' ;
3
4
import { makeStyles } from '@material-ui/core/styles' ;
@@ -16,10 +17,55 @@ import IconButton from '@material-ui/core/IconButton';
16
17
import { getParameters } from 'codesandbox/lib/api/define' ;
17
18
import Tooltip from '@material-ui/core/Tooltip' ;
18
19
import dynamic from 'next/dynamic' ;
20
+ import { useRouter } from 'next/router' ;
19
21
20
22
import GhIcon from './common/gh-svg-icon' ;
21
23
import CodesandboxIcon from './common/code-sandbox-svg-icon' ;
22
24
import CodeEditor from './code-editor' ;
25
+ import { headerToId } from '../helpers/list-of-contents' ;
26
+ import ShareButton from './mdx/share-button' ;
27
+
28
+ const useHeadingStyles = makeStyles ( ( theme ) => ( {
29
+ anchor : {
30
+ textDecoration : 'none' ,
31
+ color : 'inherit' ,
32
+ fontWeight : 'inherit'
33
+ } ,
34
+ wrapper : {
35
+ flexGrow : 1 ,
36
+ display : 'flex'
37
+ } ,
38
+ heading : {
39
+ paddingTop : 4 ,
40
+ fontSize : theme . typography . pxToRem ( 20 ) ,
41
+ fontWeight : theme . typography . fontWeightRegular ,
42
+ display : 'flex' ,
43
+ alignItems : 'center' ,
44
+ '& button' : {
45
+ visibility : 'hidden'
46
+ } ,
47
+ '&:hover button' : {
48
+ visibility : 'initial'
49
+ }
50
+ }
51
+ } ) ) ;
52
+
53
+ export const Heading = ( { level, children, component } ) => {
54
+ const router = useRouter ( ) ;
55
+ const classes = useHeadingStyles ( ) ;
56
+ const id = headerToId ( children ) ;
57
+ const path = `${ router . asPath } #${ id } ` ;
58
+ return (
59
+ < div id = { id } className = { classes . wrapper } data-scroll = "true" >
60
+ < Typography id = { `heading-${ id } ` } className = { classes . heading } variant = { `h${ level } ` } component = { component } >
61
+ < a href = { path } className = { classes . anchor } data-mdlink = "md-heading" >
62
+ { children }
63
+ < ShareButton path = { path } />
64
+ </ a >
65
+ </ Typography >
66
+ </ div >
67
+ ) ;
68
+ } ;
23
69
24
70
const useStyles = makeStyles ( ( theme ) => ( {
25
71
container : {
@@ -36,17 +82,6 @@ const useStyles = makeStyles((theme) => ({
36
82
componentPanel : {
37
83
padding : 16
38
84
} ,
39
- heading : {
40
- fontSize : theme . typography . pxToRem ( 20 ) ,
41
- fontWeight : theme . typography . fontWeightRegular ,
42
- flexGrow : 1 ,
43
- display : 'flex' ,
44
- alignItems : 'center'
45
- } ,
46
- secondaryHeading : {
47
- fontSize : theme . typography . pxToRem ( 15 ) ,
48
- color : theme . palette . text . secondary
49
- } ,
50
85
expansionPanel : {
51
86
border : 'none' ,
52
87
boxShadow : 'none' ,
@@ -55,20 +90,10 @@ const useStyles = makeStyles((theme) => ({
55
90
} ,
56
91
expansionPanelSummary : {
57
92
padding : 0
58
- } ,
59
- pf4 : {
60
- padding : 32 ,
61
- '& form' : {
62
- width : '100%'
63
- }
64
93
}
65
94
} ) ) ;
66
95
67
- /**
68
- * Generates html markup for the sandbox
69
- * @param {String } type either MUI or PF4
70
- */
71
- const generateIndex = ( type ) => `<!DOCTYPE html>
96
+ const index = `<!DOCTYPE html>
72
97
<html lang="en">
73
98
<head>
74
99
<meta charset="utf-8" />
@@ -92,12 +117,12 @@ const generateIndex = (type) => `<!DOCTYPE html>
92
117
</html>
93
118
` ;
94
119
95
- const getPayload = ( type , code , sourceFiles = { } ) =>
120
+ const getPayload = ( code , sourceFiles = { } ) =>
96
121
getParameters ( {
97
122
files : {
98
123
...sourceFiles ,
99
124
'public/index.html' : {
100
- content : generateIndex ( type )
125
+ content : index
101
126
} ,
102
127
'package.json' : {
103
128
content : {
@@ -135,7 +160,7 @@ const getPayload = (type, code, sourceFiles = {}) =>
135
160
}
136
161
} ) ;
137
162
138
- const CodeExample = ( { source, mode, mapper } ) => {
163
+ const CodeExample = ( { source, mode } ) => {
139
164
const [ name , setName ] = useState ( '' ) ;
140
165
const [ codeSource , setCodeSource ] = useState ( '' ) ;
141
166
const { current : Component } = useRef (
@@ -171,21 +196,21 @@ const CodeExample = ({ source, mode, mapper }) => {
171
196
}
172
197
>
173
198
{ Component && (
174
- < Typography className = { classes . heading } component = "h4" variant = "h3 ">
199
+ < Heading component = "h3" level = "5 ">
175
200
{ name }
176
- </ Typography >
201
+ </ Heading >
177
202
) }
178
203
< Box display = "flex" >
179
204
< form action = "https://codesandbox.io/api/v1/sandboxes/define" method = "POST" target = "_blank" >
180
- < input type = "hidden" name = "parameters" value = { getPayload ( mapper , codeSource , sourceFiles ) } />
205
+ < input type = "hidden" name = "parameters" value = { getPayload ( codeSource , sourceFiles ) } />
181
206
< Tooltip title = "Edit in codesandbox" >
182
207
< IconButton disableFocusRipple type = "submit" onClick = { ( event ) => event . stopPropagation ( ) } >
183
208
< CodesandboxIcon />
184
209
</ IconButton >
185
210
</ Tooltip >
186
211
</ form >
187
212
< Link
188
- href = { `https://github.com/data-driven-forms/react-forms/tree/master/packages/react-renderer-demo/src/app/ examples/${ source } .js` }
213
+ href = { `https://github.com/data-driven-forms/react-forms/tree/master/packages/react-renderer-demo/src/examples/${ source } .js` }
189
214
target = "_blank"
190
215
rel = "noopener noreferrer"
191
216
onClick = { ( event ) => event . stopPropagation ( ) }
@@ -237,13 +262,11 @@ const CodeExample = ({ source, mode, mapper }) => {
237
262
238
263
CodeExample . propTypes = {
239
264
source : PropTypes . string . isRequired ,
240
- mode : PropTypes . oneOf ( [ 'code' , 'preview' ] ) ,
241
- mapper : PropTypes . oneOf ( [ 'pf4' , 'mui' ] )
265
+ mode : PropTypes . oneOf ( [ 'code' , 'preview' ] )
242
266
} ;
243
267
244
268
CodeExample . defaultProps = {
245
- mode : 'code' ,
246
- mapper : 'pf4'
269
+ mode : 'code'
247
270
} ;
248
271
249
272
export default CodeExample ;
0 commit comments