Skip to content

Instantly share code, notes, and snippets.

View dutchcelt's full-sized avatar

Egor Kloos dutchcelt

View GitHub Profile
@dutchcelt
dutchcelt / getElementsByAttribute.js
Last active February 11, 2024 20:33
A function to get elements by Attribute name
const getElementsByAttribute = (target, attr) => {
const nodeArray = [];
const considerNode = node => node.hasAttribute(attr) ? 1 : 3;
const treeWalker = document.createTreeWalker(target, 1, { acceptNode: considerNode }, false);
while(treeWalker.nextNode()) nodeArray.push(treeWalker.currentNode);
return nodeArray
};
@dutchcelt
dutchcelt / line-clamp.css
Created May 26, 2020 07:43
Truncate multilined content
.line-clamp {
display: -webkit-box;
-webkit-line-clamp: 3;
webkit-box-orient: vertical;
overflow: hidden;
}
@dutchcelt
dutchcelt / sr-only.css
Created July 18, 2022 07:24
Screen Reader Only
.sr-only {
border: 0 !important;
clip: rect(1px, 1px, 1px, 1px) !important;
-webkit-clip-path: inset(50%) !important;
clip-path: inset(50%) !important;
height: 1px !important;
overflow: hidden !important;
margin: -1px !important;
padding: 0 !important;
position: absolute !important;
@dutchcelt
dutchcelt / grid-equal-col-widths.css
Last active July 19, 2022 09:26
grid-equal-col-widths
/* `1fr` in grid templates allows for content to be wider than the fraction to prevent content overflow.
* In this context `1fr` is actually: `minmax(min-content, 1fr)`.
* Replacing `min-content` with `0` forces the fraction to be upheld.
*/
.columns {
--_cols: var(--cols, 3);
display: grid;
grid-template-columns: repeat(var(--_cols), minmax(0, 1fr));
}
@dutchcelt
dutchcelt / createElement-shortcut.js
Last active February 11, 2024 20:33
Use Object.assign to create new elements in vanilla JavaScript land.
/* https://twitter.com/jh3yy/status/1564926775620243456
* This 👇 */
const ELEMENT = Object.assign(
document.createElement('button'),
{
className: 'fab',
popUp: 'manual',
style: '--index: 1;',
innerHTML: `
@dutchcelt
dutchcelt / breakoutwidth.css
Last active April 25, 2023 13:01
Force nested element to break out and use the width of the viewport
:host {
container: host / inline-size;
display: flex;
/* force host to be the width of the viewport */
width: 100vw;
margin-left: 50%;
transform: translateX(-50%);
}
@dutchcelt
dutchcelt / chevron_breadcrumb_item.css
Created July 30, 2023 08:05
A breadcrumb list items shaped as an arrow bar.
/*
https://verpex.com/blog/website-tips/how-to-create-breadcrumb-navigation-with-css
*/
.breadcrumb {
--s: 15px;
}
.breadcrumb ol li {
clip-path: polygon(0 0,calc(100% - var(--s)) 0,100% 50%,calc(100% - var(--s)) 100%,0 100%,var(--s) 50%);
}
@dutchcelt
dutchcelt / camel-Pascal-Case.js
Created October 26, 2023 13:02
camelCase and PascalCase
/**
* pascalcase
* @param {string} s
*/
const pascalcase = s => {
const wordList = s.split(/-|_|\./);
let str = '';
for (const word of wordList) {
str += word.charAt(0).toUpperCase() + word.slice(1);
}
@dutchcelt
dutchcelt / clamp.js
Created December 7, 2023 16:17
Clamp number
/**
* clamp
* @param {number} num
* @param {number} min
* @param {number} max
* @returns {number}
*/
const clamp = (num, min, max) => Math.min(Math.max(num, min), max);
@dutchcelt
dutchcelt / convertToTShirtSize.js
Last active February 28, 2024 07:17
Convert a step value to a corresponding T-shirt size
/**
* Converts a step value to a corresponding T-shirt size.
* The default value '0' is equal to 'm'.
* @param {number} step - The step value to convert into a T-shirt size.
* @returns {string} The T-shirt size corresponding to the given step value.
*/
const convertToTShirtSize = (step = 0) => {
const times = Math.max(0, Math.abs(step) - 1);
const index = Math.sign(step) + 1;
return 'x'.repeat(times) + ['s','m','l'][index];