Socket
Socket
Sign inDemoInstall

selection-popover

Package Overview
Dependencies
29
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    selection-popover

Easy-to-use, composable react selection popover.


Version published
Weekly downloads
1.9K
decreased by-18.52%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

Easy-to-use, composable react selection popover

npm version npm downloads

Install

npm install selection-popover

Content

Anatomy

Import all parts and piece them together.

import * as Selection from 'selection-popover'

export default () => (
  <Selection.Root>
    <Selection.Trigger />
    <Selection.Portal>
      <Selection.Content>
        <Selection.Arrow />
      </Selection.Content>
    </Selection.Portal>
  </Selection.Root>
)

API Reference

Root

Contains all the parts of a selection.

PropTypeDefaultDescription
defaultOpenboolean-The open state of the hover card when it is initially rendered. Use when you do not need to control its open state.
openboolean-The controlled open state of the popover. Must be used in conjunction with onOpenChange.
onOpenChange(open: boolean) => void-Event handler called when the open state of the popover changes.
whileSelectbooleanfalseWhen true, the popover will open while the text is selected, otherwise only when the mouse up.
disabledbooleanfalseWhen true, the popover won't open when text is selected.
openDelaynumber0The duration from when release the mouse until the content opens. In whileSelect is when you start the selection.
closeDelaynumber0The duration from when you click outside of the content until the content closes.

Trigger

The area that opens the popover. Wrap it around the target you want the popover to open when a text is selected.

PropTypeDefaultDescription
asChildbooleanfalseChange the component to the HTML tag or custom component of the only child. This will merge the original component props with the props of the supplied element/component and change the underlying DOM node.

Portal

When used, portals the content part into the body.

PropTypeDefaultDescription
forceMountboolean-Used to force mounting when more control is needed. Useful when controlling animation with React animation libraries. If used on this part, it will be inherited by Selection.Content.
containerHTMLElementdocument.bodySpecify a container element to portal the content into.

Content

The component that pops out when a text is selected.

PropTypeDefaultDescription
asChildbooleanfalseChange the component to the HTML tag or custom component of the only child. This will merge the original component props with the props of the supplied element/component and change the underlying DOM node.
forceMountboolean-Used to force mounting when more control is needed. Useful when controlling animation with React animation libraries. It inherits from Selection.Portal.
side"top" | "right" | "bottom" | "left"topThe preferred side of the selection to render against when open. Will be reversed when collisions occur and avoidCollisions is enabled.
sideOffsetnumber0The distance in pixels from the selection.
align"start" | "center" | "end"centerThe preferred alignment against the selection. May change when collisions occur.
alignOffsetnumber0An offset in pixels from the "start" or "end" alignment options.
avoidCollisionsbooleantrueWhen true, overrides the side and align preferences to prevent collisions with boundary edges.
collisionBoundaryElement | null | Array<Element | null>[]The element used as the collision boundary. By default this is the viewport, though you can provide additional element(s) to be included in this check.
collisionPaddingnumber | Partial<Record<Side, number>>0The distance in pixels from the boundary edges where collision detection should occur. Accepts a number (same for all sides), or a partial padding object, for example: { top: 20, left: 20 }.
arrowPaddingnumber0The padding between the arrow and the edges of the content. If your content has border-radius, this will prevent it from overflowing the corners.
sticky"partial" | "always"partialThe sticky behavior on the align axis. "partial" will keep the content in the boundary as long as the trigger is at least partially in the boundary whilst "always" will keep the content in the boundary regardless.
hideWhenDetachedbooleanfalseWhether to hide the content when the text becomes fully occluded.
onEscapeKeyDown(event: KeyboardEvent) => void-Event handler called when the escape key is down. It can be prevented by calling event.preventDefault.
onPointerDownOutside(event: PointerDownOutsideEvent) => void-Event handler called when a pointer event occurs outside the bounds of the component. It can be prevented by calling event.preventDefault.
onFocusOutside(event: FocusOutsideEvent) => void-Event handler called when focus moves outside the bounds of the component. It can be prevented by calling event.preventDefault.
onInteractOutside(event: PointerDownOutsideEvent | FocusOutsideEvent) => void-Event handler called when an interaction (pointer or focus event) happens outside the bounds of the component. It can be prevented by calling event.preventDefault.
Data AttributeValues
[data-state]"open" | "closed"
[data-side]"left" | "right" | "bottom" | "top"
[data-align]"start" | "end" | "center"
CSS VariableDescription
--selection-popover-content-transform-originThe transform-origin computed from the content and arrow positions/offsets.
--selection-popover-select-widthThe width of the select.
--selection-popover-select-heightThe height of the select.

Arrow

An optional arrow element to render alongside the popover. This can be used to help visually link the selected text with the Selection.Content. Must be rendered inside Selection.Content.

PropTypeDefaultDescription
asChildbooleanfalseChange the component to the HTML tag or custom component of the only child. This will merge the original component props with the props of the supplied element/component and change the underlying DOM node.
widthnumber10The width of the arrow in pixels.
heightnumber5The height of the arrow in pixels.

Examples

Origin-aware animations

// index.jsx
import * as Selection from 'selection-popover'
import './styles.css'

export default () => (
  <Selection.Root>
    <Selection.Trigger>...</Selection.Trigger>
    <Selection.Portal>
      <Selection.Content className="SelectionContent">...</Selection.Content>
    </Selection.Portal>
  </Selection.Root>
)
/* styles.css */
.SelectionContent {
  transform-origin: var(--selection-popover-content-transform-origin);
  animation: scaleIn 500ms cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes scaleIn {
  from {
    opacity: 0;
    transform: scale(0);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

Collision-aware animations

// index.jsx
import * as Selection from 'selection-popover'
import './styles.css'

export default () => (
  <Selection.Root>
    <Selection.Trigger>...</Selection.Trigger>
    <Selection.Portal>
      <Selection.Content className="SelectionContent">...</Selection.Content>
    </Selection.Portal>
  </Selection.Root>
)
/* styles.css */
.SelectionContent {
  animation-duration: 400ms;
  animation-timing-function: cubic-bezier(0.16, 1, 0.3, 1);
}
.SelectionContent[data-state='open'][data-side='top'] {
  animation-name: slideDownAndFade;
}
.SelectionContent[data-state='open'][data-side='bottom'] {
  animation-name: slideUpAndFade;
}

@keyframes slideDownAndFade {
  from {
    opacity: 0;
    transform: translateY(-2px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes slideUpAndFade {
  from {
    opacity: 0;
    transform: translateY(2px));
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

Unmount animations

// index.jsx
import * as Selection from 'selection-popover'
import './styles.css'

export default () => (
  <Selection.Root>
    <Selection.Trigger>...</Selection.Trigger>
    <Selection.Portal>
      <Selection.Content className="SelectionContent">...</Selection.Content>
    </Selection.Portal>
  </Selection.Root>
)
/* styles.css */
.SelectionContent {
  animation-duration: 400ms;
  animation-timing-function: cubic-bezier(0.16, 1, 0.3, 1);
}
.SelectionContent[data-state='open'] {
  animation-name: slideDownAndFade;
}
.SelectionContent[data-state='closed'] {
  animation-name: slideUpAndFade;
}

@keyframes slideDownAndFade {
  from {
    opacity: 0;
    transform: translateY(-2px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes slideUpAndFade {
  from {
    opacity: 1;
    transform: translateY(0));
  }
  to {
    opacity: 0;
    transform: translateY(-2px);
  }
}

Use with Radix Toolbar

import * as Selection from 'selection-popover'
import * as Toolbar from '@radix-ui/react-toolbar'

export default () => (
  <Selection.Root>
    <Selection.Trigger>...</Selection.Trigger>
    <Selection.Portal>
      <Selection.Content asChild>
        <Toolbar.Root>...</Toolbar.Root>
        <Selection.Arrow />
      </Selection.Content>
    </Selection.Portal>
  </Selection.Root>
)

Acknowledgements

Keywords

FAQs

Last updated on 30 Jul 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc