react-use#useLockBodyScroll TypeScript Examples

The following examples show how to use react-use#useLockBodyScroll. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Dialog.tsx    From UUI with MIT License 4 votes vote down vote up
Dialog = UUIFunctionComponent({
  name: 'Dialog',
  nodes: {
    Root: 'div',
    Portal: 'div',
    Backdrop: 'div',
    Content: 'div',
  },
  propTypes: DialogPropTypes,
}, (props: DialogFeatureProps, { nodes, NodeDataProps }) => {
  const { Root, Portal, Backdrop, Content } = nodes

  /**
   * handle optional props default value
   */
  const finalProps = {
    usePortal: props.usePortal === undefined ? true : props.usePortal,
    portalContainer: props.portalContainer || ReactHelper.document?.body,
    lockBodyScroll: props.lockBodyScroll === undefined ? true : props.lockBodyScroll,
    focusTrap: props.focusTrap === undefined ? true : props.focusTrap
  }

  useLockBodyScroll(props.open && !!finalProps.lockBodyScroll)

  const contentRef = useRef<any>(null)

  useGlobalClickAway(props.open, contentRef, props.onClickAway)

  const restoreElement = useRef<HTMLElement | null>(null)

  useEffect(() => {
    if (props.open) {
      restoreElement.current = document.activeElement as HTMLElement
    } else {
      restoreElement.current?.focus()
      restoreElement.current = null
    }
  }, [props.open])

  const backdrop = useMemo(() => {
    return (
      <FocusTrap active={props.open && finalProps.focusTrap}>
        <Backdrop
          {...NodeDataProps({
            'opened': !!props.open,
          })}
        >
          <Content
            role="dialog"
            aria-modal={props.open}
            ref={contentRef}
            tabIndex={0}
            onKeyDown={(event) => {
              switch (event.keyCode) {
                case KeyCode.Escape:
                  if (props.open) {
                    props.onClose && props.onClose(event)
                  }
                  break
                default:
                  // do nothing
              }
            }}
          >
            {props.children}
          </Content>
        </Backdrop>
      </FocusTrap>
    )
  }, [Backdrop, Content, NodeDataProps, finalProps.focusTrap, props])

  const wrappedBackdrop = useMemo(() => {
    return (finalProps.usePortal && finalProps.portalContainer)
    ? ReactDOM.createPortal(<Portal>{backdrop}</Portal>, finalProps.portalContainer)
    : <Portal>{backdrop}</Portal>
  }, [Portal, backdrop, finalProps.portalContainer, finalProps.usePortal]);
  return (
    <Root>
      {wrappedBackdrop}
    </Root>
  )
})
Example #2
Source File: Drawer.tsx    From UUI with MIT License 4 votes vote down vote up
Drawer = UUIFunctionComponent({
  name: 'Drawer',
  nodes: {
    Root: 'div',
    Portal: 'div',
    Backdrop: 'div',
    Content: 'div',
  },
  propTypes: DrawerPropTypes,
}, (props: DrawerFeatureProps, { nodes, NodeDataProps }) => {
  const { Root, Portal, Backdrop, Content } = nodes

  /**
   * handle optional props default value
   */
  const finalProps = {
    usePortal: props.usePortal === undefined ? true : props.usePortal,
    portalContainer: props.portalContainer || ReactHelper.document?.body,
    lockBodyScroll: props.lockBodyScroll === undefined ? true : props.lockBodyScroll,
    focusTrap: props.focusTrap === undefined ? true : props.focusTrap,
    placement: props.placement || 'right',
  }

  const contentRef = useRef<any>(null)

  useGlobalClickAway(props.active, contentRef, props.onClickAway)

  useLockBodyScroll(props.active && finalProps.lockBodyScroll)

  const restoreElement = useRef<HTMLElement | null>(null)

  useEffect(() => {
    if (props.active) {
      restoreElement.current = document.activeElement as HTMLElement
    } else {
      restoreElement.current?.focus()
      restoreElement.current = null
    }
  }, [props.active])

  const content = useMemo(() => {
    return (
      <FocusTrap active={props.active && finalProps.focusTrap}>
        <Backdrop
          {...NodeDataProps({
            'active': !!props.active,
            'placement': finalProps.placement,
          })}
        >
          <Content
            role="dialog"
            aria-modal={props.active}
            ref={contentRef}
            tabIndex={0}
            onKeyDown={(event) => {
              switch (event.keyCode) {
                case KeyCode.Escape:
                  if (props.active) {
                    props.onClose && props.onClose()
                  }
                  break
                default:
                  // do nothing
              }
            }}
          >{props.children}</Content>
        </Backdrop>
      </FocusTrap>
    )
  }, [props, finalProps.focusTrap, finalProps.placement, Backdrop, NodeDataProps, Content])

  const portal = useMemo(() => {
    return (finalProps.usePortal && finalProps.portalContainer)
    ? ReactDOM.createPortal(<Portal>{content}</Portal>, finalProps.portalContainer)
    : <Portal>{content}</Portal>
  }, [finalProps.usePortal, finalProps.portalContainer, Portal, content])

  return (
    <Root role="dialog">
      {portal}
    </Root>
  )
})