Skip to content

Instantly share code, notes, and snippets.

View TonyDotDev's full-sized avatar
🎯
Focusing

Tony Pettigrew TonyDotDev

🎯
Focusing
View GitHub Profile
@TonyDotDev
TonyDotDev / onClickOutside.hook.js
Last active July 11, 2022 18:10
Detect clicks outside of component (Hooks Version)
import React, { useRef, useEffect } from "react";
function useClickOutside(ref) {
useEffect(() => {
function handleClickOutside(event) {
if (ref.current && !ref.current.contains(event.target)) {
alert("Outside click");
}
}
@TonyDotDev
TonyDotDev / generateSecret.js
Created May 3, 2020 14:47
Generates a random secret
const crypto = require('crypto');
const base32 = require('hi-base32');
export default function generateSecret(length = 20) {
const randomBuffer = crypto.randomBytes(length);
return base32.encode(randomBuffer).replace(/=/g, '');
@TonyDotDev
TonyDotDev / scrape.js
Created April 19, 2020 14:34
Basic Node web scraper using puppeteer and cheerio.
const cheerio = require("cheerio");
const fs = require("fs");
const puppeteer = require("puppeteer");
const getData = (html) => {
const data = [];
const $ = cheerio.load(html);
// Select HTML elements to extract data from using Cheerio's jQuery like selectors
@TonyDotDev
TonyDotDev / useWindowSize.js
Created April 15, 2020 14:16
React hook to get window size.
import { useState, useEffect } from "react";
export const useWindowSize = () => {
const isClient = typeof window === "object";
const getSize = () => {
return {
width: isClient ? window.innerWidth : undefined,
height: isClient ? window.innerHeight : undefined,
};
@TonyDotDev
TonyDotDev / useScrollPosition.js
Created April 15, 2020 14:16
Get Scroll Position/Direction Webhook
import { useState, useEffect } from "react";
export const useScrollPosition = () => {
const [scrollPosition, setScrollPosition] = useState(0);
const [scrollDirection, setScrollDirection] = useState(-1);
useEffect(() => {
const scrollYPos = window.pageYOffset || document.documentElement.scrollTop;
let lastScrollPos = scrollYPos;
@TonyDotDev
TonyDotDev / mixins.scss
Created March 10, 2020 15:51
SCSS Utility Mixins
// Remove arrows from number input
@mixin remove-arrows-number-input {
&::-webkit-outer-spin-button,
&::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
&[type="number"] {
@TonyDotDev
TonyDotDev / datetime.js
Created March 10, 2020 01:29
Working With DateTime MomentJS
import moment from "moment";
export const convertDateTimeToCurrentTimezone = dateTime => {
const utcOffset = moment().utcOffset();
return moment(dateTime).add(utcOffset, "minutes");
};
const createUTCDateTime = date =>
moment(date)
.utc()
export const maskPrice = price => {
const scrubValue = price => {
const removedDecimal = value.replace(/\./g, "");
const onlyDigits = new RegExp(/^(\d)+$/);
if (!onlyDigits.test(removedDecimal)) return;
const removedDecimalArray = removedDecimal.split("");
removedDecimalArray.splice(-2, 0, ".");
@TonyDotDev
TonyDotDev / copy-to-clipboard.js
Last active February 24, 2020 21:21
Copy to clipboard javascript function.
function copyToClipboard(element) {
window.getSelection().removeAllRanges();
const range = document.createRange();
range.selectNode(element);
window.getSelection().addRange(range);
document.execCommand("copy");
window.getSelection().removeAllRanges();
}
@TonyDotDev
TonyDotDev / Debouncer.js
Created February 22, 2020 20:38
A higher order component for debouncing function calls.
import React, { Component } from "react";
function withDebounce(WrappedComponent) {
return class extends Component {
constructor(props) {
super(props);
this.timeout = null;
this.debounce = this.debounce.bind(this);
}