Skip to content

Commit

Permalink
Merge pull request #8 from Devlander-Software/create-npm-package
Browse files Browse the repository at this point in the history
Create npm package
  • Loading branch information
landonwjohnson authored Sep 24, 2024
2 parents bacc715 + 3d91914 commit b02f9d5
Show file tree
Hide file tree
Showing 8 changed files with 11,992 additions and 6,708 deletions.
37 changes: 20 additions & 17 deletions docs/assets/icons.js

Large diffs are not rendered by default.

2,156 changes: 2,150 additions & 6 deletions docs/assets/main.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion docs/assets/navigation.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion docs/assets/search.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devlander/higher-order-components",
"version": "0.0.10",
"version": "0.0.12",
"description": "A comprehensive collection of higher-order components for React and React Native projects, enhancing functionality and development efficiency.",
"main": "dist/umd/index.js",
"module": "dist/esm/index.js",
Expand Down Expand Up @@ -137,7 +137,7 @@
"typedoc": "^0.26.5"
},
"dependencies": {
"@devlander/hooks": "^0.0.204",
"@devlander/hooks": "^0.0.208",
"@devlander/styled-components-theme": "^1.1.254",
"@devlander/utils": "^0.0.49",
"@stylexjs/stylex": "^0.7.5",
Expand Down
42 changes: 35 additions & 7 deletions src/hocs/withVisibilitySensor/withVisibilitySensor.hoc.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,51 @@
import { useVisibilitySensor } from "@devlander/hooks"
import type { UseVisibilityOptionsConfig } from "@devlander/hooks"
import { useVisibilitySensor, VisibilityOffset } from "@devlander/hooks"
import type { ComponentType, Ref } from "react"
import React, { forwardRef, useState } from "react"
import React, { forwardRef, useState, useMemo } from "react"
import { View, Platform } from "react-native"

// Check if we are running on the web platform
const isWeb = Platform.OS === "web"

type WithVisibilitySensorProps = {
// Default prop values
const defaultVisibilityOffset = VisibilityOffset.FULL
const defaultCheckInterval = 1000
const defaultStopWatching = false

interface WithVisibilitySensorProps {
isVisible: boolean
visibilityOffset?: VisibilityOffset
checkInterval?: number
stopWatching?: boolean
}

export function withVisibilitySensor<P>(
WrappedComponent: ComponentType<P & WithVisibilitySensorProps>,
) {
const WithVisibilitySensorComponent = (props: P, ref: Ref<any>) => {
const WithVisibilitySensorComponent = (
props: P & WithVisibilitySensorProps,
ref: Ref<any>,
) => {
const [isVisible, setIsVisible] = useState(false)
const visibilitySensorRef = useVisibilitySensor((visible: boolean) => {
setIsVisible(visible)
})

// Memoize config options and allow them to update if props change
const visibilityConfig: UseVisibilityOptionsConfig = useMemo(() => {
return {
visibilityOffset: props.visibilityOffset ?? defaultVisibilityOffset,
checkInterval: props.checkInterval ?? defaultCheckInterval,
}
}, [props.visibilityOffset, props.checkInterval])

// Create a visibility sensor ref for the platform
const visibilitySensorRef = useVisibilitySensor(
(visible: boolean) => {
setIsVisible(visible)
},
visibilityConfig,
props.stopWatching ?? defaultStopWatching,
)

// Choose the correct container based on platform
const Container = isWeb ? "div" : View

return (
Expand Down
10 changes: 7 additions & 3 deletions typings/hocs/withVisibilitySensor/withVisibilitySensor.hoc.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { VisibilityOffset } from "@devlander/hooks";
import type { ComponentType } from "react";
import React from "react";
type WithVisibilitySensorProps = {
interface WithVisibilitySensorProps {
isVisible: boolean;
};
export declare function withVisibilitySensor<P>(WrappedComponent: ComponentType<P & WithVisibilitySensorProps>): React.ForwardRefExoticComponent<React.PropsWithoutRef<P> & React.RefAttributes<any>>;
visibilityOffset?: VisibilityOffset;
checkInterval?: number;
stopWatching?: boolean;
}
export declare function withVisibilitySensor<P>(WrappedComponent: ComponentType<P & WithVisibilitySensorProps>): React.ForwardRefExoticComponent<React.PropsWithoutRef<P & WithVisibilitySensorProps> & React.RefAttributes<any>>;
export {};
Loading

0 comments on commit b02f9d5

Please sign in to comment.