Skip to content

Instantly share code, notes, and snippets.

View joshuacerbito's full-sized avatar
🔥
🎸🎛🎛🎚🔊

Joshua Cerbito joshuacerbito

🔥
🎸🎛🎛🎚🔊
View GitHub Profile
@joshuacerbito
joshuacerbito / useSessionStorage.js
Created July 25, 2019 07:10
Custom React Hook for handling session storage
import { useState } from 'react';
export default function useSessionStorage(key, initialValue) {
const [item, setInnerValue] = useState(() => {
try {
return window.sessionStorage.getItem(key)
? JSON.parse(window.sessionStorage.getItem(key))
: initialValue;
} catch (error) {
return initialValue;
@joshuacerbito
joshuacerbito / module-exporter.js
Created May 18, 2017 16:37
JS Module Exporter (AMD, CommonJS, Globals)
(function(globals){
// module name
const MODULE_NAME = 'moduleName';
// define your functions here
const functions = {
fn1: alert,
fn2: console.log
};
@joshuacerbito
joshuacerbito / example_usage.php
Last active January 30, 2024 17:06
How to include PDF in Wordpress Searches
<?php
if (have_posts()) {
while (have_posts()) {
the_post();
if (get_post_mime_type() == 'application/pdf') {
// Customize how you display PDF files
echo '<a href="' . esc_url(get_permalink()) . '">' . esc_html(get_the_title()) . '</a>';
} else {
@joshuacerbito
joshuacerbito / useScroll.js
Last active January 8, 2024 13:44
Custom React hook for listening to scroll events
/**
* useScroll React custom hook
* Usage:
* const { scrollX, scrollY, scrollDirection } = useScroll();
*/
import { useState, useEffect } from "react";
export function useScroll() {
const [lastScrollTop, setLastScrollTop] = useState(0);
@joshuacerbito
joshuacerbito / extend-react-props.tsx
Created September 13, 2023 11:00
Extend or use the props interface/type of any component
// extend basic react component props
// simply replace the type and add your props' types
interface PersonCardProps extends React.ComponentProps<typeof PersonCard> {
name: string;
age: number;
isAdmin: boolean;
}
// in this example we're writing a component named PersonCard
// with props name, age, and isAdmin
@joshuacerbito
joshuacerbito / lazy.css
Created August 30, 2023 11:38
This is a CSS file that I wrote back in 2013 and used on almost all of my FE projects for a couple of years. Does this approach look familiar? 😂 For historical purposes only. DO NOT USE THIS
/*-------------------------------------------------*/
/*----------------- LAZY.CSS v1.0 -----------------*/
/*-------- Joshua Cerbito (@joshuacerbito) --------*/
/*-------------------------------------------------*/
/* RESET ALL CONTAINERS */
html, body, div, table, tbody, tfoot, thead, tr, th, td, fieldset, form, label, legend,
section, article, aside, header, footer, hgroup, nav, audio, video, canvas, figure, figcaption, time, menu, details
{
margin: 0;
@joshuacerbito
joshuacerbito / pre-fill-acf-repeater.php
Last active August 29, 2023 07:51
Pre-Fill Wordpress ACF Repeater Field
<?php
// Pre-fills a repeater field (and its sub-fields) with default values
// - assuming we have a Repeater field with the key "repeater_field_123"
// - with the sub-fields "sub_field_1" and "sub_field_2"
function prefill_repeater( $field ) {
if ($field['value'] === false) {
$field['value'] = array(
array(
@joshuacerbito
joshuacerbito / main.yml
Last active May 5, 2023 11:40
Github Action for Flywheel deployment (via ssh) [2023-05-05]
name: Flywheel SSH Deploy
on:
push:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: "shivammathur/setup-php@v2"
@joshuacerbito
joshuacerbito / respond.scss
Last active May 4, 2023 17:02
Respond Mixin
/*---------------------------------------------------------------*\
RESPONSIVE PROPERTY HANDLER
handles the per-breakpoint property for mobile-first approach
note: requires a key-less 'breakpoints' scss map
e.g. $breakpoints: ( 320px, 760px, 1080px, 1280px );
usage:
@include respond((
display: flex,
@joshuacerbito
joshuacerbito / vercel.json
Created March 8, 2023 11:23
Config file for deploying Storybook to Vercel
{
"$schema": "https://openapi.vercel.sh/vercel.json",
"buildCommand": "npm run build-storybook",
"devCommand": "npm run storybook",
"installCommand": "npm install",
"framework": null,
"outputDirectory": "./storybook-static"
}