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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@yashrajbharti/tooltip-component

v1.0.1

Published

A modern, lightweight tooltip component built with Web Components and Shadow DOM

Downloads

3

Readme

Tooltip Web Component

A modern, lightweight tooltip component built with Web Components and Shadow DOM. Features CSS logical properties, individual transform properties, and automatic width sizing.

🚀 Live Demo on CodePen

📦 npm Package

Features

  • Modern CSS: Uses logical properties (inline-size, block-size, inset-*)
  • 🎯 Shadow DOM: Proper encapsulation with style isolation
  • 📱 Responsive: Automatic width based on content length
  • 🎨 Customizable: Multiple placement options and styling
  • 🚀 Zero Dependencies: Pure vanilla JavaScript
  • Accessible: Works with screen readers and keyboard navigation

Quick Start

<!DOCTYPE html>
<html>
<head>
    <script src="tooltip-component.js"></script>
</head>
<body>
    <tooltip-component tooltip="Hello World!" placement="top" arrow>
        <button>Hover me</button>
    </tooltip-component>
</body>
</html>

API Reference

Attributes

| Attribute | Type | Default | Description | |-----------|------|---------|-------------| | tooltip | string | "" | Required. The text content to display in the tooltip | | placement | string | "top" | Position of tooltip relative to trigger element | | delay | number | 150 | Show delay in milliseconds | | arrow | boolean | false | Whether to show directional arrow | | width | string | auto | Width mode (auto-determined by text length) | | open | boolean | true | Whether tooltip functionality is enabled |

Placement Options

| Value | Description | |-------|-------------| | top | Tooltip appears above the element | | bottom | Tooltip appears below the element | | left | Tooltip appears to the left of the element | | right | Tooltip appears to the right of the element |

Width Behavior

| Text Length | Width Mode | CSS Value | Description | |-------------|------------|-----------|-------------| | ≤ 50 chars | fit | fit-content | Tooltip sizes to content | | > 50 chars | fixed | 300px | Fixed width with word wrapping |

Delay Options

| Value | Description | |-------|-------------| | 0 | Instant appearance | | 150 | Default delay (recommended) | | 500 | Slower appearance | | 1000 | Very slow appearance |

Usage Examples

Basic Tooltip

<tooltip-component tooltip="Simple tooltip">
    <span>Hover me</span>
</tooltip-component>

With Arrow

<tooltip-component tooltip="Tooltip with arrow" placement="bottom" arrow>
    <button>Button with arrow tooltip</button>
</tooltip-component>

Custom Delay

<tooltip-component tooltip="Delayed tooltip" delay="500">
    <div>Slow tooltip</div>
</tooltip-component>

Long Text (Auto-Fixed Width)

<tooltip-component tooltip="This is a very long tooltip text that will automatically use fixed width mode for better readability and proper text wrapping behavior">
    <button>Long tooltip</button>
</tooltip-component>

Disabled Tooltip

<tooltip-component tooltip="Won't show" open="false">
    <button>No tooltip</button>
</tooltip-component>

Styling

The component uses Shadow DOM for style encapsulation. Tooltips have these default styles:

  • Background: rgba(97, 97, 97, 0.9) with blur effect
  • Text: White, 11px, system font
  • Border Radius: 4px
  • Padding: 4px block, 8px inline
  • Z-index: 9999
  • Animations: Opacity and scale transitions

CSS Logical Properties Used

| Property | Physical Equivalent | Usage | |----------|-------------------|-------| | inline-size | width | Tooltip dimensions | | block-size | height | Tooltip dimensions | | padding-inline | padding-left/right | Horizontal padding | | padding-block | padding-top/bottom | Vertical padding | | inset-inline-start | left | Arrow positioning | | inset-block-start | top | Arrow positioning | | border-inline-* | border-left/right | Arrow borders | | border-block-* | border-top/bottom | Arrow borders |

Architecture

Shadow DOM Structure


tooltip-component
├── #shadow-root
│   ├── <style> (encapsulated styles)
│   ├── <slot> (projects child content)
│   └── <div class="tooltip"> (tooltip element)
│       └── <div class="tooltip-arrow"> (optional arrow)

Event Flow

  1. Mouse Enter → Start delay timer
  2. Delay Complete → Position tooltip → Show with animation
  3. Mouse Leave → Hide with animation
  4. Attribute Change → Update tooltip properties

Performance

  • No DOM Manipulation: Tooltip element persists, only visibility changes
  • Optimized Positioning: Efficient viewport boundary detection
  • CSS Animations: Hardware accelerated transforms
  • Shadow DOM: Isolated styles prevent reflow/repaint issues

Development

Files

| File | Description | |------|-------------| | tooltip-component.js | Main component implementation | | index.html | Demo page with random tooltip generator |

Building

No build process required. Pure vanilla JavaScript.

Testing

Open index.html in a browser to test all tooltip configurations with the random generator.

License

MIT License - feel free to use in personal and commercial projects.

Contributing

  1. Fork the repository
  2. Make your changes
  3. Test thoroughly
  4. Submit a pull request

Note: This component uses modern web standards. For legacy browser support, consider using polyfills for Custom Elements and Shadow DOM.