Skip to content

Instantly share code, notes, and snippets.

@romaricpascal
romaricpascal / aria_disabled_controller.js
Created June 28, 2022 15:44
Stimulus controller and helper functions for handling disabled elements via `aria-disabled`
import { Controller } from 'stimulus';
/**
* Help manage disabling of buttons through the `aria-disabled`
* attribute, rather than the harsher `disabled` attribute.
* Set on the `<html>` or `<body>` element, and get:
* - automatic cancellation of `click` events from targets
* with or within an`aria-disabled` element
* - two actions (`cancelIfDisabled`, `cancelUnlessDisabled`)
* to prevent events and stop their propagation if or unless
@romaricpascal
romaricpascal / aria_disabled.js
Created June 28, 2022 14:29
Soft disabling using `aria-disabled` rather than `disabled` attribute
/**
* Helper functions for managing button disabling through `aria-disabled`
* rather than the `disabled` attribute, which is less accessible.
* Includes functions for setting the disabled state,
* testing whether an element is disabled and a listener for preventing events
* @module aria_disabled
*/
/**
* @param {HTMLElement} element
@romaricpascal
romaricpascal / index.js
Created June 14, 2022 18:02
Puppeprint
import express from 'express';
import puppeteer from "puppeteer";
const app = express();
const port = process.env.PORT || 8080;
async function makePDF(url) {
console.log('Creating PDF', url);
const browser = await puppeteer.launch({
@romaricpascal
romaricpascal / reset.js
Last active June 6, 2022 13:14
Reset form element
/**
* Resets the value of a given form `element` to that originally set in the HTML.
* Allows for more targetted reset that a `form.reset()` as neither HTMLFieldsetElement
* nor the specific form controls API have a `reset()` method.
*
* Form elements can be accessed through:
* - `fieldset.elements` if your elements are wrapped within a fieldset
* - `ancestorElement.querySelectorAll('input,textarea,select')`
* - `form.elements` if you need specific filtering over the form fields
*
@romaricpascal
romaricpascal / keyboardEventHandler.js
Created April 1, 2022 14:15
Map keyboard events to `on{event.code}` methods
/**
* Creates an event handler for keyboard events
* that looks up the actual method to execute
* in a hash of methods using the event's `code`:
*
* ```js
* element.addEventListener(keydown, keyboardEventHandler({
* onArrowUp(event) {},
* onArrowDown(event) {},
* ...
@romaricpascal
romaricpascal / input.scss
Created December 7, 2020 18:36
Generated by SassMeister.com.
.parent {
color: red;
}
.child {
@extend .parent;
}
.container {
.parent {
@romaricpascal
romaricpascal / 4042302.php
Created February 26, 2020 17:26
4042302 in PHP
<?php
// Ideally read those 4 settings from environment variables
// with `getenv` to make the script more portable. Only one
// of the `$error_...` needs to have a value.
// The base_url to which the path will be appended
// when checking for a fallback. No trailing slash.
$base_url = "https://example.com";
// A URL to redirect to if the fallback didn't serve the page
@romaricpascal
romaricpascal / send.js
Created March 5, 2019 13:43
Get a Promise out of an XHR
function send(
url,
{ method = 'GET', data, headers = {}, user, password, ...opts } = {}
) {
const xhr = new XMLHttpRequest();
const promise = new Promise((resolve, reject) => {
xhr.open(method, url, true, user, password);
xhr.addEventListener('load', resolve);
xhr.addEventListener('error', reject);
xhr.addEventListener('abort', reject);
@romaricpascal
romaricpascal / ios-scrollable-mixin.scss
Last active July 12, 2018 09:36
Sass mixin so overlay scrolls instead of the content bellow on iOS
@mixin scrollable-overlay {
overflow: auto;
// iOS Specific tweaks
// See for hack explanation: http://browserbu.gs/css-hacks/webkit-full-page-media/
_::-webkit-full-page-media, & {
overflow-y: scroll;
-webkit-overflow-scrolling: touch;
@romaricpascal
romaricpascal / import-yesterdays-log.sh
Created March 22, 2017 11:54
Import OVH shared logs into Piwik
#! /usr/bin/env bash
cd "$(dirname "${BASH_SOURCE[0]}")"
./ovh-download-logs.sh `date --date=yesterday +"%Y-%m-%d"` | zcat | ./piwik-import.sh