Skip to content

Instantly share code, notes, and snippets.

View 4-x's full-sized avatar
🦥

Alexander Shoup 4-x

🦥
View GitHub Profile
@4-x
4-x / git-commit-meta.js
Created September 13, 2023 13:09
Get last git commit meta data in node.js
const { exec } = require('child_process')
function runGitCommand(command, callback) {
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Error executing Git command: ${error}`)
return
}
callback(stdout.trim())
})
@4-x
4-x / no-kids.css
Created December 13, 2022 19:45
CSS selector for all elements at the bottom of the DOM tree (elements without children)
*:not(:has(> *)) {
}
@4-x
4-x / noise.svg
Created December 9, 2022 00:37
Noise SVG
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@4-x
4-x / doc-height.js
Created December 6, 2022 15:50
Definitive Document Height
let height = Math.max(
document.body.scrollHeight,
document.body.offsetHeight,
document.documentElement.clientHeight,
document.documentElement.scrollHeight,
document.documentElement.offsetHeight
);

Exposing the nth-variable for use in CSS

With the advent of CSS variables (custom properties) and calc functions, it would be useful to be able to access the iterator directly for computation and design.

A pure CSS approach may look something like this:

:nth-child(1) { --n: 1 }
:nth-child(2) { --n: 2 }
:nth-child(3) { --n: 3 }
@4-x
4-x / web-color.js
Last active December 8, 2022 18:42
Named Web Colors
const colors = [
{'name':'AliceBlue','hex':'#F0F8FF'},
{'name':'AntiqueWhite','hex':'#FAEBD7'},
{'name':'Aqua','hex':'#00FFFF'},
{'name':'Aquamarine','hex':'#7FFFD4'},
{'name':'Azure','hex':'#F0FFFF'},
{'name':'Beige','hex':'#F5F5DC'},
{'name':'Bisque','hex':'#FFE4C4'},
{'name':'Black','hex':'#000000'},
{'name':'BlanchedAlmond','hex':'#FFEBCD'},
@4-x
4-x / doc-depth-var.js
Last active November 17, 2022 18:33
Add document depth CSS variable to all elements on page
function findKids(_el, _level) {
let level = _level + 1;
console.log(_el.children.length);
if (_el.children.length) {
for (const child of _el.children) {
child.style.setProperty('--depth', level);
findKids(child, level);
}
}
}
@4-x
4-x / live-css-context.js
Created November 10, 2022 21:31
Exposes a set of custom CSS properties allowing style changes based on page context and passive user interaction
function init() {
const rootEl = document.documentElement;
setWindowProps();
window.addEventListener('mousemove', evt=>{
rootEl.style.setProperty('--mouseX', evt.clientX);
rootEl.style.setProperty('--mouseY', evt.clientY);
rootEl.style.setProperty('--mouseXRel', Math.round(evt.clientX * 100 / window.innerWidth));
rootEl.style.setProperty('--mouseYRel', Math.round(evt.clientY * 100 / window.innerHeight));
@4-x
4-x / counter-var.css
Last active December 9, 2022 17:06
Exposing CSS integer variables in HTML with counter property hack
/* must be of type <integer> */
div::after {
--variable: 0;
counter-reset: counterName var(--variable);
content: counter(counterName);
}
@4-x
4-x / nth-variable.css
Last active November 10, 2022 03:16
Nth Variable Css
:nth-child(1){--n:1}:nth-child(2){--n:2}:nth-child(3){--n:3}:nth-child(4){--n:4}:nth-child(5){--n:5}:nth-child(6){--n:6}:nth-child(7){--n:7}:nth-child(8){--n:8}:nth-child(9){--n:9}:nth-child(10){--n:10}:nth-child(11){--n:11}:nth-child(12){--n:12}:nth-child(13){--n:13}:nth-child(14){--n:14}:nth-child(15){--n:15}:nth-child(16){--n:16}:nth-child(17){--n:17}:nth-child(18){--n:18}:nth-child(19){--n:19}:nth-child(20){--n:20}:nth-child(21){--n:21}:nth-child(22){--n:22}:nth-child(23){--n:23}:nth-child(24){--n:24}:nth-child(25){--n:25}:nth-child(26){--n:26}:nth-child(27){--n:27}:nth-child(28){--n:28}:nth-child(29){--n:29}:nth-child(30){--n:30}:nth-child(31){--n:31}:nth-child(32){--n:32}:nth-child(33){--n:33}:nth-child(34){--n:34}:nth-child(35){--n:35}:nth-child(36){--n:36}:nth-child(37){--n:37}:nth-child(38){--n:38}:nth-child(39){--n:39}:nth-child(40){--n:40}:nth-child(41){--n:41}:nth-child(42){--n:42}:nth-child(43){--n:43}:nth-child(44){--n:44}:nth-child(45){--n:45}:nth-child(46){--n:46}:nth-child(47){--n:47}:nth-chi