Last active
August 27, 2018 15:28
-
-
Save gtwalford/297c30d29d7222f9fea31efb68848026 to your computer and use it in GitHub Desktop.
JS Bin// source http://jsbin.com/kuyubamixu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<div id='admin'></div> | |
<div class='template'> | |
<div class='template__section' data-id='1'> | |
<p class='template__section-title'>Section 1</p> | |
</div> | |
<div class='template__section' data-id='2'> | |
<p class='template__section-title'>Section 2</p> | |
</div> | |
</div> | |
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script> | |
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body { | |
background-color: #eeeeee; | |
} | |
.template { | |
display: flex; | |
flex-direction: row; | |
justify-content: space-evenly; | |
} | |
.template__section { | |
background-color: #ffffff; | |
min-height: 100px; | |
padding: 10px; | |
position: relative; | |
width: 40%; | |
} | |
.template__section:hover .admin-edit { | |
display: block; | |
} | |
.admin-edit { | |
background: red; | |
border: 0; | |
border-radius: 4px; | |
color: #ffffff; | |
display: none; | |
font-weight: bold; | |
position: absolute; | |
right: 0; | |
top: 0; | |
transform: translate(25%, -25%); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Admin extends React.Component { | |
constructor () { | |
super(); | |
this.sections = document.querySelectorAll('.template__section'); | |
this.toggleEdit = this.toggleEdit.bind(this); | |
} | |
toggleEdit (event) { | |
console.log(`Edit Section ${event.target.parentElement.getAttribute('data-id')}`); | |
} | |
componentDidMount () { | |
this.sections.forEach( el => { | |
const button = document.createElement('button'); | |
button.className = 'admin-edit'; | |
button.innerHTML = 'Edit'; | |
el.appendChild(button); | |
el.querySelector('.admin-edit').addEventListener('click', this.toggleEdit); | |
}); | |
} | |
componentDidUnMount () { | |
this.sections.forEach( el => { | |
el.querySelector('.admin-edit').removeEventListener('click', this.toggleEdit); | |
}) | |
} | |
render () { | |
return ( | |
React.createElement("div", null, | |
React.createElement("p", null, "Admin Panel") | |
) | |
); | |
} | |
} | |
ReactDOM.render( | |
React.createElement(Admin, null), | |
document.getElementById('admin') | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment