|
| 1 | +--- |
| 2 | +title: Building Your First App |
| 3 | +--- |
| 4 | + |
| 5 | +import CodeBlock from '@theme/CodeBlock'; |
| 6 | +import LiveCodes from '../../src/components/LiveCodes.tsx'; |
| 7 | +import RunInLiveCodes from '../../src/components/RunInLiveCodes.tsx'; |
| 8 | + |
| 9 | +export const colorGeneratorParams = { |
| 10 | + html: `<div class="container"> |
| 11 | + <h1>Color Generator</h1> |
| 12 | + <div class="color-display" id="colorDisplay"> |
| 13 | + <span id="colorCode">#3498db</span> |
| 14 | + </div> |
| 15 | + <button id="generateBtn">Generate Color</button> |
| 16 | + <p class="hint">Click the button to generate a random color!</p> |
| 17 | +</div> |
| 18 | +`, |
| 19 | + css: `body { |
| 20 | + margin: 0; |
| 21 | + font-family: Arial, sans-serif; |
| 22 | + display: flex; |
| 23 | + justify-content: center; |
| 24 | + align-items: center; |
| 25 | + min-height: 100vh; |
| 26 | + background: #3498db; |
| 27 | + transition: background 0.5s ease; |
| 28 | +} |
| 29 | +
|
| 30 | +.container { |
| 31 | + text-align: center; |
| 32 | + background: white; |
| 33 | + padding: 40px; |
| 34 | + border-radius: 20px; |
| 35 | + box-shadow: 0 10px 30px rgba(0,0,0,0.2); |
| 36 | +} |
| 37 | +
|
| 38 | +h1 { |
| 39 | + margin: 0 0 30px 0; |
| 40 | + color: #333; |
| 41 | +} |
| 42 | +
|
| 43 | +.color-display { |
| 44 | + background: #f0f0f0; |
| 45 | + padding: 30px; |
| 46 | + border-radius: 10px; |
| 47 | + margin-bottom: 30px; |
| 48 | +} |
| 49 | +
|
| 50 | +#colorCode { |
| 51 | + font-size: 2rem; |
| 52 | + font-weight: bold; |
| 53 | + font-family: 'Courier New', monospace; |
| 54 | + color: #333; |
| 55 | +} |
| 56 | +
|
| 57 | +#generateBtn { |
| 58 | + background: #333; |
| 59 | + color: white; |
| 60 | + border: none; |
| 61 | + padding: 15px 40px; |
| 62 | + font-size: 1.1rem; |
| 63 | + border-radius: 10px; |
| 64 | + cursor: pointer; |
| 65 | + transition: transform 0.2s; |
| 66 | +} |
| 67 | +
|
| 68 | +#generateBtn:hover { |
| 69 | + transform: scale(1.05); |
| 70 | +} |
| 71 | +
|
| 72 | +#generateBtn:active { |
| 73 | + transform: scale(0.95); |
| 74 | +} |
| 75 | +
|
| 76 | +.hint { |
| 77 | + margin-top: 20px; |
| 78 | + color: #666; |
| 79 | + font-size: 0.9rem; |
| 80 | +} |
| 81 | +`, |
| 82 | + js: `const colorDisplay = document.getElementById('colorDisplay'); |
| 83 | +const colorCode = document.getElementById('colorCode'); |
| 84 | +const generateBtn = document.getElementById('generateBtn'); |
| 85 | +
|
| 86 | +function getRandomColor() { |
| 87 | + const letters = '0123456789ABCDEF'; |
| 88 | + let color = '#'; |
| 89 | + for (let i = 0; i < 6; i++) { |
| 90 | + color += letters[Math.floor(Math.random() * 16)]; |
| 91 | + } |
| 92 | + return color; |
| 93 | +} |
| 94 | +
|
| 95 | +function updateColor() { |
| 96 | + const newColor = getRandomColor(); |
| 97 | + document.body.style.background = newColor; |
| 98 | + colorCode.textContent = newColor; |
| 99 | +} |
| 100 | +
|
| 101 | +generateBtn.addEventListener('click', updateColor); |
| 102 | +
|
| 103 | +// Generate a color on page load |
| 104 | +updateColor(); |
| 105 | +`, |
| 106 | +}; |
| 107 | + |
| 108 | +# Building Your First App |
| 109 | + |
| 110 | +Learn how to build a simple color generator app using LiveCodes. This app will let users click a button to generate random background colors. |
| 111 | + |
| 112 | +Try the completed project below: |
| 113 | + |
| 114 | +<RunInLiveCodes linkText="open it in a new tab" params={colorGeneratorParams}></RunInLiveCodes>. |
| 115 | + |
| 116 | +<LiveCodes params={colorGeneratorParams}></LiveCodes> |
| 117 | + |
| 118 | +## What We'll Build |
| 119 | + |
| 120 | +A fun color generator with: |
| 121 | +- Random color generation |
| 122 | +- Display the color code |
| 123 | +- Simple, clean interface |
| 124 | + |
| 125 | +## Project Setup |
| 126 | + |
| 127 | +1. Open [LiveCodes](https://livecodes.io) |
| 128 | +2. We'll use HTML, CSS, and JavaScript |
| 129 | + |
| 130 | +## Implementation |
| 131 | + |
| 132 | +### HTML Structure |
| 133 | + |
| 134 | +<CodeBlock language="html">{colorGeneratorParams.html}</CodeBlock> |
| 135 | + |
| 136 | +### Styling |
| 137 | + |
| 138 | +<CodeBlock language="css">{colorGeneratorParams.css}</CodeBlock> |
| 139 | + |
| 140 | +### JavaScript Logic |
| 141 | + |
| 142 | +<CodeBlock language="js">{colorGeneratorParams.js}</CodeBlock> |
| 143 | + |
| 144 | +## How It Works |
| 145 | + |
| 146 | +1. **HTML**: Creates a simple layout with a color display and button |
| 147 | +2. **CSS**: Styles the interface and adds smooth transitions |
| 148 | +3. **JavaScript**: |
| 149 | + - `getRandomColor()`: Generates a random hex color |
| 150 | + - `updateColor()`: Changes the background and displays the color code |
| 151 | + - Event listener: Triggers color change on button click |
| 152 | + |
| 153 | +## Testing Your App |
| 154 | + |
| 155 | +Try these features: |
| 156 | +1. Click "Generate Color" multiple times |
| 157 | +2. Watch the smooth color transitions |
| 158 | +3. See the color code update |
| 159 | +4. Notice the button hover and click effects |
| 160 | + |
| 161 | +## Challenge: Enhance Your App |
| 162 | + |
| 163 | +Try adding these features: |
| 164 | +- Copy color code to clipboard when clicked |
| 165 | +- Add a history of recent colors |
| 166 | +- Let users save their favorite colors |
| 167 | +- Add different color format options (RGB, HSL) |
| 168 | + |
| 169 | +## Congratulations! |
| 170 | + |
| 171 | +You've just built your color generator app with LiveCodes! |
| 172 | + |
| 173 | +Compare your version with the completed project above. Did you add any personal touches? |
| 174 | +<RunInLiveCodes linkText="View the completed project" params={colorGeneratorParams}></RunInLiveCodes> |
| 175 | + |
| 176 | +## Next Steps |
| 177 | + |
| 178 | +- [External Resources](/docs/features/external-resources) - Add libraries like color manipulation tools |
| 179 | +- [Getting Started Guide](getting-started-guide) - Review the basics |
| 180 | + |
| 181 | +## Complete Code Summary |
| 182 | + |
| 183 | +**Concepts Covered**: DOM manipulation, events, random generation, CSS transitions |
| 184 | + |
| 185 | +**Time to Build**: 10-15 minutes |
0 commit comments