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

@marceloclp/react-aware

v1.2.0

Published

A set of declarative components that implement self aware functionalities.

Downloads

15

Readme

React Aware

A set of declarative components that implement self aware functionalities

NPM JavaScript Style Guide

Installation

npm i @marceloclp/react-aware

Usage

Components will accept a render function instead of the usual ReactNode as the children, with a shape similar to the following signature:

function render<T>(data: T | undefined | null, setRef: RefCallback<any>): JSX.Element

All components will attempt to render their own nodes - usually as a div element, and attach the ref object to it, but this behaviour can be overwrited by specifying the component to render as a Fragment and attaching the ref object yourself:

import { Fragment } from 'react'
import { SelfAware } from '@marceloclp/react-aware'

const Example = () => (
  <SelfAware as={Fragment}>
    {(_, __, setRef) => <div ref={setRef} />}
  </SelfAware>
)

RectAware

Returns a container that is aware of its bounding client rectangle.

import { RectAware } from '@marceloclp/react-aware'

const Screen = () => (
  <RectAware as="div" style={{ height: '100%', width: '100%' }}>
    {({ width, height }) => (
      <div>
        Width: {width}, height: {height}
      </div>
    )}
  </RectAware>
)

HeightAware

Returns a container that is aware of its height. Particularly useful when rendering scrollable elements that do not have a fixed height (for example, inside flexboxes).

This is a slimmed down version of the RectAware component, optimized to only trigger a state update when the height changes.

import { HeightAware, getScrollableStyle } from '@marceloclp/react-aware'

const Scrollable = ({ children }) => (
  <div style={{ height: '100%', display: 'flex', flexDirection: 'column' }}>
    <div style={{ height: '200px' }}>
    <HeightAware style={{ flexGrow: 1 }}>
      {(height) => (
        <div style={getScrollableStyle(height)}>
          {children}
        </div>
      )}
    </HeightAware>
  </div>
)

SelfAware

Returns a container that is aware of its own DOM node. Its render function receives both the element - as reactive state, and the ref object, which is particularly useful if you need access to a inside an asynchronous callback.

import { SelfAware } from '@marceloclp/react-aware'

const Button = () => (
  <SelfAware as={Fragment}>
    {(_, ref) => (
      <button onClick={() => {
        setTimeout(() => {
          console.log(`Text height is ${ref.current?.clientHeight}`)
        }, 1000)
      }}>
        <Icon />
        <span ref={ref}>Click me!</span>
      </button>
    )}
  </SelfAware>
)

ScreenAware

Returns a Fragment by default containing the screen dimensions and the currently active breakpoint, if a list of breakpoints is specified.

This is particularly useful if you want to render different components based on screen width.

import { Fragment } from 'react'
import { Breakpoint, ScreenAware } from '@marceloclp/react-aware'

const breakpoints: Breakpoint[] = [
  { name: 'mobile', min: 0 },
  { name: 'tablet', min: 500 },
  { name: 'desktop', min: 1000 },
]

export const Example = () => (
  <ScreenAware as="div" breakpoints={breakpoints}>
    {({ breakpoint: { name }}) => ({
      mobile: () => <div>Mobile screen</div>,
      tablet: () => <div>Tablet screen</div>,
      desktop: () => <div>Desktop screen</div>,
    }[name])()}
  </ScreenAware>
)

License

MIT © marceloclp