Skip to content

Instantly share code, notes, and snippets.

View elfacht's full-sized avatar
🥰
Love code, hate racism

Martin Szymanski elfacht

🥰
Love code, hate racism
View GitHub Profile
@elfacht
elfacht / if-to-object.js
Created January 5, 2022 10:57
if to object
// ❌ Before
function getErrorMessage(error) {
if(error === 'username') {
return `Username is required`
} else if (error === 'email') {
return `Don't forget your email!`
} else if (error === 'password') {
return `Please enter your password`
}
}
@elfacht
elfacht / disable-mousover.js
Created January 5, 2022 10:48
Disable mouseover while scrolling
const body = document.body;
let timer;
window.addEventListener('scroll', function() {
clearTimeout(timer);
if (!body.classList.contains('disable-hover')) {
body.classList.add('disable-hover')
}
timer = setTimeout(function(){
@elfacht
elfacht / window-resize-animation-stopper.js
Created January 5, 2022 10:44
Window Resize Animation Stopper
/**
* Stop animations and transitions
* on window resize
*
* @type {Function}
*/
let resizeTimer;
window.addEventListener('resize', () => {
document.body.classList.add('resize-animation-stopper');
clearTimeout(resizeTimer);
@elfacht
elfacht / debug-headings.css
Created January 5, 2022 10:43
Debug Out-of-order Headings in CSS
/* Headers out of order (i.e. h2 before h1, etc.)
Result: dotted blue outline
*/
h2 ~ h1,
h3 ~ h1,
h4 ~ h1,
h5 ~ h1,
h6 ~ h1,
h3 ~ h2,
h4 ~ h2,
@elfacht
elfacht / min-max-clamp.scss
Created January 5, 2022 10:41
min(), max(), clamp()
// min() / max()
.element{
// old
width: 50vw;
max-width: 500px;
// new
width: min(50vw, 500px);
}
@elfacht
elfacht / user-preferences.css
Last active January 5, 2022 10:42
User Preference Media Features in CSS
/*
@source
https://drafts.csswg.org/mediaqueries-5/#mf-user-preferences
https://css-tricks.com/weekly-platform-news-reduced-motion-cors-whitehouse-gov-popups-and-100vw/
*/
/* contrast */
@media (prefers-contrast: no-preference|less|more) {
/* … */
}
@elfacht
elfacht / svg-data-uri.scss
Created January 5, 2022 10:37
Convert SVG to data-uri in SCSS
// choose a color
$icon-color: #F84830;
// functions to urlencode the svg string
@function str-replace($string, $search, $replace: '') {
$index: str-index($string, $search);
@if $index {
@return str-slice($string, 1, $index - 1) + $replace + str-replace(str-slice($string, $index + str-length($search)), $search, $replace);
}
@return $string;
@elfacht
elfacht / base.js
Created January 5, 2022 09:53
Handlebars Helper
/**
* Returns the base path of the environment
*
* @type {Function}
* @return {String}
*/
var path = require('path');
module.exports = function (context) {
return path.dirname(path.relative(context.data.file.history.toString(), context.data.file.base.toString())) + '/';
@elfacht
elfacht / craft-rss.twig
Created January 5, 2022 09:47
Craft CMS RSS Template #craftcms
{% header "Content-Type: application/rss+xml" %}
{% if not craft.app.config.general.membersOnly %}
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
@elfacht
elfacht / related-entries.twig
Created January 5, 2022 09:44
Related Entries in Craft CMS #craftcms