Skip to content

Commit 397574b

Browse files
authored
feat(demo): Support solid and gradient by toggle (DenverCoder1#485)
1 parent e1f4836 commit 397574b

File tree

3 files changed

+51
-12
lines changed

3 files changed

+51
-12
lines changed

src/demo/css/style.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,12 @@ input:focus:invalid {
207207
margin-top: 8px;
208208
}
209209

210+
.radio-button-group {
211+
display: flex;
212+
align-items: center;
213+
gap: 0.75em;
214+
}
215+
210216
.advanced .color-properties {
211217
grid-template-columns: auto 1fr auto;
212218
}

src/demo/index.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,30 @@ function gtag() {
146146

147147
<details class="advanced">
148148
<summary>⚙ Advanced Options</summary>
149-
<div class="content color-properties parameters">
150-
<label for="theme">Add Property</label>
151-
<select id="properties" name="properties">
152-
<?php foreach ($THEMES["default"] as $option => $color): ?>
153-
<option><?php echo $option; ?></option>
154-
<?php endforeach; ?>
155-
</select>
156-
<button class="plus btn" type="button" onclick="preview.addProperty()">+</button>
149+
<div class="content">
150+
<div class="radio-buttons parameters">
151+
<!-- Radio buttons to choose between solid and gradient background -->
152+
<label for="background-type">Background Type</label>
153+
<div class="radio-button-group">
154+
<div>
155+
<input type="radio" id="background-type-solid" name="background-type" value="solid" checked>
156+
<label for="solid">Solid Color</label>
157+
</div>
158+
<div>
159+
<input type="radio" id="background-type-gradient" name="background-type" value="gradient">
160+
<label for="gradient">Gradient</label>
161+
</div>
162+
</div>
163+
</div>
164+
<div class="color-properties parameters">
165+
<label for="theme">Add Property</label>
166+
<select id="properties" name="properties">
167+
<?php foreach ($THEMES["default"] as $option => $color): ?>
168+
<option><?php echo $option; ?></option>
169+
<?php endforeach; ?>
170+
</select>
171+
<button class="plus btn" type="button" onclick="preview.addProperty()">+</button>
172+
</div>
157173
</div>
158174
<button class="btn" type="button" onclick="preview.exportPhp()">Export to PHP</button>
159175
<button id="clear-button" class="btn" type="button" onclick="preview.removeAllProperties()" disabled>Clear Options</button>

src/demo/js/script.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ const preview = {
8585
};
8686

8787
const parent = document.querySelector(".advanced .color-properties");
88-
if (propertyName === "background") {
88+
if (propertyName === "background" && document.querySelector("#background-type-gradient").checked) {
8989
const valueParts = value.split(",");
9090
let angleValue = "45";
9191
let color1Value = "#EB5454FF";
@@ -335,8 +335,26 @@ window.addEventListener(
335335
[...document.querySelectorAll("select:not(#properties)")].forEach((element) => {
336336
element.addEventListener("change", refresh, false);
337337
});
338+
// when the background-type changes, remove the background and replace it
339+
const toggleBackgroundType = () => {
340+
const value = document.querySelector("input#background, input#background-color1")?.value;
341+
preview.removeProperty("background");
342+
if (value && document.querySelector("#background-type-gradient").checked) {
343+
preview.addProperty("background", `45,${value},${value}`);
344+
} else if (value) {
345+
preview.addProperty("background", value);
346+
}
347+
};
348+
document.querySelector("#background-type-solid").addEventListener("change", toggleBackgroundType, false);
349+
document.querySelector("#background-type-gradient").addEventListener("change", toggleBackgroundType, false);
338350
// set input boxes to match URL parameters
339351
const searchParams = new URLSearchParams(window.___location.search);
352+
const backgroundParams = searchParams.getAll("background");
353+
// set background-type
354+
if (backgroundParams.length > 1) {
355+
document.querySelector("#background-type-gradient").checked = true;
356+
}
357+
// set input field and select values
340358
searchParams.forEach((val, key) => {
341359
const paramInput = document.querySelector(`[name="${key}"]`);
342360
if (paramInput) {
@@ -348,9 +366,8 @@ window.addEventListener(
348366
preview.addProperty(key, searchParams.getAll(key).join(","));
349367
}
350368
});
351-
// set background angle and colors
352-
const backgroundParams = searchParams.getAll("background");
353-
if (backgroundParams.length > 0) {
369+
// set background angle and gradient colors
370+
if (backgroundParams.length > 1) {
354371
document.querySelector("#rotate").value = backgroundParams[0];
355372
document.querySelector("#background-color1").value = backgroundParams[1];
356373
document.querySelector("#background-color2").value = backgroundParams[2];

0 commit comments

Comments
 (0)