Last active
May 28, 2019 16:16
-
-
Save DruRly/8a508813014264cd56f8cf7f188eed82 to your computer and use it in GitHub Desktop.
Color Theme Generator (Scooter 2/2)
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
import React, { useState } from "react" | |
import "./App.css" | |
const Color = props => { | |
return ( | |
<div style={{ backgroundColor: props.color.value }}> | |
<h1>{props.color.value}</h1> | |
<h3>Locked: {props.color.locked.toString()}</h3> | |
<input | |
name="isLocked" | |
type="checkbox" | |
checked={props.color.locked} | |
onChange={props.toggleLock} | |
/> | |
</div> | |
) | |
} | |
const App = () => { | |
const uniqueId = () => { return Math.random().toString(10).substring(5) } | |
const randomColor = () => `#${(Math.random().toString(16) + "000000").substring(2, 8)}` | |
const color = () => { return { id: uniqueId(), value: randomColor(), locked: false } } | |
const [colors, setColors] = useState([color(), color(), color()]) | |
const changeColors = () => { | |
colors.forEach(c => { | |
if (!c.locked) { c.value = randomColor() } | |
}) | |
setColors([...colors]) | |
} | |
const toggleLock = id => { | |
const matchedColor = colors.find(c => c.id === id) | |
matchedColor.locked = !matchedColor.locked | |
setColors([...colors]) | |
} | |
return ( | |
<div className="App"> | |
<button onClick={changeColors}>Change Colors</button> | |
{colors.map(color => ( | |
<Color key={color.id} color={color} toggleLock={() => toggleLock(color.id)} /> | |
))} | |
</div> | |
) | |
} | |
export default App |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment