Skip to content

Instantly share code, notes, and snippets.

View tilap's full-sized avatar
🏠
Working from home

Tilap tilap

🏠
Working from home
View GitHub Profile
@tilap
tilap / useScrollPosition.js
Last active April 10, 2024 06:44
React hook to get scroll position
import { useCallback, useLayoutEffect, useRef } from 'react';
const isBrowser = typeof window !== 'undefined';
const getScrollPosition = ({ element, useWindow }) => {
if (!isBrowser) return { x: 0, y: 0 };
if (useWindow) {
return { x: window.scrollX, y: window.scrollY };
}
@tilap
tilap / localtunnel.js
Last active December 20, 2023 11:03
Localtunnel auto reconnect
/**
* Stupid nodejs script to use localtunnel and auto reconnect
* usage: node localtunnel.js port=3000 subdomain=toto
*
*/
/* global console, require, process, setTimeout */
const assert = require('assert');
const localtunnel = require('localtunnel');
var argv = process.argv.reduce(function(accumulator, str) {
@tilap
tilap / useApi.ts
Created October 23, 2023 07:48
React hook to fetch api data
import { useState, useCallback } from 'react'
import { AxiosPromise } from 'axios'
export interface ApiResponse<T> {
data?: T | undefined
error?: Error | undefined
loading: boolean
}
export type UseApiResponse<T> = [ApiResponse<T>, (...args: any[]) => Promise<void>, () => void]
@tilap
tilap / functions.php
Last active July 23, 2023 09:29
Wordpress: How to automatically attache youtube/vimeo thumbnail as a post feature image from a post meta key
function insertThumbnail($data, $postArr) {
if(!isset($postArr['ID'])) {
return $data;
}
$postId = $postArr['ID'];
$metaKey = 'you-post-meta-video-key'; // the custom meta key
// If video and video format
if(isset($postArr[$metaKey]) && ''!==$postArr[$metaKey] ) {
@tilap
tilap / lilst.md
Created April 27, 2023 14:49
Carte monde codes iso drupdrup

AD | Andorre AE | Émirats arabes unis AF | Afghanistan AG | Antigua-et-Barbuda AI | Anguilla AL | Albanie AM | Arménie AO | Angola AQ | Antarctique AR | Argentine

@tilap
tilap / autoHitSlop.tsx
Last active January 23, 2023 08:45
React native hook to automatically add hit slop
import React from 'react';
import type { LayoutChangeEvent, Insets } from 'react-native';
const defaultGetHitSlopForSize = ({ width, height }: Framesize): Insets => {
const x = width > 44 ? 0 : 10;
const y = height > 44 ? 0 : 10;
return { top: y, bottom: y, left: x, right: x };
};
type Framesize = { width: number; height: number };
@tilap
tilap / useLocalStorage.js
Last active August 3, 2022 18:04
Use and sync a localStorage entry with react hook
import { useCallback, useEffect, useState, useMemo } from 'react';
const isBrowser = typeof window !== 'undefined';
const isLocalstorageAvailable = () => {
if (!isBrowser) {
return false;
}
const test = `test-${Date.now()}`;
@tilap
tilap / truncate.css
Created December 24, 2021 16:19 — forked from elky/truncate.css
text-overflow ellipsis with 100% width
/*
Demo: https://jsfiddle.net/elky/f6khaf2t/
<div class="element">
<div class="truncate">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
@tilap
tilap / install.sh
Created January 29, 2016 08:23
Share your git hooks in a project: basic script to install them all
#!/bin/bash
# Basic script to set custom project hook and share it with other developpers
# original script from http://stackoverflow.com/questions/3462955/putting-git-hooks-into-repository/3464399#3464399
#
# cd [path-of-the-script]
# . install.sh
#
# Folders usecase
# /.git