Skip to content

Instantly share code, notes, and snippets.

@crazy4groovy
crazy4groovy / useCustomPollingExample.js
Last active June 12, 2023 18:45
A simple hook to implement interval polling of any thunk (ReactJS)
import React, { useState } from 'react';
import usePolling from './usePolling';
import ...
const useCustomPolling = () => {
const [state, setState] = useState()
const cb = async (cancelPolling) => {
// do some logics here... whatever...
@crazy4groovy
crazy4groovy / useFetch.js
Last active June 12, 2023 18:46
A simple hook to implement state and data handling of a typical fetch HTTP request (ReactJS)
import { useReducer, useRef } from "react";
const initialState = {
isLoading: false,
prevData: undefined,
data: undefined,
error: undefined,
};
const reducer = (state, action) => {
@crazy4groovy
crazy4groovy / ReactForm.jsx
Last active June 12, 2023 18:45
A simple hook to implement form validation (ReactJS)
import React from 'react';
import useFormValidation from './useFormValidation';
const initialValues = {
name: '',
email: '',
password: '',
};
// You should put this config in a separate file!
@crazy4groovy
crazy4groovy / ComponentReader.svelte
Last active June 15, 2023 22:39
debounced Svelte store (JavaScript)
<script lang="ts">
/// ...
import { customStoreDebouncer /*, storeDebouncer */ } from "./stores/stringStore";
const stringStoreDebounced = customStoreDebouncer(400); // debounced store read
$: {
const text = $stringStoreDebounced; // resolved value, after debounce timeout
/// ...
}
</script>
@crazy4groovy
crazy4groovy / dl.deno.ts
Last active November 14, 2023 22:56
scrape midjourney "recent showcase" images (into folder, per hour) (JavaScript, Deno)
import { Timeout, TimeoutError } from "https://deno.land/x/timeout/mod.ts"
const delay = (ms) => new Promise((res) => setTimeout(res, ms));
function newThrottler({ isBusy, lock, unlock, waitMs, size }) {
async function throttler(cb, ...args) {
size(1);
await Promise.resolve();
while (!!isBusy()) {
await delay(waitMs()); // waits in event loop queue, until it interrupts for another attempt!
@crazy4groovy
crazy4groovy / skewY.css
Created April 15, 2023 16:39
Make angled list items/boxes/cards with skewY transform (CSS)
.item-card {
border: 1px solid #464646;
transform: skewY(-4deg);
}
.item-card > * {
transform: skewY(4deg); /* reverses the skew effect for children */
}
@crazy4groovy
crazy4groovy / ai-text-to-mp3.deno.js
Last active April 15, 2023 19:06
Text-to-audio MP3 AI API call via Eleven Labs, IP throttled (PowerShell)
import { parse } from 'https://deno.land/std/flags/mod.ts';
console.log('Example: deno run --allow-all .\ai-text-to-speech.deno.js --outFile="sample.mp3" --text="This is a sample audio body." --voice="Josh"')
const { outFile, text, voice = 'Adam' } = parse(Deno.args)
if (!outFile || !text) {
console.error("Missing required arg: outFile and/or text")
}
@crazy4groovy
crazy4groovy / percentage-titlebar.js
Created April 1, 2023 07:27
Put the Scroll Percentage in the Browser Title Bar Dynamically (JavaScript)
const percentLabel = document.querySelector("#percent");
const originalTitle = document.title;
window.addEventListener("scroll", () => {
let scrollTop = window.scrollY;
let docHeight = document.body.offsetHeight;
let winHeight = window.innerHeight;
let scrollPercent = scrollTop / (docHeight - winHeight);
let scrollPercentRounded = Math.round(scrollPercent * 100);
percentLabel.innerHTML = scrollPercentRounded;
@crazy4groovy
crazy4groovy / fit-text.js
Last active March 28, 2023 22:40
logic to compress / expand a text element to fit on one line (JavaScript)
function fitText(el, { maxFontSizePx, minFontSizePx, compressor = 1 } = {}) {
const s1 = el.clientWidth / (compressor * 10);
const s2 = parseFloat(maxFontSizePx);
const s3 = parseFloat(minFontSizePx);
el.style.fontSize = Math.max(Math.min(s1, s2), s3) + 'px';
};
@crazy4groovy
crazy4groovy / diagonal-border.css
Last active March 28, 2023 17:30
How to make and diagonal angle section of a website landing page with clip-path (CSS)
/* from: https://backstage.io/ */
.bannerSection:is(.diagonalBorder, .diagonalBottomBorder) + section {
--diagonalBorderHeight: 46px;
margin-top: calc(var(--diagonalBorderHeight)*-1);
padding-top: var(--diagonalBorderHeight);
}
.bannerSection.greenGradientBackground {
background: linear-gradient(88deg, #9bf0e1 15%, #36baa2 85%);
color: var(--color-gray-900);