Skip to content

Instantly share code, notes, and snippets.

View azaek's full-sized avatar
💭
I may be slow to respond.

AzaeK azaek

💭
I may be slow to respond.
View GitHub Profile
@azaek
azaek / SafariFocusHelper.tsx
Last active March 29, 2024 12:27
Trigger focus with soft keyboard dynamically on safari(ios)
import { RefObject } from "react";
/**
* This function helps to focus on an input element with soft keyboard on safari(ios).
* @notice This is a workaround for the issue where the soft keyboard does not show up on safari(ios) when focusing on an input element. Safari(ios) allows keyboard for subsequent focus on an input element if the focus is triggered by a user event like click or touchstart.
* @param target - RefObject of the input element to focus
* @param delay (optional) - delay in milliseconds before focusing on the input element can be 0 for instant focus
*/
export const focusInput = (target: RefObject<HTMLInputElement>, delay: number = 500) => {
// create invisible dummy input to receive the focus first
const fakeInput = document.createElement('input')
@azaek
azaek / useMutationObserver.tsx
Last active November 5, 2023 09:50
Mutation Observer to get specific element addition or removal callback on dom manipulation
import { useEffect, useRef } from "react";
interface Callbacks {
onVisible?: () => void;
onHidden?: () => void;
}
const useMutationObserver = (
elementId: string,
callbacks: Callbacks = {
@azaek
azaek / Dropdown.tsx
Created October 13, 2023 08:37
Radix Dropdown Menu issues with touch and pointer events f.e. scrolling in phone causes it to trigger open
<DropdownMenu.Trigger asChild
onPointerUp={(event) => {
if (event.pointerType === "touch" || event.pointerType === "pen") {
setOpenMenu(true);
}
}}
onPointerDown={(event) => {
if (event.pointerType === "touch" || event.pointerType === "pen") {
event.preventDefault();
}
@azaek
azaek / typescriptreact.json
Created September 4, 2023 18:28
VS Snippets
{
"Create a functional component tsx": {
"prefix": "fnc",
"body": [
"import { FC } from 'react';",
"",
"",
"interface ${1:${TM_FILENAME_BASE}}Props {",
"$2",
"}",
@azaek
azaek / Calendar.tsx
Created December 23, 2022 17:13
Calendar Component using date-fns, tailwindcss and framer-motion
import clsx from "clsx";
import {
add,
eachDayOfInterval,
endOfMonth,
format,
getDay,
isEqual,
isSameMonth,
isToday,
@azaek
azaek / Noise.tsx
Created November 22, 2022 16:40
Noise svg
<svg className='w-full h-full' viewBox="0 0 100% 100%" xmlns='http://www.w3.org/2000/svg'>
<filter id='noiseFilter'>
<feTurbulence
baseFrequency='0.65 0.4'
numOctaves='3'
result='NOISE' />
</filter>
<rect width='100%' height='100%' filter='url(#noiseFilter)' />
</svg>
@azaek
azaek / CustomTabWithIndicatorExample.tsx
Last active September 25, 2022 09:23
This is a simple hook to move indicator element related to any tab<HTMLButtonElement> it will give current selected tab position, width and height then you can apply that to animate any HTMLDivElement accordingly
const [selected, setSelected] = useState(0);
const { w, h, indicatorRef, refs } = useTabIndicator({ selected, center: false });
return (
<div className="flex items-center gap-8 absolute inset-0 m-auto w-max t-all">
<div className="relative flex items-center gap-4">
{
["Home", "Explore", "Profile"].map((item, i) => (
<>
@azaek
azaek / useImageColor.tsx
Created September 8, 2022 15:57
React Hook get image colors
import { useState, useEffect } from "react";
const useImageColor = (img: HTMLImageElement) => {
const [color, setColor] = useState<any>(null);
useEffect(() => {
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
function rgbToHex(r: any, g: any, b: any) {
r = r.toString(16);
@azaek
azaek / useOutsideClick.tsx
Created August 19, 2022 01:49
useOutsideClick
import { Ref, RefObject, useEffect, useRef } from "react";
const useOutsideClick = (ref: RefObject<HTMLDivElement> , handler: (event : any | null) => void) => {
useEffect(
() => {
const listener = (event: any) => {
// Do nothing if clicking ref's element or descendent elements
if (!ref || ref.current?.contains(event.target)) {
return;
}
@azaek
azaek / useScroll.tsx
Created August 19, 2022 01:48
useScroll
import { useEffect, useState } from "react";
const useScroll = () => {
const [scrollPosition, setScrollPosition] = useState(0);
useEffect(() => {
const updatePosition = () => {
setScrollPosition(window.pageYOffset);
}
window.addEventListener("scroll", updatePosition);
updatePosition();