Skip to content

Instantly share code, notes, and snippets.

@crazy4groovy
crazy4groovy / useEventBus.jsx
Last active September 19, 2023 18:06
A simple hook that implements an event bus with useReducer (ReactJS)
import React, {
createContext,
useContext,
useEffect,
useRef,
} from "react"
// Create the event bus context
const EventBusContext = createContext({})
// Custom hook to access the event bus context
@crazy4groovy
crazy4groovy / MyPlotFigureChart.jsx
Last active June 20, 2023 15:44
A simple wrapper for Observable Plot charting lib (ReactJS)
import * as Plot from "@observablehq/plot";
import PlotFigureChart from "./PlotFigureChart.tsx"
export default function MyPlotFigureChart(
{ filteredRequests }: { filteredRequests: Readonly<any[]> }
) {
return (
<PlotFigureChart
options={{
inset: 10,
@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 / 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 / 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 / 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 / lazyimg-IntersectionObserver.js
Last active May 12, 2023 17:37
Intersection Observer examples (JavaScript)
[...document.querySelectorAll('img[data-src]')].forEach((el, i) => {
const observer = new window.IntersectionObserver(function(entries, self) {
const {isIntersecting, target} = entries[0]
if (isIntersecting) {
const img = target
const src = img.getAttribute('data-src')
if (src) img.src = src
self.unobserve(img)
}
}, {})
@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 / 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 / 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;