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

react-native-appear-observer

v2.1.2

Published

Utility to check React Native node visibility on the screen.

Downloads

1,937

Readme


const App = () => {
  return (
    <AppearObserverProvider>
      <ScrollView>
        <TrackedComponent />
      </ScrollView>
    </AppearObserverProvider>
  )
}

const TrackedComponent = () => {
  const { refProps } = useAppearObserver({ 
    onAppear: useCallback(() => console.log('Hola!'), []) 
  })

  return <View { ...refProps } />
}
const App = () => {
  const scrollViewRef = useRef()
  const { interactionHandlers, interactionListeners } = useInteractionManager()

  return (
    <ScrollView ref={scrollViewRef} { ...interactionHandlers }>
      <TrackedComponent parentRef={scrollViewRef} interactionListeners={interactionListeners} />
    </ScrollView>
  )
}

const TrackedComponent = ({ parentRef, interactionListeners }: any) => {
  const { refProps } = useAppearObserver({
    parentRef,
    interactionListeners,
    onAppear: useCallback(() => console.log('Hi there!'), [])
  })

  return <View {...refProps} />
}
const App = () => {
  const scrollViewRef = useRef()

  return (
    <ScrollView ref={scrollViewRef}>
      <TrackedComponent parentRef={scrollViewRef} />
    </ScrollView>
  )
}

const TrackedComponent = ({ parentRef }: any) => {
  const { refProps } = useAppearObserver({
    parentRef,
    onAppear: useCallback(() => console.log('Hi there!'), []),
    options: {
      interactionModeEnabled: false,
    }
  })

  return <View {...refProps} />
}
const App = () => {
  return (
    <ScrollView>
      <TrackedComponent />
    </ScrollView>
  )
}

const TrackedComponent = () => {
  const { refProps } = useAppearObserver({
    onAppear: useCallback(() => console.log('Hi there!'), []),
    options: {
      interactionModeEnabled: false,
      useScreenIfNoParent: true,
    }
  })

  return <View {...refProps} />
}
const TrackedElement = ({ onAppear, onDisappear }: any) => {
  const isFocused = useIsFocused()

  const { refProps } = useAppearObserver({
    enabled: isFocused,
    onAppear,
    onDisappear,
    onDisable: onDisappear // Optional
  })

  return <View {...refProps} style={elementStyle} />
}
const App = () => {
  return (
    <AppearObserverProvider>
      {
        ({ 
           collapsable, 
           onLayout, 
           onScroll, 
           onScrollBeginDrag, 
           onScrollEndDrag, 
           onTouchStart, 
           onTouchEnd, 
           onTouchMove, 
           onTouchCancel, 
           onMomentumScrollEnd,
           interactionRecorders,
        }) => (
          <>
            <ScrollView 
              collapsable={collapsable} 
              onLayout={onLayout}
              onScrollBeginDrag={onScrollBeginDrag}
              onScrollEndDrag={onScrollEndDrag}
              onTouchStart={onTouchStart}
              onTouchEnd={onTouchEnd}
              onTouchMove={onTouchMove}
              onTouchCancel={onTouchCancel}
              onMomentumScrollEnd={onMomentumScrollEnd}
            >
              <TrackedComponent />
            </ScrollView>
            <ActionButton title="Scroll to top" onPress={() => {
              interactionRecorders.recordInteractionStart()
              scrollToTop()
            }} />
          </>
        )
      }
    </AppearObserverProvider>
  )
}

| Prop | Description | Default value | |------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------| | elementRef | External ref to use instead of the one created by the hook. Use if you need to work with the tracked element by ref. No ref merging happens, the external one overrides the internal default. | - | | parentRef | Optional props for usage without context. Provides parent element ref directly to the hook, overrides the ref supplied by context. | - | | onAppear | Callback that triggers when the element comes into visibility. | - | | onDisappear | Callback that triggers when the element disappears. | - | | onEnable | Callback that fires when tracking gets enabled. | - | | onDisable | Callback that fires when tracking gets disabled. | - | | enabled | Flag which toggles tracking on and off. Useful when tracking needs to be reset, like in case with react navigation. | true | | interactionListeners | Slot for interaction listeners passed from parent, for usage without context. | - |

| Option | Description | Default value | |-------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------| | visibilityThreshold | Defines what part of an element should be visible for it to trigger callback, from 0 to 1. | 0.001 | | intervalDelay | Determines a delay in milliseconds between visibility check repetitions. | 50 | | recalculateParentBoundaries | Tells whether observer should measure parent element boundaries on every on every check or measure once and cache. | true | | parentOffsets | Sets additional static offsets added to the calculated parent boundaries. Useful if you need to consider the height of some header inside a ScrollView. | 0 for each corner | | interactionModeEnabled | Makes the tracking loop fire only when user interaction has happened. When disabled, the loop runs indefinitely until stopped by enabled prop. | true | | useScreenIfNoParent | Provides an option to track the element relative to screen instead of parent element. Only works if parent ref is not passed. | false | | optimizeOutOfScreen | Doubles the delay between loop cycles if the element is out of screen. A small optimization, candidate for improvement. | true |

| Prop | Description | |-------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | refProps | Ref and props that prevent element from being collapsed, see useObservableTargetRef | | restart | A handler to manually restart the observer. Can be used to trigger onAppear manually, for example when element's visibility hasn't changed, but its content has. |

| Prop | Description | Default value | |--------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------| | enableInteractionMode | When true, the touch handlers are attached to the child element of the provider, and child observers run checks only upon touch interactions, stopping them after a period of inactivity. | true | | ref | An external ref to pass to the provider's child element. Provider sets it's own ref by default, but can use external one for its operations. No ref merging happens, the external one overrides the internal default. | _ | | children | React element or render prop for manual setup. | _ | | offsets | Additional static offsets added to the parent element's boundaries. Can be used to account for ScrollView headers in calculations. | 0 for each corner | | onLayout, onScroll etc | Callbacks to be passed to the child component on top of the provider's internal ones. When an element is wrapped with provider, you have to pass these callbacks to the provider instead of the target element, otherwise provider overwrites them. | - |

| Prop | Description | Default value | |----------------------------------------------------|--------------------------------------------------------------------|-------------------| | onScroll, onScrollBeginDrag, onScrollEndDrag etc | External listeners to be attached alongside internal service ones. | true |

| Prop | Description | |------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | interactionHandlers | Callbacks for interaction events that trigger to be attached to parent component. They trigger the tracking loop start. | | interactionRecorders | Separate callbacks that can be attached to any event to mark it as interaction start or end. For example can serve to set 'Scroll to top' button press as interaction. | | interactionListeners | Interaction listeners to be passed to useAppearObserver hook of the tracked components so that they can listen to parent interactions. |

| Prop | Description | Default value | |-------|-----------------------------------------------------------------------|---------------| | ref | External ref to be used instead of the default one set up internally. | - |

| Prop | Description | |---------------|-------------------------------------------------------------------------------------------| | ref | A regular React ref | | collapsable | Set to true, prevents native node from being collapsed. | | onLayout | Also serves for collapse prevention, as collapsable is currently only for Android. |