|
| 1 | +import { useState } from "react" |
| 2 | +import styles from "./cssQuantityQuery.module.css" |
| 3 | + |
| 4 | +const queryTypes = ["≥", "≤", "Between"] as const |
| 5 | +type QueryType = (typeof queryTypes)[number] |
| 6 | + |
| 7 | +export default function CSSQuantityQuery({ |
| 8 | + hideForm = false, |
| 9 | + showQuery = false, |
| 10 | + initialQueryType = "≥", |
| 11 | + initialAmount = 3, |
| 12 | + initialAmount2 = 5, |
| 13 | + initialBoxCount = 2, |
| 14 | +}: { |
| 15 | + hideForm?: boolean |
| 16 | + showQuery?: boolean |
| 17 | + initialQueryType?: QueryType |
| 18 | + initialAmount?: number |
| 19 | + initialAmount2?: number |
| 20 | + initialBoxCount?: number |
| 21 | +}) { |
| 22 | + const [queryType, setQueryType] = useState<QueryType>(initialQueryType) |
| 23 | + const [amount, setAmount] = useState(initialAmount) |
| 24 | + const [amount2, setAmount2] = useState(initialAmount2) |
| 25 | + const [boxCount, setBoxCount] = useState(initialBoxCount) |
| 26 | + |
| 27 | + return ( |
| 28 | + <div |
| 29 | + style={{ |
| 30 | + margin: "1rem", |
| 31 | + padding: "1rem", |
| 32 | + border: "1px dashed var(--theme-text-lighter)", |
| 33 | + }} |
| 34 | + > |
| 35 | + <div |
| 36 | + style={{ |
| 37 | + display: hideForm ? "none" : "flex", |
| 38 | + gap: "1rem", |
| 39 | + flexWrap: "wrap", |
| 40 | + marginBottom: showQuery ? "0.5rem" : "1.5rem", |
| 41 | + }} |
| 42 | + > |
| 43 | + <FormGroup label="Query Type" htmlFor="queryType"> |
| 44 | + <select |
| 45 | + id="queryType" |
| 46 | + value={queryType} |
| 47 | + onChange={e => setQueryType(e.target.value as QueryType)} |
| 48 | + style={{ width: "100%", fontSize: "1em" }} |
| 49 | + > |
| 50 | + {queryTypes.map(type => ( |
| 51 | + <option key={type}>{type}</option> |
| 52 | + ))} |
| 53 | + </select> |
| 54 | + </FormGroup> |
| 55 | + <FormGroup |
| 56 | + htmlFor="amount" |
| 57 | + label={queryType === "Between" ? "Min" : "Amount"} |
| 58 | + > |
| 59 | + <input |
| 60 | + id="amount" |
| 61 | + type="number" |
| 62 | + min="0" |
| 63 | + step="1" |
| 64 | + value={amount} |
| 65 | + style={{ width: "100%", fontSize: "1em" }} |
| 66 | + onChange={e => setAmount(e.target.valueAsNumber)} |
| 67 | + /> |
| 68 | + </FormGroup> |
| 69 | + {queryType === "Between" && ( |
| 70 | + <FormGroup htmlFor="amount2" label="Max"> |
| 71 | + <input |
| 72 | + id="amount2" |
| 73 | + type="number" |
| 74 | + min="0" |
| 75 | + step="1" |
| 76 | + value={amount2} |
| 77 | + style={{ width: "100%", fontSize: "1em" }} |
| 78 | + onChange={e => setAmount2(e.target.valueAsNumber)} |
| 79 | + /> |
| 80 | + </FormGroup> |
| 81 | + )} |
| 82 | + </div> |
| 83 | + {showQuery && ( |
| 84 | + <pre style={{ marginBottom: "1.5rem" }}> |
| 85 | + <code>{getCode({ amount, amount2, queryType })}</code> |
| 86 | + </pre> |
| 87 | + )} |
| 88 | + <div> |
| 89 | + <div style={{ display: "flex", gap: ".5rem" }}> |
| 90 | + <button |
| 91 | + className={styles.btn} |
| 92 | + onClick={() => setBoxCount(b => b + 1)} |
| 93 | + > |
| 94 | + Add Box |
| 95 | + </button> |
| 96 | + <button |
| 97 | + className={styles.btn} |
| 98 | + onClick={() => setBoxCount(b => b - 1)} |
| 99 | + > |
| 100 | + Remove Box |
| 101 | + </button> |
| 102 | + </div> |
| 103 | + <p |
| 104 | + style={{ |
| 105 | + fontSize: ".8em", |
| 106 | + color: "var(--theme-text-lighter)", |
| 107 | + marginBottom: "1rem", |
| 108 | + }} |
| 109 | + > |
| 110 | + If the quantity query is satisfied the boxes will be orange while if |
| 111 | + the query is not satisfied they will be blue. |
| 112 | + </p> |
| 113 | + <div |
| 114 | + style={{ |
| 115 | + display: "grid", |
| 116 | + gridTemplateColumns: "repeat(auto-fill, minmax(200px, 1fr))", |
| 117 | + gridGap: "1rem", |
| 118 | + gridAutoRows: "100px", |
| 119 | + alignItems: "stretch", |
| 120 | + justifyItems: "stretch", |
| 121 | + }} |
| 122 | + > |
| 123 | + {Array.from({ length: boxCount }, (_, i) => ( |
| 124 | + <div |
| 125 | + key={i} |
| 126 | + style={{ |
| 127 | + backgroundColor: getBoxColor({ |
| 128 | + amount, |
| 129 | + amount2, |
| 130 | + queryType, |
| 131 | + boxCount, |
| 132 | + }), |
| 133 | + }} |
| 134 | + /> |
| 135 | + ))} |
| 136 | + </div> |
| 137 | + </div> |
| 138 | + </div> |
| 139 | + ) |
| 140 | +} |
| 141 | + |
| 142 | +function getCode({ |
| 143 | + queryType, |
| 144 | + amount, |
| 145 | + amount2, |
| 146 | +}: { |
| 147 | + queryType: QueryType |
| 148 | + amount: number |
| 149 | + amount2: number |
| 150 | +}) { |
| 151 | + switch (queryType) { |
| 152 | + case "≥": |
| 153 | + return `/* Elements */ |
| 154 | +ul li:nth-last-child(n + ${amount}), |
| 155 | +ul li:nth-last-child(n + ${amount}) ~ li {} |
| 156 | +
|
| 157 | +/* Container */ |
| 158 | +ul:has(li:nth-last-child(n + ${amount})) {}` |
| 159 | + case "≤": |
| 160 | + return `/* Elements */ |
| 161 | +ul li:nth-last-child(-n + ${amount}):first-child, |
| 162 | +ul li:nth-last-child(-n + ${amount}):first-child ~ li {} |
| 163 | +
|
| 164 | +/* Container */ |
| 165 | +ul:has(li:nth-last-child(-n + ${amount}):first-child) {}` |
| 166 | + case "Between": |
| 167 | + return `/* Elements */ |
| 168 | +ul li:nth-last-child(n + ${amount}):nth-last-child(-n + ${amount2}):first-child, |
| 169 | +ul li:nth-last-child(n + ${amount}):nth-last-child(-n + ${amount2}):first-child ~ li {} |
| 170 | +
|
| 171 | +/* Container */ |
| 172 | +ul:has(li:nth-last-child(n + ${amount}):nth-last-child(-n + ${amount2}):first-child) {}` |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +function FormGroup({ |
| 177 | + label, |
| 178 | + htmlFor, |
| 179 | + children, |
| 180 | +}: { |
| 181 | + label: string |
| 182 | + htmlFor: string |
| 183 | + children: React.ReactNode |
| 184 | +}) { |
| 185 | + return ( |
| 186 | + <div |
| 187 | + style={{ |
| 188 | + display: "flex", |
| 189 | + flexDirection: "column", |
| 190 | + alignItems: "flex-start", |
| 191 | + maxWidth: "100px", |
| 192 | + }} |
| 193 | + > |
| 194 | + <label |
| 195 | + htmlFor={htmlFor} |
| 196 | + style={{ |
| 197 | + fontWeight: "semibold", |
| 198 | + fontSize: ".8em", |
| 199 | + }} |
| 200 | + > |
| 201 | + {label} |
| 202 | + </label> |
| 203 | + {children} |
| 204 | + </div> |
| 205 | + ) |
| 206 | +} |
| 207 | + |
| 208 | +function getBoxColor({ |
| 209 | + amount, |
| 210 | + amount2, |
| 211 | + queryType, |
| 212 | + boxCount, |
| 213 | +}: { |
| 214 | + amount: number |
| 215 | + amount2: number |
| 216 | + queryType: QueryType |
| 217 | + boxCount: number |
| 218 | +}) { |
| 219 | + switch (queryType) { |
| 220 | + case "≥": |
| 221 | + return boxCount >= amount ? "var(--theme-orange)" : "var(--theme-blue)" |
| 222 | + case "≤": |
| 223 | + return boxCount <= amount ? "var(--theme-orange)" : "var(--theme-blue)" |
| 224 | + case "Between": |
| 225 | + return boxCount >= amount && boxCount <= amount2 |
| 226 | + ? "var(--theme-orange)" |
| 227 | + : "var(--theme-blue)" |
| 228 | + } |
| 229 | +} |
0 commit comments