Popper

Floating overlay component with flexible positioning and interaction modes

Import
the javascript logo
import { Popper } from "@schukai/monster/source/components/layout/popper.mjs";
Source
the git logo
Package
the npm logo
Since
1.65.0
Hover me!
Should I Stay or Should I Go?

Introduction

This is a versatile web component. More than just a simple UI element, this component serves as a key element for enhancing interactivity and engagement within various web applications. Whether you are developing a basic website or a complex enterprise-level application, this component is designed to improve user interaction and satisfaction.

Key Features

  • Dynamic interaction: Users can interact with content dynamically, making the web experience more intuitive and user-centric.
  • Customizable appearance: Customize the appearance of the component to match the design of your brand or application, ensuring visual consistency.
  • Accessibility: Designed with accessibility in mind to ensure that all users have a seamless experience, regardless of their browsing environment.
  • Visual feedback effect: Provides subtle touch feedback to enhance user interaction, making actions more responsive and engaging.
  • Programmatic Control: Offers methods such as trigger, focus, and deactivate, allowing developers to control the component's behavior programmatically and offering greater flexibility.

Improving the user experience

This component goes beyond its traditional functionality to offer an enhanced and interactive user experience. The combination of visual feedback effects and dynamic interaction with content is not only visually appealing, but also provides clear and responsive feedback to user actions, making the web environment more intuitive and user-friendly.

These improvements are supported by user studies that demonstrate a positive impact on user engagement and satisfaction.

Efficiency in the development process

Integrating this component into your development process is straightforward. Its compatibility with standard web technologies and ease of customization ensure seamless integration with your existing tools and libraries. Whether you're working on a small project or a large-scale application, the modular design of this component guarantees easy integration, streamlining your development process and enhancing productivity.

Popper Simple

This is a simple popper example. It is a popper with the text "Should I Stay or Should I Go?". Nothing more, nothing less.

Hover me!
Should I Stay or Should I Go?

Javascript

import "@schukai/monster/source/components/layout/popper.mjs";

HTML

<monster-popper>
    <div slot="button"
         style="border:1px solid var(--monster-color-primary-2); padding: 10px; cursor: pointer;width: 100%; display:flex;align-items:center;justify-content:center;">
        Hover me!
    </div>
    Should I Stay or Should I Go?
</monster-popper>

Stylesheet

/** no additional stylesheet is defined **/

Popper Click

This is a popper example, which is triggered by a click. It is a popper with the text "Should I Stay or Should I Go?". The attribute data-monster-option-mode="click" makes the trick. The popper will be shown when the button is clicked.

Click me!
Should I Stay or Should I Go?

Javascript

import "@schukai/monster/source/components/layout/popper.mjs";

HTML

<monster-popper data-monster-option-mode="click">
    <div slot="button"
         style="border:1px solid var(--monster-color-primary-2); padding: 10px; cursor: pointer;width: 100%; display:flex;align-items:center;justify-content:center;">
        Click me!
    </div>
    Should I Stay or Should I Go?
</monster-popper>

Stylesheet

/** no additional stylesheet is defined **/

Component Design

The Monster Popper component is a floating UI element that can be toggled via various interaction modes such as click, hover, focus, or programmatically. Built using the Shadow DOM, it provides reliable encapsulation for styling and behavior, ensuring no conflicts with external code. It supports dynamic positioning using middleware for flexible placement.

Shadow DOM and Accessibility

The Monster Popper component leverages the Shadow DOM to isolate its internal structure. Accessibility is achieved through ARIA roles, focus management, and keyboard interaction support. Users relying on assistive technologies can interact seamlessly with the popper.

Customizing Through Exported Parts

The Monster Popper component exposes specific exported parts for precise styling using CSS. These parts allow developers to customize the popper's container, button, arrow, and content area.

Available Part Attributes

  • control: Represents the entire control container, including the button and popper content.
  • popper: Targets the floating popper element.
  • button: Refers to the button or trigger element used to toggle the popper.
  • arrow: Styles the directional arrow of the popper.
  • content: Targets the main content area within the popper.

Below is an example of how to style the popper and its arrow using CSS part attributes:


monster-popper::part(control) {
    display: inline-block;
    position: relative;
}

monster-popper::part(button) {
    background-color: #007bff;
    color: #fff;
    padding: 10px 16px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    transition: background-color 0.3s ease;
}

monster-popper::part(popper) {
    background-color: #ffffff;
    border: 1px solid #ddd;
    border-radius: 6px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    padding: 12px;
    z-index: 1000;
}

monster-popper::part(arrow) {
    width: 12px;
    height: 12px;
    background-color: #ffffff;
    border: 1px solid #ddd;
    transform: rotate(45deg);
    position: absolute;
}

Explanation of the Example

  • monster-popper::part(control): Styles the main control container with relative positioning.
  • monster-popper::part(button): Customizes the button with background, padding, and hover effects.
  • monster-popper::part(popper): Styles the popper element with a white background, border, and box shadow.
  • monster-popper::part(arrow): Styles the arrow with a rotated square design for visual indication.

Interaction Modes

The Monster Popper supports various interaction modes, which can be specified using the mode option:

  • click: Toggles the popper on click.
  • focus: Opens the popper when the button is focused and closes on blur.
  • enter: Shows the popper when the mouse enters the button.
  • auto: Opens on hover (mouseenter) and closes on mouse leave.
  • manual: The popper is controlled programmatically via methods.

Methods

The Monster Popper provides the following methods for programmatic control:

MethodDescription
showDialog()Displays the popper.
hideDialog()Hides the popper.
toggleDialog()Toggles the visibility of the popper.

Accessibility

The Monster Popper ensures accessibility by:

  • Using proper ARIA roles for open and closed states.
  • Managing focus on the button or interactive elements.
  • Providing smooth keyboard navigation for toggling the popper.

These features ensure that the component is usable by all users, including those relying on screen readers and keyboard interactions.

HTML Structure

<monster-popper></monster-popper>

JavaScript Initialization

const element = document.createElement('monster-popper');
document.body.appendChild(element);

Exported

Popper

Derived from

CustomElement

Options

The Options listed in this section are defined directly within the class. This class is derived from several parent classes, including the CustomElement class. Therefore, it inherits Options from these parent classes. If you cannot find a specific Options in this list, we recommend consulting the documentation of the CustomElement.

Option
Type
Default
Description
templates
object
Template configuration
templates.main
string
undefined
Main template HTML
mode
string
auto focus
Interaction mode(s): click|enter|manual|focus|auto
content
string
<slot></slot>
Content template
popper
object
Positioning options
popper.placement
string
top
Placement: top|bottom|left|right
popper.middleware
array
autoPlacement, shift, offset:15, arrow
Positioning middleware functions
features
object
Feature flags
features.preventOpenEventSent
boolean
false
Prevent open event

  • since
  • deprecated

Properties and Attributes

The Properties and Attributes listed in this section are defined directly within the class. This class is derived from several parent classes, including the CustomElement class and ultimately from HTMLElement. Therefore, it inherits Properties and Attributes from these parent classes. If you cannot find a specific Properties and Attributes in this list, we recommend consulting the documentation of the CustomElement.

  • data-monster-options: Sets the configuration options for the collapse component when used as an HTML attribute.
  • data-monster-option-[name]: Sets the value of the configuration option [name] for the collapse component when used as an HTML attribute.

Methods

The methods listed in this section are defined directly within the class. This class is derived from several parent classes, including the CustomElement class and ultimately from HTMLElement. Therefore, it inherits methods from these parent classes. If you cannot find a specific method in this list, we recommend consulting the documentation of the CustomElement.

Behavioral methods

hideDialog()
Returns
  • {popper}: popper instance
Hides the popper element
showDialog()
Returns
  • {popper}: popper instance
Shows the popper element
toggleDialog()
Returns
  • {popper}: popper instance
Toggles popper visibility

Static methods

[instanceSymbol]()
Returns
  • {symbol}: instance type symbol
Gets the instance symbol for type checking
getCSSStyleSheet()
Returns
  • {cssstylesheet[]}: of stylesheets
Gets component stylesheets
getTag()
Returns
  • {string}: tag name
Gets the custom element tag name

Lifecycle methods

Lifecycle methods are called by the environment and are usually not intended to be called directly.

[assembleMethodSymbol]()
Initialize the component Called on first connection to DOM
connectedCallback()
Lifecycle callback when element connects to DOM Sets up event listeners and initializes popper
disconnectedCallback()
Lifecycle callback when element disconnects from DOM Cleans up event listeners and observers

Events

The component emits the following events:

  • monster-popper-hide
    Fired when popper starts hiding
  • monster-popper-hidden
    Fired when popper is fully hidden
  • monster-popper-open
    Fired when popper starts opening
  • monster-popper-opened
    Fired when popper is fully opened

For more information on how to handle events, see the mdn documentation.

The current width of the area is too small to display the content correctly.