Skip to content

Instantly share code, notes, and snippets.

View xdmorgan's full-sized avatar
Unverified

Dan Morgan xdmorgan

Unverified
View GitHub Profile
@xdmorgan
xdmorgan / data-structure.md
Last active April 5, 2020 19:48
Type config data structure

Data structure notes

WYSIWYG config structure

wysiwyg:
  p: para # single assigment tag:key
  para: p, ul, ol, p.small # multiple assignment list key:selectors

Type style data object

@xdmorgan
xdmorgan / use-watch-height.jsx
Created October 6, 2019 13:03
useWatchHeight. Sometimes you just gotta know the height of an element (Safari 🙄)
import React from 'react'
import useEventListener from '@use-it/event-listener'
function getRect(el) {
return el ? el.getBoundingClientRect() : {}
}
function useWatchHeight() {
const ref = React.useRef()
const [height, setHeight] = React.useState('auto')
const Mailchimp = require("mailchimp-api-v3");
const validate = require("./validations");
const { MAILCHIMP_API_KEY } = process.env;
exports.handler = async (event, context) => {
// require an API key or throw everytime
const [is_valid_env, invalid_env_response] = check_env(process.env);
if (!is_valid_env) return invalid_env_response;
// 0. validate request body
@xdmorgan
xdmorgan / mailchimp-subscribe.js
Created October 5, 2019 01:59
Netlify Mailchimp subscribe Lambda function
const Mailchimp = require('mailchimp-api-v3')
const validate = require('./validations')
const { MAILCHIMP_API_KEY } = process.env
exports.handler = async (event, context) => {
// require an API key or throw everytime
if (!MAILCHIMP_API_KEY) {
return {
statusCode: 500,
@xdmorgan
xdmorgan / underline.scss
Created September 24, 2019 23:38
Background-image style link underline (Sass mixin with defaults)
// Sassified version of the approach discussed here...
// big up: https://eager.io/blog/smarter-link-underlines/
@mixin underline($fg: #000, $bg: #fff, $size: 1px, $ink: 0.03em, $pos: 87%) {
background: linear-gradient($bg, $bg), linear-gradient($bg, $bg),
linear-gradient($fg, $fg);
background-size: 0.05em $size, 0.05em $size, $size $size;
background-repeat: no-repeat, no-repeat, repeat-x;
text-shadow: $ink 0 $bg, -$ink 0 $bg, ($ink * 2) 0 $bg, -($ink * 2) 0 $bg,
($ink * 3) 0 $bg, -($ink * 3) 0 $bg, ($ink * 4) 0 $bg, -($ink * 4) 0 $bg,
@xdmorgan
xdmorgan / marquee.tsx
Last active September 8, 2019 20:05
React Animated Marquee (CSS Module, Custom Hook, Accessible)
import React, { useRef, useState, useEffect } from 'react'
import cx from 'classnames'
import styles from './marquee-v2.module.scss'
export default function Marquee({
children,
className = undefined,
reverse = false,
...props
}) {
@xdmorgan
xdmorgan / YouTubeEmbed.jsx
Last active September 7, 2019 17:01
React YouTube Embed Component (iframe)
import React from 'react'
export default function YouTubeEmbed({ videoId, ...props }) {
return (
<iframe
width="560"
height="315"
src={idToEmbedURL({ id: videoId })}
frameborder="0"
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
@xdmorgan
xdmorgan / useEventTracking.ts
Last active July 1, 2019 15:45
React Hook / Google gtag event tracking / TypeScript / SSR-Friendly (tested with Gatsby)
/**
"Static Tracking" for values that will not change
```js
const [onTrack] = useStaticTracking({ category: 'Home Page', label: "Register" })
return <Button onClick={onTrack} />
```
@xdmorgan
xdmorgan / _codegen.scss
Created February 17, 2019 13:03
Sass codegen functions to generate Bootstrap / Tailwind style utility classes from maps
// example config maps
$skeletor: (
breakpoints: (
sm: 500px,
md: 750px,
lg: 1000px,
xl: 1250px
),
colors: (
red: red,
@xdmorgan
xdmorgan / calculate-grid-col-width.js
Created February 9, 2019 23:26
Calculate percentage based grid columns + gaps to set a fixed size for an element spanning n columns, for reasons.
const calculateGridItemWidth = (span, cols = 24, gap = 16) =>
((window.innerWidth - (cols + 1) * gap) / cols) * span + gap * (span - 1);
this.calculateGridItemWidth(18)