Skip to content

Instantly share code, notes, and snippets.

View jorgeortega's full-sized avatar
💭
Learning all the time

Jorge Ortega jorgeortega

💭
Learning all the time
View GitHub Profile
@jorgeortega
jorgeortega / uuid.js
Last active March 2, 2020 09:58
Generate random uuid
/*
* Generates a universally unique identifier based on random values and timestamp.
* Random length can be increased to get even more unique values.
*/
function uuid() {
if (!window.crypto) throw new Error('crypto not available')
const randomLength = 8
const uuidUint8Array = new Uint8Array(randomLength)
window.crypto.getRandomValues(uuidUint8Array)
return `${uuidUint8Array.join('')}${Date.now()}`
@jorgeortega
jorgeortega / window-mode.js
Created April 30, 2019 12:24
Check if window is fullscreen, maximized or not maximized
const isAtMaxWidth = screen.availWidth - window.innerWidth === 0
const screenPixelRatio = (window.outerWidth - 8) / window.innerWidth
const isAtDefaultZoom = screenPixelRatio > 0.92 && screenPixelRatio <= 1.10
const isMaximizedAndDefaultZoom = isAtMaxWidth && isAtDefaultZoom
const isFullscreen = window.outerWidth == screen.width && window.outerHeight == screen.height
@jorgeortega
jorgeortega / react-styled-components-transition.js
Created October 31, 2018 14:57
Minimal transition using React Transition and styled-components
import Transition from 'react-transition-group/Transition'
<Transition in={isVisible} timeout={200}>
{(state) => (
<StyledComponent transitionState={state} />
)}
</Transition>
const StyledComponent = styled.div`
transition: opacity 0.2s;
@jorgeortega
jorgeortega / get-viewport-dimensions.js
Created June 8, 2018 07:48
Get viewport dimension
export function getViewportDimensions () {
const w = window
const d = document
const e = d.documentElement
const g = d.getElementsByTagName('body')[0]
const x = w.innerWidth || e.clientWidth || g.clientWidth
const y = w.innerHeight || e.clientHeight || g.clientHeight
return { x, y }
}
@jorgeortega
jorgeortega / split-text-by-tag.js
Last active May 22, 2018 16:22
Split html string in parts by tag
/**
* Splits a string in half by tag name
* i.e. '<p>' or 'p'
* returns array of 2 strings
*
*/
export const splitTextByTag = (text, tag) => {
let closingTag = '';
// clean text from line breaks
let cleanText = text.replace(/\r?\n|\r/g, '');
@jorgeortega
jorgeortega / keyboard-handler.js
Last active March 21, 2018 08:46
Handle keyboard events
export function keyboardHandler (event, keys, callback) {
if (typeof event === 'undefined') {
return
}
const key = event.key || event.keyCode
if (keys.includes(key)) {
event.preventDefault()
event.stopPropagation()
callback()
}
@jorgeortega
jorgeortega / clear-timeouts.js
Last active March 15, 2018 15:44
Clear all timeouts in window
var id=window.setTimeout(function(){},0);
while(id--){window.clearTimeout(id);}
@jorgeortega
jorgeortega / Wheel.js
Last active March 20, 2018 15:21
Utility class to handle events of a scroller wheel
import throttle from 'lodash/throttle'
export class Wheel {
constructor ({ id, wheelEndCallback }) {
this.initialPointerY = -1
this.pointerY = 0
this.currentItemIndex = -1
this.elementHeight = -1
this.childrenHeight = -1
this.isDragging = false
@jorgeortega
jorgeortega / React-Redux-Simple-Container-Component.js
Last active February 21, 2018 09:37
React Redux Simple Container Component
import React, { Component } from 'react'
import { string } from 'prop-types'
import { connect } from 'react-redux'
import { openModal } from 'actions/modal'
import SimpleModal from './index.js'
export class SimpleModalContainer extends Component {
constructor (props) {
super(props)
}
@jorgeortega
jorgeortega / React-Simple-Stateless-Component.js
Created February 21, 2018 09:23
React Simple Stateless Component
import React from 'react'
import { string } from 'prop-types'
import './_SimpleModal.css'
export function SimpleModal ({ className }) {
return (
<div className="SimpleModal__Container">
Modal
</div>
)