0.1.37 β€’ Published 3 months ago

@studio-lumio/hooks v0.1.37

Weekly downloads
-
License
ISC
Repository
github
Last release
3 months ago

HOOKS

Introduction

A compilation of react hooks we use to make our magic at Studio Lumio, heavily inspired by the SFDR's Hamo

🚩The API may be subject to change without prior notice.

Setup

$ yarn add @studio-lumio/hooks
$ npm i @studio-lumio/hooks

Features

This toolkit is composed of:

useOnClickOutside – trigger a callback when user clicks outside of a reference node.

  useOnClickOutside(ref, () => console.log('clicked outside'))

useDebug – returns true if #debug is present in the url.

  const debug = useDebug()

useDocumentReadyState – returns true if document is ready.

  const ready = useDocumentReadyState()

useFrame – use @studio-freight/tempus animation frame.

useInterval – create and dispose intervals.

useInterval(() => {
    setCount(count + 1)
},

// Delay in milliseconds or null to stop it

1000,
)

useIsClient – returns true if window is defined.

  const isclient = useIsClient()

useIsTouchDevice – returns true if client is using a touch-capable device.

  const isTouch = useIsTouchDevice()

useIsVisible – returns true if a reference node is in the viewport.

const { ref, inView } = useIsVisible({ threshold: 0.5, once: true, rootMargin: '0px', root: null })

useMediaQuery – css-like media query support in Javascript.

const match = useMediaQuery('(min-width: 1024px)')

useRect – getboundingclientrect

const { top, bottom } = useRect(elRef)

useSlots – brings vue slots to react

useFoucFix – temporary fix for FOUC bug from Next js

useWindowSize – returns window dimensions

const { width, height } = useWindowSize()

useIntersectionObserver – detects visibility of a component on the viewport using the IntersectionObserver API

const entry = useIntersectionObserver(ref, {})

useIsomorphicLayoutEffect – identical to useEffect, but fires synchronously after all DOM mutations.

useIsomorphicLayoutEffect(() => {
    console.log(useIsomorphicLayoutEffect)
}, [])

useCopyToClipboard – provides a copy method to save a string in the clipboard and the copied value

const [value, copy] = useCopyToClipboard()
const handleClick = () => {
    copy('copy me', callback)
}
 <button onClick={handleClick}>Meh</button>

useEventListener – it does what the name says. Pass in an event and a callback function as the second parameter

 const elRef = useRef<HTMLElement>(null)
const documentRef = useRef<Document>(document)

  // for window based event
  useEventListener('scroll', () => console.log('scroll'))

  // for document based event
  useEventListener('visibilitychange', () => console.log('visibilitychange'), documentRef)

  // for element based event
  useEventListener('click', () => console.log('clicked'), elRef)

useDebounce – uses an internal timer to update the variable every x seconds (2nd parameter)

const [value, setValue] = useState<string>('')
const debouncedValue = useDebounce<string>(value, 500)

useDebounceFn – uses an internal timer to execute the callback function every x seconds (2nd parameter)

const debouncedFn = useDebounceFn(() => {
  console.log('This function is debounced!');
}, 500);

useEffectOnce – useEffect that's executed only one time, on mount.

useEffectOnce(() => {
    console.log('Triggered only on mount')
})

useUpdateEffect – useEffect that ignores the first invocation (e.g. on mount)

useUpdateEffect(() => {
    console.log('ignores on mount', data)
}, [data])

useIsMounted – it returns a callback that returns true if the component is mounted

const isMounted = useIsMounted()

useFirstMountState – it returns true if component is just mounted.

const isFirstMountState = useFirstMountState()

useLocalStorage – it is used similarly to ths useState hook. It takes in 2 parameters, the key and the initial value.

const [value, setValue, removeValue] = useLocalStorage('valueKey', [])

useSessionStorage – pretty much the same as useLocalStorage just for session storage

const [value, setValue] = useSessionStorage('valueKey', [])
const {isVisible} = useIOSToolbarState()
useScript(url)
useFavicon(url);
useKeySequence({ sequence: 'sequence', callback: () => console.log('yay') })
 const idle = useIdle(5000);
 useDocumentTitle('new title');
const orientation = useOrientation();
  usePageLeave(() => {
    console.log('come back')
  });
const [value, updateCookie, deleteCookie] = useCookie(cookieName: string);
export default function App() {
  const [ref, hovering] = useHover();

  return (
      <div ref={ref}>
        IsHovering? {hovering ? "Yes" : "No"}
      </div>
  );
}
  const [list, { set, push, removeAt, insertAt, updateAt, clear }] = useList([
    "one",
    "two",
    "three",
  ]);

  console.log(list)
  const random = () => {
    set([1, 2, 3])
    push([1, 2, 3])
    removeAt(1)
    insertAt(1, "okay")
    updateAt(1, "okay")
    clear()
  }

Authors

License

MIT (c) Studio Lumio.

0.1.35

3 months ago

0.1.36

3 months ago

0.1.37

3 months ago

0.1.32

5 months ago

0.1.33

5 months ago

0.1.34

5 months ago

0.1.30

7 months ago

0.1.31

7 months ago

0.1.27

8 months ago

0.1.28

8 months ago

0.1.29

7 months ago

0.1.26

8 months ago

0.1.25

11 months ago

0.1.22

11 months ago

0.1.23

11 months ago

0.1.21

1 year ago

0.1.20

1 year ago

0.1.19

1 year ago

0.1.18

1 year ago

0.1.17

1 year ago

0.1.16

1 year ago

0.1.15

1 year ago

0.1.14

1 year ago

0.1.13

1 year ago

0.1.12

1 year ago

0.1.10

1 year ago

0.1.9

1 year ago

0.1.8

1 year ago

0.1.7

1 year ago

0.1.6

1 year ago

0.1.5

1 year ago

0.1.4

1 year ago

0.1.3

1 year ago

0.1.2

1 year ago