Socket
Book a DemoInstallSign in
Socket

@marceloclp/react-aware

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@marceloclp/react-aware

A set of declarative components that implement self aware functionalities.

1.2.0
latest
Source
npmnpm
Version published
Weekly downloads
0
-100%
Maintainers
1
Weekly downloads
 
Created
Source

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

FAQs

Package last updated on 17 Apr 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.