Skip to content

Instantly share code, notes, and snippets.

View audunolsen's full-sized avatar
🌷
Made a new avatar with Midjourney lmao

Audun Meek Olsen audunolsen

🌷
Made a new avatar with Midjourney lmao
View GitHub Profile
@audunolsen
audunolsen / chebox.module.scss
Last active April 16, 2021 13:12
All I see on the web are ridiculously overcomplicated checkboxes.
.wrapper {
display: inline-flex;
align-items: center;
flex-direction: row;
height: min-content;
label {
margin: 0;
margin-left: 10px;
}
@audunolsen
audunolsen / Inert.tsx
Created February 25, 2021 13:49
Replicate upcoming HTML inert attribute
import React, { useRef, useEffect, useCallback } from 'react';
/*
A wrapper component which replicates the upcoming inert HTML attribute.
I've made a custom implementation because the only polyfill out there (https://github.com/WICG/inert)
plays very poorly with React.
Needed for situations where an element needs to stay
visible in the DOM but any of it's children shouldn't
be reachable through keyboard navigation
@audunolsen
audunolsen / animate.scss
Created February 19, 2021 11:10
TODO: scss animate mixin idea
/*
TODO: Make mixin wich fire keyframes immidiately
Tired or writing:
.el {
opacity: 1;
animation: fadeout 0.5s ease-out forwards;
@keyframes fadeout {
@audunolsen
audunolsen / example.tsx
Last active March 14, 2021 14:49
No bullshit super simple portal hook
// …
const { openPortal, closePortal, isOpen, Portal } = usePortal(document.getElementById('modal-portal'));
// …
return (
<Portal>
<h1>HELLO WORLD</h1>
</Portal>
@audunolsen
audunolsen / useStatePromise.ts
Created February 4, 2021 08:49
useStatePromise
import { useEffect, useState, useRef, useCallback } from 'react';
// Typescripted version of https://ysfaran.github.io/blog/post/0002-use-state-with-promise/
// Used just as useState except it returns a promise resolving
// with the newly set state when component has rerendered
const useStatePromise = <T>(initialState: T): [T, (action: React.SetStateAction<T>) => Promise<T>] => {
const [state, setState] = useState<T>(initialState);
const resolverRef = useRef<null | ((state: T | PromiseLike<T>) => void)>(null);
// Resources for regular expressions:
// github.com/DamonOehlman/detect-browser/blob/master/src/index.ts
// https://github.com/faisalman/ua-parser-js/blob/master/src/ua-parser.js
const browsers = [
['edge', /Edge\/([0-9\._]+)/],
['edge-ios', /EdgiOS\/([0-9\._]+)/],
['samsung', /SamsungBrowser\/([0-9\.]+)/],
['edge-chromium', /EdgA?\/([0-9\.]+)/],
['chrome', /(?!Chrom.*OPR)Chrom(?:e|ium)\/([0-9\.]+)(:?\s|$)/],
@audunolsen
audunolsen / breakpoints.ts
Last active January 24, 2021 16:35
Responsive development SCSS JS
interface BreakpointState {
[key: string]: boolean;
}
export class Breakpoints {
get all() {
const serialized = isBrowser() && getComputedStyle(document.body).getPropertyValue('--breakpoints');
let parsed;
try {

c( 'cls' i === 3 && 'third', e.size === 'xl' ? 'x-large' : 'small', { variableName } )

@audunolsen
audunolsen / elementVisibility.js
Last active May 13, 2021 21:16
Returns a detailed set of data regarding an elements position relative to the viewport
/* eslint-disable */
/*
type Directions {
top: boolean;
left: boolean;
bottom: boolean;
right: boolean;
horizontal: boolean;
@audunolsen
audunolsen / classnames-48b.js
Last active November 6, 2020 09:13
.048kB classnames npm module alternative
/*
the npm module "classnames" has 5,549,574 weekly downloads
as of 06.11.2020. It has an unpacked size of 16.3 kB.
Why TF is such a trivial package so popular given its size?
Here's a 0.048 kB alternative:
*/