Skip to content

Instantly share code, notes, and snippets.

@DruRly
Last active May 23, 2019 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DruRly/f040cb100085e465f2c2ff1a0e70c593 to your computer and use it in GitHub Desktop.
Save DruRly/f040cb100085e465f2c2ff1a0e70c593 to your computer and use it in GitHub Desktop.
Color Theme Generator (Bike)
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 ColorTheme = props => {
return (
<div>
<h3>Color Theme</h3>
{props.colors.map(color => (
<div style={{ backgroundColor: color }}>{color}</div>
))}
</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 [colorThemes, setColorThemes] = useState([])
const changeColors = () => {
colors.forEach(c => {
if (!c.locked) { c.value = randomColor() }
})
setColors([...colors])
}
const saveColorTheme = () => {
const colorTheme = colors.map(c => c.value)
colorThemes.unshift(colorTheme)
setColorThemes([...colorThemes])
}
const clearColorThemes = () => {
setColorThemes([])
}
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>
<button onClick={saveColorTheme}>Save Colors</button>
<button onClick={clearColorThemes}>Clear Colors</button>
{colors.map(color => (
<Color key={color.id} color={color} toggleLock={() => toggleLock(color.id)} />
))}
{colorThemes.map(colorTheme => (
<ColorTheme colors={colorTheme} />
))}
</div>
)
}
export default App
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment