Skip to content

Instantly share code, notes, and snippets.

View luigircruz's full-sized avatar
🎸
Music and coding

Luigi Cruz luigircruz

🎸
Music and coding
View GitHub Profile
import { useEffect, useState } from 'react';
export default function useDeviceDetect() {
const [isMobile, setMobile] = useState(false);
useEffect(() => {
const userAgent = typeof window.navigator === 'undefined' ? '' : navigator.userAgent;
const mobile = Boolean(userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile|WPDesktop/i));
setMobile(mobile);
}, []);
/**
* Type guard to filter out null-ish values
*
* @category Guards
* @example array.filter(notNullish)
*/
export function notNullish<T>(v: T | null | undefined): v is NonNullable<T> {
return v != null
}
/**
* Replace backslash to slash
*
* @category String
*/
export function slash(str: string) {
return str.replace(/\\/g, '/')
}
/**
export type Merge<T, P> = P & Omit<T, keyof P>
export type UnionStringArray<T extends Readonly<string[]>> = T[number]
export type Omit<T, K> = Pick<T, Exclude<keyof T, K>>
export type LiteralUnion<T extends U, U extends any = string> =
| T
| (U & { _?: never })
@luigircruz
luigircruz / array.ts
Last active August 2, 2024 22:43
Array helpers
export function getFirstItem<T>(array: T[]): T | undefined {
return array != null && array.length ? array[0] : undefined
}
export function getLastItem<T>(array: T[]): T | undefined {
const length = array == null ? 0 : array.length
return length ? array[length - 1] : undefined
}
export function getPrevItem<T>(index: number, array: T[], loop = true): T {
/*
Is a given element currently in the viewport?
This hook tracks that information using
IntersectionObserver.
*/
import { useState, useEffect } from 'react';
function useIsOnscreen(
elementRef,
defaultState = false
import { Tab } from "@headlessui/react";
const feature = {
items: [
{
id: 1,
title: "Title 1",
description: `Feature <strong>Item 1</strong> description.`,
image: {
src: "https://images.unsplash.com/photo-1447078806655-40579c2520d6?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwxfDB8MXxyYW5kb218MHx8fHx8fHx8MTYyNTUyODc0MA&ixlib=rb-1.2.1&q=80&utm_campaign=api-credit&utm_medium=referral&utm_source=unsplash_source&w=1080",
import { useState, useEffect } from "react";
export default function useDebounce(value, delay) {
const [debouncedValue, setDebouncedValue] = useState(value);
useEffect(() => {
const handler = setTimeout(() => {
setDebouncedValue(value);
}, delay);
// const options = {
// root: document.querySelector('.ct-inner-content'),
// rootMargin: '0px',
// threshold: 1.0
// }
const div = document.querySelector("#div_id");
const options = {}
@luigircruz
luigircruz / generate-rss.mjs
Last active June 16, 2024 22:36
Generate rss for markdown blog posts
// https://luigicruz.dev/feed.xml
import { readFileSync, readdirSync, writeFileSync } from 'fs'
import { join } from 'path'
import RSS from 'rss'
import matter from 'gray-matter'
async function generate() {
const feed = new RSS({
title: 'Luigi Cruz',