Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@teste-ui/popper

teste-ui4MIT3.0.2TypeScript support: included

A React component and hooks wrapper for popper.js

react, popper, popover, tooltips, popper.js, positioning, popperjs-modifier, ui, chakra ui, component

readme

Popper

A React hooks wrapper for popper.js to dynamic positioning of containers around a reference.

This is an internal hook of Chakra-UI, and it's not covered by semver, and may cause unexpected or broken application behavior. Use them at your own risk.

Installation

yarn add @teste-ui/popper

Basic usage

By default, the usePopper hook returns props for the popper, reference and arrow.

import { Box } from "@teste-ui/layout"
import { Button } from "@teste-ui/button"
import { useDisclosure } from "@teste-ui/hooks"
import { usePopper } from "@teste-ui/popper"

function Example() {
  const { isOpen, onToggle } = useDisclosure()
  const { popperRef, referenceRef, getArrowProps } = usePopper()
  return (
    <>
      <Button ref={referenceRef} onClick={onToggle} mb={2}>
        {isOpen ? "Click me to see less" : "Click me to see more"}
      </Button>
      {isOpen && (
        <Box ref={popperRef} bg="red">
          <div
            {...getArrowProps({
              style: {
                background: "red",
              },
            })}
          />
          This is a popover for the button!
        </Box>
      )}
    </>
  )
}

Parameters

Changing the placement

You can change the placement of the popper by passing the placement option to usePopper and set it to the popper.js placement.

const { popperRef, referenceRef } = usePopper({
  placement: "right-start",
})

Match reference's width

In some cases, you might want to allow the popper take the width of the reference. For example, autocomplete, select, etc.

To achieve this, pass the matchWidth option and set it to true

const { popperRef, referenceRef } = usePopper({
  matchWidth: true,
})

Place the popper next to the reference

You can place the popper next to the reference without margin or distance between them. Useful to create an autocomplete or typeahead feature.

const { popperRef, referenceRef } = usePopper({
  gutter: 0,
})

Using inside a fixed container

If the reference element is inside a fixed container, you should use the fixed strategy.

const { popperRef, referenceRef } = usePopper({
  strategy: "fixed",
})

Adding transition

When add transitions to a popper component, it is usually advised to apply popper and transition to different elements.

// 1. Import components
import { useDisclosure } from "@teste-ui/hooks"
import { usePopper } from "@teste-ui/popper"
import { motion, AnimatePresence, Variants } from "framer-motion"

export function Example() {
  // 2. Create toggle state
  const { isOpen, onToggle } = useDisclosure()

  // 3. Create motion variants
  const slide: Variants = {
    exit: {
      y: -2,
      opacity: 0,
    },
    enter: {
      y: 0,
      opacity: 1,
    },
  }

  // 4. Consume the `usePopper` hook
  const { getPopperProps, getReferenceProps, getArrowProps, transformOrigin } =
    usePopper({
      placement: "bottom-start",
    })

  return (
    <>
      <button {...getReferenceProps({ onClick: onToggle })}>Toggle</button>
      <div {...getPopperProps()}>
        <AnimatePresence>
          {isOpen && (
            <motion.div
              transition={{
                type: "spring",
                duration: 0.2,
              }}
              variants={slide}
              initial="exit"
              animate="enter"
              exit="exit"
              style={{
                background: "red",
                width: 200,
                transformOrigin,
                borderRadius: 4,
              }}
            >
              Testing
              <div
                {...getArrowProps({
                  style: {
                    background: "red",
                  },
                })}
              />
            </motion.div>
          )}
        </AnimatePresence>
      </div>
    </>
  )
}

When not rendering the popper conditionally, we recommend using visibility: hidden instead of hidden or display: none

changelog

Change Log

3.0.2

Patch Changes

3.0.1

Patch Changes

3.0.0

Major Changes

Patch Changes

3.0.0-next.3

Major Changes

Patch Changes

3.0.0-next.2

Patch Changes

  • #5969 6b969b41d Thanks @segunadebayo! - Fix issue where matchWidth doesn't work as expected due to the fixed min-width: max-content style attached to the element

3.0.0-next.1

Major Changes

Patch Changes

3.0.0-next.0

Major Changes

Patch Changes

2.4.3

Patch Changes

2.4.2

Patch Changes

2.4.1

Patch Changes

2.4.0

Minor Changes

  • #4991 6095eaf9a Thanks @segunadebayo! - Update build system we use from a custom babel cli setup to preconstruct.

    The previous build system transpiles the code in src directory to dist/esm and dist/cjs keeping the same file structure. The new build system merges all files in src and transpiles to a single esm and cjs file.

    Potential Breaking Change: The side effect of this is that, if you imported any function, component or hook using the undocumented approach like import { useOutsideClick } from "@teste-ui/hooks/dist/use-outside-click", you'll notice that the this doesn't work anymore.

    Here's how to resolve it:

    `jsx live=false // Won't work 🎇 import { useOutsideClick } from "@teste-ui/hooks/dist/use-outside-click"

    // Works ✅ import { useOutsideClick } from "@teste-ui/hooks" `

    If this affected your project, we recommend that you import hooks, functions or components the way it's shown in the documentation. This will help keep your project future-proof.

Patch Changes

2.3.1

Patch Changes

  • c3f016149 Thanks @segunadebayo! - Remove default [] value for modifiers and moved it into createPopper definition. This allows memoized modifiers to work correctly in user-land when used with useCallback.

2.3.0

Minor Changes

  • 4146a9051 #3837 Thanks @mcha-dev! - usePopper now accepts a direction prop so it can handle placement for RTL languages. Values such as top-start, top-end, bottom-start and bottom-end will be flipped depending on the theme's direction value.

    In addition to the default popper.js placement, you can pass start-start, start-end, end-start and end-end. This will resolve to the equivalent popper.js placement as well.

2.2.1

Patch Changes

  • 5afa7ef49 #4180 Thanks @segunadebayo! - - Wrap force update within a function to prevent null scenarios
    • Add default inset value to prevent overflow in scenarios where enabled is false (i.e. when the popper is not visible)

2.2.0

Minor Changes

  • 5e24481fc #4026 Thanks @dodas! - Add enabled option to usePopper hook.

    The popper.js instance will not be created until this option is true.

    Menu, Popover and Tooltip components now use this option, so the popper.js instance is created only once the popper is open. This should significantly improve render and scroll performance.

2.1.2

Patch Changes

2.1.1

Patch Changes

2.1.0

Minor Changes

  • 75817ec42 #3733 Thanks @segunadebayo! - Add prop getters for popper and arrow for better ssr support

    Replace utils dependency with react-utils

Patch Changes

2.0.1

Patch Changes

2.0.0

Major Changes

Patch Changes

1.1.5

Patch Changes

1.1.4

Patch Changes

  • 51ad518e2 #3343 Thanks @dodas! - The Popper.js instance is now created only once it is actually needed for positioning.

1.1.3

Patch Changes

1.1.2

Patch Changes

1.1.1

Patch Changes

1.1.0

Minor Changes

  • 032f1678 #3022 Thanks @dodas! - Added enabled property to usePopper. Popper won't be updated while it is set to false.

    Menu now uses this option to not update its position while it's closed.

Patch Changes

1.0.3

Patch Changes

1.0.2

Patch Changes

1.0.1

Patch Changes

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

1.0.0 (2020-11-13)

Note: Version bump only for package @teste-ui/popper

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

1.0.0-rc.8 (2020-10-29)

Bug Fixes

  • toast: allow custom render in update (eb8bff9), closes #2362

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

1.0.0-rc.7 (2020-10-25)

Note: Version bump only for package @teste-ui/popper

1.0.0-rc.6 (2020-10-25)

Note: Version bump only for package @teste-ui/popper

1.0.0-rc.5 (2020-09-27)

Note: Version bump only for package @teste-ui/popper

1.0.0-rc.4 (2020-09-25)

Note: Version bump only for package @teste-ui/popper

1.0.0-rc.3 (2020-08-30)

Note: Version bump only for package @teste-ui/popper

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

1.0.0-rc.2 (2020-08-09)

Note: Version bump only for package @teste-ui/popper

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

1.0.0-rc.1 (2020-08-06)

Bug Fixes

  • variables: drop unused imports (552b2e9)

Features

  • update popper hook and use-clipboard (2659f60)

Performance Improvements

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

1.0.0-rc.0 (2020-07-26)

Note: Version bump only for package @teste-ui/popper

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

1.0.0-next.7 (2020-07-26)

Note: Version bump only for package @teste-ui/popper

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

1.0.0-next.6 (2020-07-15)

Note: Version bump only for package @teste-ui/popper

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

1.0.0-next.5 (2020-07-15)

Features

  • add support for css transition (a41614c)

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

1.0.0-next.4 (2020-07-01)

Bug Fixes

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

1.0.0-next.3 (2020-06-28)

Bug Fixes

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

1.0.0-next.2 (2020-06-21)

Bug Fixes