Skip to content

Instantly share code, notes, and snippets.

View viclafouch's full-sized avatar
🎯
Focusing

Victor de la Fouchardière viclafouch

🎯
Focusing
View GitHub Profile
@viclafouch
viclafouch / use-page-visibility.js
Last active March 3, 2024 00:54
A custom React Hook to detect if the page is visible or not.
// hooks/use-page-visibility.js
import { useState, useLayoutEffect } from 'react'
function usePageVisibility() {
const [isPageVisible, setIsPageVisible] = useState(!document.hidden)
useLayoutEffect(() => {
const handleVisibility = () => {
setIsPageVisible(!document.hidden)
}
@viclafouch
viclafouch / designMode.js
Last active January 25, 2024 14:21
Edit your website in live by using `document.designMode` and killing links
// Disable anchor links
window.addEventListener('click', event => event.preventDefault(), false)
// Disable click eventlisteners
window.addEventListener("click", event => event.stopPropagation(), true)
// Enable design mode
document.designMode = "on"
@viclafouch
viclafouch / clipboard.js
Last active December 7, 2023 23:10
How to copy an image or a text to clipboard in Javascript (new way !) See https://copy-to-clipboard.now.sh/
// @return Promise<boolean>
async function askWritePermission() {
try {
// The clipboard-write permission is granted automatically to pages
// when they are the active tab. So it's not required, but it's more safe.
const { state } = await navigator.permissions.query({ name: 'clipboard-write' })
return state === 'granted'
} catch (error) {
// Browser compatibility / Security error (ONLY HTTPS) ...
return false
@viclafouch
viclafouch / use-clipboard-api.js
Last active November 4, 2023 04:05
A custom React Hook for writing and reading from the modern clipboard API
// hooks/use-clipboard-api.js
import { useState, useCallback } from 'react'
function useClipboardApi() {
const [content, setContent] = useState(null)
const askPermission = useCallback(async queryName => {
try {
const permissionStatus = await navigator.permissions.query(queryName)
return permissionStatus.state === 'granted'
@viclafouch
viclafouch / forward-ref.ts
Created August 2, 2023 08:33
Redesign of forwardRef in TS
import React, { forwardRef } from "react";
// Declare a type that works with
// generic components
type FixedForwardRef = <T, P = {}>(
render: (props: P, ref: React.Ref<T>) => React.ReactNode
) => (props: P & React.RefAttributes<T>) => React.ReactNode;
// Cast the old forwardRef to the new one
export const fixedForwardRef =
@viclafouch
viclafouch / modal.jsx
Last active January 23, 2023 21:14
An implementation of a Modal Component with React Hooks ! See https://react-modal.viclafouch.vercel.app
import React, { useEffect, useImperativeHandle, useState, forwardRef, useCallback } from 'react'
import { createPortal } from 'react-dom'
import './styles.css'
const modalElement = document.getElementById('modal-root')
export function Modal({ children, fade = false, defaultOpened = false }, ref) {
const [isOpen, setIsOpen] = useState(defaultOpened)
const close = useCallback(() => setIsOpen(false), [])
@viclafouch
viclafouch / abort-requests-in-react.js
Last active May 21, 2022 09:34
A React component that fetching data on mount and avoiding memory leaks on unmount with AbortController
import React, { useState, useEffect, useCallback } from 'react'
function Posts() {
const [isLoading, setIsLoading] = useState(true)
const [posts, setPosts] = useState([])
const fetchPosts = useCallback(async controller => {
try {
// Imagine that the fetch is going to take 3 seconds to finish
await new Promise(resolve => setTimeout(resolve, 3000))
@viclafouch
viclafouch / scrollTop.js
Created September 26, 2021 12:35
JavaScript native API for the smooth scrolling.
function scrollTop(nodeElement = window) {
try {
// The new API
nodeElement.scroll({
top: 0,
left: 0,
behavior: 'smooth'
})
} catch (error) {
// For olders browsers
/* Prevent unsecure [target="_blank] links */
a[target='_blank']:not([rel~='noopener']):not([rel~='noreferrer']) {
outline: 2px dashed red;
}
@viclafouch
viclafouch / usePortal.js
Created December 18, 2020 10:53
A custom React Hook to create a Portal.
import { useState, useRef, useEffect, useCallback } from 'react'
import { createPortal } from 'react-dom'
export default function usePortal() {
const [isOpen, setIsOpen] = useState(false)
const portal = useRef(null)
const openPortal = useCallback((callback) => {
if (!portal.current) {
portal.current = document.createElement('div')