Skip to content

Instantly share code, notes, and snippets.

View wojtekmaj's full-sized avatar

Wojciech Maj wojtekmaj

View GitHub Profile
@wojtekmaj
wojtekmaj / react-highlight-text-by-pattern.js
Last active March 23, 2024 18:37
Highlight text in React by providing text and RegExp pattern.
function highlightPattern(text, pattern) {
const splitText = text.split(pattern);
if (splitText.length <= 1) {
return text;
}
const matches = text.match(pattern);
return splitText.reduce((arr, element, index) => (matches[index] ? [
@wojtekmaj
wojtekmaj / readdirSyncDeep.rs
Last active April 11, 2024 13:06
Recursive fs.readdirSync
import fs from 'node:fs';
import path from 'node:path';
function readdirSyncDeep(dir: string, rootDir: string = dir) {
const files = fs.readdirSync(dir);
const filelist: string[] = [];
files.forEach((file) => {
const filePath = path.join(dir, file);
@wojtekmaj
wojtekmaj / find-scroll-container.js
Last active March 18, 2024 14:40
Find the nearest scrollable container.
const findScrollContainer = (element) => {
if (!element) {
return undefined;
}
let parent = element.parentElement;
while (parent) {
const { overflow } = window.getComputedStyle(parent);
if (overflow.split(' ').every(o => o === 'auto' || o === 'scroll')) {
return parent;
@wojtekmaj
wojtekmaj / merge-refs.js
Last active March 21, 2023 13:34
Merge multiple React refs to use them on a single React element.
/**
* Allows to use multiple refs on a single React element.
* Supports both functions and ref objects created using createRef() and useRef().
*
* Usage:
* ```jsx
* <div ref={mergeRefs(ref1, ref2, ref3)} />
* ```
*
* @param {...Array<Function|Object>} inputRefs Array of refs
@wojtekmaj
wojtekmaj / table-to-json.js
Last active December 20, 2022 09:53
Transform HTML table into JSON data.
/**
* Transforms HTML table into JSON data.
*
* Sample input:
*
* <table>
* <thead>
* <tr>
* <th>Heading 1</th>
* <th>Heading 2</th>
@wojtekmaj
wojtekmaj / text-outline.js
Last active October 14, 2019 15:51
Generate CSS text outline in styled-components.
function toPx(val) {
return typeof val === 'number' ? `${val}px` : val;
}
/**
* Creates a text outline of a given width and color.
*
* Sample usage in styled-components:
* ${props => textOutline('2px', props.color)}
*
@wojtekmaj
wojtekmaj / line-background.js
Last active October 14, 2019 15:51
Generate CSS line background in styled-components.
function toPx(val) {
return typeof val === 'number' ? `${val}px` : val;
}
/**
* Creates a background with a single line, e.g. for simulating underline.
*
* Sample usage in styled-components:
* ${props => lineBackground('to bottom', '2px', props.color)}
*
@wojtekmaj
wojtekmaj / round-svg-path.js
Last active May 2, 2021 19:52
Rounds down values in SVG path
function round(value, decimals) {
return Number(`${Math.round(`${value}e${decimals}`)}e-${decimals}`);
}
function roundPath(path, decimals = 3) {
function roundPathPoint(pathPoint) {
function roundPathPointElement(pathPointElement) {
if (pathPointElement.match(/^[A-Za-z]/)) {
return `${pathPointElement[0]}${pathPointElement.slice(1) && round(pathPointElement.slice(1), decimals)}`;
}
@wojtekmaj
wojtekmaj / details-polyfill.js
Last active January 24, 2020 11:16
Polyfill for the HTML <details> element, with support for onToggle event
if (!('open' in document.createElement('details'))) {
const DETAILS = 'details';
const SUMMARY = 'summary';
function injectStyle(style) {
const styleElement = document.createElement('style');
styleElement.innerHTML = style;
document.getElementsByTagName('head')[0].appendChild(styleElement);
}
@wojtekmaj
wojtekmaj / .gitignore
Last active May 22, 2024 20:35
How to upgrade Yarn to Yarn Modern (v4 at the moment) seamlessly
.pnp.*
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions