Skip to content

Instantly share code, notes, and snippets.

View kp-gists's full-sized avatar
🎯
Focusing

kp-gists

🎯
Focusing
View GitHub Profile
@kp-gists
kp-gists / SatisfyText.tsx
Created January 10, 2025 12:36
Custom Font Text Nextjs
import React from 'react';
import { Satisfy } from 'next/font/google';
import clsx from 'clsx';
const satisfy = Satisfy({
variable: '--font-satisfy-mono',
subsets: ['latin'],
weight: '400',
});
@kp-gists
kp-gists / countries.js
Created October 4, 2024 04:38
Api to fetch countries
this.express.get('/api/countries', async (req, res) => {
console.log('ads');
const response = await axios.get(
'https://restcountries.com/v3.1/all',
);
console.log('🚀 ~ App ~ this.express.get ~ response:', response);
const countries = await response.data.map((country: Country) => {
const hasCurrency =
country.currencies &&
Object.keys(country.currencies).length > 0;
@kp-gists
kp-gists / button.css
Created January 8, 2024 09:03
css button on hover check if it is active and shows the correct hover and active state
button:not(.active):hover {
background-color: #e5f1b8;
}
// when the button is hovered has a different slight color and when it is active it has a different
//color so this snippet it helps to track the button hover it it is active it should not change the active color
// scroll to categories
const hash = this.$route.query.hash
if (hash == 'categories') {
console.log('🚀 ~ hash:', hash)
const el = document.getElementById('categories')
el.scrollIntoView({ behavior: 'smooth' })
this.$router.replace({ query: null })
} else {
console.log('this.$route.params', this.$route.query)
@kp-gists
kp-gists / useWindowSize
Created January 16, 2023 09:10
useWindowSize w useOnScreen
import { useState } from "react"
import useEventListener from "../13-useEventListener/useEventListener"
export default function useWindowSize() {
const [windowSize, setWindowSize] = useState({
width: window.innerWidth,
height: window.innerHeight,
})
useEventListener("resize", () => {
@kp-gists
kp-gists / useOnScreen
Created January 16, 2023 09:09
useOnScreen
import { useEffect, useState } from "react"
export default function useOnScreen(ref, rootMargin = "0px") {
const [isVisible, setIsVisible] = useState(false)
useEffect(() => {
if (ref.current == null) return
const observer = new IntersectionObserver(
([entry]) => setIsVisible(entry.isIntersecting),
{ rootMargin }
@kp-gists
kp-gists / useEventListener
Created January 16, 2023 09:08
useEventListener
import { useEffect, useRef } from "react"
export default function useEventListener(
eventType,
callback,
element = window
) {
const callbackRef = useRef(callback)
useEffect(() => {
@kp-gists
kp-gists / useDebounce
Created January 16, 2023 09:03
useDebounce w useTimeout
import { useEffect } from "react"
import useTimeout from "../2-useTimeout/useTimeout"
export default function useDebounce(callback, delay, dependencies) {
const { reset, clear } = useTimeout(callback, delay)
useEffect(reset, [...dependencies, reset])
useEffect(clear, [])
}
import { useCallback, useEffect, useRef } from "react"
export default function useTimeout(callback, delay) {
const callbackRef = useRef(callback)
const timeoutRef = useRef()
useEffect(() => {
callbackRef.current = callback
}, [callback])
@kp-gists
kp-gists / routes
Created January 16, 2023 08:35
isAuthenticated and role routes
import { useSelector } from 'react-redux';
import { useHistory } from 'react-router-dom';
function PrivateRoute({ children, ...rest }) {
const isAuthenticated = useSelector((state) => state.auth.isAuthenticated);
const history = useHistory();
if (!isAuthenticated) {
history.push('/login');
return null;