Skip to content

Instantly share code, notes, and snippets.

View isNan909's full-sized avatar
:shipit:
Code the Code

ishan isNan909

:shipit:
Code the Code
View GitHub Profile
@isNan909
isNan909 / index.js
Created August 1, 2023 12:46
Filtering data and sending the filtered response
//nextjs server
import { NextResponse } from 'next/server';
import pets from '../pets.json';
export async function GET(request) {
const { searchParams } = new URL(request.url);
const query = searchParams.get('query');
const filteredPets = pets.filter((pet) => {
return pet.name.toLowerCase().includes(query.toLowerCase());
});
import {useState} from "react";
function useToggle(initialState){
const [state, setState] = useState(initialState || false);
return [state, () => setState(!state)];
};
//Use it
// const [on, toggle] = useToggle();
@isNan909
isNan909 / LazyLoadImage.jsx
Created August 2, 2022 12:05
Lazy load images in React
const LazyLoadImage = ({
alt,
src,
className,
loadInitially = false,
observerOptions = { root: null, rootMargin: '200px 0px' },
...props
}) => {
const observerRef = React.useRef(null);
const imgRef = React.useRef(null);
@isNan909
isNan909 / useClickOutside.jsx
Created August 2, 2022 06:52
Listen to clicked outside
import { useState, useEffect, useRef } from "react";
function App() {
// Create a ref that we add to the element for which we want to detect outside clicks
const ref = useRef();
// State for our modal
const [isModalOpen, setModalOpen] = useState(false);
// Call hook passing in the ref and a function to call on outside click
useOnClickOutside(ref, () => setModalOpen(false));
return (
@isNan909
isNan909 / useWindowSize.jsx
Created August 2, 2022 06:34
useWindowSize hook
const useWindowSize = () => {
const [windowSize, setWindowSize] = useState({
width: undefined,
height: undefined
});
useEffect(() => {
function handleResize() {
setWindowSize({
width: window.innerWidth,
@isNan909
isNan909 / zoom-in.js
Created June 29, 2022 04:03
Add a zoom in transition when scrolling in a page
const zoom = document.querySelector('.zoomIn');
const minZoom = 1;
const maxZoom = 2;
addEventListener('scroll', e => {
const vh = window.innerHeight / 100;
const scrollTop = document.documentElement.scrollTop;
const start = 100 * vh;
const stop = 200 * vh;
if (scrollTop > start && scrollTop < stop) {
@isNan909
isNan909 / scrollTranslatex.js
Created May 5, 2022 07:40
Onscroll translate elements in page
function onScroll(container) {
const div = container.querySelector(".selector");
const rect = div.getBoundingClientRect();
const top = Math.max(rect.top, 0);
const wl =
Array.from(div.querySelectorAll(".selector")).reduce((a, b) => {
return a + b.clientWidth + 40;
}, 0) + 120; // allow for extra padding;
const hl = rect.height;
const ww = window.innerWidth;
@isNan909
isNan909 / app.js
Created October 18, 2020 16:50
Node Scrapping Example
const puppeteer = require("puppeteer");
const SCRAPE_URL = `https://coincodex.com/`;
(async () => {
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
await page.goto(SCRAPE_URL);
await page.screenshot({ path: "sample.png" });
const coining = await page.evaluate(() =>
@isNan909
isNan909 / siteposts.js
Created August 14, 2020 11:53
File inside pages to loop over the posts
import React from "react"
import { Link, graphql } from "gatsby"
import Layout from "../components/layout"
import SEO from "../components/seo"
const SitePosts = ({ data }) => {
const ourPosts = data.allContentfulSitePost.edges
// console.log(ourPosts);
return (
@isNan909
isNan909 / siteposts.js
Created August 14, 2020 11:04
Creating template with the contentful data
import React from "react"
import { Link, graphql } from "gatsby"
import Layout from "../components/layout"
import SEO from "../components/seo"
const SitePosts = ({ data }) => {
const { heading, subheading, description, image } = data.contentfulSitePost
return (
<Layout>
<SEO title={heading} />