npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cable_modal

v0.1.5

Published

Modal form workflows, powered by CableReady operations and 🧡

Downloads

2

Readme

CableModal

This plugin facilitates creating server-rendered modal workflows in Rails using CableReady custom operations. A plugin system is provided to allow you to use your modal provider of choice. A plugin for Bootstrap 5 is provided and used in these examples.

The plugin provides a <cable-modal> web component and a set of custom CableReady operations for controlling it. Once the <cable-modal> element is on the page, you can control it with the following operations:

  • openModal()
  • closeModal()
  • updateModal({html: "HTML string for modal content"})

Demo screencast

Installation

First install the gem

$ bundle add cable_modal

Next install the cable_modal NPM package (+ cable_ready if you don't have it already)

$ yarn add cable_modal cable_ready

Finally, run the generator

$ bin/rails g cable_modal:install

The generator does three things:

  1. installs a <cable-modal> web component into your application.html.erb layout
  2. installs a cable_modal.html.erb template into app/views/layouts. Use this layout to assist with rendering modal content.
  3. adds intialization code to application.js

Usage with Mrujs / CableCar

This gem adds 3 custom operations you can use anywhere you use CableReady:

  • openModal
  • closeModal
  • updateModal

A great way to to use these to control the custom modal is with mrujs and the cable_car feature added in CableReady 5. Here's an example.

First you'll want to set up mrujs with the CableCar plugin. Follow the instructions in the mrujs docs.

Now you can add data-remote to any links or forms you want to use to control the modal.

  <a href="/confirmations/new" data-remote>Open Confirmation in Modal</a>

  <!-- OR -->

  <form action="/confirmations" data-remote>
    <button>Submit form and process result in modal</button>
  </form>

Then in your controllers, process the request and use render operations: to send CableReady operations back to the client.

  # confirmations_controller.rb

  def new
    @confirmation = Confirmation.new
    render operations: cable_car
      .update_modal(
        html: self.class.render(
          template: "confirmations/new",
          assigns: {confirmation: @confirmation},
          layout: "cable_modal",
        ))
      .open_modal
  end

  def create
    @confirmation = Confirmation.new(confirmation_params)
    if @confirmation.save
      render operations: cable_car.close_modal
    else
      render operations: cable_car.update_modal(
        html: self.class.render(
          template: "confirmations/new",
          assigns: {confirmation: @confirmation},
          layout: "cable_modal",
        ))
    end
  end

There's a full reference implementation in /test/dummy of this repo.

Customization

If you don't want to use Bootstrap's modals, you can write your own plugin and then pass it to CableModal.use(plugin) in your javascript initializion.

Plugins are plain javascript objects with the following properties:

  {
    connect() {}, // runs when the <cable-modal> component is added
    disconnect() {}, // runs when the <cable-modal> component is removed
    openModal: (operation) -> {}, // open the modal
    closeModal: (operation) -> {}, // close the modal
    updateModal: (operation) -> {}, // update the modal's content
    defaultContent: string // default innerHTML of the <cable-modal> component
  }

Note that all functions will run bound to the <cable-modal> DOM node. You can access the original plugin object inside these bound functions by calling this.plugin.

Contributing

Issues and pull requests are welcome.

License

The gem is available as open source under the terms of the MIT License.