0.0.41 • Published 18 days ago

next-popover v0.0.41

Weekly downloads
-
License
MIT
Repository
github
Last release
18 days ago

next-popover

Header Image

Next-Popover is a lightweight and simple popover, tooltip, dropdown library, with no other dependencies, and Typescript friendly.

https://vywrrk-3000.csb.app/

Demo

中文文档

Install

npm i next-popover

or

yarn add next-popover

or

pnpm add next-popover

or via CDN

<script src="https://unpkg.com/next-popover@latest/dist/popover.umd.js"></script>
<script>
  const { NextPopover } = window;
  const { PlacementType, EmitType } = NextPopover;
  // use `NextPopover.default`
  new NextPopover.default({
    // config
  });
</script>

Usage

import Popover, { PlacementType, EmitType } from 'next-popover'

const trigger = document.querySelector('.trigger'); 

const content = "Hello Next-Popover";
// or
// const content = document.createElement('div'); // You need to pop up the displayed content
// content.classList.add('content');
// content.innerHTML = "Hello Next-Popover";

const appendTo = document.querySelector('.mount-container'); // default: document.body

const popover = new Popover({
  trigger, // required
  content, // required
  appendTo,
  placement: "top", // Set the position of the popover
  emit: "hover" // Set to open the popover when the mouse hovers over the trigger
});

trigger.onclick = () => {
  popover.toggle();
  // or
  // if (popover.opened) {
  //   popover.close();
  // } else {
  //   popover.open();
  // }
}

// if you don't need it anymore
popover.destroy();

CSS Animation

The animationClass parameter allows you to add CSS animations when showing and hiding the popover.

const popover = new Popover({
  animationClass: 'fade'
});

Popover will add the following 6 classes through the animationClass.

`${animationClass}-enter-from` // Starts displaying and is removed in the next frame.
`${animationClass}-enter-active` // Added in the next frame and removed when the animation ends.
`${animationClass}-enter-to` // Added in the next frame and removed when the animation ends.
`${animationClass}-exit-from` // Starts hiding and is removed in the next frame.
`${animationClass}-exit-active` // Added in the next frame and removed when the animation ends.
`${animationClass}-exit-to` // Added in the next frame and removed when the animation ends.
`${animationClass}-${placement}` // Current popover placement

You can write CSS styles like this:

.fade-enter-from,
.fade-exit-to {
  transform: scale(.7);
  opacity: 0;
}
.fade-enter-active,
.fade-exit-active {
  transition: transform .1s ease, opacity .1s ease;
}

Scroll

The closeOnScroll parameter controls whether the popover automatically closes when the trigger element is scrolled.

Hook

Popover provides rich hook functions that can execute code during various stages of the popover's lifecycle.

new Popover({
  onBeforeEnter() {
    // Executed before the CSS display animation starts.
  },
  onEntered() {
    // Executed after the CSS display animation completes.
  },
  onBeforeExit() {
    // Executed before the CSS hide animation starts.
  },
  onExited() {
    // Executed after the CSS hide animation completes.
  },
  onOpen() {
    // Executed when the popover is displayed.
  },
  onClose() {
    // Executed when the popover is closed.
  }
});

API

Config

NameTypeDefaultDescription
triggerHTMLElementRequired. The trigger element
contentHTMLElement \| string \| numberRequired. The content element to be popped up
appendToHTMLElementdocument.bodyThe element to append the popover to.
placementtop left right bottom top-left top-right bottom-left bottom-right left-top left-bottom right-top right-bottomtopThe placement of the popover.
showArrowBooleantrueWhether to show arrow
emitclick or hoverclickTrigger emit type
openbooleanWhether to open the popover box by default
openDelaynumber100Open delay
closeDelaynumber100Close delay
offsetnumber8Popover offset
enterablebooleantrueWhen emit is set to hover, can the mouse enter the popover
disabledbooleanDisabled
clickOutsideClosebooleantrueAutomatically close the popover when clicking outside
closeOnScrollbooleanWhether to automatically close the popover when the trigger element is scrolled.
triggerOpenClassstringThe class added to the trigger when the popover is opened.
wrapperClassstringThe class added to the popoverWrapper.
animationClassstringThe CSS animation class name.
onBeforeEnter() => voidCalled before the CSS enter animation starts.
onEntered() => voidCalled when the CSS enter animation ends.
onBeforeExit() => voidCalled before the CSS exit animation starts.
onExited() => voidCalled when the CSS exit animation ends.
onOpen() => voidCalled when the popover is opened.
onClose() => voidCalled when the popover is closed.

Instance properties

NameTypeDescription
configPopoverConfigPopover configuration object
popoverRootHTMLElementThe popover root element
popoverWrapperHTMLElementThe popover wrapper element
popoverContentHTMLElementThe popover Content element
arrowElementHTMLElementThe popover arrow element
openedbooleanIndicates whether the popover is currently displayed

Methods

open()

Open the Popover instance.

open(): void;

close()

Close the Popover instance.

close(): void;

toggle()

Toggle the Popover instance open or close.

toggle(): void;

openWithDelay()

Open the popover after config.openDelay time.

openWithDelay(): void;

closeWithDelay()

Close the popover after config.closeDelay time.

closeWithDelay(): void;

enable()

Enable.

enable(): void

disable()

Disable and close popover.

disable(): void

updateConfig()

Update config.

updateConfig(config: Partial<PopoverConfig>): void;

destroy()

Destroy the Popover instance.

destroy(): void;

onScroll()

Manually trigger the onScroll event.

onScroll(): void;

update()

Manually update the position of the Popover instance.

update(): void;
0.0.41

18 days ago

0.0.40

18 days ago

0.0.38

19 days ago

0.0.39

18 days ago

0.0.37

21 days ago

0.0.36

21 days ago

0.0.34

22 days ago

0.0.35

22 days ago

0.0.32

28 days ago

0.0.33

28 days ago

0.0.30

29 days ago

0.0.31

29 days ago

0.0.29

29 days ago

0.0.26

5 months ago

0.0.27

5 months ago

0.0.28

5 months ago

0.0.24

5 months ago

0.0.25

5 months ago

0.0.20

5 months ago

0.0.21

5 months ago

0.0.23

5 months ago

0.0.15

6 months ago

0.0.16

5 months ago

0.0.17

5 months ago

0.0.18

5 months ago

0.0.19

5 months ago

0.0.13

6 months ago

0.0.14

6 months ago

0.0.10

8 months ago

0.0.11

8 months ago

0.0.12

8 months ago

0.0.9

8 months ago

0.0.8

8 months ago

0.0.7

8 months ago

0.0.6

10 months ago

0.0.5

10 months ago

0.0.4

10 months ago

0.0.3

10 months ago

0.0.2

10 months ago

0.0.1

10 months ago

0.0.1-alpha.1

11 months ago