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

astro-surreal

v0.0.2

Published

Run a bundled Surreal script once for each rendered instance of an Astro component.

Downloads

166

Readme

astro-surreal

Run a bundled Surreal script once for each rendered instance of an Astro component.

npm install astro-surreal
// astro.config.mjs
import { defineConfig } from "astro/config";
import surreal from "astro-surreal";

export default defineConfig({
  integrations: [surreal()],
});

Give the component one native HTML root and one attribute-free script that imports me or any:

<div>
  <button>Hide me</button>
</div>

<script>
  import { me } from "astro-surreal";
  me("button")?.on("click", (event, element) => element.fadeOut());
</script>

Each instance gets its own local variables. Other imports stay at module scope. TypeScript, npm imports, and top-level await use Astro's bundler. Instances initialize independently, once per root, on initial load or astro:page-load. Persisted roots keep their state. Failed initializers log an error and are not retried.

Selection

  • me() returns the decorated root; any() returns a decorated array containing it.
  • Strings are ordinary descendant selectors, including nested components. Missing matches return null or an empty array. Invalid selectors throw.
  • Elements, arrays, and NodeLists can be passed directly. me() takes the first element of a collection, or returns null if empty.
  • me(event) and any(event) read currentTarget. After dispatch it is null; use the handler's element argument across await.

Only explicit imports of me and any opt a script into the integration. Aliases work. Import helpers directly inside the Astro script; pass them as arguments to shared functions in .ts files. Me, Any, SurrealElement, and SurrealArray types are exported from astro-surreal and astro-surreal/types.

Events

button.onPrevent("click", async (event, element) => {
  await save();
  element.disable();
});

on() and onPrevent() pass the event and decorated listening element, preserve native callback this, and chain. Collections invoke the callback for each listening element. onPrevent() calls .preventDefault() before calling the handler.

Each on() or onPrevent() call adds a new wrapper, so repeated registrations run multiple times, including when mixing methods. Native removeEventListener() cannot remove these wrappers using the original callback. Use native addEventListener() and removeEventListener() when you need to remove an individual listener.

Templates and props

Both methods below exist only on decorated HTML/SVG elements, excluding collections and Document.

container.each(items, template, (clone, item, index) => {
  clone.styles({ backgroundColor: item.color });
});

each() takes a readonly array, an HTMLTemplateElement, and a synchronous callback with the decorated deep clone, item, and zero-based index. The template must contain exactly one HTML or SVG root.

All clones are prepared before replacing the container's children. Validation or callback errors preserve existing children; an empty array clears them. The method returns the container. Replacement discards previous child state and listeners. Keep templates outside the replaced container.

Pass props as an attribute spread:

---
import { passProps } from "astro-surreal/plugins/props";
const { title, options } = Astro.props;
---

<div {...passProps({ title, options })}></div>

passProps(value) returns { "data-props": string } using the bundled devalue serializer. No separate dependency is needed. Read them with element.props<Props>(). Missing attributes return a fresh empty object. Malformed payloads, null, arrays, and other non-object top-level values throw. The default type is Record<string, unknown>; a generic cannot validate fields.

Limits

Roots must be statically identifiable HTML elements. Wrap fragments, components, conditional roots, or multiple roots. Script/style blocks do not count as roots. Participating scripts cannot export bindings or use TypeScript import assignments. Ordinary scripts keep Astro's behavior.

Updates are explicit. There is no reactivity, keyed reuse, async template rendering, HTML-string interpolation, or automatic initialization of inserted components. There are no cleanup hooks; manage timers, observers, and global listeners yourself. Selectors have no component ownership filtering or :global(...) support yet.

The browser runtime vendors modified Surreal 1.3.4-d. Use attr()/attribute(), offAll(), and send(). Upstream off() is omitted, as are the attributes(), off_all(), and trigger() aliases. Native .attributes is unchanged.

Production Chromium checks cover navigation and a separate Astro CSP fixture without ClientRouter. Test your own CSP policy in production. Astro's transition plugin currently warns about incomplete source maps.

Development

npm install
npm test
npm run check
npm run format:check
cd test/fixture
npm install
npx astro check
npm run dev -- --host --background

Use npm run format to apply formatting.

For browser checks, install Chromium with npx playwright install chromium, build with npm run build --prefix test/fixture, and start npm run preview --prefix test/fixture -- --port 4330. Run npm run test:browser, using URL if preview selects another port. Repeat the build and browser check with CSP=1 for the separate CSP fixture.