Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View leastbad's full-sized avatar
🤠
Keeping it real for the rest of us.

leastbad leastbad

🤠
Keeping it real for the rest of us.
View GitHub Profile
@leastbad
leastbad / application.js
Created April 21, 2021 17:14
CableReady + Notyf
// partial file
// you will need to `yarn add notyf`
import CableReady from 'cable_ready'
import { Notyf } from 'notyf'
import flash from '../shared/notyf'
CableReady.DOMOperations.toast = operation => {
new Notyf(flash).open(operation)
}
@leastbad
leastbad / remove_in_iframe.js
Created April 15, 2021 14:58
CableReady custom operations
import CableReady from 'cable_ready'
import { dispatch } from 'cable_ready/javascript/utils'
CableReady.DOMOperations.removeSubelement = operation => {
const { element, subElement } = operation
dispatch(element, 'cable-ready:before-remove-subelement', operation)
element.contentDocument.querySelector(subElement).remove()
dispatch(element, 'cable-ready:after-remove-subelement', operation)
}
@leastbad
leastbad / README.md
Last active November 11, 2022 17:27
stimulus-youtube preview

My goal with this was to wrap the terrible YouTube Embed API in a Stimulus controller that would allow me to access the underlying API while providing some convenience methods. One key outcome is that the controller emits youtube events which contain the current position in the video. This means that other code can now respond to the position you are at in the video.

<div data-controller="youtube" data-youtube-code-value="Lo_1pyQ7xvc">
  <button data-action="youtube#play">Play</button>
  <button data-action="youtube#pause">Pause</button>
  <button data-action="youtube#stop">Stop</button>
  <br>
  <div data-youtube-target="frame"></div>
@leastbad
leastbad / README.md
Last active April 15, 2021 22:30
Mapbox controller

Using Stimulus v2, FontAwesome 5, Bootstrap 5, and Mapbox

You can grab a copy of marker.svg here - put it in your /public folder.

@leastbad
leastbad / application_controller.rb
Created March 25, 2021 16:05
Notifications are fun (Devise, Bootstrap 5, Notyf, FontAwesome)
class ApplicationController < ActionController::Base
include CableReady::Broadcaster
include Toastable
end
@leastbad
leastbad / cleave_controller.js
Created March 18, 2021 22:10
Stimulus controller for cleave.js
import { Controller } from 'stimulus'
import Cleave from 'cleave.js'
import 'cleave.js/dist/addons/cleave-phone.us'
export default class extends Controller {
static values = { options: Object, digits: Number }
connect () {
this.cleave = new Cleave(this.element, this.optionsValue)
if (this.hasDigitsValue) {
@leastbad
leastbad / index.html.erb
Created March 16, 2021 19:11
Multi-row form serialization controller example
<div class="col-12 mb-4" data-controller="rows">
<%# these would usually be created by rendering a collection of partials: %>
<div id="row_1">
<form> <%# note that form is just a container %>
<input type="text" name="rows[first]" /> <%# note that I used the Rails models[attribute] form %>
<input type="text" name="rows[last]" />
<button data-reflex="click->Rows#update" data-id="1">Update</button> <%# click event For The Win %>
<button data-reflex="click->Rows#delete" data-id="1">Delete</button>
</form>
</div>
@leastbad
leastbad / registrations_controller.rb
Created March 2, 2021 01:56
Devise + Optimism integration
# frozen_string_literal: true
class Users::RegistrationsController < Devise::RegistrationsController
include Optimism
before_action :configure_permitted_parameters
def create
build_resource(sign_up_params)
resource.save
@leastbad
leastbad / example_reflex.rb
Last active January 10, 2022 09:53
slim-select Stimulus controller w/ Reflex callback
class UsersReflex < ApplicationReflex
def search(name)
users = User.where("name LIKE :prefix", prefix: "#{name}%")
result = users.map { |user| { text: user.name, value: user.id }}
cable_ready.dispatch_event(name: "data", detail: {options: result}).broadcast
morph :nothing
end
end
<div class="card">
<div class="card-header">
<h3 class="card-title">
<%= notification.to_notification.in_app_subject %>
</h3>
</div>
<div class="card-body">
<%= notification.to_notification.in_app_body %>
</div>
</div>