Skip to content

Instantly share code, notes, and snippets.

View just-boris's full-sized avatar

Boris Serdiuk just-boris

View GitHub Profile
@just-boris
just-boris / theming.js
Created April 24, 2023 19:30
Cloudscape theming demo
const { join } = require("path");
const {
buildThemedComponents,
} = require("@cloudscape-design/components-themeable/theming");
/** @type import('@cloudscape-design/components-themeable/theming').Theme */
const theme = {
tokens: {
fontFamilyBase: "'Helvetica Neue', Roboto, Arial, sans-serif",
},
import { createStateLifter } from './state-lifter'
const { Provider, useLiftedState } = createStateLifter("all");
export const LiftedCounter = () => (
<Provider>
<Filter />
<List />
</Provider>
);
@just-boris
just-boris / index.js
Created May 17, 2022 21:06
focus-soft-lock
import React, { useEffect, useRef } from 'react';
// Credits to
// https://github.com/theKashey/focus-lock/blob/33f8b4bd9675d2605b15e2e4015b77fe35fbd6d0/src/utils/tabbables.ts
const tabbables = [
'button:enabled',
'select:enabled',
'textarea:enabled',
'input:enabled',
@just-boris
just-boris / slice-items.js
Created August 10, 2021 21:52
slice a list
function sliceItems(items, limit) {
if (items.length <= limit + 1) {
return { visible: items, hidden: [] };
}
return {
visible: items.slice(0, limit),
hidden: items.slice(limit)
};
}
@just-boris
just-boris / web-components-trade-offs.md
Last active June 9, 2023 20:03
Web Components trade-offs

Web Components trade-offs

Desired state

Before we begin talking about the trade-offs, let's look at the desired state, why someone should use Web Components and what benefits it provides. This standard allows you to create framework-independent UI components. Instead of re-inventing the same concept of UI component for every framework, there could be a universal solution using Web Components standard. They will also be more simple and lightweight, as the API is already built into browsers and you do not need to load additional runtime to your web page.

Web Components are defined as custom HTML elements where you can attach your custom behavior. You do not need to learn additional proprietary framework conventions, as you can think about using Web Component same way as you would use <button> or <input>.

Now let's check how these expectations match with the real state.

import { useCallback, useEffect, useRef } from 'react';
/**
* A callback that stays stable between renders even as the dependencies change.
*
* @see https://reactjs.org/docs/hooks-faq.html#how-to-read-an-often-changing-value-from-usecallback
*/
export function useStableEventHandler<T extends (...args: any[]) => any>(fn: T): T {
const ref = useRef<T>();
@just-boris
just-boris / demo.ts
Created October 13, 2020 09:05
Nested weak map
import { NestedWeakMap } from './nested-weak-map';
const key1 = {};
const key2 = {};
const map = new NestedWeakMap();
map.get([key1, key2]); // undefined
map.getOrCreate([key1, key2], () => 'default'); // "default"
map.get([key1, key2]); // "default"
import path from 'path';
function shouldApply(url) {
return path.extname(url) === '.css';
}
export function getFormat(url, context, defaultGetFormat) {
if (shouldApply(url)) {
return {
format: 'module',
@just-boris
just-boris / debounce.js
Created May 31, 2020 12:16
small debounce implementation without lodash
export const DEBOUNCE_DEFAULT_DELAY = 200;
export default function debounce(func, delay = DEBOUNCE_DEFAULT_DELAY) {
let timeout;
return function(...args) {
if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(() => {
@just-boris
just-boris / README.md
Last active May 13, 2022 11:16
Use lint-staged to check file sizes

File size checker

Prevents you from accidentially commenting very big assets. Make sure that you have optimized all your assets.

Installation

  1. Add the script to your repo
  2. Configure lint-staged