Skip to content

Instantly share code, notes, and snippets.

View wklsh's full-sized avatar

Shawn Lim Wee Kiang wklsh

View GitHub Profile
@wklsh
wklsh / slugify.js
Created November 6, 2017 10:17 — forked from mathewbyrne/slugify.js
Javascript Slugify
function slugify(text)
{
return text.toString().toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/[^\w\-]+/g, '') // Remove all non-word chars
.replace(/\-\-+/g, '-') // Replace multiple - with single -
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, ''); // Trim - from end of text
}
@wklsh
wklsh / header.css
Created August 17, 2018 03:05
Prevent elements from shifting around on hover / active text bolds
a:hover {
font-weight:bold;
}
a::after {
display: block;
content: attr(data-text);
font-weight: bold;
height: 0;
overflow: hidden;
@wklsh
wklsh / CalMouseDist.js
Last active May 9, 2020 02:39
Calculate Distance Between Mouse and center of Element
/**
* Calculate disance between mouse and center of element
* @param {obj} element target element
* @param {num} mouseX mouse pos X value
* @param {num} mouseY mouse pos Y value
*/
export function calcDistance(element, mouseX, mouseY) {
const elDimensions = element.getBoundingClientRect();
return Math.floor(
@wklsh
wklsh / ReactCheckUpdatedPropsOrStates.js
Last active November 13, 2018 10:58
Check for updated props / states within ComponentDidUpdate lifecycle
componentDidUpdate(prevProps, prevState) {
// Get updated props
for (const index in prevProps) {
if (prevProps[index] !== this.props[index]) {
console.log(index, prevProps[index], "-->", this.props[index]);
}
}
// Get updated states
for (const index in prevState) {
var cameraZ = camera.position.z;
var planeZ = 5;
var distance = cameraZ - planeZ;
var aspect = viewWidth / viewHeight;
var vFov = camera.fov * Math.PI / 180;
var planeHeightAtDistance = 2 * Math.tan(vFov / 2) * distance;
var planeWidthAtDistance = planeHeightAtDistance * aspect;
// or
@wklsh
wklsh / function_component.jsx
Last active June 3, 2020 11:03
SSR / SR Rehydration Rendering
function function_component() {
const hasMounted = useHasMounted();
// has to be declared in function's body prior to usage
// has to be on top to nullify unmounted runs
if (!hasMounted) {
return null;
}
// ...code runs here
@wklsh
wklsh / _var.scss
Last active June 12, 2020 08:58
Responsive 1vh css variable
// ...
:root {
--vh: 1vh;
}
@wklsh
wklsh / style.scss
Last active June 17, 2020 12:24
Reponsive square CSS elements – Revised
.squareWrapper {
width: 50%;
display: inline-block; // Ensure that element is `inline-block`
&:before { // Ensure that pseudo-before element does not have any WIDTH property
content: "";
display: block;
padding-bottom: 100%;
float: left;
@wklsh
wklsh / docker_wordpress.md
Created June 23, 2020 10:29 — forked from bradtraversy/docker_wordpress.md
Docker Compose FIle For Wordpress, MySQL & phpmyadmin

Wordpress & Docker

This file will setup Wordpress, MySQL & PHPMyAdmin with a single command. Add the code below to a file called "docker-compose.yaml" and run the command

$ docker-compose up -d

# To Tear Down
$ docker-compose down --volumes
@wklsh
wklsh / UIContext.md
Last active February 13, 2022 14:34
Redux-like React Context Example

Redux-like React Context example

Example Setup of a redux-like react context. For reference visit this guide, or this guide.

Store

import React, { createContext, useReducer } from "react";

export const UIContext = createContext();