Skip to content

Instantly share code, notes, and snippets.

@bmorrall
bmorrall / use-escape-pressed.js
Last active November 26, 2022 04:34
Register a Stimulus Controller to listen to any escape key presses.
// Usage:
//
// import { Controller } from 'stimulus'
// import { useEscapePressed } from './mixins/use-escape-pressed.js
//
// export default class extends Controller {
// connect () {
// useEscapePressed(this)
// }
//
@bmorrall
bmorrall / use-trap-focus.js
Last active November 26, 2022 04:16
Prevent a user from tabbing outside of an Stimulus Controller element (Useful for Modals and Dialogs).
// Trap Focus
// https://hiddedevries.nl/en/blog/2017-01-29-using-javascript-to-trap-focus-in-an-element
//
// Usage:
//
// import { Controller } from 'stimulus'
// import { useTrapFocus } from './mixins/use-trap-focus.js
//
// export default class extends Controller {
@bmorrall
bmorrall / update_rubocop_config.sh
Created September 13, 2017 04:28
Updates Rubocop to the latest version, and populates the .rubocop_todo.yml with all all the exceptions in the codebase.
#!/bin/bash
# Bumps Rubocop, removes all fixed Cops from the list of exceptions, and adds any cops that haven't been fixed
# Run after some code refactoring, or periodically whenever rubocop is updated
bundle update rubocop rubocop-rspec
echo "" > .rubocop_todo.yml
bundle exec rubocop --auto-gen-config --exclude-limit 50
@bmorrall
bmorrall / rfc822.rb
Created July 10, 2012 01:07
RFC822 Email Validation Regex in Ruby
# RFC822 Email Address Regex
# -———————————-
# Originally written by Cal Henderson
# c.f. http://iamcal.com/publish/articles/php/parsing_email/
#
# Translated to Ruby by Tim Fletcher, with changes suggested by Dan Kubb.
#
# Licensed under a Creative Commons Attribution-ShareAlike 2.5 License
# http://creativecommons.org/licenses/by-sa/2.5/
module RFC822