michaelbaudino / stimulus-popover

A Stimulus controller to deal with HTML popover.

Home Page:https://stimulus-popover.netlify.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Stimulus Popover

Netlify Status

Getting started

A Stimulus controller to deal with HTML popover.

This controller is using Tippy behind the scene.

Installation

$ yarn add stimulus-popover

And use it in your JS file:

import { Application } from "stimulus"
import Popover from "stimulus-popover"

const application = Application.start()
application.register("popover", Popover)

Usage

In your controller:

class UsersController < ApplicationController
  def card
    render partial: 'users/card', locals: { user: @user }
  end
end

In your routes:

Rails.application.routes.draw do
ressources
  get :card, to: 'users#card'
end

In your view:

With server rendered content on the fly:

<div>
  You can load popover with AJAX. For instance, this is my
  <a
    href="/profile"
    data-controller="popover"
    data-action="mouseover->popover#mouseOver"
    data-popover-url="<%= card_path %>"
  >
    profile card
  </a>
</div>

With static html:

<div data-controller="popover">
  This is my Github card available on
  <a
    href="/profile"
    data-action="mouseover->popover#mouseOver"
  >
    Github
  </a>

  <template data-target="popover.content">
    <p>This is the popover content.</p>
  </template>
</div>

Configuration

Attribute Default Description Optional
data-popover-url undefined URL to fetch the content.

Extending Controller

You can use inheritance to extend the functionality of any Stimulus components.

import Popover from "stimulus-popover"

export default class extends Popover {
  connect() {
    super.connect()
    console.log("Do what you want here.")
  }

  // You can override this getter if needed.
  // The object returned is the props for the Tippy instance.
  get tippyOptions () {
    return {
      // Your options here
    }
  }
}

This controller will automatically has access to targets defined in the parent class.

If you override the connect, disconnect or any other methods from the parent, you'll want to call super.method() to make sure the parent functionality is executed.

Development

Project setup

$ yarn install
$ yarn dev

Linter

Prettier and ESLint are responsible to lint and format this component:

$ yarn lint
$ yarn format

Contributing

Do not hesitate to contribute to the project by adapting or adding features ! Bug reports or pull requests are welcome.

License

This project is released under the MIT license.

About

A Stimulus controller to deal with HTML popover.

https://stimulus-popover.netlify.com

License:MIT License


Languages

Language:HTML 78.5%Language:JavaScript 21.5%