Giter Club home page Giter Club logo

popup-trigger's Introduction

popup-trigger

A utility for triggering and closing popups.

Works great for tooltips, popup menus, dropdown selects, etc.

Available in two flavors:

How it works

Say you have a trigger element, and a popup.

<button>Trigger</button>
<div>Popup</div>

You only want the popup to appear if the trigger is focused or selected -- or when the popup itself has focus.

This utility handles this for you by adding events to the trigger and popup nodes, and exposing an active variable which you can use to switch the popup's visibility:

<button ref={trigger.ref}>Trigger</button>
{
  trigger.active &&
  <div ref={trigger.popupRef}>
    Popup
  </div>
}

React Hook

The simplest way to use this tool is with a React hook.

import usePopupTrigger from 'popup-trigger/hook'

function MyComponent() {
  let trigger = usePopupTrigger({
    triggerOnFocus: true,
    triggerOnHover: true,
    triggerOnSelect: true, // Pop on touch/click the trigger, or
                           // on enter/space while focused.
  })

  return (
    <>
      <button ref={trigger.ref}>Trigger!</button>
      {
        trigger.active &&
        <div ref={trigger.popupRef}>
          <a href="https://frontarm.com"></a>
        </div>
      }
    </>
  )
}

Combine with react-popper and portals for all your popup needs!

Vanilla JS

Internally, everything is contained within a vanilla JavaScript class.

import PopupTrigger from 'popup-trigger'

let trigger = new PopupTrigger({
  triggerOnFocus: true,
  triggerOnHover: true,
  triggerOnSelect: true, // Pop on touch/click the trigger, or
                          // on enter/space while focused.
})

trigger.setTriggerNode(/* ... */)
trigger.setPopupNode(/* ... */)

trigger.getState() // { active, focused, hovering, selected }
trigger.subscribe(({ active, focused, hovering, selected ) => {})
trigger.dispose() // Clean up afterwards

trigger.close() // Close the popup imperatively

popup-trigger's People

Contributors

jamesknelson avatar

Stargazers

Arun  avatar Sara Tasche avatar  avatar Timm Stokke avatar timelyportfolio avatar Mihai Cernusca avatar  avatar  avatar Mesut Koca avatar Peter MacDonald avatar gaoryrt avatar HAKASHUN avatar  avatar Idan Wender avatar Anton Van Eechaute avatar

Watchers

timelyportfolio avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.