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 🙏

© 2024 – Pkg Stats / Ryan Hefner

floating-ui-solid

v1.0.50

Published

Floating UI Primitives for SolidJS

Downloads

15

Readme

floating-ui-solid

NPM

Why choose floating-ui-solid?

  • 📦 Smaller bundle size
  • 🧹 Proper cleanup: cleanup function with autoUpdate is properly handled
  • 💪 Improved TypeScript support
  • 🤖 Better API

Installation

Choose your preferred package manager:

npm install floating-ui-solid
# or
yarn add floating-ui-solid
# or
pnpm add floating-ui-solid
# or
bun add floating-ui-solid

Usage

Here's a basic example of how to use floating-ui-solid:

import { createSignal } from 'solid-js';
import { useFloating } from 'floating-ui-solid';

export default function App() {
  const [isOpen, setIsOpen] = createSignal(false);
  const { refs, floatingStyles } = useFloating({
    placement: "bottom",
    isOpen: isOpen,
    strategy: "absolute",
  });

  return (
    <main>
      <div
        ref={refs.setReference}
        onMouseEnter={() => setIsOpen(true)}
        onMouseLeave={() => setIsOpen(false)}
        class="reference"
      >
        Hover me
      </div>
      {isOpen() && (
        <div
          ref={refs.setFloating}
          style={...floatingStyles()}
          class="floating"
        >
          Floating
        </div>
      )}
    </main>
  );
}

Using autoUpdate

To keep the floating element positioned correctly when the reference element changes, use the autoUpdate function:

import { autoUpdate, useFloating } from 'floating-ui-solid';

const { refs, floatingStyles } = useFloating({
  placement: "bottom",
  isOpen: isOpen,
  strategy: "absolute",
  whileElementsMounted: autoUpdate,
  // or for more control:
  whileElementsMounted: (reference, floating, update) => {
    const cleanup = autoUpdate(reference, floating, update, { elementResize: true });
    return cleanup;
  },
});

Applying Custom Styles

You can apply custom styles to the floating element using middleware:

import { autoUpdate, useFloating, size, offset } from 'floating-ui-solid';

const [reactiveMiddleware, setReactiveMiddleware] = createSignal([
    offset(10),
    size({
      apply({ availableHeight }) {
        setFloatingStyles({ ...floatingStyles(), "max-height": `${availableHeight}px` });
      }
    })
  ]);

const { refs, floatingStyles, setFloatingStyles } = useFloating({
  placement: "bottom",
  isOpen: isOpen,
  strategy: "absolute",
  middleware: reactiveMiddleware,
});

Arrow Element

import { autoUpdate, useFloating, arrow } from 'floating-ui-solid';
const [reactiveMiddleware, setReactiveMiddleware] = createSignal([]);
const { refs, floatingStyles, setFloatingStyles } = useFloating({
  placement: "bottom",
  isOpen: isOpen,
  strategy: "absolute",
  middleware: reactiveMiddleware,
});

  return (
        <div>
            <div
                onMouseEnter={() => setIsOpen(true)}
                onMouseLeave={() => setIsOpen(false)}
                class="reference"
                ref={refs.setReference}
            >
                Reference
            </div>

            {isOpen() && (
                <div class="floating" style={{ ...floatingStyles() }} ref={refs.setFloating}>
                    Floating
                    <div
                        class="arrow"
                        style={{
                            position: 'absolute',
                            left: middleware()?.arrow?.x != null ? `${middleware().arrow?.x}px` : '',
                            top: arrowState()?.offsetHeight != null ? `${-arrowState()?.offsetHeight! / 2}px` : '',
                        }}
                        ref={(node) => {
                            setArrow(node)
                            setReactiveMiddleware((prev) => [...prev, arrow({ element: node })]); // set it once it's ready
                        }}
                    ></div>
                </div>
            )}
        </div>
    )

License

This project is licensed under the MIT License.