1
- import React , { useState , Fragment } from 'react' ;
1
+ import React , { useState , useEffect } from 'react' ;
2
2
import Button from '@material-ui/core/Button' ;
3
3
import Checkbox from '@material-ui/core/Checkbox' ;
4
4
import Menu from '@material-ui/core/Menu' ;
@@ -8,6 +8,10 @@ import Link from 'next/link';
8
8
import { makeStyles } from '@material-ui/core/styles' ;
9
9
import { getScopedLink , getPrefix } from '../helpers/scoped-links' ;
10
10
11
+ const links = {
12
+ pf4 :
`<link class="pf4" href="https://cdn.jsdelivr.net/npm/@patternfly/[email protected] /patternfly.min.css" rel="stylesheet" />`
13
+ } ;
14
+
11
15
const useStyles = makeStyles ( ( theme ) => ( {
12
16
menuTrigger : {
13
17
color : '#fff'
@@ -25,33 +29,53 @@ const MapperMenu = () => {
25
29
const [ anchorEl , setAnchorEl ] = useState ( null ) ;
26
30
const router = useRouter ( ) ;
27
31
const classes = useStyles ( ) ;
32
+ const activeScope = getPrefix ( router . pathname ) ;
28
33
const handleClick = ( event ) => {
29
34
setAnchorEl ( event . currentTarget ) ;
30
35
} ;
31
36
32
- const handleClose = ( ) => {
37
+ const applyMapperStyle = ( mapper ) => {
38
+ const head = document . head ;
39
+ const mapperLink = head . getElementsByClassName ( activeScope ) [ 0 ] ;
40
+ if ( mapperLink ) {
41
+ head . removeChild ( mapperLink ) ;
42
+ }
43
+
44
+ if ( mapper !== 'mui' ) {
45
+ let newLink = document . createElement ( 'div' ) ;
46
+ newLink . innerHTML = links [ mapper ] ;
47
+ newLink = newLink . firstChild ;
48
+ head . appendChild ( newLink ) ;
49
+ }
50
+ } ;
51
+
52
+ useEffect ( ( ) => {
53
+ applyMapperStyle ( activeScope ) ;
54
+ } , [ ] ) ;
55
+
56
+ const handleClose = ( mapper ) => {
57
+ applyMapperStyle ( mapper ) ;
33
58
setAnchorEl ( null ) ;
34
59
} ;
35
60
36
61
const muiLink = getScopedLink ( router . pathname , 'mui' ) ;
37
62
const pf4Link = getScopedLink ( router . pathname , 'pf4' ) ;
38
- const activeScope = getPrefix ( router . pathname ) ;
39
63
40
64
return (
41
65
< div >
42
66
< Button aria-controls = "simple-menu" aria-haspopup = "true" className = { classes . menuTrigger } onClick = { handleClick } >
43
67
Mapper: { activeScope }
44
68
</ Button >
45
69
< Menu id = "simple-menu" anchorEl = { anchorEl } open = { Boolean ( anchorEl ) } onClose = { handleClose } >
46
- < MenuItem className = { classes . menuItem } onClick = { handleClose } selected = { activeScope === 'mui' } >
70
+ < MenuItem className = { classes . menuItem } onClick = { ( ) => handleClose ( 'mui' ) } selected = { activeScope === 'mui' } >
47
71
< Link href = { muiLink } >
48
72
< a href = { muiLink } className = { classes . scopeLink } >
49
73
< Checkbox checked = { activeScope === 'mui' } />
50
74
Material UI
51
75
</ a >
52
76
</ Link >
53
77
</ MenuItem >
54
- < MenuItem className = { classes . menuItem } onClick = { handleClose } selected = { activeScope === 'pf4' } >
78
+ < MenuItem className = { classes . menuItem } onClick = { ( ) => handleClose ( 'pf4' ) } selected = { activeScope === 'pf4' } >
55
79
< Link href = { pf4Link } >
56
80
< a href = { pf4Link } className = { classes . scopeLink } >
57
81
< Checkbox checked = { activeScope === 'pf4' } />
0 commit comments