Utility to track the boundaries of html-elements in SolidJS

Overview

solid-boundaries

Helps you to track the size and position of html-elements in solid-js.


What does it do?

See it in action here, or see it on CodeSandbox!

This small library exposes a small reactive primitive which tracks the size and position (bounds) of a specific html-element (subject). Bounds, just like when you call .getBoundingClientRect(), have the following structure:

interface Bounds {
  top: number;
  left: number;
  right: number;
  bottom: number;
  width: number;
  height: number;
}

This is useful for all kinds of things, like building tooltips, popovers, or other behavior and interactions related to changes regarding size and positions.

Bounds update in response to:

  • scrolling - the entire window as well as (nested-)scroll-containers
  • resizing - the entire window as well as the subject
  • mutations in the document - to be more specific:
    • any changes that may affect styling (style/class attributes)
    • adding or removal of other html-elements

Install

npm install solid-boundaries

Usage

import { trackBounds } from "solid-boundaries";

function App() {
  const { ref, bounds } = trackBounds();

  // Make sure to pass the ref properly to the element you
  // want to track, and do something with fun with the bounds!
  // Note: the bounds are `null` when the element has not been
  // connected to the DOM yet.
  return (
    <div ref={ref}>
      {bounds() && JSON.stringify(bounds())}
    <div>
  )
}

API

interface Bounds {
  top: number;
  left: number;
  right: number;
  bottom: number;
  width: number;
  height: number;
}

interface Config {
  /**
   * Whether to actively track the element's position.
   * @default () => true
   */
  enabled?: Accessor<boolean>;
  /**
   * Whether to actively track mutations in the DOM.
   * @default () => true
   */
  trackMutations?: boolean;
  /**
   * Whether to actively track resizes of the element you're
   * tracking or of the entire window.
   * @default () => true
   */
  trackResize?: boolean;
  /**
   * Whether to actively track scrolling.
   * @default () => true
   */
  trackScroll?: boolean;
  /**
   * Defines specific keys of the boundary to track.
   * By default all keys are tracked.
   * @default ["top", "right", "bottom", "left", "width", "height"]
   */
  keys?: BoundsKeys;
  /**
   * Whether not to show warning messages in the console
   */
  suppressWarnings?: boolean;
}

type TrackBoundsProps = {
  /**
   * Setter which should be passed to the `ref` prop of the
   * element you want to track.
   */
  ref: Setter<HTMLElement | null>;
  /**
   * The bounds of the element you are tracking.
   * Note: returns `null` if the element is not connected
   * to the DOM.
   */
  bounds: Accessor<Bounds | null>;
  /**
   * Provides a reference to the element.
   */
  element: Accessor<HTMLElement | null>;
};

function trackBounds(config?: Config): TrackBoundsProps;

Browser compatibility

This library makes use of ResizeObserver and MutationObserver. If you need to support older browsers I recommend to use a polyfill, otherwise certain behavior will be skipped.

Contributing

Want to contribute to solid-boundaries? Your help is very much appreciated! Please consult the contribution guide on how to get started.

License

MIT © everweij

You might also like...

A SolidJS starter template with solid-labels, solid-sfc and solid-styled

solid-sfc-styled-labels-starter This is a SolidJS starter template for easily setting up solid-sfc, solid-styled and solid-labels. Development Install

Mar 25, 2022

Cache Solidjs resources by key (derived from the resource source)

Solid Cached Resource (Experimental) Create a solid resource attached to a cached state by a unique key. Heavily inspired by react-query, but for soli

Dec 31, 2022

VSCode extension with helpful code snippets for SolidJS.

Solid Snippets VSCode extension with helpful code snippets for SolidJS. GET THE EXTENSION Snippets Trigger Content Languages JSX sinput→ Input two-way

Dec 8, 2022

Template for building SolidJS web application with Tailwind CSS faster!

Template for building SolidJS web application with Tailwind CSS faster!

Hardtail Template for building SolidJS web application with Tailwind CSS faster! Setup First you could clone or fork this project or repository to you

Sep 18, 2022

A lightweight SolidJS component for building interactive node-based diagrams and graphs.

Convert Your Ideas To Graphs With Solid Graph! Solid Graph A lightweight and minimal Solid component for building interactive graphs and node-based ed

Dec 8, 2022

A lightweight SolidJS component for building interactive node-based diagrams and graphs.

Convert Your Ideas To A Simple And Excitig Journay With Odysea! Odysea A lightweight and minimal Solid component for building interactive graphs and n

Aug 15, 2022

:mechanical_leg: Tiny and performant collapse component for SolidJS.

Solid Collapse Tiny and performant collapse component for SolidJS. Demo and examples Features Pure dynamic CSS height transition, no javascript animat

Dec 29, 2022

Minimalist UI for Stable Diffusion, built with SolidJS

Minimalist UI for Stable Diffusion, built with SolidJS

Solid Diffusion Minimalist web-based interface for Stable Diffusion with persistant storage in the browser, built with SolidJS. This project is an alp

Nov 29, 2022

A SolidJS signal tracking dependency & structural visualizer developer tool

A SolidJS signal tracking dependency & structural visualizer developer tool

Solid JS DevTool Developer tool that visualizes Solid's signal architecture, including the components. It can be added to any Solid JS application. In

Nov 5, 2022
Comments
  • Track the entire window instead of a specific element

    Track the entire window instead of a specific element

    Hi! Is there a way to track the entire window instead of a specific element? I want to handle window resizing globally not on a single component, after reading the docs I'm not sure if that's possible. Thanks!

    opened by dd8888 1
  • [BUG] Rename `createBoundaryTracker` in server.js to `trackBounds`?

    [BUG] Rename `createBoundaryTracker` in server.js to `trackBounds`?

    Describe the bug The exported function in server.js should match in name the one for client-side.

    1. I expect trackBounds to be available in server-side, even if I don't make use of it, because not having it can break the static site generation process.
    2. But I find that trackBounds is not available when running Astro build (even if the exported TypeScript types say so), and I have createBoundaryTracker instead (which is not exposed in the exported types).

    Additional context It seems both functions (even if they differ in behavior) are there to occupy the same place, that's why it seems a mistake to have them named differently.

    bug 
    opened by castarco 0
Releases(v2.0.0)
Owner
Erik Verweij
Developer@Xebia
Erik Verweij
Chrome extension that applies phrase-based line breaking and visible phrase boundaries to the current page.

BudouX Chrome Extension This extension applies the phrase-based line breaking or the Japanese Wakachi-gaki style line breaking to the current page. Pl

Google 9 Nov 18, 2022
Today began learn about MEAN stack first with Nodejs that is a server side form of javascript. Repository for track of Day by Day improvement track in new Skill.

NodeJs Today began learn about MEAN stack first with Nodejs that is a server side form of javascript. Repository for track of Day by Day improvement t

Rahul Bhati 4 Oct 14, 2022
A lightweight JavaScript utility to fade elements in and out of view on page scroll.

ScrollFade ScrollFade is used to fade elements in and out of view while scrolling through a page. Tag any elements you want to fade with the class scr

Jordan Trbuhovic 14 Dec 15, 2022
A utility for creating toggleable items with JavaScript. Inspired by bootstrap's toggle utility. Implemented in vanillaJS in a functional style.

LUX TOGGLE Demo: https://jesschampion.github.io/lux-toggle/ A utility for creating toggleable dom elements with JavaScript. Inspired by bootstrap's to

Jess Champion 2 Oct 3, 2020
Unstyled, dev error overlays for SolidJS

solid-error-overlay Unstyled, headless Error Overlay for SolidJS Install npm i solid-error-overlay yarn add solid-error-overlay pnpm add solid-error-o

Alexis H. Munsayac 20 Dec 29, 2022
A form management library for SolidJS that is very lightweight and simple!

solform A form management library for SolidJS that is very lightweight and simple! Why solform Very easy, fast, lightweight and powerful! It has built

Okan YILDIRIM 16 Nov 15, 2022
Solidex is a list of SolidJS ecosystem resources and packages.

Solidex The following is a list of articles, packages and other resources that focus on the SolidJS ecosystem. Solidex is a platform for listing and m

Solid 7 Dec 18, 2022
A modern solidjs based UI library ~ it is very lightweight!

?? 目前正在开发中,敬请期待~ cerises-ui 一个现代的基于 solidjs 的 UI 库~它是非常轻量级的! 官网:https://cerises-ui.edoc.wiki 目前计划组件 组件 说明 认领人 进度 Review & Unit Test Button、ButtonGroup

Cerises UI 4 Jul 19, 2022
A SolidJS template built with Hope-UI

solid-hope-admin 名称 简介 solid JavaScript框架 hope-ui UI组件库 solid-app-router 路由 vite-plugin-pages 自动生成文件路由 query-string 解析URL地址 steeze-ui/solid-icon 图标 功能

null 9 Dec 13, 2022
A library of high-quality primitives that help you build accessible user interfaces with SolidJS.

Solid Aria A library of high-quality primitives that help you build accessible user interfaces with SolidJS. Primitives @solid-aria/primitives - Expor

SolidJS Community 205 Jan 7, 2023