-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtool-86-Sass-SCSS-to-CSS-Playground.html
More file actions
506 lines (431 loc) · 17.5 KB
/
tool-86-Sass-SCSS-to-CSS-Playground.html
File metadata and controls
506 lines (431 loc) · 17.5 KB
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tool 86 - Sass SCSS to CSS Playground - Developer Toolbox</title>
<link rel="stylesheet" href="assets/core.css">
<style>
.split-view {
display: grid;
grid-template-columns: 1fr;
gap: 1.5rem;
margin-top: 1rem;
margin-bottom: 2rem;
}
@media (min-width: 900px) {
.split-view { grid-template-columns: 1fr 1fr; }
}
@media (min-width: 1200px) {
.split-view { grid-template-columns: 1fr 1fr 300px; }
}
.panel {
background: rgba(0,0,0,0.6);
border: 1px solid var(--border-color);
border-radius: var(--radius-md);
padding: 1.5rem;
display: flex;
flex-direction: column;
gap: 1rem;
}
.code-input {
width: 100%;
flex: 1;
background: rgba(0,0,0,0.5);
border: 1px solid var(--border-color);
border-radius: var(--radius-sm);
padding: 1rem;
font-family: var(--font-mono);
font-size: 0.85rem;
color: #ccc;
resize: none;
outline: none;
min-height: 400px;
}
.code-input.scss { color: #ffb142; }
.code-input.css { color: #7ee787; }
.code-input:focus { border-color: #56b6c2; }
.preview-box {
background: #fff;
color: #000;
border-radius: var(--radius-sm);
padding: 1.5rem;
flex: 1;
border: 1px solid #aaa;
overflow: hidden;
position: relative;
}
.var-list {
list-style: none;
padding: 0;
margin: 0;
display: flex;
flex-direction: column;
gap: 0.5rem;
}
.var-item {
display: flex;
justify-content: space-between;
background: rgba(255,255,255,0.05);
padding: 0.5rem 0.8rem;
border-radius: 3px;
font-family: var(--font-mono);
font-size: 0.8rem;
}
.var-name { color: #56b6c2; font-weight: bold; }
.var-val { color: #aaa; }
/* 3D Canvas wrapper */
#canvas-container {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -1;
opacity: 0.25;
pointer-events: none;
}
/* Demo HTML injected into preview */
#demo-content {
border: none;
width: 100%;
height: 100%;
}
</style>
</head>
<body class="theme-dark">
<div id="canvas-container"></div>
<main class="main-content" style="padding-top:2rem;">
<div class="container" style="max-width: 1500px;">
<div class="tool-header">
<div>
<a href="index.html" class="back-link">← Back to Toolbox</a>
<h1>86. Sass SCSS to CSS Playground</h1>
</div>
<div>
<select id="theme-select" aria-label="Select Theme">
<option value="dark">Dark Theme</option>
<option value="light">Light Theme</option>
<option value="solarized">Solarized</option>
<option value="neon">Neon Glow</option>
<option value="highcontrast">High Contrast</option>
</select>
</div>
</div>
<div class="split-view">
<!-- SCSS Input -->
<div class="panel">
<h3>SCSS Editor</h3>
<p style="font-size:0.8rem; color:#aaa; margin-top:-0.5rem;">Variables ($var: val) and simple nesting are supported in this demo parser.</p>
<textarea id="scss-input" class="code-input scss" spellcheck="false">$primary-color: #56b6c2;
$bg-color: #1e1e1e;
$padding: 20px;
.card {
background-color: $bg-color;
padding: $padding;
border-radius: 8px;
color: white;
box-shadow: 0 4px 6px rgba(0,0,0,0.3);
h2 {
color: $primary-color;
margin-top: 0;
}
p {
line-height: 1.5;
color: #ccc;
}
.btn {
display: inline-block;
padding: 10px 15px;
background: $primary-color;
color: #000;
text-decoration: none;
border-radius: 4px;
font-weight: bold;
&:hover {
background: #ffb142;
}
}
}</textarea>
</div>
<!-- CSS Output -->
<div class="panel">
<h3>Compiled CSS</h3>
<p style="font-size:0.8rem; color:#aaa; margin-top:-0.5rem;">Vanilla CSS output.</p>
<textarea id="css-output" class="code-input css" readonly></textarea>
</div>
<!-- Preview & Variables -->
<div style="display:flex; flex-direction:column; gap:1.5rem;">
<div class="panel" style="flex:1;">
<h3>Visual Preview</h3>
<div class="preview-box">
<iframe id="demo-frame" style="width:100%; height:100%; border:none;"></iframe>
</div>
</div>
<div class="panel" style="max-height: 250px;">
<h3>Detected Variables</h3>
<div style="overflow-y:auto;">
<ul class="var-list" id="var-list">
<!-- Populated by JS -->
</ul>
</div>
</div>
</div>
</div>
</div>
</main>
<footer class="global-footer">
<div class="container">
Made with ❤️ by <a href="https://github.com/Aliriyaj007" target="_blank">Aliriyaj007</a>
</div>
</footer>
<script src="assets/core.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r134/three.min.js"></script>
<script>
const scssInput = document.getElementById('scss-input');
const cssOutput = document.getElementById('css-output');
const varList = document.getElementById('var-list');
const demoFrame = document.getElementById('demo-frame');
const demoHTML = `
<div class="card">
<h2>Hello SCSS</h2>
<p>This is a live preview of your compiled SCSS. Update the editor and see changes instantly.</p>
<a href="#" class="btn">Click Me</a>
</div>
`;
// Very basic client-side SCSS to CSS simulated parser.
// Handles:
// 1. $var replacement
// 2. 1-level basic nesting (parent { child {} })
// 3. &:hover pseudo selectors
function compileSCSS(scssStr) {
let css = '';
let vars = {};
// 1. Extract variables
const varRegex = /\$([\w-]+)\s*:\s*([^;]+);/g;
let match;
while ((match = varRegex.exec(scssStr)) !== null) {
vars['$' + match[1]] = match[2].trim();
}
// Clean string of variable declarations
scssStr = scssStr.replace(varRegex, '').trim();
// 2. Replace variables in usages
for (let v in vars) {
// escape $ for regex
const safeV = v.replace('$', '\\$');
const replaceRegex = new RegExp(safeV + '\\b', 'g');
scssStr = scssStr.replace(replaceRegex, vars[v]);
}
// 3. Parse Nesting (Naive Block Parsing)
let blocks = [];
let currentBlock = '';
let depth = 0;
// Clean up comments and standardise braces
scssStr = scssStr.replace(/\/\*[\s\S]*?\*\//g, '').replace(/\/\/.*$/gm, '');
// We will do a regex approach for parent { props; child { props; } }
// This is super naive and limited to 1 level for demo.
let finalCssRules = [];
// Match top-level blocks: .selector { content }
let topRegex = /([^{]+)\{([^}]*(?:\{[^}]*\}[^}]*)*)\}/g;
let topMatch;
while ((topMatch = topRegex.exec(scssStr)) !== null) {
let parentSelector = topMatch[1].trim();
let content = topMatch[2].trim();
// separate direct props and nested blocks inside content
let nestedRegex = /([^{]+)\{([^}]*)\}/g;
let nestedMatch;
let directProps = content;
let nestedRules = [];
while ((nestedMatch = nestedRegex.exec(content)) !== null) {
let childSelector = nestedMatch[1].trim();
let childContent = nestedMatch[2].trim();
// Remove nested block from directProps
directProps = directProps.replace(nestedMatch[0], '');
// Handle & parent reference
let fullSelector = '';
if (childSelector.includes('&')) {
fullSelector = childSelector.replace(/&/g, parentSelector);
} else {
// Cartesian product of selectors (e.g., .a, .b { .c, .d {} })
let parents = parentSelector.split(',').map(s=>s.trim());
let children = childSelector.split(',').map(s=>s.trim());
let combos = [];
parents.forEach(p => {
children.forEach(c => combos.push(`${p} ${c}`));
});
fullSelector = combos.join(', ');
}
nestedRules.push(`${fullSelector} {\n ${childContent.trim().replace(/;/g, ';\n ')}\n}`);
}
// Add parent rule if it has direct props
if (directProps.trim()) {
finalCssRules.push(`${parentSelector} {\n ${directProps.trim().replace(/;/g, ';\n ')}\n}`);
}
// Add nested rules
finalCssRules.push(...nestedRules);
}
// Clean up any weird double spaces or empty lines
css = finalCssRules.join('\n\n').replace(/\n\s*\n/g, '\n');
// Render Variables
renderVars(vars);
return css;
}
function renderVars(vars) {
if(Object.keys(vars).length === 0) {
varList.innerHTML = `<li style="color:#666; text-align:center;">No variables found</li>`;
return;
}
varList.innerHTML = Object.entries(vars).map(([k,v]) => `
<li class="var-item">
<span class="var-name">${k}</span>
<span class="var-val">${v}</span>
</li>
`).join('');
}
function updatePreview(cssCode) {
const doc = demoFrame.contentDocument || demoFrame.contentWindow.document;
doc.open();
doc.write(`
<html>
<head>
<style>
body { font-family: sans-serif; background: #e0e0e0; display:flex; justify-content:center; align-items:center; height:100vh; margin:0; }
${cssCode}
</style>
</head>
<body>
${demoHTML}
</body>
</html>
`);
doc.close();
}
function runCompiler() {
try {
const scssCode = scssInput.value;
const cssCode = compileSCSS(scssCode);
cssOutput.value = cssCode;
updatePreview(cssCode);
// Trigger 3D factory animation
triggerFactoryFlow();
} catch(e) {
cssOutput.value = "/* Parser Error */\n" + e.message;
}
}
scssInput.addEventListener('input', runCompiler);
// 3D Scene setup: "A factory where Sass goes in and CSS comes out."
let scene, camera, renderer, factoryGrp;
let particles = [];
function init3D() {
const container = document.getElementById('canvas-container');
if(!container) return;
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(50, window.innerWidth/window.innerHeight, 0.1, 1000);
camera.position.z = 25;
camera.position.y = 10;
camera.position.x = 0;
camera.lookAt(0, 0, 0);
renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
container.appendChild(renderer.domElement);
factoryGrp = new THREE.Group();
scene.add(factoryGrp);
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
scene.add(ambientLight);
const dirLight = new THREE.DirectionalLight(0xffffff, 0.8);
dirLight.position.set(5, 10, 5);
scene.add(dirLight);
// Factory Machine
const boxMat = new THREE.MeshPhongMaterial({ color: 0x333333, flatShading: true });
const p1 = new THREE.Mesh(new THREE.BoxGeometry(6, 4, 6), boxMat);
const p2 = new THREE.Mesh(new THREE.CylinderGeometry(1, 2, 4, 8), boxMat);
p2.position.y = 4;
factoryGrp.add(p1);
factoryGrp.add(p2);
// Input funnel (left)
const funnel1Geo = new THREE.CylinderGeometry(2, 0.5, 3, 8);
funnel1Geo.rotateZ(-Math.PI/2);
const funnelIn = new THREE.Mesh(funnel1Geo, new THREE.MeshPhongMaterial({color: 0xffb142}));
funnelIn.position.set(-4, 0, 0);
factoryGrp.add(funnelIn);
// Output pipe (right)
const pipeGeo = new THREE.CylinderGeometry(0.5, 0.5, 4, 8);
pipeGeo.rotateZ(Math.PI/2);
const pipeOut = new THREE.Mesh(pipeGeo, new THREE.MeshPhongMaterial({color: 0x7ee787}));
pipeOut.position.set(4, -1, 0);
factoryGrp.add(pipeOut);
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
animate3D();
// initial run
runCompiler();
}
function triggerFactoryFlow() {
// Spawn 3 SCSS particles (orange sphere) entering left
for(let i=0; i<3; i++) {
setTimeout(() => spawnParticle(true), i * 300);
}
// Delay, then spawn CSS blocks (green cubes) exiting right
setTimeout(() => {
for(let i=0; i<3; i++) {
setTimeout(() => spawnParticle(false), i * 300);
}
}, 800);
}
function spawnParticle(isInput) {
let mesh;
if (isInput) {
mesh = new THREE.Mesh(
new THREE.SphereGeometry(0.4, 8, 8),
new THREE.MeshPhongMaterial({ color: 0xffb142, emissive: 0x552200 })
);
mesh.position.set(-10, 0, 0);
mesh.userData = { targetX: -2, speed: 0.1, type: 'in' };
} else {
mesh = new THREE.Mesh(
new THREE.BoxGeometry(0.8, 0.8, 0.8),
new THREE.MeshPhongMaterial({ color: 0x7ee787, emissive: 0x003300 })
);
mesh.position.set(2, -1, 0);
mesh.userData = { targetX: 10, speed: 0.1, type: 'out' };
}
scene.add(mesh);
particles.push(mesh);
}
function animate3D() {
requestAnimationFrame(animate3D);
factoryGrp.rotation.y = Math.sin(Date.now() * 0.0005) * 0.2;
// Animate particles
for(let i = particles.length - 1; i >= 0; i--) {
const p = particles[i];
p.position.x += p.userData.speed * (p.userData.type === 'in' ? 1 : 1);
if (p.userData.type === 'in') {
p.rotation.z -= 0.1;
if (p.position.x > p.userData.targetX) {
scene.remove(p);
particles.splice(i, 1);
// factory hop
factoryGrp.scale.set(1.1, 0.9, 1.1);
setTimeout(() => factoryGrp.scale.set(1,1,1), 100);
}
} else {
p.rotation.x += 0.05;
p.rotation.y += 0.05;
p.position.y += Math.sin(p.position.x*2) * 0.05; // bouncy exit
if (p.position.x > p.userData.targetX) {
scene.remove(p);
particles.splice(i, 1);
}
}
}
renderer.render(scene, camera);
}
document.addEventListener('DOMContentLoaded', () => {
init3D();
});
</script>
</body>
</html>