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

@aspectly/web

v0.1.0

Published

Web/iframe integration for Aspectly bridge - React hooks for embedding and communicating with iframes

Downloads

282

Readme

@aspectly/web

React hooks for embedding iframes and communicating with them using the Aspectly bridge protocol.

Installation

# npm
npm install @aspectly/web

# pnpm
pnpm add @aspectly/web

# yarn
yarn add @aspectly/web

Overview

@aspectly/web provides React hooks for the parent page to embed iframes and establish bidirectional communication with them. The iframe content should use @aspectly/core to communicate back.

Quick Start

Parent Page (Host)

import { useAspectlyIframe } from '@aspectly/web';

function App() {
  const [bridge, loaded, Iframe] = useAspectlyIframe({
    url: 'https://example.com/widget'
  });

  useEffect(() => {
    if (loaded) {
      // Initialize bridge with handlers
      bridge.init({
        getUser: async () => ({ name: 'John', id: 123 }),
        saveData: async (params) => {
          console.log('Saving:', params);
          return { success: true };
        }
      });
    }
  }, [loaded, bridge]);

  const handleSendMessage = async () => {
    try {
      const result = await bridge.send('greet', { name: 'World' });
      console.log('Response:', result);
    } catch (error) {
      console.error('Error:', error);
    }
  };

  return (
    <div>
      <h1>Parent App</h1>
      <Iframe style={{ width: '100%', height: 400 }} />
      <button onClick={handleSendMessage} disabled={!loaded}>
        Send Message
      </button>
    </div>
  );
}

iframe Content (Widget)

The iframe should use @aspectly/core:

// Inside the iframe
import { AspectlyBridge } from '@aspectly/core';

const bridge = new AspectlyBridge();

// Initialize with handlers
await bridge.init({
  greet: async (params: { name: string }) => {
    return { message: `Hello, ${params.name}!` };
  }
});

// Call parent methods
const user = await bridge.send('getUser');
console.log('User:', user);

API Reference

useAspectlyIframe

const [bridge, loaded, IframeComponent] = useAspectlyIframe(options);

Options

| Property | Type | Required | Description | |----------|------|----------|-------------| | url | string | Yes | URL to load in the iframe | | timeout | number | No | Handler execution timeout in ms (default: 100000) |

Returns

| Index | Type | Description | |-------|------|-------------| | 0 | BridgeBase | Bridge instance for communication | | 1 | boolean | Whether the iframe has finished loading | | 2 | FunctionComponent | React component to render the iframe |

IframeComponent Props

The returned iframe component accepts all standard <iframe> HTML attributes plus:

| Prop | Type | Description | |------|------|-------------| | style | CSSProperties | Custom styles (border: 0 is applied by default) | | onError | (error: unknown) => void | Optional error handler |

Patterns

Checking Method Support

const handleAction = async () => {
  if (bridge.supports('advancedFeature')) {
    await bridge.send('advancedFeature', { data: 'value' });
  } else {
    // Fallback for older widget versions
    await bridge.send('basicFeature', { data: 'value' });
  }
};

Subscribing to Events

useEffect(() => {
  const handleEvent = (result) => {
    console.log('Bridge event:', result.method, result.data);
  };

  bridge.subscribe(handleEvent);

  return () => bridge.unsubscribe(handleEvent);
}, [bridge]);

Error Handling

import { BridgeErrorType } from '@aspectly/web';

const handleSend = async () => {
  try {
    await bridge.send('action', { data: 'value' });
  } catch (error) {
    if (error.error_type === BridgeErrorType.BRIDGE_NOT_AVAILABLE) {
      console.log('Widget not ready yet');
    } else if (error.error_type === BridgeErrorType.UNSUPPORTED_METHOD) {
      console.log('Method not supported by widget');
    }
  }
};

Security Considerations

  • The bridge uses postMessage for communication
  • By default, messages are sent with '*' origin - consider restricting this in production
  • Always validate incoming data in your handlers
  • Use HTTPS for iframe sources in production

Related Packages

License

MIT