Hulian UIv0.93.5413 components0 components
  • Start
  • Components
  • Blocks
  • Pages
  • Demos
  • Changelog
ChineseEnglish
  • ComponentsComponents
  • ThemeTheme

Overlays

  • Dialog
  • Modal
  • AlertDialog
  • Drawer
  • Popover
  • Tooltip
  • HoverCard
  • Glimpse
  • HeroVideoDialog
  • ElementSelectionOverlay

Messages

  • Alert
  • InterceptCard
  • Callout
  • Banner
  • Toast
  • Notification
  • ServiceMessage
  • Result
  • GiftFeed
  • FloatingReactions
  • Popconfirm

Loading and Progress

  • Spin
  • Spinner
  • Progress

Guidance

  • Tour

ElementSelectionOverlay

element-selection-overlay

Hover and click elements in a container or same-origin iframe to get a component path back.

Usage

Basic usage (marker driven)

The preview tree marks itself with data-hulian-path / data-hulian-component, so selecting emits the marked path; highlightSelector narrows selection granularity down to components.

Point-and-edit preview area

Move the pointer in and click any block to select it.

Revenue this month

¥ 128,400

Number of orders

1,204

Upgrade to ProUpgrade now
Selected:—Hovered:—
Clicked 0 times (this must keep counting while selection mode is on)
tsx
const [root, setRoot] = useState<HTMLDivElement | null>(null);
const [selected, setSelected] = useState<string | null>(null);

<div ref={setRoot}>
  <div data-hulian-component="Hero" data-hulian-path="App/Hero">…</div>
  <div data-hulian-component="CtaBar" data-hulian-path="App/Cta">…</div>
</div>

<ElementSelectionOverlay
  target={root}
  highlightSelector="[data-hulian-component]"
  selectedPath={selected}
  onSelect={(path) => setSelected(path)}
  onClear={() => setSelected(null)}
/>

Structural path fallback without markers

When the preview tree carries no markers, the path degrades to a CSS selector that querySelector can resolve back (div > section:nth-of-type(2) > button).

First section (no markers at all)

The second section contains a button:

Selected:—Hovered:—
tsx
<ElementSelectionOverlay
  target={root}
  selectedPath={selected}
  onSelect={(path) => setSelected(path)}
/>

Same-origin iframe preview

Pass an iframe element as target to take over its document; the outline is drawn in the host layer with the iframe offset already applied. A cross-origin iframe is unsupported and reports through onError.

Selected:—Hovered:—
tsx
const [frame, setFrame] = useState<HTMLIFrameElement | null>(null);

<iframe ref={setFrame} srcDoc={html} title="Preview" />
<ElementSelectionOverlay
  target={frame}
  onSelect={(path) => setSelected(path)}
  onError={(e) => console.warn(e.code, e.message)}
/>

Hide the label / leave selection mode

showLabel=false keeps the outline only; enabled=false stops picking and click interception while the existing selection outline stays.

Point-and-edit preview area

Move the pointer in and click any block to select it.

Revenue this month

¥ 128,400

Number of orders

1,204

Upgrade to ProUpgrade now
Selected:—Hovered:—
Clicked 0 times (this must keep counting while selection mode is on)
tsx
<ElementSelectionOverlay target={root} showLabel={false} enabled={false} />

When to use

Use it to build point-and-edit: click an element in a preview, then jump to its source, open a property panel, or feed it to a prompt. It only selects and reports a path; the editing panel is yours to build.

  • To walk a user through an element you already know, use Tour; it masks the page and anchors a card, it does not pick elements.
  • To attach an annotation bubble to an element, use Annotation.
  • For a non-interactive decorative layer, use Watermark.

Import

ts
import {
  ElementSelectionOverlay,
  asElement,
  computeLabelPosition,
  elementPath,
  escapeAttributeValue,
  findMarkedElement,
  isRectVisible,
  pathLabel,
  resolveElementByPath,
  structuralPath,
  toHostRect,
} from "@hulianui/ui"

Props

NameTypeDefaultDescription
target *HTMLElement | HTMLIFrameElement | null-Target area: a plain container, or a same-origin iframe whose document is taken over. Nothing renders or listens while null.
enabledbooleantrueSelection mode. When false, picking and click interception stop, but an existing selection box stays visible.
highlightSelectorstring-Selector for selectable elements; the pointer target walks up to its closest match, and nothing highlights without a match. Use it to lock granularity to component level.
ignoreSelectorstring-Elements matching it (or having a matching ancestor) can be neither hovered nor selected.
showLabelbooleantrueShows the label. Only one label exists at a time; hover wins over selection.
pathAttributestring"data-hulian-path"Attribute holding the marked path.
componentAttributestring"data-hulian-component"Attribute holding the component name, used for the label and detail.component.
anchorOnIdbooleantrueA structural path stops at the first ancestor carrying an id instead of walking up to the root.
selectedPathstring | null-Controlled selection. Passing it (including null) makes the component controlled and it stops tracking selection itself.
interceptClicksbooleantrueSwallows clicks inside the target so preview links and buttons do not fire.
zIndexnumber100Overlay z-index.
classNamestring-Class name of the overlay container.

Events

EventTypeDescription
onSelect(path: string, detail: ElementSelectionDetail) => voidSelection by click, or by pressing Enter or Space inside the target.
onHover(path: string | null, detail: ElementSelectionDetail | null) => voidHover changed; leaving the target or landing on a non-selectable area reports (null, null).
onClear() => voidSelection cleared by clicking empty space or pressing Escape.
onError(error: ElementSelectionOverlayError) => voidThe target cannot be taken over, such as a cross-origin iframe. Reported once per target. error.message comes from the ConfigProvider locale (built-in Chinese without a provider); error.code is language independent, so branch on code, never on the message.

ElementSelectionDetail is { path, source: "marked" | "structural", component, tagName, element, rect }. source tells you how trustworthy the path is: marked comes from the marker attribute and survives re-layout, while structural is derived from the DOM shape and may break when the DOM changes.

Accessibility

  • The overlay is aria-hidden with pointer-events: none: it stays out of the accessibility tree, never takes focus, and never blocks the target.
  • State is not encoded by color alone: hover draws a thin dashed box, selection draws a thick solid one, and the label spells out the component or element name.
  • Keyboard reachable: tab to an element inside the target and press Enter or Space to select it, Escape to clear, so a pointer is never required.
  • Offer an equivalent keyboard path in the host as well, such as a component list built with Tree bound to selectedPath. Choosing a node in a list always beats hunting on a canvas for screen reader users.
  • The label avoids the top viewport edge automatically by flipping inside the box, so it is never clipped.

Pitfalls

  • Cross-origin iframes are not supported, and the failure is never silent. When contentDocument cannot be read the component renders nothing and fires onError({ code: "cross-origin" }) once, plus a development warning. Three ways out: serve the preview same-origin (srcDoc or a same-origin proxy domain), mount an overlay inside the previewed page and post the path back to the host, or drop point-and-edit. This component deliberately ships no postMessage bridge, because that is a separate protocol and does not belong inside a UI component.
  • Mark the tree whenever you can, and mark an element that actually takes up space. A structural path is inferred from the DOM shape, so one extra sibling or a flipped conditional can point it somewhere else. Put data-hulian-path on component roots to get paths that survive re-layout. Do not put the marker on a display: contents wrapper: such elements generate no box, getBoundingClientRect() is always zero, and the zero-area check treats them as invisible, so no box is ever drawn.
  • Elements inside an iframe live in another realm, so they are not instances of the host Element and node instanceof Element is always false. Use the exported asElement() when handling target events yourself; this is the classic silent failure of same-origin iframe work.
  • interceptClicks defaults to true, which means the preview is not interactive while selecting: clicks are swallowed and mousedown defaults are blocked. Pass interceptClicks={false} to keep the preview usable, or toggle selection mode through enabled.
  • Interception and clearing only happen inside the target. When target is a plain container the listeners still sit on the host document (they have to, to cover the iframe case), but the component first checks whether the event landed inside target: clicks outside are neither preventDefaulted nor stopPropagationed, and they do not fire onClear. Host navigation and inspector panels keep working, and onClear fires only when you click empty space inside the preview.
  • The target root itself is not selectable. Hovering or clicking it counts as empty space and triggers onClear. Marker lookup also skips the root on purpose, since a marker there would collapse every element onto one path.
  • getBoundingClientRect always returns zero in jsdom, and zero-area rects count as invisible, so no box renders in unit tests by default. Stub Element.prototype.getBoundingClientRect if you need to assert that a box exists, but do not assert coordinates, since they come from your own stub. Test coordinate logic through the toHostRect and computeLabelPosition pure functions instead.
  • When the target is document.body, the portal layer lives inside the target. The component already filters out MutationObserver records caused by itself, which would otherwise loop forever; do the same in any observer you add.
  • Client component: it reads the DOM and attaches listeners, so use it in a client context. It renders nothing during SSR.

Related

Tour · Annotation · Watermark · Flow · Tree

Playground

Point-and-edit preview area

Move the pointer in and click any block to select it.

Revenue this month

¥ 128,400

Number of orders

1,204

Upgrade to ProUpgrade now
Selected:—Hovered:—
Clicked 0 times (this must keep counting while selection mode is on)
<ElementSelectionOverlay
  target={previewRoot}
  highlightSelector="[data-hulian-component]"
  selectedPath={selected}
  onSelect={(path, detail) => setSelected(path)}
/>
PreviousHeroVideoDialogFeedbackNextAlert

On this page

  • Usage
  • Documentation
    • When to use
    • Import
    • Props
    • Events
    • Accessibility
    • Pitfalls
    • Related
  • Playground