/
Calculator.svelte
287 lines (267 loc) · 8.36 KB
/
Calculator.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<script>
import { fly } from "svelte/transition"
import { cubicInOut } from "svelte/easing"
import CheckIcon from "../Icons/Check.svelte"
import styles from "./Calculator.module.css"
let unit = "px" // or "rem"
$: isRem = unit === "rem"
const toRem = (value) => +(isRem ? value : value / 16)?.toFixed(3)
const toPx = (value) => +(isRem ? value * 16 : value)?.toFixed(3)
const switchToCurrentValue = (value) => (isRem ? toRem(value) : toPx(value))
let minValue = 16
let maxValue = 24
let minViewportPx = 320
let maxViewportPx = 1200
$: minViewport = switchToCurrentValue(minViewportPx)
$: maxViewport = switchToCurrentValue(maxViewportPx)
// Errors
let hasError = false
let hasNegative = false
let isMinValueGreaterThanMaxValue = false
let isMinViewPortGreaterThanMaxViewPort = false
let result
let isCopied = false
// Functions
const switchUnit = () => {
const switchValue = (value) => (isRem ? toPx(value) : toRem(value))
unit = unit === "px" ? "rem" : "px"
minValue = switchValue(minValue)
maxValue = switchValue(maxValue)
}
const copyToClipboard = () => {
navigator.clipboard.writeText(result).then(() => {
isCopied = true
setTimeout(() => {
isCopied = false
}, 2000)
})
}
const validate = () => {
hasNegative = minViewportPx < 0 || maxViewportPx < 1
isMinValueGreaterThanMaxValue = minValue >= maxValue
isMinViewPortGreaterThanMaxViewPort = minViewportPx >= maxViewportPx
hasError =
hasNegative ||
isMinValueGreaterThanMaxValue ||
isMinViewPortGreaterThanMaxViewPort
}
$: {
// Write Result
const minValuePx = isRem ? toPx(minValue) : minValue
const maxValuePx = isRem ? toPx(maxValue) : maxValue
const variablePart = (maxValuePx - minValuePx) / (maxViewport - minViewport)
const constant = parseFloat(
((maxValuePx - maxViewport * variablePart) / 16).toFixed(3)
)
// prettier-ignore
result = `clamp(${toRem(minValue)}rem,${constant ? ` ${constant}rem +` : ""} ${parseFloat((100 * variablePart).toFixed(2))}vw, ${toRem(maxValue)}rem)`
}
</script>
<section class={styles.wrapper}>
<form on:submit|preventDefault class={styles.form}>
<fieldset class={styles.fieldset} aria-label="Minimum and maximum Values">
<div class={styles.fieldsetHeader}>
<span class={styles.legend} aria-hidden="true">Values</span>
<fieldset class={styles.unitToggle} aria-label="Switch unit">
<label for="unit-px" class={styles.unitLabel} data-active={!isRem}>
<span class="sr-only">Pixel</span>
<span aria-hidden="true">px</span>
</label>
<input
type="radio"
id="unit-px"
name="unit"
on:change={switchUnit}
checked
class={styles.unitRadio}
/>
<label for="unit-rem" class={styles.unitLabel} data-active={isRem}>
rem
</label>
<input
type="radio"
id="unit-rem"
name="unit"
on:change={switchUnit}
class={styles.unitRadio}
/>
<span
class={styles.unitToggleIndicator}
data-unit={unit}
aria-hidden="true"
/>
</fieldset>
</div>
<div class={styles.inputRow}>
<div class={styles.inputWrapper}>
<div class={styles.inputInner}>
<label
class={styles.inputLabel}
for="min-value"
aria-label="Minimum Value">Min</label
>
<input
class={styles.input}
type="number"
step="any"
id="min-value"
bind:value={minValue}
aria-describedby="min-value-description"
on:blur={validate}
/>
<span class={styles.inputUnit}>
{unit}
</span>
</div>
<small class={styles.inputDescription} id="min-value-description">
can be negative, e.g. for margins or absolute positioning
</small>
</div>
<div class={styles.inputWrapper}>
<div class={styles.inputInner}>
<label
class={styles.inputLabel}
for="max-value"
aria-label="Maximum Value">Max</label
>
<input
class={styles.input}
type="number"
step="any"
min={0}
id="max-value"
bind:value={maxValue}
on:blur={validate}
/>
<span class={styles.inputUnit}>
{unit}
</span>
</div>
</div>
</div>
</fieldset>
<fieldset class={styles.fieldset} aria-label="Viewport range">
<span aria-hidden="true" class={styles.legend}>Viewport</span>
<div class={styles.inputRow}>
<div class={styles.inputWrapper}>
<div class={styles.inputInner}>
<label
class={styles.inputLabel}
for="min-viewport"
aria-label="Minimum viewport">Min</label
>
<input
class={styles.input}
type="number"
step="any"
id="min-viewport"
min={0}
bind:value={minViewportPx}
on:blur={validate}
/>
<span class={styles.inputUnit}>px</span>
</div>
</div>
<div class={styles.inputWrapper}>
<div class={styles.inputInner}>
<label
class={styles.inputLabel}
for="max-viewport"
aria-label="Maximum Viewport">Max</label
>
<input
class={styles.input}
type="number"
step="any"
id="max-viewport"
min={0}
bind:value={maxViewportPx}
on:blur={validate}
/>
<span class={styles.inputUnit}>px</span>
</div>
</div>
</div>
</fieldset>
</form>
{#if hasError}
<div class={styles.errors}>
<strong>Oh no, there are errors: </strong>
<ul>
{#if isMinValueGreaterThanMaxValue}
<li>Min value must be less than max value</li>
{/if}
{#if hasNegative}
<li>
Please make sure all the viewport values are positive numbers and
the max viewport is greater than 0
</li>
{/if}
{#if isMinViewPortGreaterThanMaxViewPort}
<li>Min viewport must be less than max viewport</li>
{/if}
</ul>
</div>
{/if}
<div class={styles.output}>
<h2 class="sr-only">Calculated Result:</h2>
<code class={styles.outputCSS}>
{result}
</code>
<div class={styles.copyButtonWrapper}>
<button
class={styles.copyButton}
data-copied={isCopied}
on:click={copyToClipboard}
>
<span class={styles.copyButtonTextPlaceholder} aria-hidden="true">
copy
</span>
{#if !isCopied}
<span
in:fly={{
x: -10,
opacity: 0,
duration: 600,
delay: 50,
easing: cubicInOut,
}}
out:fly={{ x: 10, opacity: 0, duration: 600, easing: cubicInOut }}
class={styles.copyButtonText}
>
copy
</span>
{/if}
<span class="sr-only">snippet to clipboard</span>
</button>
{#if isCopied}
<p
in:fly={{
x: -20,
opacity: 0,
duration: 600,
easing: cubicInOut,
delay: 50,
}}
out:fly={{ x: 20, opacity: 0, duration: 600, easing: cubicInOut }}
role="status"
class={styles.copySuccess}
>
<CheckIcon />
<span class="sr-only"> Copied to clipboard </span>
</p>
{/if}
</div>
</div>
</section>
<details-utils animate>
<details class={styles.liveExample} style="--variable-value: {result}">
<summary>Live font-size example</summary>
<div>
<p style="font-size:var(--variable-value)" contenteditable="true">
This text uses the calculated clamp formula. It gets invisible when you
use 0 or negative numbers as a min-value. 🥲 Try editing it, though...
</p>
</div>
</details>
</details-utils>