Skip to content

Instantly share code, notes, and snippets.

View thepuskar's full-sized avatar
👨‍💻
Coding...

Puskar thepuskar

👨‍💻
Coding...
View GitHub Profile
@thepuskar
thepuskar / text-area-button-interaction.tsx
Created December 11, 2024 16:56
The `TextAreaButtonInteraction` is a sophisticated React TypeScript component designed to provide an enhanced text editing experience with intelligent button-based text insertion. It allows users to seamlessly add predefined text to a textarea while maintaining precise cursor positioning and automatic spacing.
import React, { useState, useRef, ChangeEvent, MouseEvent } from 'react';
/**
* Configuration for buttons to be displayed
* @interface ButtonConfig
*/
interface ButtonConfig {
/** Text to be displayed and inserted */
text: string;
/** Tailwind CSS background color class */
@thepuskar
thepuskar / git.diff
Created October 6, 2024 05:40 — forked from samselikoff/git.diff
Diff from "Optimistic UI in Remix": https://www.youtube.com/watch?v=d0p95C3Kcsg
diff --git a/app/components/entry-form.tsx b/app/components/entry-form.tsx
index 50e5aeb..84c64fc 100644
--- a/app/components/entry-form.tsx
+++ b/app/components/entry-form.tsx
@@ -1,6 +1,6 @@
-import { useFetcher } from "@remix-run/react";
+import { Form, useSubmit } from "@remix-run/react";
import { format } from "date-fns";
-import { useEffect, useRef } from "react";
+import { useRef } from "react";
@thepuskar
thepuskar / Providers.tsx
Created May 22, 2024 04:45
No More React Context Hell
//If you always wanted it to look like this
<Providers providers={[
<FooContext.Provider value="foo" />,
<BarContext.Provider value="bar" />,
<BazContext.Provider value="baz" />,
]}>
<App />
</Providers>,
@thepuskar
thepuskar / password-input.tsx
Created April 26, 2024 10:05 — forked from mjbalcueva/password-input.tsx
shadcn ui custom password input
"use client"
import { forwardRef, useState } from "react"
import { EyeIcon, EyeOffIcon } from "lucide-react"
import { Button } from "@/components/ui/button"
import { Input, InputProps } from "@/components/ui/input"
import { cn } from "@/lib/utils"
const PasswordInput = forwardRef<HTMLInputElement, InputProps>(
({ className, ...props }, ref) => {
@thepuskar
thepuskar / conditionalTypes.ts
Created April 8, 2024 17:47
TypeScript conditional types based on property value
type IProps = {
children: React.ReactNode;
asChild?: boolean;
} & (IModalProp | IRedirectProp);
interface IModalProp {
mode: "modal";
}
interface IRedirectProp {
@thepuskar
thepuskar / CategoryController.cs
Created January 12, 2024 16:58
Pagination in C# Asp.net Core
public IActionResult Index(int pageNumber = 1, int pageSize = 10)
{
IEnumerable<Category> categoryList = _db.Categories.ToList();
if (pageNumber < 1) pageNumber = 1;
int totalItems = categoryList.Count();
var pager = new Pager(totalItems, pageNumber, pageSize);
int recSkip = (pageNumber - 1) * pageSize;
var data = categoryList.Skip(recSkip).Take(pager.PageSize).ToList();
this.ViewBag.Pager = pager;
@thepuskar
thepuskar / requestHandler.ts
Created December 21, 2023 16:47
Request handler in react js + Typescript for API call using axios
import { AxiosError, AxiosResponse } from 'axios'
/**
* A type representing a function that takes an optional object of type T and returns a promise resolving to an axios Response with a generic type V.
*/
type BaseRequest<T, V> = (params?: T) => Promise<AxiosResponse<V>>
/**
* A type representing the shape of a successful API response, which includes a 'code' property set to 'success', and a 'data' property containing the payload.
*/
@thepuskar
thepuskar / Form.tsx
Last active December 12, 2023 06:45
React hook form Input custom component with strict typed
import { Input } from "@/components/input-fields";
import { zodResolver } from "@hookform/resolvers/zod";
import Link from "next/link";
import { SubmitHandler, useForm } from "react-hook-form";
import { z } from "zod";
const RegisterSchema = z
.object({
email: z
.string()
@thepuskar
thepuskar / App.tsx
Created August 21, 2023 06:25
Utility component for conditional rendering in React js
import { GoalLegend } from "./components/switch";
function App() {
return (
<div className="App">
<GoalLegend goal={0} />
</div>
);
}
@thepuskar
thepuskar / useOnScreen.ts
Created July 31, 2023 06:36
The `useOnScreen` function is a custom React hook that detects whether a given element is currently visible on the scree
import { useEffect, useMemo, useState,RefObject } from "react";
export function useOnScreen(ref:RefObject<HTMLElement>):boolean {
const [isOnScreen, setIsOnScreen] = useState<boolean>(false);
const observer = useMemo(
() => new IntersectionObserver(([entry]) => setIsOnScreen(entry.isIntersecting)),
// eslint-disable-next-line react-hooks/exhaustive-deps
[ref]
);